Darknet/YOLO v3.0-177-gfa2353b
Object Detection Framework
 
Loading...
Searching...
No Matches
darknet.h File Reference

Include this file to get access to the Darknet/YOLO C API. More...

Include dependency graph for darknet.h:
This graph shows which files directly or indirectly include this file:

Classes

struct  DarknetBox
 Bounding box used with normalized coordinates (between 0.0 and 1.0). More...
 
struct  DarknetDetection
 Everything Darknet knows about a specific detection. More...
 
struct  DarknetImage
 The structure DarknetImage is used to store a normalized RGB Darknet image. More...
 

Typedefs

typedef struct DarknetBox box
 Bounding box used with normalized coordinates (between 0.0 and 1.0).
 
typedef struct DarknetBox DarknetBox
 Bounding box used with normalized coordinates (between 0.0 and 1.0).
 
typedef struct DarknetDetection DarknetDetection
 Everything Darknet knows about a specific detection.
 
typedef struct DarknetImage DarknetImage
 The structure DarknetImage is used to store a normalized RGB Darknet image.
 
typedef void * DarknetNetworkPtr
 An opaque pointer to a Darknet::Network object, without needing to expose the internals of the network structure.
 
typedef struct DarknetDetection detection
 Everything Darknet knows about a specific detection.
 
typedef struct DarknetImage image
 Darknet-style image (vector of floats).
 

Enumerations

enum  NMS_KIND {
  DEFAULT_NMS ,
  GREEDY_NMS ,
  DIOU_NMS ,
  CORNERS_NMS
}
 

Functions

void calculate_binary_weights (DarknetNetworkPtr ptr)
 This is part of the original C API.
 
void copy_image_from_bytes (DarknetImage im, char *pdata)
 This is part of the original C API. Convert from data pointer to a Darknet image. Used by the Python API.
 
void cuda_set_device (int n)
 This is part of the original C API. This function does nothing when Darknet was built to run on the CPU.
 
void darknet_add_skipped_class (DarknetNetworkPtr ptr, const int class_to_skip)
 This is the C equivalent to Darknet::add_skipped_class().
 
void darknet_clear_skipped_classes (DarknetNetworkPtr ptr)
 This is the C equivalent to Darknet::clear_skipped_classes().
 
void darknet_del_skipped_class (DarknetNetworkPtr ptr, const int class_to_include)
 This is the C equivalent to Darknet::del_skipped_classes().
 
void darknet_fix_out_of_bound_values (DarknetNetworkPtr ptr, const bool toggle)
 This is the C equivalent to Darknet::fix_out_of_bound_values().
 
void darknet_free_neural_network (DarknetNetworkPtr *ptr)
 This is the C equivalent to Darknet::free_neural_network().
 
DarknetNetworkPtr darknet_load_neural_network (const char *const cfg_filename, const char *const names_filename, const char *const weights_filename)
 This is the C equivalent to Darknet::load_neural_network().
 
void darknet_network_dimensions (DarknetNetworkPtr ptr, int *w, int *h, int *c)
 This is the C equivalent to Darknet::network_dimensions().
 
void darknet_set_detection_threshold (DarknetNetworkPtr ptr, float threshold)
 This is the C equivalent to Darknet::set_detection_threshold().
 
void darknet_set_gpu_index (int idx)
 This is the C equivalent to Darknet::set_gpu_index().
 
void darknet_set_non_maximal_suppression_threshold (DarknetNetworkPtr ptr, float threshold)
 This is the C equivalent to Darknet::set_non_maximal_suppression_threshold().
 
void darknet_set_trace (const bool flag)
 This is the C equivalent to Darknet::set_trace().
 
void darknet_set_verbose (const bool flag)
 This is the C equivalent to Darknet::set_verbose().
 
void darknet_show_version_info ()
 This is the C equivalent to Darknet::show_version_info().
 
const char * darknet_version_short ()
 Return the short version string, such as "3.0.163".
 
const char * darknet_version_string ()
 Return the full version string, such as "v3.0-163-g56145bf".
 
void diounms_sort (detection *dets, int total, int classes, float thresh, NMS_KIND nms_kind, float beta1)
 This is part of the original C API. Non Maxima Suppression.
 
void do_nms_obj (detection *dets, int total, int classes, float thresh)
 This is part of the original C API. Non Maxima Suppression.
 
void do_nms_sort (detection *dets, int total, int classes, float thresh)
 This is part of the original C API. Non Maxima Suppression.
 
void free_detections (detection *dets, int n)
 This is part of the original C API. Do not use in new code.
 
