panda1835's picture
Update app.py
64424d9
raw
history blame
1.1 kB
from PIL import Image
import numpy as np
import gradio as gr
import pandas as pd
import torch
import json
from ultralytics import YOLO
print(f"Is CUDA available: {torch.cuda.is_available()}")
print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
# Load the model
# detect_model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True, _verbose=False)
detect_model = YOLO('yolov8m_2023-10-23_best.pt')
def detect(image):
results = detect_model(image, conf=0.1)
try:
results = results[0].boxes.xyxy[0].cpu().numpy()
top = int(results[1])
left = int(results[0])
width = int(results[2] - results[0])
height = int(results[3] - results[1])
return {
"top": top,
"left": left,
"width": width,
"height": height
}
except:
return {
"top": 0,
"left": 0,
"width": 0,
"height": 0
}
title = "🐢"
gr.Interface(
fn=detect,
inputs=gr.Image(type="pil", label="Input Image"),
outputs=[gr.JSON()],
# live=True,
title=title,
).launch()