Yolov2 for Object detection from a video | by Renu Khandelwal | Towards Data Science

Following reading will help with a solid foundation on Yolo- You only look once
From CNN to Mask R-CNN and Yolo Part 1
From CNN to Mask R-CNN and Yolo Part 2
Object detection using Yolov3
Orignal paper on Yolo
Implementing-yolo-on-a-custom-dataset
Python 3.5 or higher
Tensorflow :I have used CPU version
OpenCV
VC++ build 14
We will be using DarkFlow repo which can be downloaded from here : https://github.com/thtrieu/darkflow
Weight can be downloaded from https://pjreddie.com/darknet/yolov2/
https://pjreddie.com/darknet/yolov2/
I created a new folder called bin and placed the downloaded weights there as shown below
There are three options to build the code.
Option 1:
python setup.py build_ext --inplace
Option 2: pip install darkflow globally in dev mode
pip install -e .
Option 3: Install pip globally
pip install .
For me option 2 and 3 worked well.
We are now ready to process images or video file.
We will be processing the videos using the pre-trained weights on COCO dataset on 80 classes.
We will explore object detection on a video using
Web camera
Video file
Streaming a Youtube video
Required libraries
import numpy as npimport timeimport cv2from darkflow.net.build import TFNetimport matplotlib.pyplot as plt
Creating the darkflow model instance defined using options
options = { 'model': 'cfg/yolo.cfg', 'load': 'bin/yolov2.weights', 'threshold': 0.3    }tfnet = TFNet(options)
create an instance of VideoCapture with argument as device index or the name of a video file. Pass 0 as the device index for the camera
cap = cv2.VideoCapture(0)
Once the instance of VideoCapture is created, you can capture the video frame-by-frame. Do not forget to release the capture and destroy all windows at the end.
We create a list of random colors for the frames
colors=[tuple(255 * np.random.rand(3)) for i in range(5)]
We will first check if the VideoCapture was initialized or not using cap.isOpened(). If cap.isOpened() is not True then open it using cap.open().
We set up an infinite loop and read the video frame by frame using cap.read(). cap.read() returns Boolean. If the frame is read correctly cap.read(0 return True.
We predict the objects using darkflow instance, tfnet, for each of the frame. We extract the top left, bottom right and the label to draw the bounding box with the label displayed on top of the bounding box.
We show the frame in a window using cv2.imshow().
we also setup to break the loop when the user click on ‘q’.
At the end we clean up the video capture by releasing the camera and destroying all the windows
while(cap.isOpened()):    stime= time.time()    ret, frame = cap.read()    results = tfnet.return_predict(frame)    if ret:        for color, result in zip(colors, results):            tl = (result['topleft']['x'], result['topleft']['y'])            br = (result['bottomright']['x'], result['bottomright']['y'])            label = result['label']            frame= cv2.rectangle(frame, tl, br, color, 7)            frame= cv2.putText(frame, label, tl, cv2.FONT_HERSHEY_TRIPLEX, 1, (0,0,0), 2)        cv2.imshow('frame', frame)        print('FPS {:1f}'.format(1/(time.time() -stime)))        if cv2.waitKey(1)  & 0xFF == ord('q'):            break    else:        break          cap.release()cv2.destroyAllWindows()
The code to detect objects from a video file is largely the same, the only change is that we provide a video file name to the VideoCapture.
To exit and release the capture, press q
import cv2from darkflow.net.build import TFNetimport matplotlib.pyplot as pltimport numpy as npimport timeoptions = { 'model': 'cfg/yolo.cfg', 'load': 'bin/yolov2.weights', 'threshold': 0.3    }tfnet = TFNet(options)cap = cv2.VideoCapture('video.mp4')colors=[tuple(255 * np.random.rand(3)) for i in range(5)]while(cap.isOpened()):    stime= time.time()    ret, frame = cap.read()    results = tfnet.return_predict(frame)    if ret:        for color, result in zip(colors, results):            tl = (result['topleft']['x'], result['topleft']['y'])            br = (result['bottomright']['x'], result['bottomright']['y'])            label = result['label']            frame= cv2.rectangle(frame, tl, br, color, 7)            frame= cv2.putText(frame, label, tl, cv2.FONT_HERSHEY_TRIPLEX, 1, (0,0,0), 2)        cv2.imshow('frame', frame)        print('FPS {:1f}'.format(1/(time.time() -stime)))        if cv2.waitKey(1)  & 0xFF == ord('q'):            break    else:        breakcap.release()cv2.destroyAllWindows()
To read the youtube video we need to install two libraries
Pafy : Python library to download YouTube content and retrieve metadata.
youtube_dl : allows to download videos from YouTube.com and other video sites
Installing the libraries at Anaconda prompt
pip install pafypip install --upgrade youtube_dl
import pafyimport cv2from darkflow.net.build import TFNetimport matplotlib.pyplot as pltimport numpy as npimport time
Download the youtube video with the metadata using pafy
url = 'https://www.youtube.com/watch?v=_iiOEQOtBlQ'vPafy = pafy.new(url)play = vPafy.getbest(preftype="webm")print(play.url)
We finally do a videocapture and pass the metadata for the youtube video as the argument to videocapture . Rest of the code remains the same
options = { 'model': 'cfg/yolo.cfg', 'load': 'bin/yolov2.weights', 'threshold': 0.3    }tfnet = TFNet(options)cap = cv2.VideoCapture(play.url)colors=[tuple(255 * np.random.rand(3)) for i in range(5)]while(cap.isOpened()):    stime= time.time()    ret, frame = cap.read()    results = tfnet.return_predict(frame)    if ret:        for color, result in zip(colors, results):            tl = (result['topleft']['x'], result['topleft']['y'])            br = (result['bottomright']['x'], result['bottomright']['y'])            label = result['label']            frame= cv2.rectangle(frame, tl, br, color, 7)            frame= cv2.putText(frame, label, tl, cv2.FONT_HERSHEY_TRIPLEX, 1, (0,0,0), 2)        cv2.imshow('frame', frame)        print('FPS {:1f}'.format(1/(time.time() -stime)))        if cv2.waitKey(1)  & 0xFF == ord('q'):            break    else:        breakcap.release()cv2.destroyAllWindows()