darknetd provides a service and API wrapper around the darknet command-line tool.
It allows you to efficiently run darknet YOLO image recognition on an "edge device", like Raspberry Pi or Jetson Nano, and access the results via a simple REST API.
- Runs
darknetas a service, avoiding startup time spent building the network and loading weights. - Provides an API for viewing recent object detections, including access to raw source and prediction images.
- Works with external image capture tool (such as raspistill), allowing fine-tuning of camera settings.
- Archives recent darknet predictions.jpg images for review.
- Automatically deletes old images, ensuring your SD card/disk doesn't fill up.
- Exposes performance metrics in prometheus format.
There are lots of great machine vision tools, but it's challenging to get them "into production" on remote edge devices. Performing the object detection on a commodity edge device offers many benefits:
- Images stay on the edge device (unless specifically requested), providing detection data without compromising privacy.
- Bandwidth usage is minimal for object detection API responses.
- The YOLOv3-tiny model can perform detection at about ~2 FPS on a Raspberry Pi and ~20 FPS on a Jetson Nano.
- Raspberry Pi4 Kit/Jetson Nano DevKit are $99 - 5MP camera module ~$20.
- Download and build darknet.
- IMPORTANT: darknetd depends on a modified version of darknet! This small modification causes darknet to print bounding box information for detected objects.
- We modified darknet-nnpack because it is optimized for the Raspberry Pi - you could easily apply this modification to the base darknet distribution instead.
darknetdassumes thedarknetbinary is at/usr/local/darknet/darknet.
- Install
darknetper the README. - Move the
darknetdirectory to/usr/local/:mv darknet /usr/local/darknet - Download the pre-trained YOLOv3-tiny weights:
curl -o /usr/local/darknet/yolov3-tiny.weights https://pjreddie.com/media/files/yolov3-tiny.weights - Confirm
darknetis working from the command-line:cd /usr/local/darknet && ./darknet detect cfg/yolov3-tiny.cfg yolov3-tiny.weights data/dog.jpg
- Setup the
raspistillservice -darknetdworks hand-in-hand with this service.- Get your camera working and verify you can capture images with raspistill:
raspistill -o cam.jpg. - Install the raspistill systemd file at
/etc/systemd/system/raspistill.service. - Review the
raspistill.servicefile and customize camera/capture options as desired. - Create the image archive directory:
mkdir /tmp/cap - Configure the
raspistillservice to start at boot:sudo systemctl enable raspistill - Start the
raspistillservice:sudo systemctl start raspistill - Verify images are being stored in
/tmp/cap
- Get your camera working and verify you can capture images with raspistill:
- Install
darknetd:- Download darknetd and install it at
/usr/local/sbin/darknetd. - Install the
darknetd.servicesystemd file at/etc/systemd/system/darknetd.service. - Configure
darknetdto start at boot:sudo systemctl enable darknetd - Start
darknetd:sudo systemctl start darknetd
- Download darknetd and install it at
Darknetd has sensible defaults, and supports the following options - be sure to set them in your darknetd.service file.
Usage:
darknetd [options]
darknetd -h --help
darknetd --version
Options:
--capture-dir=<path> Directory containing captured image - see raspiconfig.service [default: /tmp/]
--capture-file=<file> Filename of captured image - see raspiconfig.service [default: cap.jpg]
--archive-dir=<path> Directory containing image archive - see raspiconfig.service [default: /tmp/cap]
--archive-files=<file> Number of images to retain in archive [default: 240]
--darknet-dir=<path> Directory containing darknet installation [default: /usr/local/darknet]
--darknet-data=<file> Darknet data file, relative to darknet-dir [default: cfg/coco.data]
--model-config=<file> Darknet model config file, relative to darknet-dir [default: cfg/yolov3-tiny.cfg]
--model-weights=<file> Darknet model weights file, relative to darknet-dir [default: yolov3-tiny.weights]
--detect-timeout=<secs> Darknet detection timeout [default: 10]
--detect-delay=<msec> Darknet delay between detections in msec [default: 500]
--listen-addr=<addr:port> Darknet detection timeout [default: 0.0.0.0:8081]
--version Show version
-h, --help Show this screen- To use a custom model:
darknetd --darknet-data=cfg/YOUR.data --model-config=cfg/YOUR-MODEL.cfg --model-weights=YOUR-MODEL.weights - Note that on a Pi4, setting
--detect-delaybelow 200 msec can cause significant CPU load. The default of 500 is a reasonable balance of detection time and CPU usage.
WARNING: The API provides no authentication and is NOT intended to be exposed direclty to a public network!
GET /objects- returns JSON list of most recent predictionsGET /latest.jpg- returns latest source imageGET /image/{imagename}.jpg- returns source or prediction image (get imagename from/objectsoutput)GET /metrics- returns performance metrics in prometheus formatGET /health- returnsOKif healthy
Sample API request (note: returns up to 10 most recent detections):
$ curl -s localhost:8081/objects
[
{
"Image": "image208725.jpg",
"PredImage": "predictions_image208725.jpg",
"ImageTime": "2019-09-17T16:10:58.313756895-06:00",
"PredTime": "2019-09-17T16:10:59.812694026-06:00",
"TimeDetect": 0.767813,
"TimeTotal": 0.893044895,
"Objects": [
{
"Class": "person",
"Prob": 85,
"Left": 365,
"Right": 445,
"Top": 314,
"Bot": 413
}
]
}
]
$ curl -s localhost:8081/image/image189401.jpg -o src_image.jpg
$ curl -s localhost:8081/image/predictions_image189401.jpg -o pred_image.jpg