Analyzing frames from OpenCV cv2.VideoCapture()



I’m working on an embedded system (Jetson Nano). I have connected a RaspiCam, and i grab frames using gstreamer and cv2.VideoCapture.

For now i have been saving images using cv2.imsave(), and open_image() to parse it into my learn.predict().

I want to avoid saving the file because it seens very inefficient. I have adapted a method from here but the predictions are not the same.

from PIL import Image as PImage
from import *

frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
pil_im = PImage.fromarray(frame) 
pil_im = 255-pil_im
x = pil2tensor(pil_im ,np.float32)
preds_num = learn.predict(Image(x))[2].numpy()

If i run:


then i see the same image as the one saved with cv2.imwrite(), but the prediction is way different. And if i run:

frame = open_image()

I get a third prediction.

Does anyone know how to do this without saving the image first?

1 Like


Got it working now:

def grab_image() :
    ret_val, img =
    img_col = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
    pil_img = PImage.fromarray(img_col.astype('uint8'), 'RGB')
    pil_img = pil2tensor(pil_img,np.float32)
    inf_img = Image(pil_img.div_(255))
    return(inf_img, img)