Skip to content

Commit fee0450

Browse files
committed
Update code and dataset
1 parent a9885fe commit fee0450

3 files changed

Lines changed: 39 additions & 27 deletions

File tree

21.7 KB
Loading
46 KB
Loading

examples/official/semi_font_ocr/read_semi_ocr.py

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,37 +42,49 @@
4242
print("File not found: " + path)
4343
continue
4444
else:
45-
cv_image = cv2.imread(path)
46-
47-
result = cvr.capture(path, "recognize_semi_ocr")
45+
files_list = []
46+
47+
if os.path.isfile(path):
48+
files_list.append(path)
49+
elif os.path.isdir(path):
50+
files = os.listdir(path)
51+
for file in files:
52+
if file.endswith(".jpg") or file.endswith(".jpeg") or file.endswith(".png"):
53+
files_list.append(os.path.join(path, file))
4854

49-
# 4.Outputs the result.
50-
if result.get_error_code() != EnumErrorCode.EC_OK:
51-
print("Error: " + str(result.get_error_code())+ result.get_error_string())
52-
else:
53-
items = result.get_items()
54-
# print(f'read {len(items)} items')
55-
for item in items:
56-
if isinstance(item, TextLineResultItem):
57-
print(f"{RED}{item.get_text()}{RESET}")
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}")
5869

59-
location = item.get_location()
60-
x1 = location.points[0].x
61-
y1 = location.points[0].y
62-
x2 = location.points[1].x
63-
y2 = location.points[1].y
64-
x3 = location.points[2].x
65-
y3 = location.points[2].y
66-
x4 = location.points[3].x
67-
y4 = location.points[3].y
70+
location = item.get_location()
71+
x1 = location.points[0].x
72+
y1 = location.points[0].y
73+
x2 = location.points[1].x
74+
y2 = location.points[1].y
75+
x3 = location.points[2].x
76+
y3 = location.points[2].y
77+
x4 = location.points[3].x
78+
y4 = location.points[3].y
6879

69-
cv2.drawContours(
70-
cv_image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
80+
cv2.drawContours(
81+
cv_image, [np.intp([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
7182

72-
cv2.putText(cv_image, item.get_text(), (x1 + 10, y1 + 20),
73-
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
83+
cv2.putText(cv_image, item.get_text(), (x1 + 10, y1 + 20),
84+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
7485

75-
cv2.imshow(
76-
"SEMI-OCR", cv_image)
86+
cv2.imshow(
87+
os.path.basename(image_path), cv_image)
88+
7789
cv2.waitKey(0)
7890
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)