latest/how_to/filter_detections/ #929
Replies: 5 comments 7 replies
-
can i use it to filter the predictions in video? |
Beta Was this translation helpful? Give feedback.
-
Hi can you help me I have this code: from ultralytics import YOLO
import supervision as sv
import numpy as np
import cv2
# Replace 'rtsp://your_rtsp_stream_url' with your actual RTSP stream URL
rtsp_url = 'rtsp_url'
# Create a VideoCapture object
cap = cv2.VideoCapture(rtsp_url)
MODEL = "yolov8n.pt"
model = YOLO(MODEL)
model.fuse()
selected_classes = [0]
video_info_url = sv.VideoInfo(width=1920, height=1080, fps=15)
# settings
LINE_START = sv.Point(974, 757)
LINE_END = sv.Point(1450, 843)
# Check if the camera opened successfully
if not cap.isOpened():
print("Error: Couldn't open the RTSP stream.")
exit()
# create BYTETracker instance
byte_tracker = sv.ByteTrack(track_thresh=0.25, track_buffer=30, match_thresh=0.8, frame_rate=25)
# create VideoInfo instance
video_info = sv.VideoInfo.from_video_path(rtsp_url)
# create LineZone instance, it is previously called LineCounter class
triggering_anchors = [sv.Position.BOTTOM_LEFT, sv.Position.BOTTOM_RIGHT]
line_zone = sv.LineZone(start=LINE_START, end=LINE_END, triggering_anchors=triggering_anchors)
# create instance of BoxAnnotator
box_annotator = sv.BoxAnnotator(thickness=2, text_thickness=2, text_scale=1)
# create instance of TraceAnnotator
trace_annotator = sv.TraceAnnotator(thickness=2, trace_length=50)
# create LineZoneAnnotator instance, it is previously called LineCounterAnnotator class
line_zone_annotator = sv.LineZoneAnnotator(thickness=1, text_thickness=1, text_scale=0.5)
with sv.VideoSink(target_path='target_video.mp4', video_info=video_info_url) as sink:
while True:
try:
ret, frame_ori = cap.read()
if not ret:
print("Error: Couldn't read frame.")
raise Exception("Error: Couldn't read frame.")
results = model(frame_ori, verbose=False)[0]
detections = sv.Detections.from_ultralytics(results)
# only consider class id from selected_classes define above
detections = detections[np.isin(detections.class_id, selected_classes)]
# tracking detections
detections = byte_tracker.update_with_detections(detections)
labels = [
f"#{tracker_id} {model.model.names[class_id]} {confidence:0.2f}"
for confidence, class_id, tracker_id
in zip(detections.confidence, detections.class_id, detections.tracker_id)
]
annotated_frame = trace_annotator.annotate(
scene=frame_ori.copy(),
detections=detections
)
annotated_frame=box_annotator.annotate(
scene=annotated_frame,
detections=detections,
labels=labels)
# update line counter
results_line = line_zone.trigger(detections)
# Trigger Orang Masuk dan Keluar
if results_line[0].size > 0:
if results_line[0][0] == True:
print("Ada Orang Masuk")
with sv.ImageSink(target_dir_path="result") as sink:
for xyxy in detections.xyxy:
cropped_image = sv.crop_image(image=frame_ori, xyxy=xyxy)
sink.save_image(image=cropped_image)
if results_line[1].size > 0:
if results_line[1][0] == True:
print("Ada Orang Keluar")
cv2.imshow('RTSP Stream', line_zone_annotator.annotate(annotated_frame, line_counter=line_zone))
sink.write_frame(frame=line_zone_annotator.annotate(annotated_frame, line_counter=line_zone))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except Exception as e:
print("Error occurred:", e)
cap = cv2.VideoCapture(rtsp_url)
# Release the VideoCapture and close the window
cap.release()
cv2.destroyAllWindows() When someone enters, the code should cut the image and continue, but instead it creates an error occurred: 'ImageSink' object has no attribute 'write_frame' and makes it not respond. Whats wrong with this code?? |
Beta Was this translation helpful? Give feedback.
-
hello, how can we filter using IOU? im using inferencepipeline for the detections, i want to make the iou can be modify by the user but it seem like the iou parameter for inferencepipeline init will not change when user modify the iou value |
Beta Was this translation helpful? Give feedback.
-
Amazingly, it makes computer vision development much easier. |
Beta Was this translation helpful? Give feedback.
-
latest/how_to/filter_detections/
A set of easy-to-use utilities that will come in handy in any computer vision project.
https://supervision.roboflow.com/latest/how_to/filter_detections/
Beta Was this translation helpful? Give feedback.
All reactions