Webcams and python

S

Synt4x

I'm using VideoCapture in windows to obtain images from my webcam. The
thing is, if i want to show a live video from my webcam i have to make
an infinite loop and request an image everytime:

from VideoCapture import Device
cam = Device()

while 1:
img = cam.getImage()

Now, by doing this, my processor fires up to 100% load, which
obviously makes my pc useless.

Is there any way or algorithm to lower the cpu load?

Thx.
 
M

Marc 'BlackJack' Rintsch

from VideoCapture import Device
cam = Device()

while 1:
img = cam.getImage()

Now, by doing this, my processor fires up to 100% load, which
obviously makes my pc useless.

Is there any way or algorithm to lower the cpu load?

Just throw a small `time.sleep()` into the loop.

Ciao,
Marc 'BlackJack' Rintsch
 
S

Synt4x

Just throw a small `time.sleep()` into the loop.

Ciao,
Marc 'BlackJack' Rintsch

The problem with adding a sleep() instrucction is that the "video
feed" looks lagged, if you know what I mean. It looks interrupted.
 
S

sturlamolden

The problem with adding a sleep() instrucction is that the "video
feed" looks lagged, if you know what I mean. It looks interrupted.

You could try win32api.Sleep(0) instead, which will release the
reminder of the time slice.
 
S

Synt4x

You could try win32api.Sleep(0) instead, which will release the
reminder of the time slice.

I haven't been able to find the win32api extension, but i've read on
the web that time.sleep() calls win32api.Sleep().
 
D

Dennis Lee Bieber

The problem with adding a sleep() instrucction is that the "video
feed" looks lagged, if you know what I mean. It looks interrupted.

Other alternatives, presuming you are on Windows... Reduce your
process priority group -- and hope it only interrupts between frames <G>

While this can be done from the Windows task manager, process page,
you'll probably want to embed it in the program:
win32process.SetPriorityClass() is likely the one... Part of the win32
extensions (that come standard with the ActiveState build; you'll have
to download them separately for other builds:
http://sourceforge.net/projects/pywin32/ )
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
S

sturlamolden

I haven't been able to find the win32api extension, but i've read on
the web that time.sleep() calls win32api.Sleep().


I tested VideoCapture using PyGame to draw the graphics. PyGame wraps
SDL which uses DirectDraw on Windows. I don't think it matters much,
but DirectX is known to be faster than e.g. GDI and DIB sections.

With 10 fps, I get around 10-15% CPU usage on my laptop. The video
does not look too bad, but it's not perfect. With 20 fps I get 30-40%
CPU usage, and the video looks very smooth. I don't think my webcam
could do any better anyway. CPU use in not anywhere near saturation.

pygame.time.delay() just calls Sleep() in the Windows API, after
setting time resolution to multimedia mode. So adjust the amount of
time you keep the thread asleep.


import VideoCapture
import pygame
from pygame.locals import *
import sys

fps = 20.0
webcam = VideoCapture.Device()
webcam.setResolution(640,480)
pygame.init()
window = pygame.display.set_mode((640,480))
pygame.display.set_caption("WebCam Demo")
screen = pygame.display.get_surface()
while True:
events = pygame.event.get()
for event in events:
if event.type == QUIT or event.type == KEYDOWN:
sys.exit(0)
im = webcam.getImage()
pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode)
screen.blit(pg_img, (0,0))
pygame.display.flip()
pygame.time.delay(int(1000 * 1.0/fps))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top