Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,8 @@ import torch
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
|
@@ -17,15 +19,35 @@ model.max_det = 1000
|
|
| 17 |
def detect(img):
|
| 18 |
|
| 19 |
|
| 20 |
-
results = model(
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def drawRectangles(image, dfResults):
|
| 31 |
for index, row in dfResults.iterrows():
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
+
from sahi.prediction import ObjectPrediction
|
| 6 |
+
from sahi.utils.cv import visualize_object_predictions, read_image
|
| 7 |
|
| 8 |
|
| 9 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
|
|
|
| 19 |
def detect(img):
|
| 20 |
|
| 21 |
|
| 22 |
+
results = model.predict(image, imgsz=image_size, return_outputs=True)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
object_prediction_list = []
|
| 26 |
+
for _, image_results in enumerate(results):
|
| 27 |
+
if len(image_results)!=0:
|
| 28 |
+
image_predictions_in_xyxy_format = image_results['det']
|
| 29 |
+
for pred in image_predictions_in_xyxy_format:
|
| 30 |
+
x1, y1, x2, y2 = (
|
| 31 |
+
int(pred[0]),
|
| 32 |
+
int(pred[1]),
|
| 33 |
+
int(pred[2]),
|
| 34 |
+
int(pred[3]),
|
| 35 |
+
)
|
| 36 |
+
bbox = [x1, y1, x2, y2]
|
| 37 |
+
score = pred[4]
|
| 38 |
+
category_name = model.model.names[int(pred[5])]
|
| 39 |
+
category_id = pred[5]
|
| 40 |
+
object_prediction = ObjectPrediction(
|
| 41 |
+
bbox=bbox,
|
| 42 |
+
category_id=int(category_id),
|
| 43 |
+
score=score,
|
| 44 |
+
category_name=category_name,
|
| 45 |
+
)
|
| 46 |
+
object_prediction_list.append(object_prediction)
|
| 47 |
+
|
| 48 |
+
image = read_image(image)
|
| 49 |
+
output_image = visualize_object_predictions(image=image, object_prediction_list=object_prediction_list)
|
| 50 |
+
return output_image['image']
|
| 51 |
|
| 52 |
def drawRectangles(image, dfResults):
|
| 53 |
for index, row in dfResults.iterrows():
|