roboflow / supervision

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

Format for displaying the number of objects in a zone #390

Closed epigraphe closed 1 year ago

epigraphe commented 1 year ago

Search before asking

Question

Hello. I re-read the documentation and did not find how to fix this. Counting objects in a zone looks like the number point zero for me. (1.0 for example) How to remove .0?

image

In all your examples, the objects have an integer number, without dots and numbers after the dot

import cv2
import streamlit as st
import numpy as np
from ultralytics import YOLO
import supervision as sv
import time

ZONE_POLYGON = np.array([
    [0, 0],
    [1, 0],
    [1, 1],
    [0, 1]
])

def main():
    st.title("YOLOv8 Live Demo")

#    frame_width, frame_height = st.slider("Select Webcam Resolution (Width, Height)", 0, 1920, 1280, 1), st.slider("", 0, 1080, 720, 1)
    frame_width, frame_height = 1080, 720

    id_cap = cv2.VideoCapture(0)
    id_cap.set(cv2.CAP_PROP_FRAME_WIDTH, frame_width)
    id_cap.set(cv2.CAP_PROP_FRAME_HEIGHT, frame_height)

    model = YOLO("yolov8n.pt")
    model.predict(source="0", show=False, stream=True, classes=0)
    box_annotator = sv.BoxAnnotator(
        thickness=4,
        text_thickness=4,
        text_scale=2
    )

    zone_polygon = (ZONE_POLYGON * np.array([frame_width, frame_height])).astype(int)
    zone = sv.PolygonZone(polygon=zone_polygon, frame_resolution_wh=(frame_width, frame_height))
    zone_annotator = sv.PolygonZoneAnnotator(
        zone=zone, 
        color=sv.Color.white(),
        thickness=6,
        text_thickness=6,
        text_scale=4
    )

    show_video = st.checkbox("Show Video", value=True)

    st_frame = st.empty()

    start_time_hourly = time.time()
    start_time_10_sec = time.time()

    while show_video:
        success, frame = id_cap.read()

        if success:
            result = model(frame, agnostic_nms=True)[0]
            detections = sv.Detections.from_ultralytics(result)

            labels = [
                f"{model.model.names[class_id]} {confidence:0.2f}"
                for _, _, confidence, class_id, _ in detections
            ]

            frame = box_annotator.annotate(
                scene=frame, 
                detections=detections, 
#                labels=labels
                skip_label=True
            )

            zone.trigger(detections=detections)
            zone_value = zone.current_count
            current_hour = int(time.strftime("%H"))
            filename_hourly = f"{current_hour}-{(current_hour + 1) % 24:02}_{time.strftime('%d-%m-%Y')}.txt"
            current_time = time.strftime("%H:%M:%S")
            current_time_10_sec = time.time()
            elapsed_time_10_sec = current_time_10_sec - start_time_10_sec

            if elapsed_time_10_sec >= 10:
                with open(filename_hourly, "a") as file:
                    file.write(f"{current_time} {zone_value}\n")
                start_time_10_sec = time.time() 
            current_time_hourly = time.time()
            elapsed_time_hourly = current_time_hourly - start_time_hourly

            if elapsed_time_hourly >= 3600:
                start_time_hourly = time.time()

            frame = zone_annotator.annotate(scene=frame)

            st_frame.image(frame, channels="BGR")

        else:
            id_cap.release()
            break

if __name__ == "__main__":
    main()

Additional

No response

SkalskiP commented 1 year ago

Hi, @epigraphe 👋🏻! We fixed that in the current develop. Try to install it like this: pip install supervision==0.15.0rc2, and let us know if the issue persists.

epigraphe commented 1 year ago

Yes fine. After updating to the version you specified, everything works as it should. Thank you very much

SkalskiP commented 1 year ago

Excellent! supervision-0.15.0 should be officially out this week. Then simple pip install supervision will be enough. I'm closing the issue for now.