Skip to content

Commit

Permalink
Add OpenCV driver for camera logging
Browse files Browse the repository at this point in the history
  • Loading branch information
m3d committed Jan 31, 2019
1 parent 11d9647 commit d240a53
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
16 changes: 16 additions & 0 deletions config/test-opencv-camera.json
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": []
}
}
39 changes: 39 additions & 0 deletions osgar/drivers/opencv.py
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

0 comments on commit d240a53

Please sign in to comment.