-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OpenCV driver for camera logging
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"version": 2, | ||
"robot": { | ||
"modules": { | ||
"camera": { | ||
"driver": "osgar.drivers.opencv:LogOpenCVCamera", | ||
"in": [], | ||
"out": ["raw"], | ||
"init": { | ||
"port": 0 | ||
} | ||
} | ||
}, | ||
"links": [] | ||
} | ||
} |
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 @@ | ||
""" | ||
Log video stream provided by OpenCV camera | ||
""" | ||
|
||
import cv2 | ||
from threading import Thread | ||
|
||
from osgar.logger import LogWriter | ||
from osgar.bus import BusShutdownException | ||
|
||
|
||
class LogOpenCVCamera: | ||
def __init__(self, config, bus): | ||
self.input_thread = Thread(target=self.run_input, daemon=True) | ||
self.bus = bus | ||
|
||
port = config.get('port', 0) | ||
self.cap = cv2.VideoCapture(port) | ||
|
||
def start(self): | ||
self.input_thread.start() | ||
|
||
def join(self, timeout=None): | ||
self.input_thread.join(timeout=timeout) | ||
|
||
def run_input(self): | ||
while self.bus.is_alive(): | ||
# Capture frame-by-frame | ||
ret, frame = self.cap.read() | ||
if ret: | ||
retval, data = cv2.imencode('*.jpeg', frame) | ||
if len(data) > 0: | ||
self.bus.publish('raw', data.tobytes()) | ||
self.cap.release() | ||
|
||
def request_stop(self): | ||
self.bus.shutdown() | ||
|
||
# vim: expandtab sw=4 ts=4 |