Dan D.
Dan D.

Reputation: 8557

YOLO (Darknet): How to detect a whole directory of images?

The Darknet guide to detect objects in images using pre-trained weights is here: https://pjreddie.com/darknet/yolo/

The command to run is:

./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg

The last argument is the path to a file, I've tried to change it to data/*.jpg but didn't work.

How to use Darknet to detect a whole directory of images?

Upvotes: 2

Views: 11676

Answers (4)

ZenjieLi
ZenjieLi

Reputation: 301

There is a simple way to detect objects on a list of images based on this repository AlexeyAB/darknet.

./darknet detector test cfg/obj.data cfg/yolov3.cfg yolov3.weights < images_files.txt

You can generate the file list either from the command line (Send folder files to txt ) or using a GUI tool like Nautilus on Ubuntu.

Two extra flags -dont_show -save_labels will disable the user interaction, and save the detection results to text files instead.

Upvotes: 2

Dan D.
Dan D.

Reputation: 8557

Another solution is loading Darknet from Python2 (not 3, Darknet is using Python2).

1a) Clone darknet as described in https://pjreddie.com/darknet/yolo/

1b) Go to the cloned dir, download yolov3-tiny.weights and yolov3.weights as said in https://pjreddie.com/darknet/yolo/

2) Copy darknet/examples/detector.py to darknet dir

3) Edit the new detector.py

  • Change .load_net line to use: cfg/yolov3-tiny.cfg and yolov3-tiny.weights
  • Change .load_meta line to use: cfg/coco.data

4a) Detect objects in images by adding some dn.dectect lines in detector.py

4b) Run detector.py

Upvotes: 0

Dan D.
Dan D.

Reputation: 8557

There's a trick to make Darknet executable load weights once and infer multiple image files. Use expect to do the trick.

Install expect:

sudo yum install expect -y
#sudo apt install expect -y

Do object detection on multiple images:

expect <<"HEREDOC"
  puts "Spawning...";
  spawn ./darknet detect cfg/yolov3-tiny.cfg yolov3-tiny.weights;
  set I 0;

  expect {
    "Enter Image Path" {
      set timeout -1;

      if {$I == 0} {
        send "data/dog.jpg\r";
        incr I;
      } elseif {$I == 1} {
        send "data/kite.jpg\r";
        incr I;
      } else {
        exit;
      }

      exp_continue;
    }
  }
HEREDOC

Upvotes: 1

Amey Shahane
Amey Shahane

Reputation: 61

As per the link mentioned below, one can use cv2.dnn.readNetFromDarknet module to read darknet, trained weights and configuration file to make a loaded model in python. Once the model is loaded, one can simply use for loop for prediction. Please refer this link for further clarification

Upvotes: 2

Related Questions