Spaces:
Runtime error
Runtime error
import torch | |
import cv2 | |
import numpy as np | |
import gradio as gr | |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True) | |
model.conf = 0.25 | |
model.iou = 0.45 | |
model.agnostic = False | |
model.multi_label = False | |
model.max_det = 1000 | |
def detect(img): | |
results = model(img, size=640) | |
predictions = results.pred[0] | |
boxes = predictions[:, :4] # x1, y1, x2, y2 | |
scores = predictions[:, 4] | |
categories = predictions[:, 5] | |
dfResults = results.pandas().xyxy[0] | |
return drawRectangles(image, dfResults[['xmin', 'ymin', 'xmax','ymax']].astype(int)) | |
def drawRectangles(image, dfResults): | |
for index, row in dfResults.iterrows(): | |
print( (row['xmin'], row['ymin'])) | |
image = cv2.rectangle(image, (row['xmin'], row['ymin']), (row['xmax'], row['ymax']), (255, 0, 0), 2) | |
return image | |
img = gr.inputs.Image(shape=(192, 192)) | |
intf = gr.Interface(fn=detect, inputs=img, outputs='image') | |
intf.launch(inline=False) |