Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
epigraphe opened this issue Oct 3, 2023 · 3 comments
Closed
1 task done

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

epigraphe opened this issue Oct 3, 2023 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@epigraphe
Copy link

epigraphe commented Oct 3, 2023

Search before asking

  • I have searched the Supervision issues and found no similar feature requests.

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

@epigraphe epigraphe added the question Further information is requested label Oct 3, 2023
@SkalskiP SkalskiP self-assigned this Oct 3, 2023
@SkalskiP
Copy link
Collaborator

SkalskiP commented Oct 3, 2023

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
Copy link
Author

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

@SkalskiP
Copy link
Collaborator

SkalskiP commented Oct 3, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants