Commit 670438b9 by 최애림

-이미지 경로를 파라미터로 받아 ocr 인식결과를 반환하는 run_ocr 함수 생성

- ocr인식 결과, 저장할 csv파일명을 파라미터로 받아 다운로드 실행하는
download_csv_file 함수 생성
- 이미지 폴더 목록, 결과 파일 저장 경로를 파라미터로 받아 위의 함수들을
호출하여 csv로 반환받는 main함수 생성
parent 20c840b1
import os
import requests import requests
import pprint import json
import os
# print(os.getcwd())
############################# 인식 수행 #########################################
# 엔진 경로 설정 # 엔진 경로 설정
engine_address = 'http://192.168.1.149:62975' ENGINE_ADDRESS = 'http://192.168.1.149:62975'
ocr_sdk_path = '/sdk/ocr'
url = engine_address + ocr_sdk_path
# 파라미터 지정
img_path = '../../../Downloads/0025_0023.jpg'
data = {
'api_key': 'SNOCR-0fb7010e88fd4ad99033947351711c43',
'type': 'local',
'boxes_type': 'all',
'path': img_path,
'lang': 'all',
'recog_form': 'true', # 서식 인식 여부
}
# 인식
response = requests.post(url, data=data)
# 출력
# print(response.status_code)
# print(response.text)
pprint.pprint(response.json())
result = response.json()
# csv파일 이름 받기 def run_ocr(image_path):
print(result['result']['csv_file_name']) global ENGINE_ADDRESS
# 경로 설정
run_address = '/sdk/ocr'
url = ENGINE_ADDRESS + run_address
############################# 여러개 인식 수행 ######################################### # 파라미터 지정
# 경로 설정 data = {
base_path = '../../../Downloads/' 'api_key': 'SNOCR-0fb7010e88fd4ad99033947351711c43',
img_paths = [base_path + file for file in os.listdir(base_path) if file.split('.')[1] == 'jpg'] 'type': 'local',
print(img_paths) 'boxes_type': 'all',
'path': image_path,
'lang': 'all',
'recog_form': 'true' # 서식 인식 여부
}
# 반복 인식 # 인식
for img_path in img_paths: response = requests.post(url, data=data)
data['path'] = img_path result = response.json()
res = requests.post(url, data=data) # result_dict = json.loads(result)
print(res.json())
############################# 인식 후 csv파일 저장 #########################################
# 경로 설정 return result
ocr_file_path = '/sdk/out/' + result['result']['csv_file_name']
url2 = engine_address + ocr_file_path
# 파라미터 설정
data2 = {'api_key': 'SNOCR-0fb7010e88fd4ad99033947351711c43'}
# 받아오기 def download_csv_file(ocr_result, output_file_path):
response2 = requests.post(url2, data = data2) global ENGINE_ADDRESS
res_content = response2.content
# 저장 # 경로 설정
with open('test.csv', 'wb') as csv_file: ocr_file_address = '/sdk/out/' + ocr_result['result']['csv_file_name']
csv_file.write(res_content) url = ENGINE_ADDRESS + ocr_file_address
# 파라미터 지정
data = {'api_key': 'SNOCR-0fb7010e88fd4ad99033947351711c43'}
# 파일 받고 저장
response = requests.post(url, data=data)
res_content = response.content
with open(output_file_path + '.csv', 'wb') as csv_file:
csv_file.write(res_content)
def main(image_dir_path, output_dir_path):
extension_li = ['png', 'jpg', 'jpeg']
file_names = [file_name for file_name in os.listdir(image_dir_path) if file_name.split('.')[1] in extension_li]
############################# 여러개 인식 후 csv파일 저장 ######################################### # 반복 인식 후 저장
# 경로 설정: jpg 파일만 리스트화 for file_name in file_names:
base_path = '../../../Downloads/' # 인식
img_paths = [base_path + file for file in os.listdir(base_path) if file.split('.')[1] == 'jpg'] img_path = os.path.join(image_dir_path, file_name)
ocr_result = run_ocr(img_path)
# 반복 인식 후 저장 # csv파일 받고 저장
for img_path in img_paths: output_file_path = os.path.join(output_dir_path, file_name.split('.')[0])
# 인식 download_csv_file(ocr_result, output_file_path)
data['path'] = img_path
res = requests.post(url, data=data)
result2 = res.json()['result']['csv_file_name']
# csv파일 받기
res2 = requests.post(engine_address + '/sdk/out/' + result2, data=data2)
res_content2 = res2.content
# 바이트를 문자로 변환 후 저장
save_name = img_path[-13:-4]
with open('test' + save_name + '.csv', 'wb') as w:
w.write(res_content2)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment