본문 바로가기
개발/AI 공부

파이썬 각종 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
반응형

댓글