testmail-gmail commited on
Commit
6f506cf
·
1 Parent(s): d712bb9

Adding passport_MRZ weight

Browse files
Files changed (1) hide show
  1. app.py +27 -9
app.py CHANGED
@@ -14,6 +14,8 @@ import keras_ocr
14
  import matplotlib.pyplot as plt
15
  from numpy import asarray
16
  import pybboxes as pbx
 
 
17
 
18
 
19
  from utils.datasets import LoadStreams, LoadImages
@@ -25,8 +27,8 @@ os.system("wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolo
25
  os.system("wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-e6.pt")
26
  pipeline = keras_ocr.pipeline.Pipeline()
27
 
28
- def detect_Custom(img,boundedImage):
29
- model='best' # Naming Convention for yolov7 See output file of https://www.kaggle.com/code/owaiskhan9654/training-yolov7-on-kaggle-on-custom-dataset/data
30
  parser = argparse.ArgumentParser()
31
  parser.add_argument('--weights', nargs='+', type=str, default=model+".pt", help='model.pt path(s)')
32
  parser.add_argument('--source', type=str, default='Inference/', help='source')
@@ -130,10 +132,19 @@ def detect_Custom(img,boundedImage):
130
 
131
  if save_img or view_img:
132
  label = f'{names[int(cls)]} {conf:.2f}'
 
 
 
 
 
 
 
 
 
133
  plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)
134
  W, H = 300, 300
135
  box_voc = pbx.convert_bbox(xyxy, from_type="yolo", to_type="voc", image_size=(W,H))
136
- print(box_voc)
137
  if view_img:
138
  cv2.imshow(str(p), im0)
139
  cv2.waitKey(1)
@@ -157,11 +168,18 @@ def detect_Custom(img,boundedImage):
157
  vid_writer.write(im0)
158
 
159
  output_text = ''
160
- images = [keras_ocr.tools.read(img) for img in [boundedImage]]
161
- prediction_groups = pipeline.recognize(images)
162
- first=prediction_groups[0]
163
- for text,box in first:
164
- output_text += ' '+ text
 
 
 
 
 
 
 
165
 
166
  if save_txt or save_img:
167
  s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else ''
@@ -189,4 +207,4 @@ examples1=[["Image1.jpeg", "Yolo_v7_Custom_trained_By_Owais"],["Image2.jpeg", "Y
189
  Top_Title="<center>Intelligent Image to Text - IIT </center></a>"
190
 
191
  css = ".output-image, .input-image, .image-preview {height: 300px !important}"
192
- gr.Interface(detect_Custom,[gr.Image(type="pil"),gr.Image(type="filepath")],[gr.Image(type="pil"),output],css=css,title=Top_Title,examples=examples1,description=Custom_description,article=Footer,cache_examples=False).launch()
 
14
  import matplotlib.pyplot as plt
15
  from numpy import asarray
16
  import pybboxes as pbx
17
+ import pytesseract
18
+ from datetime import date
19
 
20
 
21
  from utils.datasets import LoadStreams, LoadImages
 
27
  os.system("wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-e6.pt")
28
  pipeline = keras_ocr.pipeline.Pipeline()
29
 
30
+ def detect_Custom(img):
31
+ model='passport_mrz' # Naming Convention for yolov7 See output file of https://www.kaggle.com/code/owaiskhan9654/training-yolov7-on-kaggle-on-custom-dataset/data
32
  parser = argparse.ArgumentParser()
33
  parser.add_argument('--weights', nargs='+', type=str, default=model+".pt", help='model.pt path(s)')
34
  parser.add_argument('--source', type=str, default='Inference/', help='source')
 
132
 
133
  if save_img or view_img:
134
  label = f'{names[int(cls)]} {conf:.2f}'
135
+ if(cls == 1):
136
+ x1 = int(xyxy[0].item())
137
+ y1 = int(xyxy[1].item())
138
+ x2 = int(xyxy[2].item())
139
+ y2 = int(xyxy[3].item())
140
+
141
+ orig_img = im0
142
+ crop_img = im0[y1:y2, x1:x2]
143
+ cv2.imwrite('MRZ_1.png', crop_img)
144
  plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)
145
  W, H = 300, 300
146
  box_voc = pbx.convert_bbox(xyxy, from_type="yolo", to_type="voc", image_size=(W,H))
147
+
148
  if view_img:
149
  cv2.imshow(str(p), im0)
150
  cv2.waitKey(1)
 
168
  vid_writer.write(im0)
169
 
170
  output_text = ''
171
+ text = pytesseract.image_to_string(Image.open('MRZ_1.png'))
172
+ text = text.replace(" ", "")
173
+ text=text[22:28]
174
+ today = date.today()
175
+ s = today.strftime('%Y%m%d')[2:]
176
+ if(text > s):
177
+ output_text = 'This is a Valid Passport'
178
+ #images = [keras_ocr.tools.read(img) for img in [boundedImage]]
179
+ #prediction_groups = pipeline.recognize(images)
180
+ #first=prediction_groups[0]
181
+ #for text,box in first:
182
+ #output_text += ' '+ text
183
 
184
  if save_txt or save_img:
185
  s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else ''
 
207
  Top_Title="<center>Intelligent Image to Text - IIT </center></a>"
208
 
209
  css = ".output-image, .input-image, .image-preview {height: 300px !important}"
210
+ gr.Interface(detect_Custom,[gr.Image(type="pil")],[gr.Image(type="pil"),output],css=css,title=Top_Title,examples=examples1,description=Custom_description,article=Footer,cache_examples=False).launch()