capture -- live preview
[imago.git] / camera.py
index a1be68b..d5dac75 100644 (file)
--- a/camera.py
+++ b/camera.py
@@ -3,21 +3,24 @@ import os
 if os.name == 'posix':
     
     import Image
-    import cv2.cv as cv
+    import cv
 
     class Camera:
-        def __init__(self, device=0):
-            self._cam = cv.CaptureFromCAM(device)
-            print cv.GetCaptureProperty(self._cam, cv.CV_CAP_PROP_CONVERT_RGB)
-            cv.SetCaptureProperty(self._cam, cv.CV_CAP_PROP_CONVERT_RGB, True)
+        def __init__(self, vid=0, res=None):
+            self._cam = cv.CreateCameraCapture(vid)
+            if res:
+                cv.SetCaptureProperty(self._cam, cv.CV_CAP_PROP_FRAME_WIDTH, res[0])
+                cv.SetCaptureProperty(self._cam, cv.CV_CAP_PROP_FRAME_HEIGHT,
+                                      res[1])
 
         def get_image(self):
-            for _ in range(8): #HACK
+            for _ in range(5): #HACK
                 im = cv.QueryFrame(self._cam)
             # Add the line below if you need it (Ubuntu 8.04+)
             #im = cv.GetMat(im)
             #convert Ipl image to PIL image
-            return Image.fromstring("RGB", cv.GetSize(im), im.tostring())
+            return Image.fromstring("RGB", cv.GetSize(im), im.tostring(), "raw",
+                                    "BGR", 0, 1) 
         
         def __del__(self):
             del self._cam 
@@ -29,7 +32,7 @@ elif os.name in ('ce', 'nt', 'dos'):
 
     # TODO exception handling
     class Camera:
-        def __init__(self, device=0):
+        def __init__(self):
             self._cam = Device()
             self._cam.setResolution(640, 480)