Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from object_detector import DocumentObjects
|
2 |
+
import cv2
|
3 |
+
import gradio as gr
|
4 |
+
import uuid,os
|
5 |
+
|
6 |
+
detector = DocumentObjects()
|
7 |
+
print('Detector ready')
|
8 |
+
|
9 |
+
def showStamp(path,bounding_boxes,output_path=None):
|
10 |
+
image = cv2.imread(path)
|
11 |
+
for box in bounding_boxes:
|
12 |
+
x1, y1, x2, y2 = map(int, box)
|
13 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
14 |
+
cv2.putText(image, 'stamp', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
15 |
+
|
16 |
+
if output_path:
|
17 |
+
input_img = Image.fromarray(image)
|
18 |
+
input_img.save(output_path)
|
19 |
+
else:
|
20 |
+
return image
|
21 |
+
|
22 |
+
def inference(input_img):
|
23 |
+
|
24 |
+
input_img = Image.fromarray(input_img)
|
25 |
+
file_id = str(uuid.uuid4())
|
26 |
+
file_name = f'Img_{file_id}.png'
|
27 |
+
input_img.save(file_name)
|
28 |
+
|
29 |
+
resp = detector.detect_objects(file_name)
|
30 |
+
found = resp['stamp']['found']
|
31 |
+
coordinates = resp['stamp']['loc']
|
32 |
+
annotated_img = showStamp(file_name,coordinates)
|
33 |
+
os.remove(file_name)
|
34 |
+
|
35 |
+
if found:
|
36 |
+
return annotated_img, 'Stamp found'
|
37 |
+
else:
|
38 |
+
return annotated_img, 'Stamp not found'
|
39 |
+
|
40 |
+
title = "Stamp Detection usong YOLOV9"
|
41 |
+
description = "A simple Gradio interface to infer on Yolo model trained on custom data for stamp detection"
|
42 |
+
examples = [
|
43 |
+
'example1.png',
|
44 |
+
'example2.jpeg'
|
45 |
+
]
|
46 |
+
demo = gr.Interface(
|
47 |
+
inference,
|
48 |
+
inputs = [
|
49 |
+
gr.Image(label="Input Image"),
|
50 |
+
],
|
51 |
+
outputs = [
|
52 |
+
gr.Image(label="Output Image"),
|
53 |
+
gr.Label()
|
54 |
+
],
|
55 |
+
title = title,
|
56 |
+
description = description,
|
57 |
+
examples = examples,
|
58 |
+
)
|
59 |
+
demo.launch(debug=True)
|