feat(time-in-zone): responsive windows size and video output saving #1964
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Video output saving on
ultralytics_file_example.py
& draw window / zone -> JSON saving indraw_zones.py
Description
Summary / change:
This change contains two related enhancements:
draw_zones.py
: The drawing UI/window now adapts to the input video resolution so the drawing canvas never overflows the physical screen. The code computes a scale factor relative to an allowed maximum window size (based on the video resolution and the detected screen area), displays a scaled canvas for the user to draw zones (polygons), and — after drawing — rescales the drawn coordinates back to the original video resolution and writes them to a JSON file. This ensures the saved zone coordinates are accurate for the original video size and that the interactive window is always visible on-screen.ultralytics_file_example.py
: Added video output saving capability. The detection/rendering loop now initializes an OpenCVVideoWriter
(configurable codec, fps and output path), writes processed frames to a file, and cleanly finalizes the writer on exit. This allows generated/annotated video output to be persisted as a standard video file (MP4) for review or downstream pipelines.Motivation & context:
namedWindow
+resizeWindow
andVideoWriter
usage.(References: OpenCV
resizeWindow
/namedWindow
andVideoWriter
usage patterns; ultralytics inference loop that yields frames to be rendered and saved.)What is saved in the JSON:
Each zone is stored as a polygon array in video native coordinates (pixels) so the JSON can be immediately used for runtime logic (counting, alerts, cropping). Example schema below.
List any dependencies that are required for this change
opencv-python
(cv2) — for windowing, drawing andVideoWriter
.numpy
— coordinate arrays / transforms.pathlib
/json
(stdlib) — path handling and JSON writing.ultralytics
(or whichever detection package used previously) — unchanged, only integrated with writer.Minimum recommendations:
opencv-python >= 4.5
numpy >= 1.19
Type of change
Please delete options that are not relevant.
How has this change been tested, please provide a testcase or example of how you tested the change?
Manual test (draw_zones):
The script should:
s
), the script rescales drawn coordinates back to 1280×720 and writes them tozones_1280x720.json
.Verify JSON content (example below) and visually validate by running a small overlay script (below) that loads the JSON and draws zones on the original video frames.
Manual test (ultralytics_file_example):
The script should:
cv2.VideoWriter
with matching size (1280×720), fps (inferred from the source), and codec (H264 with XVID fallback).Play
output/annotated_1280x720.mp4
— annotations and zones should align with the original video.Quick validation overlay script (example):
Automated / unit test ideas:
VideoWriter
results in a readable mp4 file (useffprobe
or cv2 to open it in a test).Any specific deployment considerations
cv2.VideoWriter
requires available codecs on the host. H264 is commonly available, but some environments requireffmpeg
or platform-specific codecs. The implementation includes XVID fallback for better compatibility.Docs
Docs updated? What were the changes:
Added a short section in
docs/
(orREADME
) describing:ultralytics_file_example.py
and codec/fps options;Example JSON schema (actual output format)
Usage Examples
Drawing Zones
Running Detection with Video Output
Key Controls
Drawing Interface:
Video Processing:
--output_video_path
is providedTechnical Details
Coordinate Scaling
The drawing interface automatically scales the video to fit your screen while maintaining aspect ratio. The scaling logic:
Video Codec Support
The video writer tries multiple codecs in order of preference:
JSON Format
The saved JSON contains an array of polygons, where each polygon is an array of [x, y] coordinate pairs representing the vertices of the zone in the original video resolution.
This format is directly compatible with the
load_zones_config()
function used by the detection pipeline.