error surface plots
[imago.git] / camera.py
1 import os
2
3 if os.name == 'posix':
4     
5     import Image
6     import cv
7
8     class Camera:
9         def __init__(self, vid=0, res=None):
10             self._cam = cv.CreateCameraCapture(vid)
11             if res:
12                 cv.SetCaptureProperty(self._cam, cv.CV_CAP_PROP_FRAME_WIDTH, res[0])
13                 cv.SetCaptureProperty(self._cam, cv.CV_CAP_PROP_FRAME_HEIGHT,
14                                       res[1])
15
16         def get_image(self):
17             for _ in range(5): #HACK
18                 im = cv.QueryFrame(self._cam)
19             # Add the line below if you need it (Ubuntu 8.04+)
20             #im = cv.GetMat(im)
21             #convert Ipl image to PIL image
22             return Image.fromstring("RGB", cv.GetSize(im), im.tostring(), "raw",
23                                     "BGR", 0, 1) 
24         
25         def __del__(self):
26             del self._cam 
27
28
29 elif os.name in ('ce', 'nt', 'dos'):
30     
31     from VideoCapture import Device
32
33     # TODO exception handling
34     class Camera:
35         def __init__(self):
36             self._cam = Device()
37             self._cam.setResolution(640, 480)
38
39         def get_image(self):
40             return self._cam.getImage()
41
42         def __del__(self):
43             del self._cam