473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Webcams and python

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.

Mar 18 '07 #1
5 3231
In <11**********************@p15g2000hsd.googlegroups .com>, Synt4x wrote:
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
Mar 18 '07 #2
On 18 mar, 04:24, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
In <1174182674.500068.252...@p15g2000hsd.googlegroups .com>, Synt4x wrote:
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
The problem with adding a sleep() instrucction is that the "video
feed" looks lagged, if you know what I mean. It looks interrupted.

Mar 18 '07 #3
On Mar 18, 3:41 pm, "Synt4x" <ianmurr...@gmail.comwrote:
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.

Mar 18 '07 #4
On 18 mar, 14:17, "sturlamolden" <sturlamol...@yahoo.nowrote:
On Mar 18, 3:41 pm, "Synt4x" <ianmurr...@gmail.comwrote:
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.
I haven't been able to find the win32api extension, but i've read on
the web that time.sleep() calls win32api.Sleep().

Mar 18 '07 #5
On Mar 18, 8:01 pm, "Synt4x" <ianmurr...@gmail.comwrote:
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))
Mar 18 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Emile van Sebille | last post by:
QOTW: "If we get 2.3.3c1 out in early December, we could release 2.3.3 final before the end of the year, and start 2004 with a 100% bug-free codebase <wink>." -- Tim Peters "cjOr proWe vbCould...
0
by: Emile van Sebille | last post by:
QOTW: "Have you ever used the copy module? I am *not* a beginner, and have used it *once* (and I can't remember what for, either)." -- Michael Hudson "It will likely take a little practice...
0
by: Emile van Sebille | last post by:
QOTW (in the OS agnostic category): "There is a (very popular) Python package out there which exposes the win32 api. I'm not sure what it's called. (win32api? pythonwin? win32all?)" -- Francis...
0
by: Emile van Sebille | last post by:
QOTW (advanced interfaces track): "I'm firmly in favour of any language that can DWIMNWIS." -- Tim Delaney QOTW (MS roadkill track): "Underestimate MS at your own risk. It is one thing to not...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.