Spaces:
Runtime error
Runtime error
testmail-gmail
commited on
Commit
·
73df16c
1
Parent(s):
f7cedb3
Update app.py
Browse files
app.py
CHANGED
@@ -4,11 +4,18 @@ import time
|
|
4 |
import torch
|
5 |
import argparse
|
6 |
import gradio as gr
|
|
|
7 |
from PIL import Image
|
8 |
from numpy import random
|
9 |
from pathlib import Path
|
10 |
import torch.backends.cudnn as cudnn
|
11 |
from models.experimental import attempt_load
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
from utils.datasets import LoadStreams, LoadImages
|
14 |
from utils.general import check_img_size, check_requirements, check_imshow, non_max_suppression, apply_classifier, \
|
@@ -18,14 +25,13 @@ from utils.torch_utils import select_device, load_classifier, time_synchronized,
|
|
18 |
os.system("wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt")
|
19 |
os.system("wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-e6.pt")
|
20 |
|
21 |
-
def detect_Custom(img
|
22 |
-
|
23 |
-
model='best' # Naming Convention for yolov7 See output file of https://www.kaggle.com/code/owaiskhan9654/training-yolov7-on-kaggle-on-custom-dataset/data
|
24 |
parser = argparse.ArgumentParser()
|
25 |
parser.add_argument('--weights', nargs='+', type=str, default=model+".pt", help='model.pt path(s)')
|
26 |
parser.add_argument('--source', type=str, default='Inference/', help='source')
|
27 |
parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
|
28 |
-
parser.add_argument('--conf-thres', type=float, default=0.
|
29 |
parser.add_argument('--iou-thres', type=float, default=0.45, help='IOU threshold for NMS')
|
30 |
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
31 |
parser.add_argument('--view-img', action='store_true', help='display results')
|
@@ -84,8 +90,8 @@ def detect_Custom(img,model):
|
|
84 |
|
85 |
# Inference
|
86 |
t1 = time_synchronized()
|
87 |
-
|
88 |
-
|
89 |
|
90 |
pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, classes=opt.classes, agnostic=opt.agnostic_nms)
|
91 |
t2 = time_synchronized()
|
@@ -93,7 +99,7 @@ def detect_Custom(img,model):
|
|
93 |
|
94 |
if classify:
|
95 |
pred = apply_classifier(pred, modelc, img, im0s)
|
96 |
-
|
97 |
for i, det in enumerate(pred):
|
98 |
if webcam:
|
99 |
p, s, im0, frame = path[i], '%g: ' % i, im0s[i].copy(), dataset.count
|
@@ -124,6 +130,15 @@ def detect_Custom(img,model):
|
|
124 |
if save_img or view_img:
|
125 |
label = f'{names[int(cls)]} {conf:.2f}'
|
126 |
plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
if view_img:
|
128 |
cv2.imshow(str(p), im0)
|
129 |
cv2.waitKey(1)
|
@@ -146,16 +161,25 @@ def detect_Custom(img,model):
|
|
146 |
vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
|
147 |
vid_writer.write(im0)
|
148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
if save_txt or save_img:
|
150 |
s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else ''
|
151 |
|
152 |
print(f'Done. ({time.time() - t0:.3f}s)')
|
153 |
|
154 |
-
return Image.fromarray(im0[:,:,::-1])
|
155 |
|
156 |
|
157 |
-
|
158 |
-
Custom_description="<center>Custom Training Performed on
|
159 |
|
160 |
Footer = (
|
161 |
"<center>Model Trained by: Owais Ahmad Data Scientist at <b> Thoucentric </b> <a href=\"https://www.linkedin.com/in/owaiskhan9654/\">Visit Profile</a> <br></center>"
|
@@ -169,7 +193,7 @@ Footer = (
|
|
169 |
|
170 |
examples1=[["Image1.jpeg", "Yolo_v7_Custom_trained_By_Owais"],["Image2.jpeg", "Yolo_v7_Custom_trained_By_Owais"],["Image3.jpeg", "Yolo_v7_Custom_trained_By_Owais",],["Image4.jpeg", "Yolo_v7_Custom_trained_By_Owais"],["Image5.jpeg", "Yolo_v7_Custom_trained_By_Owais"],["Image6.jpeg", "Yolo_v7_Custom_trained_By_Owais"],["horses.jpeg", "yolov7"],["horses.jpeg", "yolov7-e6"]]
|
171 |
|
172 |
-
Top_Title="<center>
|
173 |
|
174 |
css = ".output-image, .input-image, .image-preview {height: 300px !important}"
|
175 |
-
gr.Interface(detect_Custom,
|
|
|
4 |
import torch
|
5 |
import argparse
|
6 |
import gradio as gr
|
7 |
+
import io
|
8 |
from PIL import Image
|
9 |
from numpy import random
|
10 |
from pathlib import Path
|
11 |
import torch.backends.cudnn as cudnn
|
12 |
from models.experimental import attempt_load
|
13 |
+
import keras_ocr
|
14 |
+
import matplotlib.pyplot as plt
|
15 |
+
from numpy import asarray
|
16 |
+
import pytesseract
|
17 |
+
from datetime import date
|
18 |
+
|
19 |
|
20 |
from utils.datasets import LoadStreams, LoadImages
|
21 |
from utils.general import check_img_size, check_requirements, check_imshow, non_max_suppression, apply_classifier, \
|
|
|
25 |
os.system("wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt")
|
26 |
os.system("wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-e6.pt")
|
27 |
|
28 |
+
def detect_Custom(img):
|
29 |
+
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
|
|
|
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')
|
33 |
parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
|
34 |
+
parser.add_argument('--conf-thres', type=float, default=0.45, help='object confidence threshold')
|
35 |
parser.add_argument('--iou-thres', type=float, default=0.45, help='IOU threshold for NMS')
|
36 |
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
37 |
parser.add_argument('--view-img', action='store_true', help='display results')
|
|
|
90 |
|
91 |
# Inference
|
92 |
t1 = time_synchronized()
|
93 |
+
with torch.no_grad():
|
94 |
+
pred = model(img, augment=opt.augment)[0]
|
95 |
|
96 |
pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, classes=opt.classes, agnostic=opt.agnostic_nms)
|
97 |
t2 = time_synchronized()
|
|
|
99 |
|
100 |
if classify:
|
101 |
pred = apply_classifier(pred, modelc, img, im0s)
|
102 |
+
|
103 |
for i, det in enumerate(pred):
|
104 |
if webcam:
|
105 |
p, s, im0, frame = path[i], '%g: ' % i, im0s[i].copy(), dataset.count
|
|
|
130 |
if save_img or view_img:
|
131 |
label = f'{names[int(cls)]} {conf:.2f}'
|
132 |
plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)
|
133 |
+
if(cls == 1):
|
134 |
+
x1 = int(xyxy[0].item())
|
135 |
+
y1 = int(xyxy[1].item())
|
136 |
+
x2 = int(xyxy[2].item())
|
137 |
+
y2 = int(xyxy[3].item())
|
138 |
+
orig_img = im0
|
139 |
+
crop_img = im0[y1:y2, x1:x2]
|
140 |
+
cv2.imwrite('MRZ_1.png', crop_img)
|
141 |
+
|
142 |
if view_img:
|
143 |
cv2.imshow(str(p), im0)
|
144 |
cv2.waitKey(1)
|
|
|
161 |
vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
|
162 |
vid_writer.write(im0)
|
163 |
|
164 |
+
output_text = 'This is not a valid Passport'
|
165 |
+
text = pytesseract.image_to_string(Image.open('MRZ_1.png'))
|
166 |
+
text = text.replace(" ", "")
|
167 |
+
text=text[22:28]
|
168 |
+
today = date.today()
|
169 |
+
s = today.strftime('%Y%m%d')[2:]
|
170 |
+
if(text > s):
|
171 |
+
output_text = 'This is a Valid Passport'
|
172 |
+
|
173 |
if save_txt or save_img:
|
174 |
s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else ''
|
175 |
|
176 |
print(f'Done. ({time.time() - t0:.3f}s)')
|
177 |
|
178 |
+
return [Image.fromarray(im0[:,:,::-1]), output_text]
|
179 |
|
180 |
|
181 |
+
output = gr.Textbox(label="Validation",elem_id="opbox")
|
182 |
+
Custom_description="<center>Custom Training Performed on Colab <a href='https://colab.research.google.com/github/roboflow-ai/notebooks/blob/main/notebooks/train-yolov7-object-detection-on-custom-data.ipynb?authuser=2#scrollTo=1iqOPKjr22mL' style='text-decoration: underline' target='_blank'>Link</a> </center><br> <center>Model trained with test dataset of 'aadhar-card', 'credit-card','prescription' and 'passport' </center>"
|
183 |
|
184 |
Footer = (
|
185 |
"<center>Model Trained by: Owais Ahmad Data Scientist at <b> Thoucentric </b> <a href=\"https://www.linkedin.com/in/owaiskhan9654/\">Visit Profile</a> <br></center>"
|
|
|
193 |
|
194 |
examples1=[["Image1.jpeg", "Yolo_v7_Custom_trained_By_Owais"],["Image2.jpeg", "Yolo_v7_Custom_trained_By_Owais"],["Image3.jpeg", "Yolo_v7_Custom_trained_By_Owais",],["Image4.jpeg", "Yolo_v7_Custom_trained_By_Owais"],["Image5.jpeg", "Yolo_v7_Custom_trained_By_Owais"],["Image6.jpeg", "Yolo_v7_Custom_trained_By_Owais"],["horses.jpeg", "yolov7"],["horses.jpeg", "yolov7-e6"]]
|
195 |
|
196 |
+
Top_Title="<center>Intelligent Image to Text - IIT </center></a>"
|
197 |
|
198 |
css = ".output-image, .input-image, .image-preview {height: 300px !important}"
|
199 |
+
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()
|