-
Notifications
You must be signed in to change notification settings - Fork 33
/
webcam_sensor.py
132 lines (111 loc) · 4 KB
/
webcam_sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import cv2
import logging
import numpy as np
import os
import subprocess
import shlex
from . import CameraSensor, ColorImage, CameraIntrinsics
class WebcamSensor(CameraSensor):
"""Class for interfacing with a Logitech webcam sensor (720p).
"""
def __init__(self, frame='webcam', device_id=0):
"""Initialize a Logitech webcam sensor.
Parameters
----------
frame : str
A name for the frame in which RGB images are returned.
device_id : int
The device ID for the webcam (by default, zero).
"""
self._frame = frame
self._camera_intr = None
self._device_id = device_id
self._cap = None
self._running = False
self._adjust_exposure = True
# Set up camera intrinsics for the sensor
width, height = 1280, 960
focal_x, focal_y = 1430., 1420.
center_x, center_y = 1280/2, 960/2
self._camera_intr = CameraIntrinsics(self._frame, focal_x, focal_y,
center_x, center_y,
height=height, width=width)
def __del__(self):
"""Automatically stop the sensor for safety.
"""
if self.is_running:
self.stop()
@property
def color_intrinsics(self):
"""CameraIntrinsics : The camera intrinsics for the PhoXi Greyscale camera.
"""
return self._camera_intr
@property
def is_running(self):
"""bool : True if the stream is running, or false otherwise.
"""
return self._running
@property
def frame(self):
"""str : The reference frame of the sensor.
"""
return self._frame
@property
def color_frame(self):
"""str : The reference frame of the sensor.
"""
return self._frame
def start(self):
"""Start the sensor.
"""
self._cap = cv2.VideoCapture(self._device_id + cv2.CAP_V4L2)
if not self._cap.isOpened():
self._running = False
self._cap.release()
self._cap = None
return False
self._cap.set(cv2.CAP_PROP_FRAME_WIDTH, self._camera_intr.width)
self._cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self._camera_intr.height)
self._running = True
# Capture 5 frames to flush webcam sensor
for _ in range(5):
_ = self.frames()
return True
def stop(self):
"""Stop the sensor.
"""
# Check that everything is running
if not self._running:
logging.warning('Webcam not running. Aborting stop')
return False
if self._cap:
self._cap.release()
self._cap = None
self._running = False
return True
def frames(self, most_recent=False):
"""Retrieve a new frame from the PhoXi and convert it to a ColorImage,
a DepthImage, and an IrImage.
Parameters
----------
most_recent: bool
If true, the OpenCV buffer is emptied for the webcam before reading the most recent frame.
Returns
-------
:obj:`tuple` of :obj:`ColorImage`, :obj:`DepthImage`, :obj:`IrImage`, :obj:`numpy.ndarray`
The ColorImage, DepthImage, and IrImage of the current frame.
"""
if most_recent:
for i in range(4):
self._cap.grab()
for i in range(1):
if self._adjust_exposure:
try:
command = 'v4l2-ctl -d /dev/video{} -c exposure_auto=1 -c exposure_auto_priority=0 -c exposure_absolute=100 -c saturation=60 -c gain=140'.format(self._device_id)
FNULL = open(os.devnull, 'w')
subprocess.call(shlex.split(command), stdout=FNULL, stderr=subprocess.STDOUT)
except:
pass
ret, frame = self._cap.read()
rgb_data = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
return ColorImage(rgb_data, frame=self._frame), None, None