| | import gradio as gr
|
| | import requests
|
| | import numpy as np
|
| | from PIL import Image
|
| | import io
|
| | from ultralytics import YOLO
|
| | import cv2
|
| |
|
| |
|
| | try:
|
| | model = YOLO('modelo_epoch_50.pt')
|
| | print("Model loaded successfully")
|
| | except Exception as e:
|
| | print(f"Error loading model: {str(e)}")
|
| | model = None
|
| |
|
| |
|
| | def process_image(image):
|
| |
|
| | if image is None:
|
| | raise gr.Error("Please take a picture first before analyzing!")
|
| |
|
| |
|
| | if image.mode != 'RGB':
|
| | image = image.convert('RGB')
|
| |
|
| |
|
| | results = model.predict(image, save=True, conf=0.5)
|
| |
|
| |
|
| | print("Model predictions:", results[0].boxes)
|
| |
|
| |
|
| | image_cv = np.array(image)
|
| |
|
| | image_cv = cv2.cvtColor(image_cv, cv2.COLOR_RGB2BGR)
|
| |
|
| |
|
| | boxes = results[0].boxes
|
| | for box in boxes:
|
| | b = box.xyxy[0]
|
| | c = box.cls
|
| | confidence = box.conf
|
| |
|
| | x1, y1, x2, y2 = map(int, b)
|
| | cv2.rectangle(image_cv, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
| |
|
| | label = f"{results[0].names[int(c)]} {confidence.item():.2f}"
|
| | cv2.putText(image_cv, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
|
| | print(label)
|
| |
|
| |
|
| | image_cv = cv2.cvtColor(image_cv, cv2.COLOR_BGR2RGB)
|
| |
|
| | final_image = Image.fromarray(image_cv)
|
| |
|
| | final_image.save('FINAL.jpg', 'JPEG', quality=95)
|
| |
|
| | return final_image
|
| |
|
| |
|
| | with gr.Blocks() as demo:
|
| | with gr.Row():
|
| |
|
| | with gr.Column(scale=1):
|
| | gr.Markdown("### Step 1: Take a Picture")
|
| | camera = gr.Image(type="pil", label="Camera View", sources=["webcam"])
|
| | gr.Markdown("Click the 'πΈ Take Photo' button in the camera view above")
|
| |
|
| | with gr.Row():
|
| | analyze_btn = gr.Button("π Analyze", variant="primary")
|
| | new_picture_btn = gr.Button("π Reset")
|
| |
|
| |
|
| | with gr.Column(scale=1):
|
| | gr.Markdown("### Step 2: View Results")
|
| | modified_image = gr.Image(label="Analyzed Image")
|
| |
|
| |
|
| | analyze_btn.click(
|
| | fn=process_image,
|
| | inputs=[camera],
|
| | outputs=[modified_image]
|
| | )
|
| |
|
| | new_picture_btn.click(
|
| | fn=lambda: (None, None),
|
| | inputs=[],
|
| | outputs=[camera, modified_image]
|
| | )
|
| |
|
| |
|
| | if __name__ == "__main__":
|
| | demo.launch(share=True) |