-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lukas Winkler
authored and
Lukas Winkler
committed
Jul 19, 2020
1 parent
9c0a538
commit 08fd3e5
Showing
4 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import cv2 | ||
from streamz.core import Source, Stream | ||
from tornado import gen | ||
|
||
|
||
@Stream.register_api(staticmethod) | ||
class from_opencv(Source): | ||
""" Stream data from opencv """ | ||
def __init__(self, src: str, poll_interval=0.1, start=False, **kwargs): | ||
self.src = src | ||
self.poll_interval = poll_interval | ||
|
||
super(from_opencv, self).__init__(ensure_io_loop=True, **kwargs) | ||
|
||
self.stopped = True | ||
self.started = False | ||
|
||
if start: | ||
self.start() | ||
|
||
def start(self): | ||
if self.stopped: | ||
self.stopped = False | ||
self.started = True | ||
self.stream = cv2.VideoCapture(self.src) | ||
self.loop.add_callback(self.poll_opencv) | ||
|
||
def do_poll(self): | ||
grabbed, frame = self.stream.read() | ||
if grabbed: | ||
return frame | ||
|
||
@gen.coroutine | ||
def poll_opencv(self): | ||
while True: | ||
frame = self.do_poll() | ||
if frame: | ||
yield self._emit(frame) | ||
else: | ||
yield gen.sleep(self.poll_interval) | ||
if self.stopped: | ||
break | ||
self.stream.release() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from streamz import Stream | ||
from tornado import gen | ||
|
||
from streamz_opencv import from_opencv | ||
|
||
|
||
def test_from_webcam(): | ||
stream = Stream.from_opencv("0", asynchronous=True) | ||
out = stream.sink_to_list() | ||
stream.start() | ||
yield gen.sleep(0.1) |