Get frames from Picamera and add opencv filters in realtime

2k Views Asked by At

According to this Picamera document, Picamera can capturing frame as openCV object. but seems not working . how can i get frames from Picamera and add opencv filters in realtime. Rpi 3b+ . Python 2.7 i need an example code please.

import time
import picamera
import numpy as np
import cv2

with picamera.PiCamera() as camera:
    camera.resolution = (320, 240)
    camera.framerate = 24
    time.sleep(2)
    image = np.empty((240 * 320 * 3,), dtype=np.uint8)
    camera.capture(image, 'bgr')
    image = image.reshape((240, 320, 3))
1

There are 1 best solutions below

3
On

Edit: 23rd April, 2022. Ok. I'm going back to your previously original coded. I'm using picamera v1. kindly, try to understanding. while cap.isOpened() is equivalent to camera.start_preview() You need time interval how long do you want to stay. So I set time.sleep(30) for 30 seconds. Do not used image.reshape. You already have camera.resolution = (320, 240). So here is previously coded. So I commented it out. try this:

import time
import picamera
import numpy as np
import cv2

with picamera.PiCamera() as camera:
    camera.resolution = (320, 240)
    camera.start_preview()
    camera.framerate = 24
    time.sleep(30)
    #image = np.empty((240 * 320 * 3,), dtype=np.uint8)
    camera.capture(image, 'bgr')
    #image = image.reshape((240, 320, 3))

Here another one how to save multiple images.

import time
import picamera

with picamera.PiCamera() as camera:
    camera.resolution = (640, 480)
    camera.start_preview()
    time.sleep(2)
    for filename in camera.capture_continuous('img{counter:03d}.jpg'):
        print('Captured %s' % filename)
        time.sleep(10) # wait 10 seconds