cv-frames 0.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
cvframes/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
cvframes/iterate.py,sha256=XA5HOTkF-6y-5bkd8ExZn4H9_S_CT1WaJ05byZ-3h2Q,2688
|
|
3
|
+
cv_frames-0.0.0.dist-info/METADATA,sha256=MriWGUqYdMSzEoDCbZKpj1MlZdMS_f8fsvxmW70WWe8,148
|
|
4
|
+
cv_frames-0.0.0.dist-info/WHEEL,sha256=1tXe9gY0PYatrMPMDd6jXqjfpz_B-Wqm32CPfRC58XU,91
|
|
5
|
+
cv_frames-0.0.0.dist-info/top_level.txt,sha256=GJnY7NlV_RqhT_FG8XHpzTfSdF4GlYJR6pWj8iJ0BSk,9
|
|
6
|
+
cv_frames-0.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
cvframes
|
cvframes/__init__.py
ADDED
|
File without changes
|
cvframes/iterate.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import Callable, Generator, Optional, Tuple, TypeVar
|
|
3
|
+
|
|
4
|
+
import cv2
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
T = TypeVar("T")
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class IOCapture:
|
|
11
|
+
def __init__(self, source: str | Path, oname: str | Path = ""):
|
|
12
|
+
self.icap = cv2.VideoCapture(str(source))
|
|
13
|
+
self.ocap = (
|
|
14
|
+
cv2.VideoWriter(
|
|
15
|
+
str(oname),
|
|
16
|
+
cv2.VideoWriter_fourcc(*"mp4v"),
|
|
17
|
+
self.icap.get(cv2.CAP_PROP_FPS),
|
|
18
|
+
(
|
|
19
|
+
int(self.icap.get(cv2.CAP_PROP_FRAME_WIDTH)),
|
|
20
|
+
int(self.icap.get(cv2.CAP_PROP_FRAME_HEIGHT)),
|
|
21
|
+
),
|
|
22
|
+
)
|
|
23
|
+
if oname
|
|
24
|
+
else None
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
def is_opened(self) -> bool:
|
|
28
|
+
return self.icap.isOpened()
|
|
29
|
+
|
|
30
|
+
def read(self) -> Tuple[bool, np.ndarray]:
|
|
31
|
+
return self.icap.read()
|
|
32
|
+
|
|
33
|
+
def write(self, frame: np.ndarray) -> None:
|
|
34
|
+
if self.ocap is not None:
|
|
35
|
+
self.ocap.write(frame)
|
|
36
|
+
|
|
37
|
+
def release(self) -> None:
|
|
38
|
+
self.icap.release()
|
|
39
|
+
if self.ocap is not None:
|
|
40
|
+
self.ocap.release()
|
|
41
|
+
|
|
42
|
+
def set(self, prop_id: int, value: float) -> None:
|
|
43
|
+
self.icap.set(prop_id, value)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def iterate_generic(
|
|
47
|
+
ipath: Path,
|
|
48
|
+
opath: Optional[Path],
|
|
49
|
+
start_frame: int,
|
|
50
|
+
stop_frame: int,
|
|
51
|
+
process_frames: Callable[[np.ndarray], T],
|
|
52
|
+
) -> Generator[tuple[IOCapture, T], None, None]:
|
|
53
|
+
capture = IOCapture(str(ipath), oname=opath or "")
|
|
54
|
+
capture.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
|
|
55
|
+
count = start_frame
|
|
56
|
+
|
|
57
|
+
if not capture.is_opened():
|
|
58
|
+
return
|
|
59
|
+
|
|
60
|
+
try:
|
|
61
|
+
while True:
|
|
62
|
+
ret, frame = capture.read()
|
|
63
|
+
count += 1
|
|
64
|
+
if not ret:
|
|
65
|
+
break
|
|
66
|
+
if stop_frame > 0 and count >= stop_frame:
|
|
67
|
+
break
|
|
68
|
+
|
|
69
|
+
yield capture, process_frames(frame)
|
|
70
|
+
finally:
|
|
71
|
+
capture.release()
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def iterate(
|
|
75
|
+
ipath: Path,
|
|
76
|
+
opath: Optional[Path] = None,
|
|
77
|
+
start_frame: int = -1,
|
|
78
|
+
stop_frame: int = -1,
|
|
79
|
+
) -> Generator[tuple[IOCapture, np.ndarray], None, None]:
|
|
80
|
+
return iterate_generic(
|
|
81
|
+
ipath,
|
|
82
|
+
opath,
|
|
83
|
+
start_frame,
|
|
84
|
+
stop_frame,
|
|
85
|
+
lambda frame: frame,
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def iterate_sbs(
|
|
90
|
+
ipath: Path,
|
|
91
|
+
opath: Optional[Path] = None,
|
|
92
|
+
start_frame: int = -1,
|
|
93
|
+
stop_frame: int = -1,
|
|
94
|
+
) -> Generator[Tuple[IOCapture, Tuple[np.ndarray, np.ndarray]], None, None]:
|
|
95
|
+
def processor(frame: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
|
|
96
|
+
_, width, _ = frame.shape
|
|
97
|
+
mid = width // 2
|
|
98
|
+
return frame[:, :mid, :], frame[:, mid:, :]
|
|
99
|
+
|
|
100
|
+
return iterate_generic(
|
|
101
|
+
ipath,
|
|
102
|
+
opath,
|
|
103
|
+
start_frame,
|
|
104
|
+
stop_frame,
|
|
105
|
+
processor,
|
|
106
|
+
)
|