Install
pip install opencv-python
pip install numpy
git clone https://github.com/hank-ai/darknet
cd darknet
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j4 package
sudo dpkg -i darknet-<INSERT-VERSION-YOU-BUILT-HERE>.deb
Inference
import os
import cv2
import darknet
cfg_file = "yolov2-tiny.cfg"
names_file = "coco.names"
weights_file = "yolov2-tiny.weights"
darknet.set_verbose(True)
darknet.show_version_info()
network = darknet.load_net_custom(cfg_file.encode("ascii"), weights_file.encode("ascii"), 0, 1)
class_names = open(names_file).read().splitlines()
colours = darknet.class_colors(class_names)
prediction_threshold = 0.5
width = darknet.network_width(network)
height = darknet.network_height(network)
for filename in ["dog.jpg"]:
print(filename)
image_bgr = cv2.imread(filename)
image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
image_resized = cv2.resize(image_rgb, (width, height), interpolation=cv2.INTER_LINEAR)
darknet_image = darknet.make_image(width, height, 3)
darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes())
detections = darknet.detect_image(network, class_names, darknet_image, thresh=prediction_threshold)
darknet.free_image(darknet_image)
darknet.print_detections(detections, True)
image_with_boxes = darknet.draw_boxes(detections, image_resized, colours)
if cv2.waitKey() & 0xFF == ord('q'):
break;
darknet.free_network_ptr(network)