Skip to content

Commit ef5eeaf

Browse files
committed
feat(try_grab): clean up Python API and update readme
1 parent f6a64c7 commit ef5eeaf

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ Alternatively, you can install suitable wheel from [releases page](https://githu
2222
from zbl import Capture
2323

2424
with Capture(window_name='visual studio code') as cap:
25-
frame = next(cap.frames())
26-
print(frame.shape)
25+
# grab a single frame (numpy.ndarray) and print its shape
26+
print(cap.grab().shape)
2727
```
2828

29-
The snippet above will capture a window which title contains the string `visual studio code`, take one frame (which is represented as a `numpy` array) and print its shape.
29+
The snippet above will capture a window which title contains the string `visual studio code`,
30+
grab one frame and print its shape.
3031

31-
See `Capture` constructor for more options. It is possible to capture the entire screen using `display_id` argument,
32-
for example.
32+
See [Capture](https://github.com/modelflat/zbl/blob/master/zbl_py/zbl/__init__.py) for other API methods.
3333

3434
To run an example using OpenCV's `highgui`:
3535

zbl_py/zbl/__init__.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ def __init__(
4646
def handle(self) -> int:
4747
return self._inner.handle()
4848

49-
def grab(self) -> Frame:
49+
def grab(self) -> numpy.ndarray:
50+
"""
51+
Same as `grab_raw`, but converts captured frame to numpy array.
52+
"""
53+
return frame_to_numpy_array(self.grab_raw())
54+
55+
def grab_raw(self) -> Frame:
5056
"""
5157
Grab the next frame from the capture.
5258
@@ -55,7 +61,16 @@ def grab(self) -> Frame:
5561
"""
5662
return self._inner.grab()
5763

58-
def try_grab(self) -> Optional[Frame]:
64+
def try_grab(self) -> Optional[numpy.ndarray]:
65+
"""
66+
Same as `try_grab_raw`, but converts captured frame to numpy array.
67+
"""
68+
frame = self.try_grab_raw()
69+
if frame is None:
70+
return None
71+
return frame_to_numpy_array(frame)
72+
73+
def try_grab_raw(self) -> Optional[Frame]:
5974
"""
6075
Try grabbing the next frame from the capture.
6176
@@ -64,16 +79,28 @@ def try_grab(self) -> Optional[Frame]:
6479
"""
6580
return self._inner.try_grab()
6681

67-
def raw_frames(self) -> Iterator[Frame]:
82+
def frames(self) -> Iterator[numpy.ndarray]:
83+
"""
84+
Returns an iterator over numpy frames in this capture.
85+
"""
86+
for frame in self.frames_raw():
87+
yield frame_to_numpy_array(frame)
88+
89+
def frames_raw(self) -> Iterator[Frame]:
90+
"""
91+
Returns an iterator over frames in this capture.
92+
"""
6893
while True:
6994
try:
70-
yield self.grab()
71-
except StopIteration:
72-
break
95+
yield self.grab_raw()
96+
except StopIteration as _:
97+
return
7398

74-
def frames(self) -> Iterator[numpy.ndarray]:
75-
for frame in self.raw_frames():
76-
yield frame_to_numpy_array(frame)
99+
def raw_frames(self) -> Iterator[Frame]:
100+
"""
101+
Deprecated, prefer `frames_raw` instead.
102+
"""
103+
yield from self.frames_raw()
77104

78105
def __enter__(self) -> "Capture":
79106
self._inner.start()

0 commit comments

Comments
 (0)