a1be68b2546e90724978eaddb9dc5fe12ae3d44a
[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                 im = cv.QueryFrame(self._cam)
17             # Add the line below if you need it (Ubuntu 8.04+)
18             #im = cv.GetMat(im)
19             #convert Ipl image to PIL image
20             return Image.fromstring("RGB", cv.GetSize(im), im.tostring())
21         
22         def __del__(self):
23             del self._cam 
24
25
26 elif os.name in ('ce', 'nt', 'dos'):
27     
28     from VideoCapture import Device
29
30     # TODO exception handling
31     class Camera:
32         def __init__(self, device=0):
33             self._cam = Device()
34             self._cam.setResolution(640, 480)
35
36         def get_image(self):
37             return self._cam.getImage()
38
39         def __del__(self):
40             del self._cam