@@ -52,21 +52,50 @@ def ocrOutput(self, img_name, img, bbox):
52
52
it saves the txt outputs as a json format
53
53
"""
54
54
crop_img_names = cropRoi (img , bbox , self .denoise )
55
+ print ("crop img names:" , crop_img_names )
55
56
id_infos = ["Tc" , "Surname" , "Name" , "DateofBirth" ]
56
57
jsonData = JsonData ()
57
-
58
+ text_output = { "Tc" : "" , "Surname" : "" , "Name" : "" , "DateofBirth" : "" }
58
59
for info , img in zip (id_infos , crop_img_names ):
59
60
result = self .reader .readtext (img )
60
61
if (len (result )):
61
62
box , text , prob = result [0 ]
62
- jsonData .text_output [info ] = text .upper ()
63
- jsonData .text_output ["DateofBirth" ] = getonlyDigits (jsonData .text_output ["DateofBirth" ])
63
+ text_output [info ] = text .upper ()
64
+
65
+ text_output ["DateofBirth" ] = getonlyDigits (text_output ["DateofBirth" ])
64
66
65
- CardInfo [img_name ] = jsonData . text_output
67
+ CardInfo [img_name ] = text_output
66
68
jsonData .saveDict (CardInfo )
67
69
68
- return jsonData . text_output
70
+ return text_output
69
71
72
+ class TesseractOcr :
73
+
74
+ def __init__ (self , border_thresh , denoise ,) -> None :
75
+ self .denoise = denoise
76
+ self .BORDER_THRSH = border_thresh
77
+
78
+
79
+
80
+ def ocrOutput (self , img_name , img , bbox ):
81
+ """
82
+ it saves the txt outputs as a json format
83
+ """
84
+ crop_img_names = cropRoi (img , bbox , self .denoise )
85
+ id_infos = ["Tc" , "Surname" , "Name" , "DateofBirth" ]
86
+ jsonData = JsonData ()
87
+ text_output = {"Tc" :"" , "Surname" :"" , "Name" :"" , "DateofBirth" :"" }
88
+ for info , img in zip (id_infos , crop_img_names ):
89
+ text = pytesseract .image_to_string (img )
90
+
91
+ text_output [info ] = text .upper ()
92
+
93
+ text_output ["DateofBirth" ] = getonlyDigits (text_output ["DateofBirth" ])
94
+
95
+ CardInfo [img_name ] = text_output
96
+ jsonData .saveDict (CardInfo )
97
+
98
+ return text_output
70
99
71
100
def cropRoi (img , bbox , denoise ):
72
101
@@ -84,9 +113,9 @@ def cropRoi(img, bbox, denoise):
84
113
85
114
if not os .path .exists ("outputs/target_crops/" ):
86
115
os .makedirs ("outputs/target_crops/" )
87
- crop_name = "outputs/target_crops/" + str (info ) + ".jpg"
88
- plt .imsave (crop_name , crop_img )
89
- crop_img_names .append (crop_name )
116
+ crop_name = "outputs/target_crops/" + str (info ) + ".jpg"
117
+ plt .imsave (crop_name , crop_img )
118
+ crop_img_names .append (crop_name )
90
119
91
120
return crop_img_names
92
121
@@ -116,32 +145,7 @@ def denoiseImage(img):
116
145
117
146
118
147
119
- class TesseractOcr :
120
-
121
- def __init__ (self , border_thresh , denoise ,) -> None :
122
- self .denoise = denoise
123
- self .BORDER_THRSH = border_thresh
124
-
125
-
126
- def ocrOutput (self , img_name , img , bbox ):
127
- """
128
- it saves the txt outputs as a json format
129
- """
130
- crop_img_names = cropRoi (img , bbox , self .denoise )
131
- id_infos = ["Tc" , "Surname" , "Name" , "DateofBirth" ]
132
- jsonData = JsonData ()
133
-
134
- for info , img in zip (id_infos , crop_img_names ):
135
- text = pytesseract .image_to_string (img )
136
-
137
- jsonData .text_output [info ] = text .upper ()
138
-
139
- jsonData .text_output ["DateofBirth" ] = getonlyDigits (jsonData .text_output ["DateofBirth" ])
140
-
141
- CardInfo [img_name ] = jsonData .text_output
142
- jsonData .saveDict (CardInfo )
143
-
144
- return jsonData .text_output
148
+
145
149
146
150
def factory (ocr_method = "EasyOcr" , border_thresh = 3 , denoise = False ):
147
151
ocr_factory = {"EasyOcr" : EasyOcr ,
0 commit comments