8
8
YELLOW = "\033 [33m"
9
9
RESET = "\033 [0m"
10
10
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 ():
15
37
err_code , err_str = LicenseManager .init_license ("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==" )
16
38
if err_code != EnumErrorCode .EC_OK and err_code != EnumErrorCode .EC_LICENSE_CACHE_USED :
17
39
print ("License initialization failed: ErrorCode: " + str (err_code ) + ", ErrorString: " + err_str )
18
40
else :
19
- # 2.Creates an instance of CaptureVisionRouter.
20
41
cvr = CaptureVisionRouter ()
21
42
with open ('models/semi-ocr.data' , 'rb' ) as f :
22
43
model_data = f .read ()
23
44
24
45
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 } " )
26
47
27
- # 3.Performs capture jobs(recognizing text lines) on an image
28
48
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
+
30
51
31
52
while True :
32
- files_list = []
33
53
path = input (
34
54
">> Input your image full path:\n "
35
55
">> 'Enter' for sample image or 'Q'/'q' to quit\n "
42
62
print ("File not found: " + path )
43
63
continue
44
64
else :
45
- files_list = []
46
-
47
65
if os .path .isfile (path ):
48
- files_list . append (path )
66
+ process_image (path , cvr )
49
67
elif os .path .isdir (path ):
50
68
files = os .listdir (path )
51
69
for file in files :
52
70
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 )
80
72
81
73
cv2 .waitKey (0 )
82
- cv2 .destroyAllWindows ()
74
+ cv2 .destroyAllWindows ()
75
+
76
+ if __name__ == '__main__' :
77
+ main ()
0 commit comments