Skip to content

Commit

Permalink
Merge pull request #662 from coddingtonbear/add_camera_property_settings
Browse files Browse the repository at this point in the history
Add functionality allowing one to specify camera settings via one's INI settings.
  • Loading branch information
vlachoudis authored Oct 2, 2017
2 parents 4aa7125 + 576c931 commit 38f4b00
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,57 @@ def __init__(self, prefix=""):
if cv is None: return
self.prefix = prefix
self.idx = Utils.getInt("Camera", prefix)
self.props = self._getCameraProperties(prefix)
self.camera = None
self.image = None
self.frozen = None
self.imagetk = None

def _getCameraProperties(self, prefix):
""" Gather user-defined camera configuration properties
Allows user to specify parameters like::
[Camera]
aligncam_height = 640
aligncam_width = 480
to adjust the capture settings of the 'aligncam' camera.
:param str prefix: Prefix to gather
:returns: Dictionary mapping property suffix with a 2-tuple of
a function defining the data type, and a CV_CAP_PROP
property.
"""
POSSIBLE_PROPERTIES = {
'height': (Utils.getInt, cv.cv.CV_CAP_PROP_FRAME_HEIGHT, ),
'width': (Utils.getInt, cv.cv.CV_CAP_PROP_FRAME_WIDTH, ),
'fps': (Utils.getInt, cv.cv.CV_CAP_PROP_FPS, ),
'codec': (Utils.getStr, cv.cv.CV_CAP_PROP_FOURCC, ),
'brightness': (Utils.getInt, cv.cv.CV_CAP_PROP_BRIGHTNESS, ),
'contrast': (Utils.getInt, cv.cv.CV_CAP_PROP_CONTRAST, ),
'saturation': (Utils.getInt, cv.cv.CV_CAP_PROP_SATURATION, ),
'hue': (Utils.getInt, cv.cv.CV_CAP_PROP_HUE, ),
'gain': (Utils.getInt, cv.cv.CV_CAP_PROP_GAIN, ),
'exposure': (Utils.getInt, cv.cv.CV_CAP_PROP_EXPOSURE, ),
}

UNSPECIFIED = object()
properties_set = {}

for key, data in POSSIBLE_PROPERTIES.items():
fn, prop = data
value = fn(
'Camera',
'_'.join([prefix, key]),
default=UNSPECIFIED
)

if value is not UNSPECIFIED:
properties_set[prop] = value

return properties_set

#-----------------------------------------------------------------------
def isOn(self):
if cv is None: return False
Expand All @@ -55,6 +101,10 @@ def isOn(self):
def start(self):
if cv is None: return
self.camera = cv.VideoCapture(self.idx)

for prop_id, prop_value in self.props.items():
self.camera.set(prop_id, prop_value)

if self.camera is None: return
s, self.image = self.camera.read()
if not self.camera.isOpened():
Expand Down

0 comments on commit 38f4b00

Please sign in to comment.