Skip to content

Commit 4fa4a21

Browse files
committed
Update
1 parent 97a450c commit 4fa4a21

File tree

1 file changed

+35
-40
lines changed

1 file changed

+35
-40
lines changed

examples/official/semi_font_ocr/read_semi_ocr.py

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,48 @@
88
YELLOW = "\033[33m"
99
RESET = "\033[0m"
1010

11-
if __name__ == '__main__':
12-
# 1.Initializes license.
13-
# You can request or extend a trial license at: https://www.dynamsoft.com/customer/license/trialLicense?product=dcv&utm_source=samples&package=python
14-
# The string below is a free public trial license. Note: an active internet connection is required for this license to work.
11+
12+
def process_image(image_path, cvr):
13+
cv_image = cv2.imread(image_path)
14+
15+
result = cvr.capture(image_path, "recognize_semi_ocr")
16+
17+
if result.get_error_code() != EnumErrorCode.EC_OK:
18+
print("Error: " + str(result.get_error_code())+ result.get_error_string())
19+
else:
20+
items = result.get_items()
21+
for item in items:
22+
if isinstance(item, TextLineResultItem):
23+
print(f"{RED}{item.get_text()}{RESET}")
24+
25+
location = item.get_location()
26+
points = [(p.x, p.y) for p in location.points]
27+
cv2.drawContours(cv_image, [np.intp(points)], 0, (0, 255, 0), 2)
28+
29+
cv2.putText(cv_image, item.get_text(), (points[0][0] + 10, points[0][1] + 20),
30+
31+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
32+
33+
cv2.imshow(
34+
os.path.basename(image_path), cv_image)
35+
36+
def main():
1537
err_code, err_str = LicenseManager.init_license("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
1638
if err_code != EnumErrorCode.EC_OK and err_code != EnumErrorCode.EC_LICENSE_CACHE_USED:
1739
print("License initialization failed: ErrorCode: " + str(err_code) + ", ErrorString: " + err_str)
1840
else:
19-
# 2.Creates an instance of CaptureVisionRouter.
2041
cvr = CaptureVisionRouter()
2142
with open('models/semi-ocr.data', 'rb') as f:
2243
model_data = f.read()
2344

2445
err_code, err_str = cvr.append_model_buffer('semi-ocr', model_data, 1)
25-
print("Model initialization: ErrorCode: " + str(err_code) + ", ErrorString: " + err_str)
46+
print(f"{GREEN}Model initialization: ErrorCode: " + str(err_code) + ", ErrorString: " + err_str + f"{RESET}")
2647

27-
# 3.Performs capture jobs(recognizing text lines) on an image
2848
err_code, err_str = cvr.init_settings_from_file("semi-ocr.json")
29-
print("Template initialization: ErrorCode: " + str(err_code) + ", ErrorString: " + err_str)
49+
print(f"{GREEN}Template initialization: ErrorCode: " + str(err_code) + ", ErrorString: " + err_str + f"{RESET}")
50+
3051

3152
while True:
32-
files_list = []
3353
path = input(
3454
">> Input your image full path:\n"
3555
">> 'Enter' for sample image or 'Q'/'q' to quit\n"
@@ -42,41 +62,16 @@
4262
print("File not found: " + path)
4363
continue
4464
else:
45-
files_list = []
46-
4765
if os.path.isfile(path):
48-
files_list.append(path)
66+
process_image(path, cvr)
4967
elif os.path.isdir(path):
5068
files = os.listdir(path)
5169
for file in files:
5270
if file.endswith(".jpg") or file.endswith(".jpeg") or file.endswith(".png"):
53-
files_list.append(os.path.join(path, file))
54-
55-
for image_path in files_list:
56-
cv_image = cv2.imread(image_path)
57-
58-
result = cvr.capture(image_path, "recognize_semi_ocr")
59-
60-
# 4.Outputs the result.
61-
if result.get_error_code() != EnumErrorCode.EC_OK:
62-
print("Error: " + str(result.get_error_code())+ result.get_error_string())
63-
else:
64-
items = result.get_items()
65-
# print(f'read {len(items)} items')
66-
for item in items:
67-
if isinstance(item, TextLineResultItem):
68-
print(f"{RED}{item.get_text()}{RESET}")
69-
70-
location = item.get_location()
71-
points = [(p.x, p.y) for p in location.points]
72-
cv2.drawContours(cv_image, [np.intp(points)], 0, (0, 255, 0), 2)
73-
74-
cv2.putText(cv_image, item.get_text(), (points[0][0] + 10, points[0][1] + 20),
75-
76-
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
77-
78-
cv2.imshow(
79-
os.path.basename(image_path), cv_image)
71+
process_image(os.path.join(path, file), cvr)
8072

8173
cv2.waitKey(0)
82-
cv2.destroyAllWindows()
74+
cv2.destroyAllWindows()
75+
76+
if __name__ == '__main__':
77+
main()

0 commit comments

Comments
 (0)