I ran simple python script on Raspberry Pi 3. This script is responsible to open video device and stream data (800x600) to HTTP endpoint using MJPEG. When I receive this stream one of my Raspberry Pi cores works on 100%. It possible to run OpenCV with multi threading?
This is my code
import cv2
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import time
import argparse
import socket as Socket    
camera = None  
def setUpCameraCV():
    global camera
    camera = cv2.VideoCapture(0)
class mjpgServer(BaseHTTPRequestHandler):
    ip = None
    hostname = None
    def do_GET(self):
        print('connection from:', self.address_string())
        if self.ip is None or self.hostname is None:
            self.ip, _ = 0.0.0.0
            self.hostname = Socket.gethostname()
        if self.path == '/mjpg':
            self.send_response(200)
            self.send_header('Cache-Control', 'no-cache')
            self.send_header('Pragma', 'no-cache')
            self.send_header('Connection', 'close')
            self.send_header(
                'Content-type',
                'multipart/x-mixed-replace; boundary=mjpegstream'
            )
            self.end_headers()
            while True:
                if camera:
                    ret, img = camera.read()
                else:
                    raise Exception('Error, camera not setup')
                if not ret:
                    print('no image from camera')
                    time.sleep(1)
                    continue
                ret, jpg = cv2.imencode('.jpg', img)
                
                self.end_headers()
                self.wfile.write('--mjpegstream')
                self.end_headers()
                self.send_header('Content-type', 'image/jpeg')
                self.send_header('Content-length', str(jpg.size))
                self.end_headers()
                self.wfile.write(jpg.tostring())    
def main():
    try:
        setUpCameraCV()         
        mjpgServer.ip = 0.0.0.0
        mjpgServer.hostname = Socket.gethostname()
        server = HTTPServer((ipv4, args['port']), mjpgServer)
        print("server started on {}:{}".format(Socket.gethostname(), args['port']))
        server.serve_forever()
    except KeyboardInterrupt:
        print('KeyboardInterrupt')
    server.socket.close()
if __name__ == '__main__':
    main()
Another question, how to get timestamp of each frame on the client side (receiver) it possible?
