Upload 2 files
Browse files- main2.py +52 -0
- yolov8m.pt +3 -0
main2.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
|
| 5 |
+
# Load a larger YOLOv8 model for better accuracy
|
| 6 |
+
model = YOLO('yolov8m.pt') # You can use 'yolov8m.pt', 'yolov8l.pt', etc., for more accuracy
|
| 7 |
+
|
| 8 |
+
# Initialize the video capture
|
| 9 |
+
cap = cv2.VideoCapture(0)
|
| 10 |
+
|
| 11 |
+
with tf.device('/device:GPU:0'): # Ensuring we use the GPU if available
|
| 12 |
+
while cap.isOpened():
|
| 13 |
+
ret, frame = cap.read()
|
| 14 |
+
if not ret:
|
| 15 |
+
break
|
| 16 |
+
|
| 17 |
+
# Perform inference on the frame
|
| 18 |
+
results = model(frame)
|
| 19 |
+
|
| 20 |
+
# Extract detection results
|
| 21 |
+
for result in results:
|
| 22 |
+
boxes = result.boxes.xyxy.cpu().numpy() # Bounding boxes
|
| 23 |
+
scores = result.boxes.conf.cpu().numpy() # Confidence scores
|
| 24 |
+
classes = result.boxes.cls.cpu().numpy() # Class IDs
|
| 25 |
+
|
| 26 |
+
# Draw bounding boxes and labels on the frame
|
| 27 |
+
for i in range(len(boxes)):
|
| 28 |
+
box = boxes[i]
|
| 29 |
+
score = scores[i]
|
| 30 |
+
class_id = int(classes[i])
|
| 31 |
+
label = model.names[class_id]
|
| 32 |
+
|
| 33 |
+
if score > 0.5: # Adjusted threshold for more confident detections
|
| 34 |
+
# Extract box coordinates
|
| 35 |
+
start_x, start_y, end_x, end_y = map(int, box[:4])
|
| 36 |
+
|
| 37 |
+
# Draw the bounding box
|
| 38 |
+
cv2.rectangle(frame, (start_x, start_y), (end_x, end_y), (0, 255, 0), 2)
|
| 39 |
+
|
| 40 |
+
# Draw the label and score
|
| 41 |
+
label_text = f"{label}: {score:.2f}"
|
| 42 |
+
cv2.putText(frame, label_text, (start_x, start_y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
| 43 |
+
|
| 44 |
+
# Display the frame
|
| 45 |
+
cv2.imshow('YOLOv8 Object Detection', frame)
|
| 46 |
+
|
| 47 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 48 |
+
break
|
| 49 |
+
|
| 50 |
+
# Release resources
|
| 51 |
+
cap.release()
|
| 52 |
+
cv2.destroyAllWindows()
|
yolov8m.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6c25b0b63b1a433843f06d821a9ac1deb8d5805f74f0f38772c7308c5adc55a5
|
| 3 |
+
size 52117635
|