7efacb4b62a04ce5b3d934821dd81913c495f4e8
[imago.git] / camera.py
1 import os
2
3 if os.name == 'posix':
4     
5     import Image
6     import cv2.cv as cv
7
8     class Camera:
9         def __init__(self, device=0):
10             self._cam = cv.CaptureFromCAM(device)
11             print cv.GetCaptureProperty(self._cam, cv.CV_CAP_PROP_CONVERT_RGB)
12             cv.SetCaptureProperty(self._cam, cv.CV_CAP_PROP_CONVERT_RGB, True)
13
14         def get_image(self):
15             for _ in range(8): #HACK
16                 cv.QueryFrame(self._cam)
17             im = cv.QueryFrame(self._cam)
18             # Add the line below if you need it (Ubuntu 8.04+)
19             #im = cv.GetMat(im)
20             #convert Ipl image to PIL image
21             return Image.fromstring("RGB", cv.GetSize(im), im.tostring())
22         
23         def __del__(self):
24             del self._cam 
25
26
27 elif os.name in ('ce', 'nt', 'dos'):
28     
29     from VideoCapture import Device
30     import time
31
32     # TODO exception handling
33     class Camera:
34         def __init__(self, device=0):
35             self._cam = Device()
36             self._cam.setResolution(640, 480)
37             #HACK to let the camera self-adjust:
38             print "The device is getting ready."
39             for _ in xrange(20):
40                self._cam.getImage()
41                time.sleep(0.5)
42
43         def get_image(self):
44             return self._cam.getImage()
45
46         def __del__(self):
47             del self._cam