This is Python wrapper library for Darknet which is a deep neural network framework by AlexeyAB running YOLO object detector.
This is implemented by ctypes
and you can use complied shared library file (libdark.so
/ darknet.dll
) on Darknet directely in you Python project.
- opencv-python
pip install -r requirements.txt
- Copy compiled shared library file to on your project.
- Rename the shared library file
libdarknet-gpu.so
orlibdarknet.so
.- pyDarknet selects
libdarknet-gpu.so
supporting GPU acceleration first. If there's no library surpoting GPU acceleration, pyDarknet selectlibdarknet.so
alternatively. - pyDarknet can detect shared library extensions by operating systems.
e.g.)libdarknet.so
is for Linux,libdarknet.dll
is for Windows.
- pyDarknet selects
- Just import
pydarknet
package on your project.import pydarknet
It's simple. Just create a Detector
.
detector = pydarknet.Detector('yolov4.cfg', 'yolov4.weights', 'coco.names')
cfg_filename
and weights_filename
assigned as each yolov4.cfg
and yolov4.weights
must be filled. On the other hand, names_filename
assigned as coco.names
can be keep empty by passing None
.
-
Prepare or load image.
- The input must have HWC shape.
- The input must be 3 channel BGR (NOT RGB) image commonly used in OpenCV.
If you are using OpenCV mainly, you can use
cv2.imread
/cv2.imdecode
.import cv2 img = cv2.imread('image.png', cv2.IMREAD_COLOR)
-
Call
detect
method inDetector
.bboxes = detector.detect(img)
detect
method hasobj_thres
,hier_thresh
,nms_thresh
andobj_filter
.
Three threshold paramaters having a posfix as_thresh
are object detection paramaters. You can refence original paper or repository.obj_filter
is an option filtering bounding boxes you want by object names. It is must be list of object names or indices.detector.detect(..., obj_filter=['car', 'bus'])
or
detector.detect(..., obj_filter['1', '2'])
The detector returns list of tuples like (name, prob, gebox)
.
for name, prob, geobox in bboxes:
x, y, w, h = geobox
What is geobox
? To distinguish a bounding box including both name and geometry informations on image and a box including only geometry informations on image. I called the latter geobox
.
name
is detected object name defined in .names
file. if you not set .names
file in the detector, name
is object index (NOT A NUMBER, BUT A STRING PRESENTATION NUMBER) defined in the model.