Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
from PIL import Image
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
model = pipeline('object-detection')
|
7 |
+
|
8 |
+
def draw_box(image):
|
9 |
+
img = cv2.imread(image)
|
10 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
11 |
+
test = model(image)
|
12 |
+
|
13 |
+
for objects in test:
|
14 |
+
if objects['score'] < .5:
|
15 |
+
continue
|
16 |
+
|
17 |
+
coord = objects['box']
|
18 |
+
label = objects['label']
|
19 |
+
color = (0,0,255)
|
20 |
+
img = cv2.rectangle(img, (coord['xmin'],coord['ymin']) , (coord['xmax'],coord['ymax']), color,1 )
|
21 |
+
img = cv2.putText(img,label,(coord['xmin'], coord['ymin']-10), cv2.FONT_HERSHEY_PLAIN, 1, color , 2)
|
22 |
+
|
23 |
+
return img
|
24 |
+
|
25 |
+
|
26 |
+
with gr.Blocks() as demo:
|
27 |
+
|
28 |
+
|
29 |
+
gr.Markdown("""# Object Detection using the Transformers library <br>
|
30 |
+
Enter an Image on the left and view the localized objects on the right.
|
31 |
+
""")
|
32 |
+
|
33 |
+
with gr.Row():
|
34 |
+
inp = gr.Image( type='filepath')
|
35 |
+
out = gr.Image()
|
36 |
+
btn = gr.Button('Detect Objects')
|
37 |
+
|
38 |
+
|
39 |
+
btn.click(fn = draw_box, inputs = inp, outputs = out)
|
40 |
+
|
41 |
+
demo.launch()
|
42 |
+
|
43 |
+
|