Spaces:
Runtime error
Runtime error
| import torch | |
| import cv2 | |
| import numpy as np | |
| import gradio as gr | |
| # load model | |
| model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True) | |
| # set model parameters | |
| model.conf = 0.25 # NMS confidence threshold | |
| model.iou = 0.45 # NMS IoU threshold | |
| model.agnostic = False # NMS class-agnostic | |
| model.multi_label = False # NMS multiple labels per box | |
| model.max_det = 1000 # maximum number of detections per image | |
| def detect(img): | |
| # perform inference | |
| results = model(img, size=640) | |
| # inference with test time augmentation | |
| results = model(img, augment=True) | |
| # parse results | |
| predictions = results.pred[0] | |
| boxes = predictions[:, :4] # x1, y1, x2, y2 | |
| scores = predictions[:, 4] | |
| categories = predictions[:, 5] | |
| return results | |
| # show detection bounding boxes on image | |
| img = gr.inputs.Image(shape=(192, 192)) | |
| intf = gr.Interface(fn=detect, inputs=img, outputs='image') | |
| intf.launch(inline=False) |