250x250
Link
๋‚˜์˜ GitHub Contribution ๊ทธ๋ž˜ํ”„
Loading data ...
Notice
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Data Science LAB

[Python] Naver Cloud ํ•œ๊ธ€ OCR ๋ณธ๋ฌธ

๐Ÿ–ฅ๏ธ Computer Vision/ocr

[Python] Naver Cloud ํ•œ๊ธ€ OCR

ใ…… ใ…œ ใ…” ใ…‡ 2022. 10. 18. 00:52
728x90

๋„ค์ด๋ฒ„์—์„œ ๊ฐœ๋ฐœํ•œ ์ธ๊ณต์ง€๋Šฅ ํ”Œ๋žซํผ์ธ Naver CLOVA์—์„œ ์ œ๊ณตํ•˜๋Š” OCR API๋ฅผ ํ™œ์šฉํ•ด ์ด๋ฏธ์ง€์—์„œ ํ…์ŠคํŠธ ์˜์—ญ์„ ๊ฐ์ง€ํ•˜๊ณ  ๋ฌธ์ž ์ธ์‹(OCR)์„ ํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค. 

 

 

Naver Cloud Platform

https://www.ncloud.com/

 

NAVER CLOUD PLATFORM

cloud computing services for corporations, IaaS, PaaS, SaaS, with Global region and Security Technology Certification

www.ncloud.com

 

 

 

 

 

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 ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์†๋„๊ฐ€ ๋Š๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ์…‹์ด ๋งŽ์ง€ ์•Š์€ ๊ฒฝ์šฐ ์ถ”์ฒœ

728x90
Comments