roboflow / supervision

We write your reusable computer vision tools. 💜
https://supervision.roboflow.com
MIT License
23.9k stars 1.78k forks source link

Class none person, how to remove it ? #686

Closed Rasantis closed 10 months ago

Rasantis commented 10 months ago

Search before asking

Question

I'm running this code on my raspberry pi 4 with picamera to detect and count people, using yolov8n, but sometimes it detects a class called none person, and then shows several boxes, when crossing the line it ends up counting these boxes with the class none person, and then, it ends up uncalibrating the count... I didn't find it in the documentation about this class called none person... how to disable it?

`import cv2 import json import numpy as np from picamera2 import Picamera2 from ultralytics import YOLO import supervision as sv import os

class PiLineCounter: def init(self, lines_json_path, model_path): with open(lines_json_path, 'r') as f: self.lines_data = json.load(f) self.model = YOLO(model_path)

    # Inicialização dos anotadores
    self.line_annotator = sv.LineZoneAnnotator(
        thickness=1,
        text_thickness=1,
        text_scale=1,
        custom_in_text="entrando",
        custom_out_text="saindo"
    )

    self.box_annotator = sv.BoxAnnotator(
        thickness=2,
        text_thickness=1,
        text_scale=0.5
    )

    # Inicialização da PiCamera2
    self.picam2 = Picamera2()
    preview_config = self.picam2.create_preview_configuration()
    self.picam2.configure(preview_config)
    self.picam2.start()

    # Inicialização dos contadores de linha
    self.line_counters = {}
    self.initialize_counters()

def initialize_counters(self):
    for line_key, line_value in self.lines_data.items():
        # Usar as coordenadas das linhas diretamente do JSON
        start_point_x, start_point_y = line_value['points'][0]
        end_point_x, end_point_y = line_value['points'][1]
        start_point = sv.Point(start_point_x, start_point_y)
        end_point = sv.Point(end_point_x, end_point_y)
        self.line_counters[line_key] = sv.LineZone(start=start_point, end=end_point)

def run(self):
    while True:
            frame = self.picam2.capture_array()
            frame = cv2.cvtColor(np.array(frame), cv2.COLOR_RGB2BGR)

            results = self.model.track(frame, show=False, stream=False, agnostic_nms=True, imgsz=320)
            print(f"Número de resultados de detecção: {len(results)}")
            for result in results:
                detections = sv.Detections.from_ultralytics(result)

                if detections is None or len(detections.xyxy) == 0:
                    print("Nenhuma detecção neste frame. Pulando...")
                    continue

                # Imprimir todas as detecções e seus respectivos class_id, labels e confianças
                for d in detections:
                    class_id = d[3]
                    label = self.model.model.names[class_id]
                    confidence = d[2]
                    print(f"Detecção: class_id={class_id}, label={label}, confiança={confidence:.2f}")

                detections_filtered = [d for d in detections if d[3] == 0]
                print(f"Número de detecções de pessoas: {len(detections_filtered)}")

                labels = [f"{d[4]} {self.model.model.names[d[3]]} {d[2]:0.2f}" for d in detections_filtered]

                for line_key in self.line_counters.keys():
                    in_count, out_count = self.line_counters[line_key].trigger(detections=detections_filtered)
                    print(f"Linha {line_key}: Entrando - {in_count}, Saindo - {out_count}")

                # Criar um objeto Detections que possa ser usado pelo BoxAnnotator
                if len(detections_filtered) > 0:
                    xyxy = np.array([d[0] for d in detections_filtered])
                    confidences = np.array([d[2] for d in detections_filtered])
                    class_ids = np.array([d[3] for d in detections_filtered])
                    tracker_ids = np.array([d[4] for d in detections_filtered])

                    detections_for_annotation = sv.Detections(xyxy=xyxy, confidence=confidences, class_id=class_ids, tracker_id=tracker_ids)

                    frame = self.box_annotator.annotate(
                        scene=frame,
                        detections=detections_for_annotation,
                        labels=labels
                    )
                else:
                    print("Nenhuma detecção de pessoas neste frame.")

                for line_key in self.line_counters.keys():
                    self.line_annotator.annotate(frame=frame, line_counter=self.line_counters[line_key])

            # Exibir o frame original sem redimensionamento
            cv2.imshow("PiCamera Line Counter", frame)
            #cv2.imwrite('/dev/shm/frame.jpg', frame)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
    cv2.destroyAllWindows()

if name == 'main': lines_json_path = "lines_with_doubled_data.json"

model_path = "yolov8n.pt"

pi_line_counter = PiLineCounter(
    lines_json_path=lines_json_path,
    model_path=model_path
)
pi_line_counter.run()

`

Additional

lines_with_doubled_data.json

SkalskiP commented 10 months ago

Hi, @Rasantis 👋🏻 ! Thanks a lot for your interest in supervision. I am sorry that I have not been responsive for the last few days. Before Christmas, I was busy with duties unrelated to supervision, and I was off for the last few days.

Before we start, let's convert this issue into a discussion and move it to the Q&A section.