1

I'm following this tutorial (specifically step 6) to get basic video working with openCV and my Pi 3 B. I know the camera works, because the camera.start_preview() and camera.stop_preview() code works just fine. For reference this is the code I'm using:

from picamera.array import PiRGBArray
from time import sleep
from PIL import Image
import imagehash
import cv2
import time
from scipy.spatial import distance

# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))

# allow the camera to warmup
time.sleep(0.1)
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    # grab the raw NumPy array representing the image, then initialize the timestamp
    # and occupied/unoccupied text
    image = frame.array

    # show the frame
    cv2.imshow("Frame", image)
    key = cv2.waitKey(1) & 0xFF

    # clear the stream in preparation for the next frame
    rawCapture.truncate(0)

    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break

When I run the program (using python camera.py) I get a window showing up on the Pi's screen, but it's black and hitting the 'q' key does nothing to stop it (I have to ctrl+c to quit). I put prints throughout the loop and they show up fine, so the loop is running without crashing. I printed out the 'key' value and it's always 255, no matter what key I press, so that's weird too.

I suspect the problem may be that I'm using old openCV (2.4.9 I think, or whatever is in the Debian repo), which I installed using sudo apt-get install python-opencv libopencv-dev. I tried installing openCV 3.1 with this script, but despite having 2.5gb space on my 8gb microSD card, the massive amount of code filled it up completely and gave me an error. Can my issue be explained by different openCV version, and if so, how can I install 3.1 without consuming all my 'disk' space?

edit: I managed to install openCV 3.1 with the script I mentioned, and despite that, the camera python script still fails to run properly, only showing a black window. Why is this happening? The code is literally line for line the same as the tutorial, and now even the library is the same.

edit again: Minor breakthrough; discovered that hitting the 'q' key on the keyboard attached to the Pi itself was the trick. Also, changed the format="bgr" line to format="rgb" out of curiosity and found that, hooray, I get a video stream! Except the blues are now red and the reds are blue. I can see why the format was 'bgr', but I don't yet see why it doesn't show me any video in that format. I don't think the colour change matters to me much, since what I'll be doing (pHashing) doesn't require colour anyway, but it's still weird.

2

Thanks to Adrian Rosebrock at pyimagesearch.com for providing me with this answer; it was my picamera version. I had v1.11, but downgrading to v1.10 worked. I did this with sudo pip uninstall picamera and sudo pip install "picamera[array]==1.10".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.