Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,33 +3,35 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
|
| 7 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
model.conf = 0.25
|
| 11 |
-
model.iou = 0.45
|
| 12 |
-
model.agnostic = False
|
| 13 |
-
model.multi_label = False
|
| 14 |
-
model.max_det = 1000
|
| 15 |
|
| 16 |
|
| 17 |
def detect(img):
|
| 18 |
|
| 19 |
-
|
| 20 |
results = model(img, size=640)
|
| 21 |
|
| 22 |
-
# inference with test time augmentation
|
| 23 |
-
results = model(img, augment=True)
|
| 24 |
-
# parse results
|
| 25 |
predictions = results.pred[0]
|
| 26 |
boxes = predictions[:, :4] # x1, y1, x2, y2
|
| 27 |
scores = predictions[:, 4]
|
| 28 |
categories = predictions[:, 5]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
# save image as numpy array
|
| 31 |
-
return results.numpy()
|
| 32 |
-
# show detection bounding boxes on image
|
| 33 |
|
| 34 |
img = gr.inputs.Image(shape=(192, 192))
|
| 35 |
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
|
| 7 |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
| 8 |
|
| 9 |
+
|
| 10 |
+
model.conf = 0.25
|
| 11 |
+
model.iou = 0.45
|
| 12 |
+
model.agnostic = False
|
| 13 |
+
model.multi_label = False
|
| 14 |
+
model.max_det = 1000
|
| 15 |
|
| 16 |
|
| 17 |
def detect(img):
|
| 18 |
|
| 19 |
+
|
| 20 |
results = model(img, size=640)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
predictions = results.pred[0]
|
| 23 |
boxes = predictions[:, :4] # x1, y1, x2, y2
|
| 24 |
scores = predictions[:, 4]
|
| 25 |
categories = predictions[:, 5]
|
| 26 |
+
dfResults = results.pandas().xyxy[0]
|
| 27 |
+
return drawRectangles(image, dfResults[['xmin', 'ymin', 'xmax','ymax']].astype(int))
|
| 28 |
+
|
| 29 |
+
def drawRectangles(image, dfResults):
|
| 30 |
+
for index, row in dfResults.iterrows():
|
| 31 |
+
print( (row['xmin'], row['ymin']))
|
| 32 |
+
image = cv2.rectangle(image, (row['xmin'], row['ymin']), (row['xmax'], row['ymax']), (255, 0, 0), 2)
|
| 33 |
+
return image
|
| 34 |
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
img = gr.inputs.Image(shape=(192, 192))
|
| 37 |
|