-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
46 lines (40 loc) · 1.44 KB
/
test.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
import argparse
import yaml
import cv2
import numpy as np
from PIL import ImageFont, ImageDraw, Image
from core.PaddleOCR import PaddleOCR
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--params", type=str, default='data/params.yaml')
return parser.parse_args()
def draw_result(img, result):
img_rec = np.ones_like(img, np.uint8)*255
img_pil = Image.fromarray(img_rec)
draw = ImageDraw.Draw(img_pil)
fontpath = "font/simsun.ttc"
font = ImageFont.truetype(fontpath, 16)
for info in result:
bbox, rec_info = info
pts=np.array(bbox, np.int32)
pts=pts.reshape((-1,1,2))
cv2.polylines(img,[pts],True,(0,255,0),2)
txt = rec_info[0] + str(rec_info[1])
draw.text(tuple(pts[0][0]), txt, font=font, fill =(0,255,0))
bk_img = np.array(img_pil)
draw_img = np.hstack([img,bk_img])
return draw_img
if __name__ == '__main__':
args = parse_args()
with open(args.params) as f:
data_dict = yaml.load(f, Loader=yaml.FullLoader) # data dict
ocr_engine = PaddleOCR(data_dict)
img = cv2.imread(data_dict['image_dir'])
result = ocr_engine.ocr(img,
det=data_dict['det'],
rec=data_dict['rec'],
cls=data_dict['use_angle_cls'])
draw_img = draw_result(img, result)
cv2.imwrite('result.jpg', draw_img)
cv2.imshow("img", draw_img)
cv2.waitKey(0)