void free_image (image im)
 This is part of the original C API.
 
void free_network_ptr (DarknetNetworkPtr ptr)
 This is part of the original C API.
 
void free_ptrs (void **ptrs, int n)
 This is part of the original C API.
 
detectionget_network_boxes (DarknetNetworkPtr ptr, int w, int h, float thresh, float hier, int *map, int relative, int *num, int letter)
 This is part of the original C API.
 
DarknetImage load_image_v2 (const char *filename, int desired_width, int desired_height, int channels)
 This is part of the original C API. This function exists for the Python API.
 
DarknetImage make_empty_image (int w, int h, int c)
 This is part of the original C API.
 
DarknetImage make_image (int w, int h, int c)
 This is part of the original C API.
 
detectionmake_network_boxes (DarknetNetworkPtr ptr, float thresh, int *num)
 This is part of the original C API.
 
float * network_predict_image (DarknetNetworkPtr ptr, const DarknetImage im)
 This is part of the original C API.
 
float * network_predict_image_letterbox (DarknetNetworkPtr ptr, DarknetImage im)
 This is part of the original C API.
 
float * network_predict_ptr (DarknetNetworkPtr ptr, float *input)
 This is part of the original C API.
 
void reset_rnn (DarknetNetworkPtr ptr)
 This is part of the original C API.
 
DarknetNetworkPtr load_network (const char *cfg, const char *weights, int clear)
 
DarknetNetworkPtr load_network_custom (const char *cfg, const char *weights, int clear, int batch)
 

Detailed Description

Include this file to get access to the Darknet/YOLO C API.

  • The new (V3, July 2024) Darknet/YOLO C API is at the top of the file.
  • The original V2 Darknet/YOLO C API is at the bottom of this file (DARKNET_INCLUDE_ORIGINAL_API).
  • The new (V3, July 2024) Darknet/YOLO C++ API is in darknet.hpp.

https://darknetcv.ai/api/api.html

The old C V2 API did not have "darknet" in the function names nor the structures returned. It defined things like network and image in the global namespace, which can cause problems since those are common words. By default this old API is no longer exposed. If you're using some old software that expects the original C API in the darknet library, then make sure you "#define DARKNET_INCLUDE_ORIGINAL_API" before you include this header file.

Internally, Darknet still uses the old C V2 API.

Typedef Documentation

◆ box

typedef struct DarknetBox box

Bounding box used with normalized coordinates (between 0.0 and 1.0).

◆ DarknetBox

typedef struct DarknetBox DarknetBox

Bounding box used with normalized coordinates (between 0.0 and 1.0).

◆ DarknetDetection

Everything Darknet knows about a specific detection.

This structure is used by the old API. If using the new API, use Darknet::Prediction instead.

◆ DarknetImage

typedef struct DarknetImage DarknetImage

The structure DarknetImage is used to store a normalized RGB Darknet image.

The format is intended to be used for internal use by Darknet, but there are some situations where it may also be used or referenced externally via the Darknet API.

Moving forward starting with Darknet V3 (August 2024), where possible the emphasis will be to use OpenCV cv::Mat objects in the external-facing API instead of DarknetImage. Internally, Darknet will continue to use Image.

Warning
Keep this structure as POD (plain-old-data) since there are many places in the old code where memory for these image objects is calloc'd. This structure was originally part of the old Darknet C API, which is why it exists this way and not as a C++ class with methods.

Within data, the image is stored as 3 non-interlaced channels in RGB order. Each channel is stored as rows, from top to bottom. So a 5x3 RGB image would look like this:

r r r r r
r r r r r
r r r r r
g g g g g
g g g g g
g g g g g
b b b b b
b b b b b
b b b b b

A 5x3 pure red image, with a blueish/green square in the bottom-right corner would look like this:

1.0 1.0 1.0 1.0 1.0 // red channel
1.0 1.0 1.0 0.0 0.0 // note the lack of red in the bottom-right corner
1.0 1.0 1.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 // green channel
0.0 0.0 0.0 0.5 0.5
0.0 0.0 0.0 0.5 0.5
0.0 0.0 0.0 0.0 0.0 // blue channel
0.0 0.0 0.0 1.0 1.0 // note the blue square in the bottom-right corner
0.0 0.0 0.0 1.0 1.0

For additional information or to help debug the internals of DarknetImage, see Darknet::image_as_debug_string().

See also
Darknet::load_image()
Darknet::copy_image()
make_empty_image()
make_image()
Darknet::free_image()

◆ DarknetNetworkPtr

typedef void* DarknetNetworkPtr

