Skip to main content

Darknet

Darknet

Darknet is a state-of-the-art object detector that uses the YOLO (You Only Look Once) framework. It is built on a singel-stage algorithm to achieve both speed and accuracy.

YOLOv7 is currently the most accurate and fastest model and has hardware acceleration support on both GPUs and CPUs.

If CUDA is available on your system, darknet will run on your GPU.

note

darknet component uses the official Darknet implementation when running on a GPU. When running on a CPU, it uses OpenCV's implementation of Darknet.

info

YOLOv7 is the default model used by darknet in all images.

Configuration

Configuration example
darknet:
object_detector:
cameras:
viseron_camera1:
fps: 1
scan_on_motion_only: true
log_all_objects: false
labels:
- label: dog
confidence: 0.7
trigger_recorder: false
- label: cat
confidence: 0.8
zones:
- name: zone1
coordinates:
- x: 0
y: 500
- x: 1920
y: 500
- x: 1920
y: 1080
- x: 0
y: 1080
labels:
- label: person
confidence: 0.8
trigger_recorder: true
mask:
- coordinates:
- x: 400
y: 200
- x: 1000
y: 200
- x: 1000
y: 750
- x: 400
y: 750
darknetmap required
Darknet configuration.

Object detector

An object detector scans an image to identify multiple objects and their position.

tip

Object detectors can be taxing on the system, so it is wise to combine it with a motion detector

Labels

Labels are used to tell Viseron what objects to look for and keep recordings of. The available labels depends on what detection model you are using.

The max/min width/height is used to filter out any unreasonably large/small objects to reduce false positives.
Objects can also be filtered out with the use of an optional mask.

tip
To see the default available labels you can inspect the label_path file.
docker exec -it viseron cat /detectors/models/darknet/coco.names

Zones

Zones are used to define areas in the cameras field of view where you want to look for certain objects (labels).
Say you have a camera facing the sidewalk and have labels setup to record the label person.
This would cause Viseron to start recording people who are walking past the camera on the sidewalk. Not ideal.
To remedy this you define a zone which covers only the area that you are actually interested in, excluding the sidewalk.

darknet:
object_detector:
cameras:
camera_one:
...
zones:
- name: sidewalk
coordinates:
- x: 522
y: 11
- x: 729
y: 275
- x: 333
y: 603
- x: 171
y: 97
labels:
- label: person
confidence: 0.8
trigger_recorder: true
tip

See Mask for how to get the coordinates for a zone.

Mask

Masks are used to exclude certain areas in the image from object detection. If a detected object has its lower portion inside of the mask it will be discarded.

The coordinates form a polygon around the masked area.
To easily generate coordinates you can use a tool like image-map.net.
Just upload an image from your camera, choose the Poly shape and start drawing your mask.
Then click Show me the code! and adapt it to the config format.
Coordinates coords="522,11,729,275,333,603,171,97" should be turned into this:

darknet:
object_detector:
cameras:
camera_one:
...
mask:
- coordinates:
- x: 522
y: 11
- x: 729
y: 275
- x: 333
y: 603
- x: 171
y: 97

Paste your coordinates here and press Get config to generate a config example

Pre-trained models

The included models are placed inside the /detectors/models/darknet folder.

Included models:
  • yolov3-tiny.weights
  • yolov3.weights
  • yolov4-tiny.weights
  • yolov4.weights
  • yolov7-tiny.weights
  • yolov7.weights
  • yolov7x.weights
tip

This GitHub issue explains the models quite well.

To make an educated guess of what model to use, you can reference this image.
It will help you find the perfect trade-off between accuracy and latency.

danger

The image roflcoopter/rpi3-viseron only includes the yolov7-tiny.weights model.

tip
The containers also has *-tiny.weights model included in the image. The tiny-models can be used to reduce CPU and RAM usage. If you want to swap to a tiny-model you can change these configuration options:
darknet:
object_detector:
model_path: /detectors/models/darknet/yolov7-tiny.weights
model_config: /detectors/models/darknet/yolov7-tiny.cfg
note
The tiny-models have significantly worse accuracy than their larger counterparts.

Hardware acceleration

Hardware accelerated object detection is supported on NVIDIA GPUs and Intel CPUs with integrated GPUs. If you dont have a GPU available, darknet will run on the CPU.

NVIDIA GPUs

If your system supports CUDA it is recommended to use the roflcoopter/amd64-cuda-viseron image. It will automatically use CUDA for object detection.

info

When running on CUDA, native Darknet is used.

tip

If you want to force darknet to run on OpenCL even if you have an NVIDIA GPU you can set these config options:

darknet:
object_detector:
dnn_backend: opencv
dnn_target: opencl

Intel CPUs with integrated GPUs

If you are running on an Intel CPU with integrated GPU, you can use the roflcoopter/amd64-viseron image. It will automatically use OpenCV with OpenCL for object detection.

The dnn_backend and dnn_target controls how the model runs.

danger

Since upgrading to Ubuntu 22.04, OpenCV 4.9.0 and OpenVINO 2023.3, the openvino backend is broken and causes segmentation faults. Hopefully this will be resolved in future updates of the libraries.

info

When not running on CUDA, OpenCVs implementation of Darknet is used.

Troubleshooting

To enable debug logging for darknet, add the following to your config.yaml
/config/config.yaml
logger:
logs:
viseron.components.darknet: debug