2

I've got a Rasberry Pi with a camera and am streaming video to my browser using the RPi Cam Web interface. I run a script to read in the images and process them like below. Running the code opens a window with the processed image at the current time. When I close the window, I get an updated processed image.

What I'd like to do, however, is output a continuous video of the processed images. What approach should I take to do that?

while True: 
    image = io.imread('http://[ip-address]/cam_pic.php')
    image_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    faces = detect(image_gray)
    image_with_detected_faces = faces_draw(image, faces)
    plt.imshow(image_with_detected_faces)
    plt.show()
megashigger
  • 8,695
  • 17
  • 47
  • 79
  • This github [repo](https://github.com/harvitronix/continuous-online-video-classification-blog) which explains how to classify continuous videos using tensorflow might be of help. – Sriram Sitharaman Jul 11 '17 at 05:48
  • There is a lot of missing information here, that makes this hard to answer. You can save the images using `plt.savefig()` instead of `plt.show()` then I would suggest that you use `ffmpeg` (or similar) to convert the images to a video. For this to yield a reasonable result, your processing time must be rather low, to get a decent frame rate. Also, you probably want to capture images at set times, rather than as quickly as possible, to get a better 'flow'. And lastly, this will of course not yield the video real-time. – JohanL Jul 11 '17 at 07:22
  • @JohanL that's helpful, thanks. What information should I add to my question? – megashigger Jul 11 '17 at 18:08
  • E.g., if my proposed approach, using `ffmpeg` and not getting a real time stream is OK. Also, what frame rate do you expect? How long is the sequence to store? Should you do all the processing on the RPi or can you off-load it to a more powerful server? Also, do you want to encode your video to reduce its size, et c.? – JohanL Jul 11 '17 at 19:10

1 Answers1

4

You may want to look at this question: update frame in matplotlib with live camera preview which directly uses VideoCapture. If instead you want to read the images from http you can change this to one of the following.

Interactive mode

import cv2
import matplotlib.pyplot as plt

def grab_frame():
    image = io.imread('http://[ip-address]/cam_pic.php')
    image_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    faces = detect(image_gray)
    return faces_draw(image, faces)

#create axes
ax1 = plt.subplot(111)

#create image plot
im1 = ax1.imshow(grab_frame())

plt.ion()

while True:
    im1.set_data(grab_frame())
    plt.pause(0.2)

plt.ioff() # due to infinite loop, this gets never called.
plt.show()

FuncAnimation

import cv2
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

def grab_frame():
    image = io.imread('http://[ip-address]/cam_pic.php')
    image_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    faces = detect(image_gray)
    return faces_draw(image, faces)

#create axes
ax1 = plt.subplot(111)

#create axes
im1 = ax1.imshow(grab_frame())

def update(i):
    im1.set_data(grab_frame())

ani = FuncAnimation(plt.gcf(), update, interval=200)
plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712