Spaces:
Runtime error
Runtime error
| import torch | |
| import cv2 | |
| import numpy as np | |
| import gradio as gr | |
| from sahi.prediction import ObjectPrediction | |
| from sahi.utils.cv import visualize_object_predictions, read_image | |
| 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.predict(image, imgsz=image_size, return_outputs=True) | |
| object_prediction_list = [] | |
| for _, image_results in enumerate(results): | |
| if len(image_results)!=0: | |
| image_predictions_in_xyxy_format = image_results['det'] | |
| for pred in image_predictions_in_xyxy_format: | |
| x1, y1, x2, y2 = ( | |
| int(pred[0]), | |
| int(pred[1]), | |
| int(pred[2]), | |
| int(pred[3]), | |
| ) | |
| bbox = [x1, y1, x2, y2] | |
| score = pred[4] | |
| category_name = model.model.names[int(pred[5])] | |
| category_id = pred[5] | |
| object_prediction = ObjectPrediction( | |
| bbox=bbox, | |
| category_id=int(category_id), | |
| score=score, | |
| category_name=category_name, | |
| ) | |
| object_prediction_list.append(object_prediction) | |
| image = read_image(image) | |
| output_image = visualize_object_predictions(image=image, object_prediction_list=object_prediction_list) | |
| return output_image['image'] | |
| 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) |