Using RPi Cam Web Interface for real time computer vision tasks with OpenCV
• Reading time: 2 minutes
If you have a raspberry pi camera, the RPi Cam Web Interface is a very useful bit of software. It serves up a page that allows you to view a realtime MJPEG camera stream with remarkably low latency and play with the camera settings. Compared to the RTSP stream that can be launched through the system camera software, the MJPEG stream hosted by the web interface has considerably lower latency with the added benefit that you can easily view the stream on any device with a web browser.
While you can run this code with standard python calls, using asynchronous functions might be a prefereable solution if you’re building an application where you want to modify the behavior of your system as it runs.
I use this in a jupyter notebook with widgets in order to capture and save images during the computer vision task.
import numpy as np
import urllib.request
import cv2
camera_name = "raspberrypi.local"
stream = urllib.request.urlopen('http://' + camera_name +'/html/cam_pic.php')
WINDOW_NAME = "Camera"
cv2.namedWindow(WINDOW_NAME)
cv2.startWindowThread()
async def main():
while True:
bytes += stream.read(1024)
a = bytes.find(b'\xff\xd8') #frame starting
b = bytes.find(b'\xff\xd9') #frame ending
# new frame is available
if a != -1 and b != -1:
jpg = bytes[a:b+2]
bytes = bytes[b+2:]
frame = cv2.imdecode(np.frombuffer(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
#do your CV stuff here
cv2.imshow(WINDOW_NAME, frame)
#get the next frame by adding any new string to the end of the url, here we use a timestamp
url = 'http://' + camera_name +'/html/cam_pic.php?' + str(timestamp)
#print(url)
stream = urllib.request.urlopen(url)
Now you can run you cv code from any machine on the same local network as your pi.