An opaque pointer to a Darknet::Network object, without needing to expose the internals of the network structure.

◆ detection

typedef struct DarknetDetection detection

Everything Darknet knows about a specific detection.

◆ image

typedef struct DarknetImage image

Darknet-style image (vector of floats).

Enumeration Type Documentation

◆ NMS_KIND

enum NMS_KIND
See also
diounms_sort()
Enumerator
DEFAULT_NMS 
GREEDY_NMS 
DIOU_NMS 
CORNERS_NMS 

Function Documentation

◆ calculate_binary_weights()

void calculate_binary_weights ( DarknetNetworkPtr  ptr)

This is part of the original C API.

Do not use in new code.

See also
darknet_load_neural_network()
Darknet::load_neural_network()

If you were previously using calculate_binary_weights() from within C code, it used to pass the network by value. Starting with Darknet V3 in 2024-08-16, the network is now passed in as a pointer.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ copy_image_from_bytes()

void copy_image_from_bytes ( DarknetImage  im,
char *  pdata 
)

This is part of the original C API. Convert from data pointer to a Darknet image. Used by the Python API.

◆ cuda_set_device()

void cuda_set_device ( int  n)

This is part of the original C API. This function does nothing when Darknet was built to run on the CPU.

Here is the caller graph for this function:

◆ darknet_add_skipped_class()

void darknet_add_skipped_class ( DarknetNetworkPtr  ptr,
const int  class_to_skip 
)

This is the C equivalent to Darknet::add_skipped_class().

Here is the call graph for this function:

◆ darknet_clear_skipped_classes()

void darknet_clear_skipped_classes ( DarknetNetworkPtr  ptr)

This is the C equivalent to Darknet::clear_skipped_classes().

Here is the call graph for this function:

◆ darknet_del_skipped_class()

void darknet_del_skipped_class ( DarknetNetworkPtr  ptr,
const int  class_to_include 
)

This is the C equivalent to Darknet::del_skipped_classes().

Here is the call graph for this function:

◆ darknet_fix_out_of_bound_values()

void darknet_fix_out_of_bound_values ( DarknetNetworkPtr  ptr,
const bool  toggle 
)

This is the C equivalent to Darknet::fix_out_of_bound_values().

Here is the call graph for this function:

◆ darknet_free_neural_network()

void darknet_free_neural_network ( DarknetNetworkPtr ptr)

This is the C equivalent to Darknet::free_neural_network().

Here is the call graph for this function:

◆ darknet_load_neural_network()

DarknetNetworkPtr darknet_load_neural_network ( const char *const  cfg_filename,
const char *const  names_filename,
const char *const  weights_filename 
)

This is the C equivalent to Darknet::load_neural_network().

Here is the call graph for this function:

◆ darknet_network_dimensions()

void darknet_network_dimensions ( DarknetNetworkPtr  ptr,
int *  w,
int *  h,
int *  c 
)

This is the C equivalent to Darknet::network_dimensions().

Here is the call graph for this function:

◆ darknet_set_detection_threshold()

void darknet_set_detection_threshold ( DarknetNetworkPtr  ptr,
float  threshold 
)

This is the C equivalent to Darknet::set_detection_threshold().

Here is the call graph for this function:

◆ darknet_set_gpu_index()

void darknet_set_gpu_index ( int  idx)

This is the C equivalent to Darknet::set_gpu_index().

Here is the call graph for this function:

◆ darknet_set_non_maximal_suppression_threshold()

void darknet_set_non_maximal_suppression_threshold ( DarknetNetworkPtr  ptr,
float  threshold 
)

This is the C equivalent to Darknet::set_non_maximal_suppression_threshold().

Here is the call graph for this function:

◆ darknet_set_trace()

void darknet_set_trace ( const bool  flag)

This is the C equivalent to Darknet::set_trace().

Here is the call graph for this function:

◆ darknet_set_verbose()

void darknet_set_verbose ( const bool  flag)

This is the C equivalent to Darknet::set_verbose().

Here is the call graph for this function:

◆ darknet_show_version_info()

void darknet_show_version_info ( )

This is the C equivalent to Darknet::show_version_info().

Here is the call graph for this function:

◆ darknet_version_short()

const char * darknet_version_short ( )

Return the short version string, such as "3.0.163".

See also
DARKNET_VERSION_SHORT

◆ darknet_version_string()

const char * darknet_version_string ( )

Return the full version string, such as "v3.0-163-g56145bf".

See also
DARKNET_VERSION_STRING

◆ diounms_sort()

