Skip to content

Commit

Permalink
Ability to select camera number in OpenCV plugin
Browse files Browse the repository at this point in the history
When you select a different camera, the plugin should test if it can grab
a frame from the camera, and if not, revert back to the old camera
number.

This should allow switching between two connected webcams with the OpenCV
plugin, not just using the default webcam. (Often the default webcam on a
laptop is the built-in camera, not useful for beam profiling.)
  • Loading branch information
ptomato committed May 23, 2016
1 parent 6be0330 commit 020080a
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions beams/Webcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from cv import CV_CAP_PROP_FRAME_WIDTH as FRAME_WIDTH
from cv import CV_CAP_PROP_FRAME_HEIGHT as FRAME_HEIGHT
import numpy as N
from traitsui.api import Item, Label, RangeEditor, VGroup, View

from Camera import Camera, CameraError

Expand Down Expand Up @@ -34,6 +35,16 @@ class Webcam(Camera):
'copyright year': '2011',
}

view = View(
VGroup(
Label('Select a different camera number if you\n'
'have more than one webcam attached.\n'
'-1 is the default camera.'),
Item('camera_number',
editor=RangeEditor(mode='spinner', low=-1, high=10)),
),
)

def __init__(self, **traits):
super(Webcam, self).__init__(
id_string='OpenCV driver, unknown camera',
Expand All @@ -60,6 +71,14 @@ def query_frame(self):
raise CameraError('Could not query image', self.camera_number)
self.frame = ipl2array(iplimage)

def _camera_number_changed(self, old, new):
try:
self.open()
except CameraError:
print 'Camera', new, 'was not found or could not be initialized.'
print 'Changing back to camera', old, 'instead.'
self.camera_number = old

def _resolution_default(self):
'''Resolution of the webcam - a 2-tuple'''
width = cv.GetCaptureProperty(self._capture, FRAME_WIDTH)
Expand Down

0 comments on commit 020080a

Please sign in to comment.