Webcams and python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Synt4x

    #1

    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.

  • Marc 'BlackJack' Rintsch

    #2
    Re: Webcams and python

    In <1174182674.500 068.252550@p15g 2000hsd.googleg roups.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

    Comment

    • Synt4x

      #3
      Re: Webcams and python

      On 18 mar, 04:24, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
      In <1174182674.500 068.252...@p15g 2000hsd.googleg roups.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.

      Comment

      • sturlamolden

        #4
        Re: Webcams and python

        On Mar 18, 3:41 pm, "Synt4x" <ianmurr...@gma il.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.

        Comment

        • Synt4x

          #5
          Re: Webcams and python

          On 18 mar, 14:17, "sturlamold en" <sturlamol...@y ahoo.nowrote:
          On Mar 18, 3:41 pm, "Synt4x" <ianmurr...@gma il.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( ).

          Comment

          • sturlamolden

            #6
            Re: Webcams and python

            On Mar 18, 8:01 pm, "Synt4x" <ianmurr...@gma il.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.del ay() 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.De vice()
            webcam.setResol ution(640,480)
            pygame.init()
            window = pygame.display. set_mode((640,4 80))
            pygame.display. set_caption("We bCam Demo")
            screen = pygame.display. get_surface()
            while True:
            events = pygame.event.ge t()
            for event in events:
            if event.type == QUIT or event.type == KEYDOWN:
            sys.exit(0)
            im = webcam.getImage ()
            pg_img = pygame.image.fr ombuffer(im.tos tring(), im.size, im.mode)
            screen.blit(pg_ img, (0,0))
            pygame.display. flip()
            pygame.time.del ay(int(1000 * 1.0/fps))


            Comment

            Working...