I have a web camera running in Linux using the uvcvideo module. And I'm using a python application to access the web camera and display the image.
I want the python program to handle it if the web camera for some reason don't work anymore. Have tested with just unloading the module. Works fine if i just unload the module before I run the python code, but if force it to unload in use, I get the following feedback.
VIDIOC_DQBUF: Inappropriate ioctl for device
And if I kill the python code, and restart it the whole machine freezes.
The code I'm trying to run is
import pygame
import Image
from pygame.locals import *
import sys
import time, os
import opencv
from opencv import highgui
camera = highgui.cvCreateCameraCapture(0)
fps = 10.0
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)
while True:
try:
ima = highgui.cvQueryFrame(camera)
im = opencv.adaptors.Ipl2PIL(ima)
break;
except TypeError:
print 'No camera'
os.system('sudo modprobe uvcvideo')
time.sleep(1)
camera = highgui.cvCreateCameraCapture(0)
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))
It's a modified version of http://www.jperla.com/blog/2007/09/26/capturing-frames-from-a-webcam-on-linux/ It's using openvc version 1.x and not 2.x.
Any idea on how to make this work?