Simulate a Webcam with Pictures (Or IP Camera)

Does anyone know of an easy way or some free software that you can run on the RPI (Raspberry Pi) to simulate a webcam or make a IP camera with sets of pictures that are in a folder?

You could use gstreamer to do that. The following script will run an MJPEG server on localhost port 8080 using JPEG images from the current directory that match the name pattern. They will be served in a loop until the server is killed.


#!/bin/bash
gst-launch-1.0 multifilesrc location="img.%04d.jpg" index=0 \
    caps="image/jpeg,framerate=\(fraction\)12/1" loop=true ! jpegdec ! \
    videoconvert ! videorate ! jpegenc ! queue ! multipartmux ! \
    tcpserversink host=127.0.0.1 port=8080

%04d represents an integer padded on the left with zeroes to four digits. I used the following script to rename all .jpg files in the current directory to the form the gstreamer command accepts.


#!/bin/bash
count=0
for file in *.jpg; do
  new=$(printf "img.%04d.jpg" "$count")
  mv -- "$file" "$new"
  let count=count+1
done

The key thing to remember about webcams is they just send a series of still JPG images with some header information. It’s nothing more complex than that. It wouldn’t take much to write your own streaming service.

It will depend on what you are using to open those images for processing on the Pi.
If you use OpenCV, it is as simple as using one command line to open an image vs another to grab a frame from a video stream.
I’m certain most other image processing code works almost identically.

For example, to grab a video frame, you could do this:

camera = cv2.VideoCapture(0)
# Later followed by this to grab the frame
(grabbed, frame) = camera.read()

To open a image file instead:

frame = cv2.imread(PathToImageFile)

mjpg-streamer can send a stream from a directory of jpeg files.