😷 Add stride to get_video_frames_generator function. Initial from_yolov8 with masks refactor.

This commit is contained in:
SkalskiP committed 2023-06-06 17:35:40 +02:00
1 parent 1c043d6cc7
commit 98f60c24d3
2 files changed
+26 -9

No files matched your search

+13 -5
View File
@@ -3,8 +3,9 @@ from __future__ import annotations
from dataclasses import astuple, dataclass
from typing import Any, Iterator, List, Optional, Tuple, Union
import numpy as np
import cv2
import numpy as np
from supervision.detection.utils import non_max_suppression, xywh_to_xyxy
from supervision.geometry.core import Position
from supervision.internal import deprecated
@@ -196,11 +197,18 @@ class Detections:
pad = (0, 0)
gain = 0
if inference_shape != orig_shape:
gain = min(inference_shape[0] / orig_shape[0], inference_shape[1] / orig_shape[1]) # gain = old / new
pad = (inference_shape[1] - orig_shape[1] * gain) / 2, (inference_shape[0] - orig_shape[0] * gain) / 2
gain = min(
inference_shape[0] / orig_shape[0],
inference_shape[1] / orig_shape[1],
) # gain = old / new
pad = (inference_shape[1] - orig_shape[1] * gain) / 2, (
inference_shape[0] - orig_shape[0] * gain
) / 2
top, left = int(pad[1]), int(pad[0]) # y, x
bottom, right = int(inference_shape[0] - pad[1]), int(inference_shape[1] - pad[0])
bottom, right = int(inference_shape[0] - pad[1]), int(
inference_shape[1] - pad[0]
)
mask_maps = []
for i in range(masks.shape[0]):
@@ -213,7 +221,7 @@ class Detections:
xyxy=yolov8_results.boxes.xyxy.cpu().numpy(),
confidence=yolov8_results.boxes.conf.cpu().numpy(),
class_id=yolov8_results.boxes.cls.cpu().numpy().astype(int),
mask=mask_maps
mask=mask_maps,
)
@classmethod
+13 -4
View File
@@ -97,31 +97,40 @@ class VideoSink:
self.__writer.release()
def get_video_frames_generator(source_path: str) -> Generator[np.ndarray, None, None]:
def get_video_frames_generator(
source_path: str,
stride: int = 1
) -> Generator[np.ndarray, None, None]:
"""
Get a generator that yields the frames of the video.
Args:
source_path (str): The path of the video file.
stride (int): The number of frames to skip before returning the next one.
Returns:
(Generator[np.ndarray, None, None]): A generator that yields the frames of the video.
Examples:
```python
>>> from supervision import get_video_frames_generator
>>> import supervision as sv
>>> for frame in get_video_frames_generator(source_path='source_video.mp4'):
>>> for frame in sv.get_video_frames_generator(source_path='source_video.mp4', stride=2):
... ...
```
"""
video = cv2.VideoCapture(source_path)
if not video.isOpened():
raise Exception(f"Could not open video at {source_path}")
frame_count = 0
success, frame = video.read()
while success:
yield frame
if frame_count % stride == 0:
yield frame
success, frame = video.read()
frame_count += 1
video.release()