[DRF] CLOVA-OCR 적용(1)

yuKeon·2023년 1월 19일
0

DRF

목록 보기
4/7
post-thumbnail

2023-01-09

NAVER CLOUD PLATFORM 공식 문서를 참고하여 OCR API를 호출하는데 성공했다.

https://guide.ncloud-docs.com/docs/clovaocr-overview

https://api.ncloud-docs.com/docs/ai-application-service-ocr

  • ocr/views.py
@api_view(['POST'])
def ocr_receipt(request):
    api_url = OCR_API_URL
    secret_key = OCR_SECRET_KEY
    image_file = request.data['url']

    request_json = {
        'images': [
            {
                'format': 'jpg',
                'name': 'demo'
            }
        ],
        'requestId': str(uuid.uuid4()),
        'version': 'V2',
        'timestamp': int(round(time.time() * 1000))
    }

    payload = {'message': json.dumps(request_json).encode('UTF-8')}
    files = [
        ('file', open(image_file, 'rb'))
    ]
    headers = {
        'X-OCR-SECRET': secret_key
    }

    response = requests.request("POST", api_url, headers=headers, data=payload, files=files)
		return Response(json.loads(response.text))

postman에서 테스트를 해보니 아래와 같은 결과가 나왔다.

호출되는 건 좋지만 너무 많은 데이터가 나온다.

{
    "version": "V2",
    "requestId": "c903e3cf-ff04-4061-8919-71006738ee50",
    "timestamp": 1673250181444,
    "images": [
        {
            "receipt": {
                "meta": {
                    "estimatedLanguage": "ko"
                },
                "result": {
                    "storeInfo": {
                        "name": {
                            "text": "LOTTE super",
                            "formatted": {
                                "value": "LOTTE super"
                            },
                            "boundingPolys": [
                                {
                                    "vertices": [
                                        {
                                            "x": 1322.0,
                                            "y": 530.0
                                        },
                                        {
                                            "x": 1677.0,
                                            "y": 548.0
                                        },
                                        {
                                            "x": 1673.0,
                                            "y": 633.0
                                        },
                                        {
                                            "x": 1318.0,
                                            "y": 615.0
                                        }
                                    ]
                                },
                                {
                                    "vertices": [
                                        {
                                            "x": 1692.0,
                                            "y": 533.0
																...

0개의 댓글

관련 채용 정보