Skip to content

Instantly share code, notes, and snippets.

@ansarid
Created August 1, 2019 22:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ansarid/b820026d2fd169fe729e8e8b651eba67 to your computer and use it in GitHub Desktop.
Save ansarid/b820026d2fd169fe729e8e8b651eba67 to your computer and use it in GitHub Desktop.
mjpg_streamer opencv module
sudo apt update
sudo apt install mjpg-streamer-opencv-python

Create filter file. (/home/debian/opencv_filter.py)

import cv2
import numpy as np

class MyFilter:
    
    def process(self, img):
        '''
            :param img: A numpy array representing the input image
            :returns: A numpy array to send to the mjpg-streamer
                      output plugin
        '''
        
        # silly routine that overlays a really large crosshair over the image
        h = img.shape[0]
        w = img.shape[1]
        
        w2 = int(w/2)
        h2 = int(h/2)
        
        cv2.line(img, (int(w/4), h2), (int(3*(w/4)), h2), (0xff, 0, 0), thickness=3)
        cv2.line(img, (w2, int(h/4)), (w2, int(3*(h/4))), (0xff, 0, 0), thickness=3)
        
        return img
  
   
def init_filter():
    '''
        This function is called after the filter module is imported.
        It MUST return a callable object (such as a function or
        bound method). 
    '''
    f = MyFilter()
    return f.process
/usr/bin/mjpg_streamer -i "/usr/lib/mjpg-streamer/input_opencv.so --filter /usr/lib/mjpg-streamer/cvfilter_py.so --fargs /home/debian/opencv_filter.py" -o "/usr/lib/mjpg-streamer/output_http.so -p 8090 -w /usr/share/mjpg-streamer/www"

Visit http://192.168.8.1:8090/?action=stream

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment