This is to demonstrate a way to broadcast video from a webcam to web clients (browsers) using:
- Python
- OpenCV (opencv-python) - getting frames from camera and apply face detection
- WebSocket
- Http server
- Using OpenCV (cv2) to read frames from the webcam
- Using OpenCV to process the frames
- Using WebSocket to push the frames to connected clients (browsers)
- Asynchronous tasks using asyncio
- Multi-threading using threading
This will require you computer having a webcam. It assumes that the webcam device index to be zero. Change it in camera.py if necessary. Install Python dependencies (Python 3.11 or greater is recommended)
pip install -r requirements.txt
Run main.py:
python main.py
Open a browser and navigate to localhost:8080. This works for multiple clients. Hence, you may open multiple browser windows/tabs to test.
There are numerous ways to do this and the way used here might not be the best. This is experimental, learn as you code and no way near to anything for production. Therefore, there is no guarantee in terms of performance and security. Suggestions to a better way or how it can be improved are welcomed.
The metadata for the face detections comes in the same payload as the frames. To test, use the https://github.com/joshting/golang-react-video-wall