AlexeyAB / darknet

YOLOv4 / Scaled-YOLOv4 / YOLO - Neural Networks for Object Detection (Windows and Linux version of Darknet )
http://pjreddie.com/darknet/
Other
21.7k stars 7.96k forks source link

How to run YOLO on multiple images using API in my own program #6602

Open marcobevih2o opened 4 years ago

marcobevih2o commented 4 years ago

how can run yolo on a batch of images by API? actually i process an images at time

deep-learning-newbie commented 4 years ago

@marcobevih2o

add system variable DARKNET_PATH which points to a folder where your libdarknet.so file - like this export DARKNET_PATH=/home/user/yolov4/darknet/

and then use this code:

import os
import glob
import random
import darknet
import time
import cv2
import numpy as np

config_file = './cfg/yolov4.vy4.cfg'
data_file = './data/vy4.data'
weights = './backup/yolov4_final.weights'
batch_size=1

def check_batch_shape(images, batch_size):
    shapes = [image.shape for image in images]
    if len(set(shapes)) > 1:
        raise ValueError("Images don't have same shape")
    if len(shapes) > batch_size:
        raise ValueError("Batch size higher than number of images")
    return shapes[0]

def prepare_batch(images, network, channels=3):
    width = darknet.network_width(network)
    height = darknet.network_height(network)

    darknet_images = []
    for image in images:
        image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image_resized = cv2.resize(image_rgb, (width, height),
                                   interpolation=cv2.INTER_LINEAR)
        custom_image = image_resized.transpose(2, 0, 1)
        darknet_images.append(custom_image)

    batch_array = np.concatenate(darknet_images, axis=0)
    batch_array = np.ascontiguousarray(batch_array.flat, dtype=np.float32)/255.0
    darknet_images = batch_array.ctypes.data_as(darknet.POINTER(darknet.c_float))
    return darknet.IMAGE(width, height, channels, darknet_images)

def batch_detection(network, images, class_names, class_colors,
                    thresh=0.25, hier_thresh=.5, nms=.45, batch_size=4):
    image_height, image_width, _ = check_batch_shape(images, batch_size)
    darknet_images = prepare_batch(images, network)
    batch_detections = darknet.network_predict_batch(network, darknet_images, batch_size, image_width,
                                                     image_height, thresh, hier_thresh, None, 0, 0)
    batch_predictions = []
    for idx in range(batch_size):
        num = batch_detections[idx].num
        detections = batch_detections[idx].dets
        if nms:
            darknet.do_nms_obj(detections, num, len(class_names), nms)
        predictions = darknet.remove_negatives(detections, class_names, num)
        images[idx] = darknet.draw_boxes(predictions, images[idx], class_colors)
        batch_predictions.append(predictions)
    darknet.free_batch_detections(batch_detections, batch_size)
    return images, batch_predictions

def batch_detection_example():

    batch_size = 3
    random.seed(3)  # deterministic bbox colors
    network, class_names, class_colors = darknet.load_network(config_file, data_file, weights, batch_size=batch_size)
    source_path = '/home/ip/testing/darknet/data/vy4/test/'
    image_names = [
        source_path+'frame_4450_png.rf.006e6ecaf47a5531044c019846c59122.jpg', 
        source_path+'frame_4201_png.rf.0bb484b7b7cbda8305dce45d6a99f5c2.jpg',  
        source_path+'frame_3537_png.rf.d09322cea4e081cfb4e9811a097afd49.jpg', ]

    images = [cv2.imread(image) for image in image_names]
    images, detections,  = batch_detection(network, images, class_names, class_colors, batch_size=batch_size)

    for name, image in zip(image_names, images):
        # print(name.replace("data/", "my/"))
        cv2.imwrite(name.replace(source_path, "my/"), image)
    # print(detections)

def main():
    print('aa')
    batch_detection_example()

if __name__ == "__main__":
    # unconmment next line for an example of batch processing
    main()
marcobevih2o commented 4 years ago

thanks for the reply. But i forgot to write that my program is in c++

stephanecharette commented 4 years ago

If you're willing to use the DarkHelp C++ wrapper, here is how I do it: https://www.ccoderun.ca/darkhelp/api/API.html

elipriaulx commented 4 years ago

