Detection on youtube livestream walk in Tokyo, Japan.
This package contains two modules that perform real-time object detection from Youtube video stream. A possible use case is detection with a drone's camera since most of them support Youtube live-streaming (with some constant delay ~ 7secs). It can be also used with simple youtube videos just by providing the URL.
The SSD_youtube.py uses the Single Shot Detection which runs on CPU i5 8400, with ~ 30 hz. The YOLO_Youtube.py, uses the YOLO algorithm and runs on the same CPU with ~1-3 hz. If you want high quality results, setup CUDA, CUDA-toolkit and CUDNN for opencv and use the YOLO detection algorithm.
- Python3
- OpenCV
- Pafy
- Youtube-dl
This code is tested on Ubuntu 18.04 with python3.6 and Ubuntu 20.04 with python3.8. In case you want to install the following packages for a specific python version, replace
$ pip install <package_name>
with
$ pythonX.X -m pip install <package_name>
- Install OpenCV
$ pip install opencv-python
- Install pafy and youtube-dl
$ pip install pafy
$ pip install youtube-dl
Because youtube has removed the dislike count, you will get an error later on, when the library tries to extract the dislike counts of the video. To fix this:
- Edit ~/.local/lib/pythonX.X/site-packages/pafy/backend_youtube_dl.py -> Replace X.X with your python version
- Comment out line 53 & 54:
#self._likes = self._ydl_info['like_count']
#self._dislikes = self._ydl_info['dislike_count']
To subscribe to live-stream there is a method called .getbest() which grabs the highest possible quality of the stream. In case you want to increase performance you can go to
~/.local/lib/python3.6/site-packages/pafy/backend_shared.py
line 358 and change:
# r = max(streams, key = _sortkey)
r = streams[-1] # -1 is just the best possible quality
Choose a value for i = 0,...,size(streams) is a different quality with streams[-1] being the max possible quality and streams[0] the lowest.
$ git clone https://github.com/MichaelMarav/RealTimeDetectionYoutube.git
Open SSD_Youtube.py and change the url parameter to: The url you wish to subscribe from Youtube
Ready to go! Run:
$ pythonX.X SSD_Youtube.py
An opencv window should open and automatically starts detecting.
$ wget https://pjreddie.com/media/files/yolov3.weights
https://github.com/pjreddie/darknet/blob/master/data/coco.names
https://github.com/pjreddie/darknet/blob/master/cfg/yolov3.cfg
Open YOLO_Youtube.py and change the following parameters:
- url : The url you wish to subscribe from Youtube
- abs_path_labels : The absolute path for coco.names
- abs_path_weights : The absolute path for yolov3.weights
- abs_path_config : The absolute path for yolov3.cfg
Ready to go! Run the detection algorithm:
$ pythonX.X YOLO_Youtube.py
Requirements (OpenCV > 4.2)
You will need an Nvidia GPU. Setup CUDA, CUDA-toolkit and install opencv and opencv_contrib from source. Follow this tutorial: https://pyimagesearch.com/2020/02/03/how-to-use-opencvs-dnn-module-with-nvidia-gpus-cuda-and-cudnn/
After you've installed all of the above, to enable the network to run with GPU you will need to set:
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)