comments to the specification
[imago.git] / src / camera.py
1 """Camera module.
2
3 This module handles various backends (different for every OS) for streaming the video from a (web)camera.
4 """
5
6 import os
7
8 if os.name == 'posix':
9     
10     import Image
11     import cv
12
13     class Camera:
14         """Implement basic camera capabilities
15         
16         This class has different implementations for different OS. On posix
17         systems it calls to opencv, on Windows to VideoCapture."""
18         # TODO what about win 64?
19         # TODO why not openCV on win?
20         # TODO document VideoCapture as a dependency
21         def __init__(self, vid=0, res=None):
22             self._cam = cv.CreateCameraCapture(vid)
23             if res:
24                 cv.SetCaptureProperty(self._cam, cv.CV_CAP_PROP_FRAME_WIDTH, res[0])
25                 cv.SetCaptureProperty(self._cam, cv.CV_CAP_PROP_FRAME_HEIGHT,
26                                       res[1])
27
28         def get_image(self):
29             """Get a new image from the camera."""
30             for _ in range(5): #HACK TODO document this
31                 im = cv.QueryFrame(self._cam)
32             return Image.fromstring("RGB", cv.GetSize(im), im.tostring(), "raw",
33                                     "BGR", 0, 1) 
34         
35         def __del__(self):
36             del self._cam 
37
38
39 elif os.name in ('ce', 'nt', 'dos'):
40     
41     from VideoCapture import Device
42
43     # TODO exception handling
44     class Camera:
45         def __init__(self):
46             self._cam = Device()
47             self._cam.setResolution(640, 480)
48
49         def get_image(self):
50             return self._cam.getImage()
51
52         def __del__(self):
53             del self._cam
54
55 else:
56     pass # TODO exception "Cannot recognise OS." or back to posix?