Ten years ago, computer vision researchers thought that getting a computer to tell the difference between a cat and a dog would be almost impossible, even with the significant advance in the state of artificial intelligence. But now using image classification computers can not only detect the object but also can categorize thousand different types.
Object detection is not a new term. It was there from the 1980s but the problem with it was the accuracy and speed.
The reason being why speed is more important in this field is whenever we detect the object using traditional methods of open-cv and python the entire state of the environment was changed, making the use of that technology in real-world was limited.
YOLO (You Ony Look Once) changed the entire scenario when it came out in 2017.
Let's get started with how to detect objects with YOLOv4
We will use Google colab to run our code. Go to colab.research.google.com and create a new notebook.
Connect google drive to colab to be able to run the detector on our desired picture or video.
Run the below code in a new cell
from google.colab import drive drive.mount('/content/gdrive') # this creates a symbolic link so that now the path /content/gdrive/My\ Drive/ is equal to /mydrive !ln -s /content/gdrive/My\ Drive/ /mydrive !ls /mydrive
clone darknet repo. Run this in a new cell
!git clone https://github.com/AlexeyAB/darknet
change the makefile to have GPU and OPENCV enabled
%cd darknet !sed -i 's/OPENCV=0/OPENCV=1/' Makefile !sed -i 's/GPU=0/GPU=1/' Makefile !sed -i 's/CUDNN=0/CUDNN=1/' Makefile !sed -i 's/CUDNN_HALF=0/CUDNN_HALF=1/' Makefile
Builds darknet so that you can then use the darknet executable file to run or train object detectors.
Download yolov4 weight file
These three functions will help to directly upload and download files from your local computer to google colab. This step is only if your not using google drive(step 2).
def imShow(path): import cv2 import matplotlib.pyplot as plt %matplotlib inline image = cv2.imread(path) height, width = image.shape[:2] resized_image = cv2.resize(image,(3*width, 3*height), interpolation = cv2.INTER_CUBIC) fig = plt.gcf() fig.set_size_inches(18, 10) plt.axis("off") plt.imshow(cv2.cvtColor(resized_image, cv2.COLOR_BGR2RGB)) plt.show() # use this to upload files def upload(): from google.colab import files uploaded = files.upload() for name, data in uploaded.items(): with open(name, 'wb') as f: f.write(data) print ('saved file', name) # use this to download a file def download(path): from google.colab import files files.download(path)
Run Your Detections with Darknet and YOLOv4!
!./darknet detector test <path to .data file> <path to config> <path to weights> <path to image>
run darknet detection on test images
!./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights data/person.jpg
!./darknet detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights -dont_show test.mp4 -i 0 -out_filename results.avi