roboflow / notebooks

Examples and tutorials on using SOTA computer vision models and techniques. Learn everything from old-school ResNet, through YOLO and object-detection transformers like DETR, to the latest models like Grounding DINO and SAM.
https://roboflow.com/models
5.27k stars 814 forks source link

Regarding not able count correctly #285

Closed thalapandi closed 3 months ago

thalapandi commented 3 months ago

Search before asking

Notebook name

how-to-track-and-count-vehicles-with-yolov8.ipynb

when camera is straight then we are able to count correctly but when camera is in side angle then we could not count accurately so what we have to change in that code with where if we change the code then the issue will be resolved and how to overcome this issue in future and how to modify this code for people counting in and out accurately for all video with different combination of camera angle,help me to resolve the probelm

and i have provided the output image in below link so check the output once Screenshot (38)

Bug

This is not but just not get count correctly

Environment

Google colab,python 3.10.0

Minimal Reproducible Example

dict maping class_id to class_name

CLASS_NAMES_DICT = model.model.names

class_ids of interest - person

CLASS_ID = [0]

from tqdm.notebook import tqdm import numpy as np from google.colab.patches import cv2_imshow import cv2 import os if not os.path.exists("output_frame"): os.mkdir("output_frame")

create BYTETracker instance

byte_tracker = BYTETracker(BYTETrackerArgs())

create VideoInfo instance

video_info = VideoInfo.from_video_path(SOURCE_VIDEO_PATH)

create frame generator

generator = get_video_frames_generator(SOURCE_VIDEO_PATH)

create LineCounter instance

line_counter = LineCounter(start=LINE_START, end=LINE_END)

create instance of BoxAnnotator and LineCounterAnnotator

box_annotator = BoxAnnotator(color=ColorPalette(), thickness=4, text_thickness=4, text_scale=2) line_annotator = LineCounterAnnotator(thickness=4, text_thickness=4, text_scale=2)

open target video file

with VideoSink(TARGET_VIDEO_PATH, video_info) as sink:

loop over video frames\

frame_count = 1
for frame in tqdm(generator, total=video_info.total_frames):
    # cv2.imwrite(f"/content/input_frame/output_{frame_count}.png",frame)
    print("original frame shape:",frame.shape)
    # model prediction on single frame and conversion to supervision Detections
    results = model(frame)
    detections = 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)
    )
    # filtering out detections with unwanted classes
    mask = np.array([class_id in CLASS_ID for class_id in detections.class_id], dtype=bool)
    detections.filter(mask=mask, inplace=True)
    # tracking detections
    tracks = byte_tracker.update(
        output_results=detections2boxes(detections=detections),
        img_info=frame.shape,
        img_size=frame.shape
    )
    tracker_id = match_detections_with_tracks(detections=detections, tracks=tracks)
    detections.tracker_id = np.array(tracker_id)
    # filtering out detections without trackers
    mask = np.array([tracker_id is not None for tracker_id in detections.tracker_id], dtype=bool)
    detections.filter(mask=mask, inplace=True)
    # format custom labels
    labels = [
        f"#{tracker_id} {CLASS_NAMES_DICT[class_id]} {confidence:0.2f}"
        for _, confidence, class_id, tracker_id
        in detections
    ]
    # updating line counter
    line_counter.update(detections=detections)
    # annotate and display frame
    frame = box_annotator.annotate(frame=frame, detections=detections, labels=labels)
    line_annotator.annotate(frame=frame, line_counter=line_counter)
    print("output frame.shape",frame.shape)
    frame_count = frame_count+1
    cv2.imwrite(f"/content/output_frame/counting_{frame_count}.png",frame)
    frame_count = frame_count+1
    # cv2_imshow(frame)
    sink.write_frame(frame)

Additional

no

Are you willing to submit a PR?

thalapandi commented 3 months ago

Kindly provide any solution for getting better accuracy in people in and out counting