This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
-
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.
added asynchronous video capture and video for both wide and far cameras
- Loading branch information
Your Name
committed
Feb 29, 2020
1 parent
e965e46
commit f9f5bea
Showing
11 changed files
with
246 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import cv2 | ||
import threading | ||
from cameras.camera import USBCam | ||
|
||
class VideoCaptureAsync: | ||
def __init__(self,camera): | ||
self.camera = camera | ||
self.grabbed, self.frame = self.camera.read() | ||
self.started = False | ||
self.read_lock = threading.Lock() | ||
|
||
|
||
def startReading(self): | ||
if self.started: | ||
print('Started video capture async') | ||
return None | ||
self.started = True | ||
self.thread = threading.Thread(target=self.update, args = ()) | ||
self.thread.start() | ||
|
||
def update(self): | ||
while self.started: | ||
grabbed, frame = self.camera.read() | ||
with self.read_lock: | ||
self.frame = frame | ||
self.grabbed = grabbed | ||
|
||
def read(self): | ||
with self.read_lock: | ||
frame = self.frame.copy() | ||
grabbed = self.grabbed | ||
return grabbed, frame | ||
|
||
def stop(self): | ||
self.started = False | ||
self.thread.join() | ||
|
||
def __exit__(self): | ||
self.camera.stop() |
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
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,44 @@ | ||
import uuid | ||
from tornado.websocket import WebSocketHandler, WebSocketClosedError | ||
import logging | ||
import json | ||
logger = logging.getLogger(__name__) | ||
|
||
class FarCameraFeedWS(WebSocketHandler): | ||
""" | ||
watchers is a class level array, anyone connecting shares the same array | ||
""" | ||
watchers = set() | ||
def open(self): | ||
self.uid = str(uuid.uuid4()) | ||
logger.info("CameraFeed websocket opened %s" % self.uid) | ||
self.write_message('connected') | ||
self.write_message(json.dumps({ | ||
'socketid':self.uid | ||
})) | ||
|
||
def check_origin(self, origin): | ||
""" | ||
Allow CORS requests | ||
""" | ||
return True | ||
|
||
""" | ||
broadcast to clients, assumes its target data | ||
""" | ||
def on_message(self, message): | ||
# logger.info('pushing image') | ||
if isinstance(message, str): | ||
logger.info(message) | ||
if message == 'open feed': | ||
FarCameraFeedWS.watchers.add(self) | ||
if message == 'close feed': | ||
FarCameraFeedWS.watchers.remove(self) | ||
else: | ||
for waiter in FarCameraFeedWS.watchers: | ||
waiter.write_message(message, binary=True) | ||
|
||
def on_close(self): | ||
logger.info("CameraFeed websocket closed %s" % self.uid) | ||
if self in FarCameraFeedWS.watchers: | ||
FarCameraFeedWS.watchers.remove(self) |
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 uuid | ||
from tornado.websocket import WebSocketHandler, WebSocketClosedError | ||
import logging | ||
import json | ||
logger = logging.getLogger(__name__) | ||
|
||
class WideCameraFeedWS(WebSocketHandler): | ||
""" | ||
""" | ||
watchers = set() | ||
def open(self): | ||
self.uid = str(uuid.uuid4()) | ||
logger.info("WideCameraFeed websocket opened %s" % self.uid) | ||
self.write_message('connected') | ||
self.write_message(json.dumps({ | ||
'socketid':self.uid | ||
})) | ||
|
||
def check_origin(self, origin): | ||
""" | ||
Allow CORS requests | ||
""" | ||
return True | ||
|
||
""" | ||
broadcast to clients, assumes its target data | ||
""" | ||
def on_message(self, message): | ||
# logger.info('pushing image') | ||
if isinstance(message, str): | ||
logger.info(message) | ||
if message == 'open feed': | ||
WideCameraFeedWS.watchers.add(self) | ||
if message == 'close feed': | ||
WideCameraFeedWS.watchers.remove(self) | ||
else: | ||
for waiter in WideCameraFeedWS.watchers: | ||
waiter.write_message(message, binary=True) | ||
|
||
def on_close(self): | ||
logger.info("CameraFeed websocket closed %s" % self.uid) | ||
if self in WideCameraFeedWS.watchers: | ||
WideCameraFeedWS.watchers.remove(self) |
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
Oops, something went wrong.