jsr90 commited on
Commit
212a236
1 Parent(s): 8b2a2d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -29
app.py CHANGED
@@ -1,36 +1,50 @@
1
- import gradio as gr
2
- import torch
3
- import numpy as np
4
- from PIL import Image
5
 
6
- # Load the custom-trained YOLOv5 model
7
  model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', _verbose=False)
8
 
9
- def gradio_wrapper(img):
10
- global model
11
- # Run the model on the input image and get the results
12
- results = model(img)
13
- # Render the results and return the annotated image
14
- return results.render()[0]
 
 
15
 
16
- # Set up the Gradio image input component with the option to upload an image or use the webcam
17
- s = gr.Radio(["upload", "webcam"], type="index")
 
 
 
 
18
 
19
- if s == "upload":
20
- image = gr.inputs.Image(source="upload")
21
- else:
22
- image = gr.inputs.Image(source="webcam")
 
 
 
 
 
 
 
 
 
23
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- # Create the Gradio interface with the gradio_wrapper function, the image input component, and an image output component
26
- demo = gr.Interface(
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()