์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- ํ์ด์ฌ
- ๋ฐ์ดํฐ๋ถ๊ท ํ
- ๋์ํ๋ณธ
- ์๋ํด๋ผ์ฐ๋
- LDA
- ํฌ๋กค๋ง
- ๋ฐ์ดํฐ๋ถ์์ ๋ฌธ๊ฐ
- ๋ ๋ฆฝํ๋ณธ
- iloc
- DBSCAN
- t-test
- ์ฃผ์ฑ๋ถ๋ถ์
- numpy
- ๊ตฐ์งํ
- ์ธ๋์ํ๋ง
- ํ ์คํธ๋ถ์
- Lambda
- ๋น ๋ฐ์ดํฐ๋ถ์๊ธฐ์ฌ
- pandas
- ๋ฐ์ดํฐ๋ถ์์ค์ ๋ฌธ๊ฐ
- ADsP
- dataframe
- ADP
- ์ค๋ฒ์ํ๋ง
- ๋ฐ์ดํฐ๋ถ์
- Python
- PCA
- datascience
- ๋น ๋ฐ์ดํฐ
- opencv
Data Science LAB
[Python] Naver Cloud ํ๊ธ OCR ๋ณธ๋ฌธ
๋ค์ด๋ฒ์์ ๊ฐ๋ฐํ ์ธ๊ณต์ง๋ฅ ํ๋ซํผ์ธ Naver CLOVA์์ ์ ๊ณตํ๋ OCR API๋ฅผ ํ์ฉํด ์ด๋ฏธ์ง์์ ํ ์คํธ ์์ญ์ ๊ฐ์งํ๊ณ ๋ฌธ์ ์ธ์(OCR)์ ํด๋ณด๋ ค๊ณ ํ๋ค.
Naver Cloud Platform
1. ๋๋ฉ์ธ ์์ฑ
Naver Cloud Platform ๊ณ์ ์ด ์๋ค๋ฉด ํ์๊ฐ์ ํ ๋ก๊ทธ์ธํ๋ค.
๋จผ์ , ๊ฒฐ์ ์๋จ ๋ฑ๋ก์ ํด์ผํ๋ค.
๋ง์ดํ์ด์ง -> ๊ฒฐ์ ์๋จ๊ด๋ฆฌ ํด๋ฆญ!
์ฒซ ๊ฐ์ ์ด๋ผ๋ฉด 100,000์์ ํฌ๋ ๋ง์ ์ ๊ณตํด์ค๋ค.
๊ฒฐ์ ์๋จ ๋ฑ๋ก์ด ์๋ฃ๋์๋ค๋ฉด, ์๋น์ค ํด๋ฆญ
AI Service -> CLOVA OCR ํด๋ฆญ
์ด์ฉ ์ ์ฒญํ๊ธฐ
์ฝ์ ์ผ์ชฝ์ CLOVA OCR์ด ๋ณด์ด๋ฉด ์ด์ฉ์ ์ฒญ ์๋ฃ
Invoke URL ์์ฑ์ ์ํด API Gateway ์ด์ฉ ์ ์ฒญ
์๋น์ค -> Application Services -> API Gateway
๋๋ฉ์ธ ์ถ๊ฐ
Naver Cloud Platform ์ ์ ์ํ์ฌ CLOVA OCR -> Domain -> ๋๋ฉ์ธ ์์ฑ ํด๋ฆญ
๋๋ฉ์ธ ์ด๋ฆ๊ณผ ์ฝ๋, ์ง์์ธ์ด๋ ํ๊ตญ์ด๋ก ์ ํํ ๋ค, ์๋น์คํ์ ์ General๋ก ์ ํ!
์ธ์๋ชจ๋ธ์ Basic ์ผ๋ก ์ค์
Demo ๋ฒํผ์ ํด๋ฆญํ๋ฉด ํ ์คํธ ๊ฐ๋ฅ!
์ด๋ฏธ์ง๋ฅผ ๋๋๊ทธํ๋ฉด ํ ์คํธ ๊ฐ๋ฅํฉ๋๋ค
2. Naver Clova OCR API ๊ฐ๋ฐ
๋ฐ์ดํฐ์ ๊ตฌ์ฑ
- train.csv, test.csv ํ์ผ์๋ ๊ฐ ์ด๋ฏธ์ง์ ๊ฒฝ๋ก๊ฐ ์กด์ฌ
- train, test ํด๋์๋ ๊ฐ๊ฐ์ ์ด๋ฏธ์ง๋ค ์กด์ฌ
ex) train_00001.png
CLOVA OCR์ REST API ์ด์ฉ
import numpy as np
import platform
from PIL import ImageFont, ImageDraw, Image
from matplotlib import pyplot as plt
import uuid
import json
import time
import cv2
import requests
import pandas as pd
from tqdm import tqdm
import os
๋๋ฉ์ธ ์์ฑ์ ์์ฑํ Invoke URL์ ํค ์ด์ฉํ์ฌ ๊ฐ๊ฐ api_url๊ณผ secret_key์ ์ ๋ ฅ
api_url = <๊ณ ์ API_URL>
secret_key = <๊ณ ์ SECRET_KEY>
์ด๋ฏธ์ง์ ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ๊ธฐ ์ํ ํจ์ ์์ฑ
def plt_imshow(title='image', img=None, figsize=(8 ,5)):
plt.figure(figsize=figsize)
if type(img) == list:
if type(title) == list:
titles = title
else:
titles = []
for i in range(len(img)):
titles.append(title)
for i in range(len(img)):
if len(img[i].shape) <= 2:
rgbImg = cv2.cvtColor(img[i], cv2.COLOR_GRAY2RGB)
else:
rgbImg = cv2.cvtColor(img[i], cv2.COLOR_BGR2RGB)
plt.subplot(1, len(img), i + 1), plt.imshow(rgbImg)
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()
else:
if len(img.shape) < 3:
rgbImg = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
else:
rgbImg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(rgbImg)
plt.title(title)
plt.xticks([]), plt.yticks([])
plt.show()
ํ๊ธ ์ถ๋ ฅ์ ์ํ ํจ์ ์์ฑ
def put_text(image, text, x, y, color=(0, 255, 0), font_size=22):
if type(image) == np.ndarray:
color_coverted = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = Image.fromarray(color_coverted)
if platform.system() == 'Darwin':
font = 'AppleGothic.ttf'
elif platform.system() == 'Windows':
font = 'malgun.ttf'
image_font = ImageFont.truetype(font, font_size)
font = ImageFont.load_default()
draw = ImageDraw.Draw(image)
draw.text((x, y), text, font=image_font, fill=color)
numpy_image = np.array(image)
opencv_image = cv2.cvtColor(numpy_image, cv2.COLOR_RGB2BGR)
return opencv_image
test
img = cv2.imread(path)
roi_img = img.copy()
for field in result['images'][0]['fields']:
text = field['inferText']
vertices_list = field['boundingPoly']['vertices']
pts = [tuple(vertice.values()) for vertice in vertices_list]
topLeft = [int(_) for _ in pts[0]]
topRight = [int(_) for _ in pts[1]]
bottomRight = [int(_) for _ in pts[2]]
bottomLeft = [int(_) for _ in pts[3]]
cv2.line(roi_img, topLeft, topRight, (0,255,0), 2)
cv2.line(roi_img, topRight, bottomRight, (0,255,0), 2)
cv2.line(roi_img, bottomRight, bottomLeft, (0,255,0), 2)
cv2.line(roi_img, bottomLeft, topLeft, (0,255,0), 2)
roi_img = put_text(roi_img, text, topLeft[0], topLeft[1] - 10, font_size=30)
print(text)
plt_imshow("Original", img, figsize=(16, 10))
plt_imshow("ROI", roi_img, figsize=(16, 10))
๊ฒฐ๊ณผ ์ถ์ถ
test_df = pd.read_csv('../data/test.csv')
results = []
for i_path in test_df['img_path']:
path = os.path.join('../data',i_path[2:])
files = [('file',open(path, 'rb'))]
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')}
headers = {
'X-OCR-SECRET': secret_key,
}
response = requests.request("POST", api_url, headers=headers, data=payload, files=files)
result = response.json()
img = cv2.imread(path)
roi_img = img.copy()
text = ''
try:
for field in result['images'][0]['fields']:
t = field['inferText']
text = "".join([text,t])
results.append(text)
print(text)
except:
results.append(text)
for ๋ฌธ์ ์ฌ์ฉํ์ฌ ์๋๊ฐ ๋๋ฆฌ๊ธฐ ๋๋ฌธ์ ๋ฐ์ดํฐ์ ์ด ๋ง์ง ์์ ๊ฒฝ์ฐ ์ถ์ฒ
'๐ฅ๏ธ Computer Vision > ocr' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] EasyOCR ์ ์ด์ฉํ ์ด๋ฏธ์ง์์ ํ๊ธ ์ธ์ํ๊ธฐ (0) | 2022.10.19 |
---|---|
[Python] Mac์์ tesseract ์ค์นํ๊ธฐ ๋ฐ ํ๊ธ ์ถ๊ฐ (0) | 2022.09.15 |