void diounms_sort ( detection dets,
int  total,
int  classes,
float  thresh,
NMS_KIND  nms_kind,
float  beta1 
)

This is part of the original C API. Non Maxima Suppression.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ do_nms_obj()

void do_nms_obj ( detection dets,
int  total,
int  classes,
float  thresh 
)

This is part of the original C API. Non Maxima Suppression.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ do_nms_sort()

void do_nms_sort ( detection dets,
int  total,
int  classes,
float  thresh 
)

This is part of the original C API. Non Maxima Suppression.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_detections()

void free_detections ( detection dets,
int  n 
)

This is part of the original C API. Do not use in new code.

Here is the caller graph for this function:

◆ free_image()

void free_image ( image  im)

This is part of the original C API.

Free the data pointer that stores the image . All image objects must eventually call either this function or Darknet::free_image() to prevent memory leaks.

See also
Darknet::free_image()
Note
The image is passed by value, meaning that the data pointer in the caller's copy of the image will be left dangling. Be careful not to reference it once this call returns. Where possible when using C++, call Darknet::free_image() instead.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_network_ptr()

void free_network_ptr ( DarknetNetworkPtr  ptr)

This is part of the original C API.

Do not use in new code.

See also
darknet_free_neural_network()
Darknet::free_neural_network()

If you were previously using free_network() from within C code, please use free_network_ptr() instead by passing in the address of the network structure (pointer to the network).

Note
See the additional comments in free_network().
Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_ptrs()

void free_ptrs ( void **  ptrs,
int  n 
)

This is part of the original C API.

Here is the caller graph for this function:

◆ get_network_boxes()

detection * get_network_boxes ( DarknetNetworkPtr  ptr,
int  w,
int  h,
float  thresh,
float  hier,
int *  map,
int  relative,
int *  num,
int  letter 
)

This is part of the original C API.

Do not use in new code.

You must call free_detections() to free up memory once done with the detections.

See also
Darknet::predict()
free_detections()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ load_image_v2()

DarknetImage load_image_v2 ( const char *  filename,
int  desired_width,
int  desired_height,
int  channels 
)

This is part of the original C API. This function exists for the Python API.

Here is the call graph for this function:

◆ load_network()

DarknetNetworkPtr load_network ( const char *  cfg,
const char *  weights,
int  clear 
)

This is part of the original C API. Do not use in new code.

See also
darknet_load_neural_network()
Darknet::load_neural_network()
Todo:
V3 why do we not call fuse_conv_batchnorm() here?
Here is the call graph for this function:

◆ load_network_custom()

DarknetNetworkPtr load_network_custom ( const char *  cfg,
const char *  weights,
int  clear,
int  batch 
)
Todo:
V3 Some code seems to also call this next function, and some not. This was not originally called here, but I copied it from several other code locations. Need to invetigate whether or not it should be here. 2024-08-03
Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_empty_image()

DarknetImage make_empty_image ( int  w,
int  h,
int  c 
)

This is part of the original C API.

Make an empty image with the given dimensions. The data pointer will be nullptr.

See also
free_image()
make_image()
Here is the caller graph for this function:

◆ make_image()

DarknetImage make_image ( int  w,
int  h,
int  c 
)

This is part of the original C API.

Similar to make_empty_image() but the data pointer is fully allocated.

See also
free_image()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_network_boxes()

detection * make_network_boxes ( DarknetNetworkPtr  ptr,
float  thresh,
int *  num 
)

This is part of the original C API.

See also
make_network_boxes_batch()
Todo:
V3 JAZZ: this is where we spend all our time
Here is the call graph for this function:
Here is the caller graph for this function:

◆ network_predict_image()

float * network_predict_image ( DarknetNetworkPtr  ptr,
const DarknetImage  im 
)

This is part of the original C API.

Do not use in new code.

See also
network_predict_ptr()
Darknet::predict()
Here is the call graph for this function:

◆ network_predict_image_letterbox()

float * network_predict_image_letterbox ( DarknetNetworkPtr  ptr,
DarknetImage  im 
)

This is part of the original C API.

Here is the call graph for this function:

◆ network_predict_ptr()

float * network_predict_ptr ( DarknetNetworkPtr  ptr,
float *  input 
)

This is part of the original C API.

Do not use in new code.

See also
network_predict_image()
Darknet::predict()

If you were previously using network_predict() from within C code, please use network_predict_ptr() instead by passing in the address of the network structure (pointer to the network).

Here is the call graph for this function:

◆ reset_rnn()

void reset_rnn ( DarknetNetworkPtr  ptr)

This is part of the original C API.

Here is the call graph for this function: