File size: 2,376 Bytes
00cade8
59a9100
 
5bd9c6c
00cade8
59a9100
 
 
 
00cade8
59a9100
 
 
 
 
 
 
 
 
 
5bd9c6c
 
59a9100
 
 
 
 
 
5bd9c6c
59a9100
5bd9c6c
59a9100
 
 
 
 
 
 
 
 
 
 
 
64eb379
 
59a9100
 
 
5bd9c6c
 
 
 
 
 
59a9100
5bd9c6c
 
 
59a9100
64eb379
 
59a9100
64eb379
 
 
 
 
 
 
 
 
 
 
 
 
59a9100
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import gradio as gr
import torch
from PIL import ImageDraw
import pathlib

# Model  
model_path = 'model_torch.pt'
model = torch.hub.load('Ultralytics/yolov5', 'custom', model_path, verbose = False)
model.eval()

labels = model.names

colors = ["red", "blue", "green", "yellow"]

def detect_objects(image):

    draw = ImageDraw.Draw(image)

    detections = model(image)

    probabilities = {}

    for detection in detections.xyxy[0]:
        
        x1, y1, x2, y2, p, category_id = detection
        x1, y1, x2, y2, category_id = int(x1), int(y1), int(x2), int(y2), int(category_id)
        draw.rectangle((x1, y1, x2, y2), outline=colors[category_id], width=4)
        draw.text((x1, y1), labels[category_id], colors[category_id])
        probabilities[labels[category_id]] = float(p)

    return [image, probabilities]


demo = gr.Blocks()#(css=css)

title = '# 3D print failures detection App'
description = 'App for detect errors in the 3D printing'

with demo:
    gr.Markdown(title)
    gr.Markdown(description)
    
    with gr.Tabs():

        #Image static
        with gr.TabItem('Image Upload'):
            with gr.Row():
                
                with gr.Column():
                    img_input = gr.Image(type='pil')
                    examples_images2 = gr.Examples(examples = [[path.as_posix()] for path in sorted(pathlib.Path('images').rglob('*.jpg'))],
                                               inputs=img_input)
                    labels_bars = gr.Label(label = "Categories")
                    print(labels_bars)
                
                with gr.Column():
                    img_output= gr.Image()

            img_button = gr.Button('Detect')

        img_button.click(detect_objects,inputs=img_input,outputs=[img_output, labels_bars])
        
        #Image static
        with gr.TabItem('Webcam'):
            with gr.Row():
                with gr.Column():
                    webcam_input = gr.Image(shape=(320,240), source='webcam', type="pil", )
                    webcam_output = gr.Image()
                with gr.Column():
                    labels_bars2 = gr.Label(label = "Categories")
                
            webcam_button = gr.Button('Detect')

        webcam_button.click(detect_objects,inputs=webcam_input,outputs=[webcam_output, labels_bars2])
    
    

if __name__ == "__main__":
    demo.launch()