exzile
Posts: 132
Joined: Sun Jul 20, 2014 5:33 am

Need USB Camera Python Code!

Wed Aug 13, 2014 12:08 am

I have done a search to find some example code or libraries for using the webcam with the raspberry 2 via python code. I am looking to just take still shots. Does anyone know of any sources?

exzile
Posts: 132
Joined: Sun Jul 20, 2014 5:33 am

Re: Need USB Camera Python Code!

Thu Aug 14, 2014 7:50 pm

bump!

exzile
Posts: 132
Joined: Sun Jul 20, 2014 5:33 am

Re: Need USB Camera Python Code!

Fri Aug 15, 2014 5:04 am

Unless there is a way to call a current software program within python to take a picture from the web cam.

User avatar
AndyD
Posts: 2331
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: Need USB Camera Python Code!

Fri Aug 15, 2014 5:16 am

You could try this or search the internet for linux python v4l2 example.

mikerr
Posts: 2778
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: Need USB Camera Python Code!

Fri Aug 15, 2014 8:09 am

Opencv:

Code: Select all

import cv2
cam=cv2.VideoCapture(0)
img=cam.read()

cv2.namedwindow("camera", cv2.CV_WINDOW_AUTOSIZE)
cv2.imshow("camera",img)
cv2.waitKey(0)
cv2.destroywindow("camera")
Android app - Raspi Card Imager - download and image SD cards - No PC required !

thsBavR10
Posts: 233
Joined: Sat Jul 21, 2012 3:11 pm

Re: Need USB Camera Python Code!

Fri Aug 15, 2014 8:35 am

Are you looking for "Pure Python camera interface" ?
http://www.raspberrypi.org/forums/viewt ... 43&t=56478

gordon77
Posts: 4170
Joined: Sun Aug 05, 2012 3:12 pm

Re: Need USB Camera Python Code!

Fri Aug 15, 2014 9:36 am

try this...

gordon77

Code: Select all

#!/usr/bin/python
import os
import pygame, sys

from pygame.locals import *
import pygame.camera

width = 640
height = 480

#initialise pygame   
pygame.init()
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0",(width,height))
cam.start()

#setup window
windowSurfaceObj = pygame.display.set_mode((width,height),1,16)
pygame.display.set_caption('Camera')

#take a picture
image = cam.get_image()
cam.stop()

#display the picture
catSurfaceObj = image
windowSurfaceObj.blit(catSurfaceObj,(0,0))
pygame.display.update()

#save picture
pygame.image.save(windowSurfaceObj,'picture.jpg')
   

gordon77
Posts: 4170
Joined: Sun Aug 05, 2012 3:12 pm

Re: Need USB Camera Python Code!

Sun Aug 17, 2014 4:15 pm

Did you manage to get it to work with any of the suggestions?

SlashWW3
Posts: 3
Joined: Wed Aug 20, 2014 9:23 am

Re: Need USB Camera Python Code!

Wed Aug 20, 2014 11:18 am

I personally prefer FSWEBCAM. Here is sample code of what I use:

import time
from subprocess import call

for i in xrange(1,11):
call(["fswebcam", "-d", "/dev/video1", "-r", "1280x720", "--no-banner", "./%d.jpg" % i])
time.sleep(1)

exzile
Posts: 132
Joined: Sun Jul 20, 2014 5:33 am

Re: Need USB Camera Python Code!

Wed Aug 20, 2014 1:55 pm

I got it to work with pygame. But the resolution and fps suck. But it is workable. I want to go to a wireless ip camera now. Any suggestions on how to make such a thing work, where I am capable of capturing an image?

gordon77
Posts: 4170
Joined: Sun Aug 05, 2012 3:12 pm

Re: Need USB Camera Python Code!

Wed Aug 20, 2014 4:26 pm

exzile wrote:I got it to work with pygame. But the resolution and fps suck. But it is workable. I want to go to a wireless ip camera now. Any suggestions on how to make such a thing work, where I am capable of capturing an image?
What resolutions are you using ? Is this limited by the webcam ?. I can manage about 4 fps at 640x480 with a Philips webcam but it does have a habit of locking up.

Gordon77

exzile
Posts: 132
Joined: Sun Jul 20, 2014 5:33 am

Re: Need USB Camera Python Code!

Wed Aug 20, 2014 4:56 pm

I will check the resolution later. I'm focusing more on trying to use a wireless ip camera now.

amulla
Posts: 3
Joined: Mon Jan 02, 2017 2:05 pm
Contact: Website

Re: Need USB Camera Python Code!

Mon Jan 02, 2017 9:03 pm

I edited the post by slashWW3 slightly. Seems to work for me.

Code: Select all

from subprocess import call
from datetime import datetime


def capture():
    dt = datetime.now()
    dtime = dt[0:4]+dt[5:7]+dt[8:10]+dt[11:13]+dt[14:16]+dt[17:19]
    call(["fswebcam", "-d","/dev/video0", "-r", "640x480", "--no-banner", "./%d.jpg" % int(dtime)])
capture()

nilssh23
Posts: 1
Joined: Sun Jan 13, 2019 12:43 pm

Re: Need USB Camera Python Code!

Sun Jan 13, 2019 12:45 pm

output will store at what location?

DirkS
Posts: 9941
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Need USB Camera Python Code!

Sun Jan 13, 2019 5:14 pm

nilssh23 wrote:
Sun Jan 13, 2019 12:45 pm
output will store at what location?
What/who is this question aimed at?
Do you realise you're resurrecting a topic that has been dead and buried for 2 years?

Return to “Python”