4

I am working on a project where I need to use opencv to get image data in python from the pi camera module. I have seen the tutorials on how to do this with C++ but not too much in python. I did find this link which had me install the official kernel V4L2 driver bcm2835-v4l2. Once I did this I was able to capture the stream from the pi but the stream shows up tiny...probably about 5x5 pixels. I couldn't find anyone else who has encountered this problem. Any ideas?

Below is the simple code I am using for the capture:

import cv2
import numpy as np

cap = cv2.VideoCapture(-1)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

EDIT: I also forgot to mention that when I run the script, I get this error before the window shows up. I am assuming it occurs when cap is created.

VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
TooMuchDog
  • 43
  • 1
  • 1
  • 4

1 Answers1

2

You may find you need to use v4l2-ctl to configure the camera module as detailed in this forum thread (you could do this before running your script, or have your script call it via subprocess.call).

An alternative (given that OpenCV's image format is simply a numpy array in BGR order), is to use the unencoded capture feature in picamera (based on your code and the RGB capture recipe from the picamera 1.8 docs):

import cv2
import picamera
import picamera.array

with picamera.PiCamera() as camera:
    with picamera.array.PiRGBArray(camera) as stream:
        camera.resolution = (320, 240)

        while True:
            camera.capture(stream, 'bgr', use_video_port=True)
            # stream.array now contains the image data in BGR order
            cv2.imshow('frame', stream.array)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            # reset the stream before the next capture
            stream.seek(0)
            stream.truncate()

        cv2.destroyAllWindows()
Dave Jones
  • 3,988
  • 16
  • 22