I’d successfully imported https://www.youtube.com/watch?v=hg4oVgNq7Do&t=368s detection software on VS Code. While, I now can apply Yolov8 data classification on images/videos. However, I would like to implement data classification on live video feeds.
I utilized this guide on creating a live video feed: https://www.youtube.com/watch?v=IHbJcOex6dk&t=334s. I imported supervision onto the terminal. I implemented the code suggested within this Stack Overflow Answer post: Python ModuleNotFoundError when importing custom module.
However, I am now faced with another debugging issue.
yolov8_live.py
import torch
import numpy as np
import cv2
from time import time
from ultralytics import YOLO
import supervision as sv
from supervision import Detections, BoxAnnotator
class ObjectDetection:
def __init__(self, capture_index):
self.capture_index = capture_index
self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
print("Using Device: ", self.device)
self.model = self.load_model()
self.CLASS_NAMES_DICT = self.model.model.names
self.box_annotator = sv.BoxAnnotator(sv.ColorPalette.default(), thickness=3, text_thickness=3, text_scale=1.5)
def load_model(self):
model = YOLO("yolov8m.pt") # load a pretrained YOLOv8n model
model.fuse()
return model
def predict(self, frame):
results = self.model(frame)
return results
def plot_bboxes(self, results, frame):
xyxys = []
confidences = []
class_ids = []
# Extract detections for person class
for result in results:
boxes = result.boxes.cpu().numpy()
class_id = boxes.cls[0]
conf = boxes.conf[0]
xyxy = boxes.xyxy[0]
if class_id == 0.0:
xyxys.append(result.boxes.xyxy.cpu().numpy())
confidences.append(result.boxes.conf.cpu().numpy())
class_ids.append(result.boxes.cls.cpu().numpy().astype(int))
# Setup detections for visualization
detections = sv.Detections(
xyxy=results[0].boxes.xyxy.cpu().numpy(),
confidence=results[0].boxes.conf.cpu().numpy(),
class_id=results[0].boxes.cls.cpu().numpy().astype(int),
)
# Format custom labels
# self.labels = [f"{self.CLASS_NAMES_DICT[class_id]} {confidence:0.2f}"
# for _, xyxy, confidenc0e, class_id in detections]
self.labels = [f"{self.CLASS_NAMES_DICT[detection.class_id] if detection.class_id else None} {detection.confidence:0.2f}"
for detection in detections]
# Annotate and display frame
frame = self.box_annotator.annotate(scene=frame, detections=detections, labels=self.labels)
return frame
def __call__(self):
cap = cv2.VideoCapture(self.capture_index)
assert cap.isOpened()
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
while True:
start_time = time()
ret, frame = cap.read()
assert ret
results = self.predict(frame)
frame = self.plot_bboxes(results, frame)
end_time = time()
fps = 1/np.round(end_time - start_time, 2)
cv2.putText(frame, f'FPS: {int(fps)}', (20,70), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0,255,0), 2)
cv2.imshow('YOLOv8 Detection', frame)
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
detector = ObjectDetection(capture_index=0)
detector()
The error outputted by the terminal.
Traceback (most recent call last):
File "c:UsersusernameDownloadsVSC_YOLOV8_Applicationyolov8-silvayolov8_live.py", line 116, in <module>
detector()
File "c:UsersusernameDownloadsVSC_YOLOV8_Applicationyolov8-silvayolov8_live.py", line 97, in __call__
frame = self.plot_bboxes(results, frame)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:UsersusernameDownloadsVSC_YOLOV8_Applicationyolov8-silvayolov8_live.py", line 72, in plot_bboxes
self.labels = [f"{self.CLASS_NAMES_DICT[detection.class_id] if detection.class_id else None} {detection.confidence:0.2f}"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:UsersusernameDownloadsVSC_YOLOV8_Applicationyolov8-silvayolov8_live.py", line 72, in <listcomp>
self.labels = [f"{self.CLASS_NAMES_DICT[detection.class_id] if detection.class_id else None} {detection.confidence:0.2f}"
^^^^^^^^^^^^^^^^^^
AttributeError: 'tuple' object has no attribute 'class_id'
I tried looking on openstax/reddit for additional information. However, I was unable to find anything. Does anyone know how to resolve this?
Thank you!
Legion is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.