Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,50 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import torch
|
3 |
-
import
|
4 |
-
from PIL import Image
|
5 |
|
6 |
-
# Load
|
7 |
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', _verbose=False)
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
15 |
|
16 |
-
#
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
gradio_wrapper,
|
28 |
-
image,
|
29 |
-
'image',
|
30 |
-
live=True,
|
31 |
-
title="CiclopeIA",
|
32 |
-
description="App based on the CiclopeIA project from Saturdays AI. Identifies the value of Euro banknotes."
|
33 |
-
)
|
34 |
-
|
35 |
-
# Launch the Gradio interface
|
36 |
-
demo.launch()
|
|
|
1 |
+
import gradio as gr # Import Gradio library for creating UI
|
2 |
+
import torch # Import PyTorch for YOLOv5 model
|
3 |
+
from PIL import Image # Import Pillow for image manipulation
|
|
|
4 |
|
5 |
+
# Load YOLOv5 model from Ultralytics' GitHub repository
|
6 |
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', _verbose=False)
|
7 |
|
8 |
+
# Define a function to predict objects in an image
|
9 |
+
def predict(image_in_img, image_in_video):
|
10 |
+
global model # Use the global YOLOv5 model
|
11 |
+
if image_in_video == None and image_in_img == None: # If both inputs are None, raise an error
|
12 |
+
raise gr.Error("Please upload an image.")
|
13 |
+
if image_in_video or image_in_img: # If either input is not None,
|
14 |
+
image = image_in_video or image_in_img # set the image variable to the non-None input
|
15 |
+
return model(image).render()[0] # Use the YOLOv5 model to predict objects in the image and return the rendered output
|
16 |
|
17 |
+
# Define a function to toggle between webcam and file inputs
|
18 |
+
def toggle(choice):
|
19 |
+
if choice == "webcam": # If "webcam" is selected,
|
20 |
+
return gr.update(visible=True, value=None), gr.update(visible=False, value=None) # Show webcam input and hide file input
|
21 |
+
else: # Otherwise, if "file" is selected,
|
22 |
+
return gr.update(visible=False, value=None), gr.update(visible=True, value=None) # Show file input and hide webcam input
|
23 |
|
24 |
+
# Create Gradio UI blocks
|
25 |
+
with gr.Blocks() as blocks:
|
26 |
+
gr.Markdown("WebCam or Upload?""") # Display text in Markdown format
|
27 |
+
with gr.Row(): # Create a row of UI elements
|
28 |
+
with gr.Column(): # Create a column of UI elements
|
29 |
+
# Create a radio button to choose between webcam and file inputs
|
30 |
+
image_or_file_opt = gr.Radio(["webcam", "file"], value="webcam",
|
31 |
+
label="How would you like to upload your image?")
|
32 |
+
# Create an image input for the webcam
|
33 |
+
image_in_video = gr.Image(source="webcam", type="filepath")
|
34 |
+
# Create an image input for a file, initially hidden
|
35 |
+
image_in_img = gr.Image(
|
36 |
+
source="upload", visible=False, type="filepath")
|
37 |
|
38 |
+
# Bind the toggle function to the radio button to switch between webcam and file inputs
|
39 |
+
image_or_file_opt.change(fn=toggle, inputs=[image_or_file_opt],
|
40 |
+
outputs=[image_in_video, image_in_img], queue=False)
|
41 |
+
with gr.Column(): # Create another column of UI elements
|
42 |
+
# Create an output image to display the predicted objects
|
43 |
+
image_out = gr.Image()
|
44 |
+
# Create a button to run the prediction function and display the output image
|
45 |
+
run_btn = gr.Button("Run")
|
46 |
+
run_btn.click(fn=predict, inputs=[
|
47 |
+
image_in_img, image_in_video], outputs=[image_out])
|
48 |
|
49 |
+
# Launch the Gradio UI blocks
|
50 |
+
blocks.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|