python-2.7opencvflycapture

PyCapture2 Video through opencv


Having trouble importing my Point Grey Chameleon3 camera through opencv and PyCapture2. Currently Taking photos/images individually but want a constant flow.I think it has to do with camera.retrieveBuffer() but can not find a way around.

import PyCapture2
import cv2
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image

bus = PyCapture2.BusManager()
numCams = bus.getNumOfCameras()
camera = PyCapture2.Camera()
uid = bus.getCameraFromIndex(0)
camera.connect(uid)
camera.startCapture()

while True:
    image = camera.retrieveBuffer()
    row_bytes = float(len(image.getData())) / float(image.getRows());
    cv_image = np.array(image.getData(), dtype="uint8").reshape((image.getRows(), image.getCols()) );
    cv2.imshow('frame',cv_image)
    cv2.waitKey(10)

If anyone has any pointers or links to more documentation would be greatly appreciated Thanks


Solution

  • I'm using a very similar code with a BlackFly camera. The flow problem is because the loop is very slow. You are calling image.getData() twice which makes the problem somewhat worse but the main issue is that reading and converting the data into an image format is very slow.

    Diving a little deeper and timing some functions on my computer (my image is 1920 by 1200):

    image = camera.retrieveBuffer() - takes about as much time as the set frame rate.

    image.getData() takes about 220 ms.

    np.array(image.getData(), dtype="uint8").reshape((image.getRows(), image.getCols()) ) takes about 540 ms.

    So the refresh rate cannot be faster than that.