๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐˜ผ๐™„/๐™Ž๐™ฉ๐™ช๐™™๐™ฎ

ํŒŒ์ด์ฌ ๊ฐ์ข… OCR ์‚ฌ์šฉํ•ด๋ณด๊ธฐ ๋ฐ ๋„ค์ด๋ฒ„ ํด๋ผ์šฐ๋“œ AI OCR ์‚ฌ์šฉ๋ฒ• (2)

by beomcoder 2023. 4. 5.
728x90
๋ฐ˜์‘ํ˜•

 

 

ํŒŒ์ด์ฌ ๊ฐ์ข… OCR ์‚ฌ์šฉํ•ด๋ณด๊ธฐ ๋ฐ ๋„ค์ด๋ฒ„ ํด๋ผ์šฐ๋“œ AI OCR ์‚ฌ์šฉ๋ฒ• (1)

์œ ํ•ด์ด๋ฏธ์ง€๋ฅผ ํŒ๋ณ„ํ•ด์•ผํ•˜๋Š”๋ฐ ์ฒ˜์Œ์—๋Š” ์•ผํ•œ์ด๋ฏธ์ง€๋‚˜ ์ž”์ธํ•œ์ด๋ฏธ์ง€๋ฅผ ํŒ๋ณ„ํ•˜๋Š” ๋ชจ๋ธ์„ ๋งŒ๋“ค์—ˆ์—ˆ๋‹ค. ๋งŒ๋“ค๊ณ ๋‚˜์„œ ์–ดํ”Œ ๋‚ด์—์„œ ์ด์ „์˜ ๊ด‘๊ณ ๋กœ ํŒ๋‹จ๋˜์–ด ์‚ญ์ œ๋˜์—ˆ๋˜ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ง€๊ณ  ์™€์„œ ํ…Œ์ŠคํŠธ

beomcoder.tistory.com

 

(1)์— ์ด์–ด์„œ ๋งˆ์ € ์“ฐ๋ ค๊ณ  ํ•œ๋‹ค.

 

๋จผ์ € ์ฝ˜์†”์— ๊ฐ€์„œ OCR Domain์ƒ์„ฑํ•˜๊ธฐ๋ฅผ ํด๋ฆญํ•˜์—ฌ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•ด์ค€๋‹ค.

๋„๋ฉ”์ธ ์ด๋ฆ„, ๋„๋ฉ”์ธ ์ฝ”๋“œ๋Š” ์•„๋ฌด๊ฑฐ๋‚˜ ์ ์–ด๋„ ์ƒ๊ด€์—†๋‹ค.

๋‚˜๋Š” ๊ทธ๋ƒฅ ์ด๋ฏธ์ง€์—์„œ ๊ธ€์ž๋ฅผ ์ถ”์ถœํ•˜๊ธฐ ์œ„ํ•ด์„œ General๋กœ ์„ ํƒํ•˜์˜€๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  SecretKey๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ณต์‚ฌํ•˜๊ณ , APIGW ์ž๋™ ์—ฐ๋™์„ ์„ ํƒํ•˜์—ฌ URL์„ ๋ฐ›๋Š”๋‹ค.

์—ฌ๊ธฐ์„œ ์˜์•„ํ–ˆ๋˜๊ฒŒ OCR์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š”๋ฐ API๋Š” ๋”ฐ๋กœ ์‹ ์ฒญํ•ด์ค˜์•ผ ํ•œ๋‹ค๋Š” ์ ์ด์—ˆ๋‹ค.

 

 

NAVER CLOUD PLATFORM

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

www.ncloud.com

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์„ ์ด์šฉํ•˜์—ฌ ๋ถˆ๋ฒ•์ด๋ฏธ์ง€ ๊ฒ€์ถœํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋งŒ๋“ค ์˜ˆ์ •์ด๋‹ค.

 

ํ˜น์‹œ๋ผ๋„ ์„ค๋ช…์ด ๋ถ€์กฑํ•˜๊ฒŒ ๋Š๊ปด์กŒ๋‹ค๋ฉด ์„ค๋ช…์„œ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด ๋œ๋‹ค.

 

General

 

guide.ncloud-docs.com

 

๊ทธ๋ฆฌ๊ณ  ์ด๋ฏธ์ง€๋Š” JPG, JPEG, PNG 3๊ฐœ๋งŒ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํŒŒ์ผ์ด ๋‹ค๋ฅด๋‹ค๋ฉด ๋ณ€ํ™˜์‹œ์ผœ ๋ณด๋‚ด์•ผํ•œ๋‹ค.

API ๊ฐ€์ด๋“œ๋„ ์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๋“ฏํ•˜๋‹ค.

 

CLOVA OCR Custom API

 

api.ncloud-docs.com

 

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€