opencv-adaptors.py problem iniciating camera "must be called with a cv.CvMat!"

Asked by Diego Diaz

HI
I just starting to program a python script for use my fireware web cam.
I am using ubuntu 8.04
what I did is:
sudo modprobe raw1394
for load the module and
sudo chmod 666 /dev/raw1394
for add writing permission
so it was working before

but now I got this problem:

  File "/var/lib/python-support/python2.5/opencv/adaptors.py", line 79, in Ipl2PIL
    raise TypeError, 'must be called with a cv.CvMat!'
TypeError: must be called with a cv.CvMat!

this is the script that I use in python for run the camera. I think the script is correct because it was working before.

#! /usr/bin/python
import opencv, pygame
from opencv import highgui
from pygame.locals import *

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
camera = highgui.cvCreateCameraCapture(0)
delay = 0

def get_image():

    im = highgui.cvQueryFrame(camera)

    #convert Ipl image to PIL image
    return opencv.adaptors.Ipl2PIL(im)

im = get_image()

fps = 30.0
pygame.init()
window = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT),pygame.HWSURFACE|pygame.DOUBLEBUF)#|pygame.FULLSCREEN)
pygame.display.set_caption("Observatorio")
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 = get_image()

    pg_img_temp = pygame.image.frombuffer(im.tostring(), im.size, im.mode)
    pg_img = pygame.transform.scale(pg_img_temp,(SCREEN_WIDTH,SCREEN_HEIGHT))
    screen.blit(pg_img, (0,0))

    pygame.display.flip()
    pygame.time.delay(int(2000 * 1.0/fps))

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu opencv Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
David Coles (dcoles) said :
#1

Hi Diego,

I've just tried you script out on the package included with Ubuntu 9.04 alpha that I'm running and it seems to work alright. I can see there's been a couple of patches to the python-opencv package since 8.04. I'll see if I can give you a hand anyway.

First thing to check would be that highgui.cvQueryFrame(camera) is returning a cv.CvMat object. If it's returning a None type there might be an issue with OpenCV capturing the image from your camera.

Secondly a quick Google reveals that some other people have had similar issues with capturing images in 8.04. From the sounds of things the python wrappers in the 8.04 package might be incorrectly returning a IplImage rather than a CvMat (I belive that all the python version OpenCV functions are supposed to return CvMats due to wrapping issues). A quick fix would just be to add 'im = opencv.cvGetMat(im)' directly after the 'im = highgui.cvQueryFrame(camera)'.

Hopefully that might fix your problem.

Revision history for this message
David Schwartz (dms-csun) said :
#2

Hi Diego,
Have you had any luck figuring this out yet? I used the same python code and added both Diego's patch and the follow code to see that the method was actually returning a NULLI

     im = highgui.cvQueryFrame(camera)
     if im:
       print im[0]
     else:
       print "highgui.cvCreateCameraCapture returned a NULL pointer"

I have searched the web and tried a number of "random actions" with no luck so am really curious to see if you have figured this out yet.

Revision history for this message
encompass (encompass) said :
#3

Bummer, I couldn't get it to work either... I ended up with this error...
Traceback (most recent call last):
  File "webcamTest.py", line 30, in <module>
    im = get_image()
  File "webcamTest.py", line 13, in get_image
    im = opencv.cvGetMat(thing)
  File "/var/lib/python-support/python2.6/opencv/cv.py", line 3826, in cvGetMat
    return _cv.cvGetMat(*args)
RuntimeError: openCV Error:
        Status=Null pointer
        function name=cvGetMat
        error message=NULL array pointer is passed
        file_name=cxarray.cpp
        line=2780

------------------
(program exited with code: 1)
Press return to continue

Can you help with this problem?

Provide an answer of your own, or ask Diego Diaz for more information if necessary.

To post a message you must log in.