1
1
import os
2
2
from dynamsoft_capture_vision_bundle import *
3
+ import cv2
4
+ import numpy as np
5
+
6
+ RED = "\033 [31m"
7
+ GREEN = "\033 [32m"
8
+ YELLOW = "\033 [33m"
9
+ RESET = "\033 [0m"
3
10
4
11
if __name__ == '__main__' :
5
12
# 1.Initializes license.
21
28
err_code , err_str = cvr .init_settings_from_file ("semi-ocr.json" )
22
29
print ("Template initialization: ErrorCode: " + str (err_code ) + ", ErrorString: " + err_str )
23
30
24
- files_list = []
25
31
while True :
26
- path = input ("Please enter the image file/directory path: " ).strip ('\' "' )
32
+ files_list = []
33
+ path = input (
34
+ ">> Input your image full path:\n "
35
+ ">> 'Enter' for sample image or 'Q'/'q' to quit\n "
36
+ ).strip ('\' "' )
37
+
38
+ if path .lower () == "q" :
39
+ sys .exit (0 )
40
+
27
41
if not os .path .exists (path ):
28
42
print ("File not found: " + path )
43
+ continue
29
44
else :
30
- if os .path .isfile (path ):
31
- files_list .append (path )
32
- elif os .path .isdir (path ):
33
- files = os .listdir (path )
34
- for file in files :
35
- if file .endswith (".jpg" ) or file .endswith (".jpeg" ) or file .endswith (".png" ):
36
- files_list .append (os .path .join (path , file ))
37
- break
38
-
39
- for file_path in files_list :
40
- result = cvr .capture (file_path , "recognize_semi_ocr" )
45
+ cv_image = cv2 .imread (path )
46
+
47
+ result = cvr .capture (path , "recognize_semi_ocr" )
48
+
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 } " )
41
58
42
- print ("File: " + file_path )
43
-
44
- # 4.Outputs the result.
45
- if result .get_error_code () != EnumErrorCode .EC_OK :
46
- print ("Error: " + str (result .get_error_code ())+ result .get_error_string ())
47
- else :
48
- items = result .get_items ()
49
- # print(f'read {len(items)} items')
50
- for item in items :
51
- if isinstance (item , TextLineResultItem ):
52
- print (item .get_text ())
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
68
+
69
+ cv2 .drawContours (
70
+ cv_image , [np .intp ([(x1 , y1 ), (x2 , y2 ), (x3 , y3 ), (x4 , y4 )])], 0 , (0 , 255 , 0 ), 2 )
71
+
72
+ cv2 .putText (cv_image , item .get_text (), (x1 + 10 , y1 + 20 ),
73
+ cv2 .FONT_HERSHEY_SIMPLEX , 0.5 , (0 , 0 , 255 ), 1 )
74
+
75
+ cv2 .imshow (
76
+ "SEMI-OCR" , cv_image )
77
+ cv2 .waitKey (0 )
78
+ cv2 .destroyAllWindows ()
0 commit comments