-
Notifications
You must be signed in to change notification settings - Fork 1
/
kakao.py
75 lines (54 loc) · 2.14 KB
/
kakao.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import json
import cv2
import requests
import sys
import os
LIMIT_PX = 1024
LIMIT_BYTE = 1024 * 1024 # 1MB
LIMIT_BOX = 40
def kakao_ocr_resize(image_path: str):
"""
ocr detect/recognize api helper
ocr api의 제약사항이 넘어서는 이미지는 요청 이전에 전처리가 필요.
pixel 제약사항 초과: resize
용량 제약사항 초과 : 다른 포맷으로 압축, 이미지 분할 등의 처리 필요. (예제에서 제공하지 않음)
:param image_path: 이미지파일 경로
:return:
"""
image = cv2.imread(image_path)
height, width, _ = image.shape
if LIMIT_PX < height or LIMIT_PX < width:
ratio = float(LIMIT_PX) / max(height, width)
image = cv2.resize(image, None, fx=ratio, fy=ratio)
height, width, _ = height, width, _ = image.shape
# api 사용전에 이미지가 resize된 경우, recognize시 resize된 결과를 사용해야함.
image_path = "{}_resized.jpg".format(image_path)
cv2.imwrite(image_path, image)
return image_path
return None
def kakao_ocr(image_path: str, appkey: str):
"""
OCR api request example
:param image_path: 이미지파일 경로
:param appkey: 카카오 앱 REST API 키
"""
API_URL = "https://dapi.kakao.com/v2/vision/text/ocr"
headers = {"Authorization": "KakaoAK {}".format(appkey)}
image = cv2.imread(image_path)
jpeg_image = cv2.imencode(".jpg", image)[1]
data = jpeg_image.tobytes()
return requests.post(API_URL, headers=headers, files={"image": data})
def main():
if len(sys.argv) != 3:
print("Please run with args: $ python example.py /path/to/image appkey")
image_path, appkey = sys.argv[1], sys.argv[2]
json_dict = dict()
for (path, dir, files) in os.walk(image_path):
files = list(files)
for file in files:
output = kakao_ocr(os.path.join(path, file), appkey).json()
json_dict[file.split(".jpg")[0]] = output
with open("./kakao.json", "w", encoding="UTF8") as result:
result.write(json.dumps(json_dict, indent="\t", ensure_ascii=False))
if __name__ == "__main__":
main()