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