|
import gradio as gr |
|
import torch |
|
from PIL import Image |
|
|
|
|
|
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', _verbose=False) |
|
|
|
|
|
def predict(image_in_img, image_in_video): |
|
global model |
|
if image_in_video == None and image_in_img == None: |
|
raise gr.Error("Please upload an image.") |
|
if image_in_video or image_in_img: |
|
image = image_in_video or image_in_img |
|
return model(image).render()[0] |
|
|
|
|
|
def toggle(choice): |
|
if choice == "webcam": |
|
return gr.update(visible=True, value=None), gr.update(visible=False, value=None) |
|
else: |
|
return gr.update(visible=False, value=None), gr.update(visible=True, value=None) |
|
|
|
|
|
ex = [["img1.jpeg"], ["img2.jpeg"], ["img3.jpeg"]] |
|
|
|
|
|
with gr.Blocks() as blocks: |
|
gr.Markdown("# CiclopeIA: Imaginando tu futuro") |
|
gr.Markdown("## Application based on [CiclopeIA Saturdays' project](https://medium.com/saturdays-ai/ciclopeia-imaginando-tu-entorno-14dd3781a7ac)") |
|
gr.Markdown("### Take a photo of a € bill or make it directly with the camera and the model will recognize its value") |
|
with gr.Row(): |
|
with gr.Column(): |
|
|
|
image_or_file_opt = gr.Radio(["file", "webcam"], value="file", |
|
label="How would you like to upload your image?") |
|
|
|
image_in_img = gr.Image( |
|
source="upload", type="filepath") |
|
|
|
image_in_video = gr.Image(source="webcam", visible=False, type="filepath") |
|
|
|
|
|
image_or_file_opt.change(fn=toggle, inputs=[image_or_file_opt], |
|
outputs=[image_in_video, image_in_img], queue=False) |
|
with gr.Column(): |
|
|
|
image_out = gr.Image() |
|
|
|
|
|
run_btn = gr.Button("Run") |
|
run_btn.click(fn=predict, inputs=[ |
|
image_in_img, image_in_video], outputs=[image_out]) |
|
|
|
gr.Examples( |
|
examples = ex, |
|
inputs = [image_in_img, image_in_video], |
|
outputs = image_out, |
|
) |
|
|
|
|
|
blocks.launch() |
|
|
|
|