Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,59 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from PIL import ImageDraw
|
4 |
|
5 |
+
# Model
|
6 |
+
model_path = 'model_torch.pt'
|
7 |
+
model = torch.hub.load('Ultralytics/yolov5', 'custom', model_path, verbose = False)
|
8 |
+
model.eval()
|
9 |
|
10 |
+
labels = model.names
|
11 |
+
|
12 |
+
colors = ["red", "blue", "green", "yellow"]
|
13 |
+
|
14 |
+
def detect_objects(image):
|
15 |
+
|
16 |
+
draw = ImageDraw.Draw(image)
|
17 |
+
|
18 |
+
detections = model(image)
|
19 |
+
|
20 |
+
for detection in detections.xyxy[0]:
|
21 |
+
|
22 |
+
x1, y1, x2, y2, p, category_id = detection
|
23 |
+
x1, y1, x2, y2, category_id = int(x1), int(y1), int(x2), int(y2), int(category_id)
|
24 |
+
draw.rectangle((x1, y1, x2, y2), outline=colors[category_id], width=4)
|
25 |
+
draw.text((x1, y1), labels[category_id], colors[category_id])
|
26 |
+
|
27 |
+
return image
|
28 |
+
|
29 |
+
|
30 |
+
demo = gr.Blocks()#(css=css)
|
31 |
+
|
32 |
+
title = '# 3D print failures detection App'
|
33 |
+
description = 'App for detect errors in the 3D printing'
|
34 |
+
|
35 |
+
urls = ["https://c8.alamy.com/comp/J2AB4K/the-new-york-stock-exchange-on-the-wall-street-in-new-york-J2AB4K.jpg"]
|
36 |
+
|
37 |
+
with demo:
|
38 |
+
gr.Markdown(title)
|
39 |
+
gr.Markdown(description)
|
40 |
+
|
41 |
+
with gr.Tabs():
|
42 |
+
with gr.TabItem('Image Upload'):
|
43 |
+
with gr.Row():
|
44 |
+
img_input = gr.Image(type='pil')
|
45 |
+
img_output= gr.Image()
|
46 |
+
|
47 |
+
#with gr.Row():
|
48 |
+
# example_images = gr.Dataset(components=[img_input],
|
49 |
+
# samples=[[path.as_posix()]
|
50 |
+
# for path in sorted(pathlib.Path('images').rglob('*.JPG'))])
|
51 |
+
|
52 |
+
img_button = gr.Button('Detect')
|
53 |
+
|
54 |
+
|
55 |
+
img_button.click(detect_objects,inputs=img_input,outputs=img_output)
|
56 |
+
|
57 |
+
|
58 |
+
if __name__ == "__main__":
|
59 |
+
demo.launch()
|