Spaces:
Runtime error
Runtime error
Commit
·
fd44d26
1
Parent(s):
eac9d61
Update detection.py
Browse files- detection.py +62 -73
detection.py
CHANGED
|
@@ -1,73 +1,62 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
.
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
}
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
background-color: #FF8C00;
|
| 64 |
-
color: #FFFFFF;
|
| 65 |
-
padding: 12px;
|
| 66 |
-
border-bottom-left-radius: 10px;
|
| 67 |
-
border-bottom-right-radius: 10px;
|
| 68 |
-
}
|
| 69 |
-
</style>
|
| 70 |
-
"""
|
| 71 |
-
|
| 72 |
-
# Inject custom CSS into the interface
|
| 73 |
-
interface.launch(share=False, custom_css=custom_css)
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import IPython
|
| 3 |
+
from PIL import ImageColor
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
|
| 6 |
+
class ObjectDetection:
|
| 7 |
+
def __init__(self, model_name='Yolov8'):
|
| 8 |
+
self.model_name = model_name
|
| 9 |
+
self.model = self.load_model()
|
| 10 |
+
self.classes = self.model.names
|
| 11 |
+
self.device = 'cpu'
|
| 12 |
+
|
| 13 |
+
def load_model(self):
|
| 14 |
+
model = YOLO(f"weights/{self.model_name}_best.pt")
|
| 15 |
+
return model
|
| 16 |
+
|
| 17 |
+
def v8_score_frame(self, frame):
|
| 18 |
+
results = self.model(frame)
|
| 19 |
+
|
| 20 |
+
labels = []
|
| 21 |
+
confidences = []
|
| 22 |
+
coords = []
|
| 23 |
+
|
| 24 |
+
for result in results:
|
| 25 |
+
boxes = result.boxes.cpu().numpy()
|
| 26 |
+
|
| 27 |
+
label = boxes.cls
|
| 28 |
+
conf = boxes.conf
|
| 29 |
+
coord = boxes.xyxy
|
| 30 |
+
|
| 31 |
+
labels.extend(label)
|
| 32 |
+
confidences.extend(conf)
|
| 33 |
+
coords.extend(coord)
|
| 34 |
+
|
| 35 |
+
return labels, confidences, coords
|
| 36 |
+
|
| 37 |
+
def get_coords(self, frame, row):
|
| 38 |
+
return int(row[0]), int(row[1]), int(row[2]), int(row[3])
|
| 39 |
+
|
| 40 |
+
def class_to_label(self, x):
|
| 41 |
+
return self.classes[int(x)]
|
| 42 |
+
|
| 43 |
+
def get_color(self, code):
|
| 44 |
+
rgb = ImageColor.getcolor(code, "RGB")
|
| 45 |
+
return rgb
|
| 46 |
+
|
| 47 |
+
def plot_bboxes(self, results, frame, threshold=0.5, box_color='red', text_color='white'):
|
| 48 |
+
labels, conf, coord = results
|
| 49 |
+
|
| 50 |
+
frame = frame.copy()
|
| 51 |
+
box_color = self.get_color(box_color)
|
| 52 |
+
text_color = self.get_color(text_color)
|
| 53 |
+
|
| 54 |
+
for i in range(len(labels)):
|
| 55 |
+
if conf[i] >= threshold:
|
| 56 |
+
x1, y1, x2, y2 = self.get_coords(frame, coord[i])
|
| 57 |
+
class_name = self.class_to_label(labels[i])
|
| 58 |
+
|
| 59 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), box_color, 2)
|
| 60 |
+
cv2.putText(frame, f"{class_name} - {conf[i]*100:.2f}%", (x1, y1), cv2.FONT_HERSHEY_COMPLEX, 0.5, text_color)
|
| 61 |
+
|
| 62 |
+
return frame
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|