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
Step 1:
We will use Google colab to run our code. Go to colab.research.google.com and create a new notebook.
Step 2:
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
Step 3:
clone darknet repo. Run this in a new cell
!git clone https://github.com/AlexeyAB/darknet
Step 4:
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
Step 5:(optional)
verify CUDA
!/usr/local/cuda/bin/nvcc --version
Step 6:
Builds darknet so that you can then use the darknet executable file to run or train object detectors.
!make
Step 7:
Download yolov4 weight file
!wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights
Step 8:(optional)
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)
Step 9:
Run Your Detections with Darknet and YOLOv4!
!./darknet detector test <path to .data file> <path to config> <path to weights> <path to image>
Example:
run darknet detection on test images
!./darknet detector test cfg/coco.data cfg/yolov4.cfg yolov4.weights data/person.jpg
For video
!./darknet detector demo cfg/coco.data cfg/yolov4.cfg yolov4.weights -dont_show test.mp4 -i 0 -out_filename results.avi
Thank you!!
Top comments (2)
CUDA status Error: file: ./src/dark_cuda.c : () : line: 39 : build time: May 24 2021 - 14:07:28
CUDA Error: no CUDA-capable device is detected
CUDA Error: no CUDA-capable device is detected: Bad file descriptor
darknet: ./src/utils.c:331: error: Assertion `0' failed.
Sorry for the late reply.
What output did you got in step 5 ?