Untitled conversation
- User
import cv2 import torch import numpy as np # Load YOLOv5 model model = torch.hub.load('ultralytics/yolov5', 'yolov5n6') # Your supervision library would be used here import supervision as sv # Initialize the camera (0 for default camera, 1 for external cameras, and so on) camera = cv2.VideoCapture(0) # Define the BoxAnnotator from the supervision library box_annotator = sv.BoxAnnotator(thickness=4, text_thickness=4, text_scale=2) def live_inference(camera): while True: # Capture frame-by-frame from camera ret, frame = camera.read() if not ret: print("Failed to grab frame") break # Convert the image from BGR to RGB frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Perform inference results = model(frame_rgb, size=700) detections = sv.Detections.from_yolov5(results) detections = detections[(detections.class_id == 0) & (detections.confidence > 0.5)] # Annotate frame_annotated = box_annotator.annotate(scene=frame_rgb, detections=detections) # Convert the image from RGB to BGR for OpenCV display frame_annotated_bgr = cv2.cvtColor(frame_annotated, cv2.COLOR_RGB2BGR) # Display the resulting frame cv2.imshow('YOLOv5 Live Detection', frame_annotated_bgr) # Break the loop if ‘q’ is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture camera.release() cv2.destroyAllWindows() # Run the live inference function live_inference(camera)how can i draw a small center square polygon and only run the inference inside meaning only objects inside are detected?
- Assistant
- User
- Assistant
The error message you're receiving is due to the incorrect use of the
sizeparameter when calling the YOLO model for inference. According to the error, 'size' is not a valid YOLO argument. The traceback provides additional information about how to use the command syntax correctly. - User
Traceback (most recent call last): File "C:\Users\User\Desktop\New folder\yolov8.py", line 53, in live_inference(camera) File "C:\Users\User\Desktop\New folder\yolov8.py", line 32, in live_inference detections = sv.Detections.from_yolov8(results) AttributeError: type object 'Detections' has no attribute 'from_yolov8'
- Assistant
- User
but from_yolov8(results) work here: from ultralytics import YOLO
model = YOLO('yolov8s.pt') import supervision as sv
extract video frame
generator = sv.get_video_frames_generator(MALL_VIDEO_PATH) iterator = iter(generator) frame = next(iterator)
detect
results = model(frame, imgsz=1280)[0] detections = sv.Detections.from_yolov8(results)
annotate
box_annotator = sv.BoxAnnotator(thickness=4, text_thickness=4, text_scale=2) frame = box_annotator.annotate(scene=frame, detections=detections)
%matplotlib inline
sv.show_frame_in_notebook(frame, (16, 16)) - Assistant
- User
import cv2 import torch import numpy as np import ultralytics from ultralytics import YOLO
Load YOLOv8 model
model = YOLO('yolov8s.pt')
Your supervision library would be used here
import supervision as sv
Initialize the camera (0 for default camera, 1 for external cameras, and so on)
camera = cv2.VideoCapture(0)
Define the BoxAnnotator from the supervision library
box_annotator = sv.BoxAnnotator(thickness=4, text_thickness=4, text_scale=2)
def live_inference(camera): while True: # Capture frame-by-frame from camera ret, frame = camera.read() if not ret: print("Failed to grab frame") break
# Convert the image from BGR to RGB frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Perform inference results = model(frame_rgb, imgsz=1280) detections = sv.Detections.from_yolov8(results) detections = detections[(detections.class_id == 0) & (detections.confidence > 0.5)] # Annotate frame_annotated = box_annotator.annotate(scene=frame_rgb, detections=detections) # Convert the image from RGB to BGR for OpenCV display frame_annotated_bgr = cv2.cvtColor(frame_annotated, cv2.COLOR_RGB2BGR) # Display the resulting frame cv2.imshow('YOLOv8 Live Detection', frame_annotated_bgr) # Break the loop if ‘q’ is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture camera.release() cv2.destroyAllWindows() - Assistant
Given the information you provided, there are no immediately apparent typos or errors in the script itself. Your issue might be arising from a discrepancy in the actual installation or configuration of the library.