camera capture
authorTomas Musil <tomik.musil@gmail.com>
Mon, 30 Apr 2012 01:24:07 +0000 (03:24 +0200)
committerTomas Musil <tomik.musil@gmail.com>
Mon, 30 Apr 2012 01:24:07 +0000 (03:24 +0200)
camera.py [new file with mode: 0644]
capture.py [new file with mode: 0755]

diff --git a/camera.py b/camera.py
new file mode 100644 (file)
index 0000000..0f524f3
--- /dev/null
+++ b/camera.py
@@ -0,0 +1,34 @@
+import os
+
+if os.name == 'posix':
+    
+    import Image
+    import cv
+
+    class Camera:
+        def __init__(self):
+            self._cam = cv.CreateCameraCapture(0)
+
+        def get_image(self):
+            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())
+        
+
+elif os.name in ('ce', 'nt', 'dos'):
+    
+    from VideoCapture import Device
+
+    # TODO exception handling
+    class Camera:
+        def __init__(self):
+            self._cam = Device()
+            self._cam.setResolution(640, 480)
+
+        def shot(self):
+            self._cam.saveSnapshot('image.jpg', quality=100)
+
+        def __del__(self):
+            del self._cam
diff --git a/capture.py b/capture.py
new file mode 100755 (executable)
index 0000000..6aeaf53
--- /dev/null
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+
+import sys
+
+import pygame
+from pygame.locals import QUIT, KEYDOWN
+import Image
+
+from camera import Camera
+
+
+pygame.init()
+window = pygame.display.set_mode((640,480))
+pygame.display.set_caption("WebCam Demo")
+screen = pygame.display.get_surface()
+
+cam = Camera()
+
+while True:
+    events = pygame.event.get()
+    for event in events:
+        if event.type == QUIT:
+            sys.exit(0)
+        elif event.type == KEYDOWN:
+            im = cam.get_image()
+            pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode)
+            screen.blit(pg_img, (0,0))
+            pygame.display.flip()