Skip to content

Commit 020080a

Browse files
committed
Ability to select camera number in OpenCV plugin
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.)
1 parent 6be0330 commit 020080a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

beams/Webcam.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from cv import CV_CAP_PROP_FRAME_WIDTH as FRAME_WIDTH
33
from cv import CV_CAP_PROP_FRAME_HEIGHT as FRAME_HEIGHT
44
import numpy as N
5+
from traitsui.api import Item, Label, RangeEditor, VGroup, View
56

67
from Camera import Camera, CameraError
78

@@ -34,6 +35,16 @@ class Webcam(Camera):
3435
'copyright year': '2011',
3536
}
3637

38+
view = View(
39+
VGroup(
40+
Label('Select a different camera number if you\n'
41+
'have more than one webcam attached.\n'
42+
'-1 is the default camera.'),
43+
Item('camera_number',
44+
editor=RangeEditor(mode='spinner', low=-1, high=10)),
45+
),
46+
)
47+
3748
def __init__(self, **traits):
3849
super(Webcam, self).__init__(
3950
id_string='OpenCV driver, unknown camera',
@@ -60,6 +71,14 @@ def query_frame(self):
6071
raise CameraError('Could not query image', self.camera_number)
6172
self.frame = ipl2array(iplimage)
6273

74+
def _camera_number_changed(self, old, new):
75+
try:
76+
self.open()
77+
except CameraError:
78+
print 'Camera', new, 'was not found or could not be initialized.'
79+
print 'Changing back to camera', old, 'instead.'
80+
self.camera_number = old
81+
6382
def _resolution_default(self):
6483
'''Resolution of the webcam - a 2-tuple'''
6584
width = cv.GetCaptureProperty(self._capture, FRAME_WIDTH)

0 commit comments

Comments
 (0)