correct colors on linux (camera)
authorTomas Musil <tomik.musil@gmail.com>
Fri, 10 Aug 2012 15:17:03 +0000 (17:17 +0200)
committerTomas Musil <tomik.musil@gmail.com>
Fri, 10 Aug 2012 15:17:03 +0000 (17:17 +0200)
camera.py
capture.py

index 7efacb4..308ca93 100644 (file)
--- a/camera.py
+++ b/camera.py
@@ -3,22 +3,20 @@ 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):
+            self._cam = cv.CreateCameraCapture(vid)
 
         def get_image(self):
-            for _ in range(8): #HACK
-                cv.QueryFrame(self._cam)
-            im = cv.QueryFrame(self._cam)
+            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 
@@ -27,18 +25,12 @@ if os.name == 'posix':
 elif os.name in ('ce', 'nt', 'dos'):
     
     from VideoCapture import Device
-    import time
 
     # TODO exception handling
     class Camera:
-        def __init__(self, device=0):
+        def __init__(self):
             self._cam = Device()
             self._cam.setResolution(640, 480)
-            #HACK to let the camera self-adjust:
-            print "The device is getting ready."
-            for _ in xrange(20):
-               self._cam.getImage()
-               time.sleep(0.5)
 
         def get_image(self):
             return self._cam.getImage()
index 98ad7be..b9879d8 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 
-"""Go image capture."""
+"""Go image capture"""
 
 import sys
 import argparse
@@ -26,23 +26,13 @@ class Screen:
 
 def main():
     parser = argparse.ArgumentParser(description=__doc__)
-    #parser.add_argument('file', metavar='file', nargs=1,
-    #                    help="image to analyse")
-    #parser.add_argument('-w', type=int, default=640,
-    #                    help="scale image to the specified width before analysis")
-    parser.add_argument('-d', '--device', type=int, default=0,
-                        help="number of video device to use")
-    #parser.add_argument('-s', '--save', dest='do_something', action='store_const',
-    #                    const=image_save, default=im_debug.show,
-    #                    help="save images instead of displaying them")
+    parser.add_argument('-d', type=int, default=0,
+                        help="video device id")
     args = parser.parse_args()
-
-    cam = Camera(device=args.device)
+   
+    cam = Camera(vid=args.d)
     screen = Screen()
 
-    im = cam.get_image()
-    screen.display_picture(im)
-
     im_number = 0
 
     while True: