step1.py
2.12 KB
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
import json
import os
import base64
import requests
import cv2
import time
import numpy as np
from PIL import Image, ImageDraw, ImageFont
base_dir = os.path.dirname(os.path.abspath(__file__))
img_dir = '/home/zwq/data/gcfp/valid/image'
draw_dir = os.path.join(base_dir, 'draw', 'valid')
sign_dir = os.path.join(base_dir, 'sign_res', 'valid')
go_dir = os.path.join(base_dir, 'go_res', 'valid')
font_path = os.path.join(base_dir, 'simhei.ttf')
font = ImageFont.truetype(font_path, 10, encoding="utf-8")
for image_name in os.listdir(img_dir):
print('start: {0}'.format(image_name))
base_image_name, _ = os.path.splitext(image_name)
image_path = os.path.join(img_dir, image_name)
output_path = os.path.join(draw_dir, image_name)
go_res_path = os.path.join(go_dir, '{0}.json'.format(base_image_name))
sign_res_path = os.path.join(sign_dir, '{0}.json'.format(base_image_name))
go_response = requests.post(url=r'http://139.196.149.46:9001/gen_ocr', files={'file': open(image_path, 'rb')})
go_res = go_response.json()['ocr_results']
# print(go_res)
with open(go_res_path, 'w') as fp:
json.dump(go_res, fp, ensure_ascii=False)
img = cv2.imread(image_path)
for coordinates, text in go_res.values():
# print(coordinates)
# print(text)
cv2.rectangle(img, (coordinates[0], coordinates[1]), (coordinates[4], coordinates[5]), (0, 255, 0), 2)
pil_img = Image.fromarray(img)
draw = ImageDraw.Draw(pil_img)
draw.text((coordinates[0], coordinates[1]), text, (255, 0, 0), font=font)
img = np.array(pil_img)
cv2.imwrite(output_path, img)
sign_response = requests.post(url=r'http://139.196.149.46:9001/signature_detect', files={'file': open(image_path, 'rb')})
signature_res = sign_response.json()
with open(sign_res_path, 'w') as fp:
json.dump(signature_res, fp, ensure_ascii=False)
# print(signature_res)
# start_time = time.time()
# res = retriever_individuals.get_target_fields(go_res, signature_res)
# print(res)
# end_time = time.time()
# print('time: {0}'.format(end_time - start_time))
# break