(Off-topic) @stephanecharette I'm curious - why make DarkHelp a separate project, rather than PR to this repository? I'm currently weighing up options to wrap this more cleanly myself, and am somewhat conflicted about what approach to take.

stephanecharette commented 4 years ago

DarkHelp started as a personal project to get darknet/yolo projects into some commercial applications that I write. And from what I understand, Darknet already has a C++ library/wrapper that some people use.

EndErr commented 4 years ago

DarkHelp started as a personal project to get darknet/yolo projects into some commercial applications that I write. And from what I understand, Darknet already has a C++ library/wrapper that some people use.

I've tested your work but can't find a way to make prediction in batch.

Bubble-water commented 3 years ago

@marcobevih2o

add system variable DARKNET_PATH which points to a folder where your libdarknet.so file - like this export DARKNET_PATH=/home/user/yolov4/darknet/

and then use this code:

import os
import glob
import random
import darknet
import time
import cv2
import numpy as np

config_file = './cfg/yolov4.vy4.cfg'
data_file = './data/vy4.data'
weights = './backup/yolov4_final.weights'
batch_size=1

def check_batch_shape(images, batch_size):
    shapes = [image.shape for image in images]
    if len(set(shapes)) > 1:
        raise ValueError("Images don't have same shape")
    if len(shapes) > batch_size:
        raise ValueError("Batch size higher than number of images")
    return shapes[0]

def prepare_batch(images, network, channels=3):
    width = darknet.network_width(network)
    height = darknet.network_height(network)

    darknet_images = []
    for image in images:
        image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image_resized = cv2.resize(image_rgb, (width, height),
                                   interpolation=cv2.INTER_LINEAR)
        custom_image = image_resized.transpose(2, 0, 1)
        darknet_images.append(custom_image)

    batch_array = np.concatenate(darknet_images, axis=0)
    batch_array = np.ascontiguousarray(batch_array.flat, dtype=np.float32)/255.0
    darknet_images = batch_array.ctypes.data_as(darknet.POINTER(darknet.c_float))
    return darknet.IMAGE(width, height, channels, darknet_images)

def batch_detection(network, images, class_names, class_colors,
                    thresh=0.25, hier_thresh=.5, nms=.45, batch_size=4):
    image_height, image_width, _ = check_batch_shape(images, batch_size)
    darknet_images = prepare_batch(images, network)
    batch_detections = darknet.network_predict_batch(network, darknet_images, batch_size, image_width,
                                                     image_height, thresh, hier_thresh, None, 0, 0)
    batch_predictions = []
    for idx in range(batch_size):
        num = batch_detections[idx].num
        detections = batch_detections[idx].dets
        if nms:
            darknet.do_nms_obj(detections, num, len(class_names), nms)
        predictions = darknet.remove_negatives(detections, class_names, num)
        images[idx] = darknet.draw_boxes(predictions, images[idx], class_colors)
        batch_predictions.append(predictions)
    darknet.free_batch_detections(batch_detections, batch_size)
    return images, batch_predictions

def batch_detection_example():

    batch_size = 3
    random.seed(3)  # deterministic bbox colors
    network, class_names, class_colors = darknet.load_network(config_file, data_file, weights, batch_size=batch_size)
    source_path = '/home/ip/testing/darknet/data/vy4/test/'
    image_names = [
        source_path+'frame_4450_png.rf.006e6ecaf47a5531044c019846c59122.jpg', 
        source_path+'frame_4201_png.rf.0bb484b7b7cbda8305dce45d6a99f5c2.jpg',  
        source_path+'frame_3537_png.rf.d09322cea4e081cfb4e9811a097afd49.jpg', ]

    images = [cv2.imread(image) for image in image_names]
    images, detections,  = batch_detection(network, images, class_names, class_colors, batch_size=batch_size)

    for name, image in zip(image_names, images):
        # print(name.replace("data/", "my/"))
        cv2.imwrite(name.replace(source_path, "my/"), image)
    # print(detections)

def main():
    print('aa')
    batch_detection_example()

if __name__ == "__main__":
    # unconmment next line for an example of batch processing
    main()

my system is Windows,and I meet an error "OSError: exception: access violation reading 0x000002E88587A040" when I runing darknet_images.py in the line darknet.network_predict_batch.please give a way to fix it.Thanks!