(1)์ ์ด์ด์ ๋ง์ ์ฐ๋ ค๊ณ ํ๋ค.
๋จผ์ ์ฝ์์ ๊ฐ์ OCR Domain์์ฑํ๊ธฐ๋ฅผ ํด๋ฆญํ์ฌ ์ ๋ณด๋ฅผ ์ ๋ ฅํด์ค๋ค.
๋๋ฉ์ธ ์ด๋ฆ, ๋๋ฉ์ธ ์ฝ๋๋ ์๋ฌด๊ฑฐ๋ ์ ์ด๋ ์๊ด์๋ค.
๋๋ ๊ทธ๋ฅ ์ด๋ฏธ์ง์์ ๊ธ์๋ฅผ ์ถ์ถํ๊ธฐ ์ํด์ General๋ก ์ ํํ์๋ค.
๊ทธ๋ฆฌ๊ณ SecretKey๋ฅผ ์์ฑํ์ฌ ๋ณต์ฌํ๊ณ , APIGW ์๋ ์ฐ๋์ ์ ํํ์ฌ URL์ ๋ฐ๋๋ค.
์ฌ๊ธฐ์ ์์ํ๋๊ฒ OCR์๋น์ค๋ฅผ ์ ๊ณตํ๋๋ฐ API๋ ๋ฐ๋ก ์ ์ฒญํด์ค์ผ ํ๋ค๋ ์ ์ด์๋ค.
API๋ฅผ ์ฐ๋ํ๊ณ ๋๋ฉด ์ฌ์ฉํ ์ค๋น๊ฐ ๋ชจ๋ ๋๋ฌ๋ค. API์ฃผ์์ KEY๊ฐ์ ๋ณต์ฌํ์ฌ ๋ฐ๋ก ์ ์ฅํ๋ค.
import cv2, os
import numpy as np
from PIL import Image
import requests
import uuid
import time
import json
# with open('secrets.json', 'r') as f:
# secret = json.load(f)
# ILLEGAL_THRESHOLD = 0
# ILLEGAL_WORD_FILE_PATH = secret['ILLEGAL_WORD_FILE_PATH']
# NAVER_OCR_API_URL = secret['NAVER_OCR_API_URL']
# NAVER_OCR_API_KEY = secret['NAVER_OCR_API_KEY']
api_url = NAVER_OCR_API_URL
secret_key = NAVER_OCR_API_KEY
image_file = 'test.png'
request_json = {
'images': [{'format': 'png', '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).json()
print(response)
์ ์ฝ๋๋ฅผ ๋ณต์ฌํ์ฌ ์ฌ์ฉํ๋ฉด ์ฌ์ฉํ ์ ์๋ค.
๋ฐ๊ฟ์ผ ํ๋๊ฑด api_url๊ณผ secet_key๋ฅผ ์๊น ์ ์ฅํด๋์๋ ๊ฐ์ ๋ฃ์ผ๋ฉด ๋๋ค.
๊ทธ๋ฆฌ๊ณ image_file์ ๋ณธ์ธ์ด ocr์ ์ฌ์ฉํ ์ด๋ฏธ์ง์ ํ์ผ๊ฒฝ๋ก๋ฅผ ์ ๋ ฅํ๋ฉด ๋๋ค.
ํด๋๊ฐ ๋ค๋ฅด๋ค๋ฉด ํด๋๊ฒฝ๋ก๋ ํจ๊ป ๋ฃ์ด์ค์ผ ํ๋ค.
์ ์๊ธ์ ์ถ๋ ฅ์์์ด๋ค.
{'version': 'V2',
'requestId': 'f496aa15-ca5f-45c3-be3e-fdaae4d15128',
'timestamp': 1680601634158,
'images': [{'uid': 'f42756da35ba44cd9c7e216eeb275c4c',
'name': 'demo',
'inferResult': 'SUCCESS',
'message': 'SUCCESS',
'validationResult': {'result': 'NO_REQUESTED'},
'convertedImageInfo': {'width': 225,
'height': 225,
'pageIndex': 0,
'longImage': False},
'fields': [{'valueType': 'ALL',
'boundingPoly': {'vertices': [{'x': 8.0, 'y': 7.0},
{'x': 46.0, 'y': 7.0},
{'x': 46.0, 'y': 30.0},
{'x': 8.0, 'y': 30.0}]},
'inferText': '์
๊ณ',
'inferConfidence': 1.0,
'type': 'NORMAL',
'lineBreak': False},
{'valueType': 'ALL',
'boundingPoly': {'vertices': [{'x': 48.0, 'y': 9.0},
{'x': 77.0, 'y': 9.0},
{'x': 77.0, 'y': 27.0},
{'x': 48.0, 'y': 27.0}]},
'inferText': '1์',
'inferConfidence': 1.0,
'type': 'NORMAL',
'lineBreak': False},
{'valueType': 'ALL',
'boundingPoly': {'vertices': [{'x': 80.0, 'y': 8.0},
{'x': 136.0, 'y': 8.0},
{'x': 136.0, 'y': 28.0},
{'x': 80.0, 'y': 28.0}]},
'inferText': '๋ญ์
๋ฆฌ',
'inferConfidence': 1.0,
'type': 'NORMAL',
'lineBreak': False},
{'valueType': 'ALL',
'boundingPoly': {'vertices': [{'x': 141.0, 'y': 9.0},
{'x': 175.0, 'y': 7.0},
{'x': 176.0, 'y': 27.0},
{'x': 142.0, 'y': 28.0}]},
'inferText': '์คํผ',
'inferConfidence': 1.0,
'type': 'NORMAL',
'lineBreak': True},
{'valueType': 'ALL',
'boundingPoly': {'vertices': [{'x': 37.0, 'y': 49.0},
{'x': 104.0, 'y': 58.0},
{'x': 101.0, 'y': 83.0},
{'x': 34.0, 'y': 74.0}]},
'inferText': 'ํ',
'inferConfidence': 0.7623,
'type': 'NORMAL',
'lineBreak': False},
{'valueType': 'ALL',
'boundingPoly': {'vertices': [{'x': 119.0, 'y': 49.0},
{'x': 196.0, 'y': 49.0},
{'x': 196.0, 'y': 85.0},
{'x': 119.0, 'y': 85.0}]},
'inferText': '์คํผ',
'inferConfidence': 1.0,
'type': 'NORMAL',
'lineBreak': True},
{'valueType': 'ALL',
'boundingPoly': {'vertices': [{'x': 11.0, 'y': 106.0},
{'x': 53.0, 'y': 106.0},
{'x': 53.0, 'y': 130.0},
{'x': 11.0, 'y': 130.0}]},
'inferText': '๋ด์',
'inferConfidence': 0.9919,
'type': 'NORMAL',
'lineBreak': False},
{'valueType': 'ALL',
'boundingPoly': {'vertices': [{'x': 57.0, 'y': 105.0},
{'x': 97.0, 'y': 105.0},
{'x': 97.0, 'y': 125.0},
{'x': 57.0, 'y': 125.0}]},
'inferText': '์ ๋ก',
'inferConfidence': 1.0,
'type': 'NORMAL',
'lineBreak': False},
{'valueType': 'ALL',
'boundingPoly': {'vertices': [{'x': 106.0, 'y': 103.0},
{'x': 167.0, 'y': 106.0},
{'x': 166.0, 'y': 130.0},
{'x': 105.0, 'y': 127.0}]},
'inferText': '๋ฌด์กฐ๊ฑด',
'inferConfidence': 0.9999,
'type': 'NORMAL',
'lineBreak': False},
{'valueType': 'ALL',
'boundingPoly': {'vertices': [{'x': 170.0, 'y': 104.0},
{'x': 214.0, 'y': 104.0},
{'x': 214.0, 'y': 133.0},
{'x': 170.0, 'y': 133.0}]},
'inferText': '์ฆ๋ฌ',
'inferConfidence': 0.9918,
'type': 'NORMAL',
'lineBreak': True},
{'valueType': 'ALL',
'boundingPoly': {'vertices': [{'x': 19.0, 'y': 142.0},
{'x': 204.0, 'y': 139.0},
{'x': 205.0, 'y': 168.0},
{'x': 20.0, 'y': 170.0}]},
'inferText': 'https://@@@@.net',
'inferConfidence': 0.9998,
'type': 'NORMAL',
'lineBreak': True},
{'valueType': 'ALL',
'boundingPoly': {'vertices': [{'x': 3.0, 'y': 165.0},
{'x': 225.0, 'y': 167.0},
{'x': 225.0, 'y': 202.0},
{'x': 3.0, 'y': 200.0}]},
'inferText': '์ ๊ตญ์ ํฅ์ปค๋ฎค๋ํฐ',
'inferConfidence': 0.9988,
'type': 'NORMAL',
'lineBreak': True},
{'valueType': 'ALL',
'boundingPoly': {'vertices': [{'x': 0.0, 'y': 200.0},
{'x': 204.0, 'y': 200.0},
{'x': 204.0, 'y': 225.0},
{'x': 0.0, 'y': 225.0}]},
'inferText': '๋ฐค์๋ฌ2n',
'inferConfidence': 0.6879,
'type': 'NORMAL',
'lineBreak': True}]}]}
์ฌ๊ธฐ์ ํ๋ฒ ํ ๋ 0.5์ด์ ๋ ๊ฑธ๋ ค์ ์ด๊ฑธ ํ๋ฒ์ ํ ์ ์์๊นํ์ฌ ๋ฆฌ์คํธ๋ก ํ๋ฒ์ ๋ณด๋๋๋ 1๊ฐ๋ฐ์ ๋์ง ์์๋ค.
๊ทธ๋์ ๋ค์ด๋ฒ์ ๋ฌธ์๋ฅผ ๋ฃ์๋๋ ํ API์ ํ ์ด๋ฏธ์ง๋ฐ์ ์๋๋ค๊ณ ๋ต๋ณ์ด ์๋ค.
๋ค์ ๊ฒ์๊ธ๋ถํฐ๋ ์ด OCR์ ์ด์ฉํ์ฌ ๋ถ๋ฒ์ด๋ฏธ์ง ๊ฒ์ถํ๋ ์๊ณ ๋ฆฌ์ฆ์ ๋ง๋ค ์์ ์ด๋ค.
ํน์๋ผ๋ ์ค๋ช ์ด ๋ถ์กฑํ๊ฒ ๋๊ปด์ก๋ค๋ฉด ์ค๋ช ์๋ฅผ ์ฐธ๊ณ ํ๋ฉด ๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ด๋ฏธ์ง๋ JPG, JPEG, PNG 3๊ฐ๋ง ์๋น์ค๋ฅผ ์ ๊ณตํ๊ธฐ ๋๋ฌธ์ ํ์ผ์ด ๋ค๋ฅด๋ค๋ฉด ๋ณํ์์ผ ๋ณด๋ด์ผํ๋ค.
API ๊ฐ์ด๋๋ ์ฐธ๊ณ ํ๋ฉด ์ข์ ๋ฏํ๋ค.
'๐ผ๐ > ๐๐ฉ๐ช๐๐ฎ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋์ฉ๋ ์ฒ๋ฆฌ์๋ for ๋ฃจํ๋ณด๋ค numpy, df ๋ฒกํฐ๋ฅผ ์ฌ์ฉํด๋ณด์. (0) | 2024.01.11 |
---|---|
๋จธ์ ๋ฌ๋(CatBoost, LightGBM, XGBoost) ๊ณต๋ถํ๊ธฐ (0) | 2023.03.14 |
AI ํ์คํ์ ๋ํด์ (0) | 2023.03.13 |
๋๊ธ