darknetlib 0.1.0 发布,Darknet 神经网络框架的 C API 库

  

darknetlib 是 darknet 的 C API 库,简单的封装了 darknet 的一些接口,主要用于为 LC-Finder 项目提供图像识别功能。

这是第一个版本,只提供了目标检测接口,但接口设计还不稳定,在后续更新中可能会调整。

示例代码:

#include <stdio.h>
#include <string.h>
#include <time.h>

#include "../include/darknet.h"

void print_detections(const darknet_detections_t *dets)
{
	size_t i, j;
	darknet_detection_t *det;

	printf("detections:\n");
	for (i = 0; i < dets->length; ++i) {
		det = &dets->list[i];
		printf("[%zu] best name: %s, box: (%g, %g, %g, %g)\n", i,
		       det->best_name, det->box.x, det->box.y, det->box.w,
		       det->box.h);
		printf("names: \n");
		for (j = 0; j < det->names_count; ++j) {
			printf("%s, %g%%\n", det->names[j],
			       det->prob[j] * 100.0f);
		}
	}
}

int detect(void)
{
	clock_t c;
	int code = 0;

	// initialize variables so that darknet can check if they need to be
	// destroyed after catching exception
	darknet_config_t *cfg = NULL;
	darknet_dataconfig_t *datacfg = NULL;
	darknet_detections_t dets = { 0 };
	darknet_detector_t *d = NULL;
	darknet_network_t *net = NULL;

	c = clock();
	darknet_try
	{
		cfg = darknet_config_load("cfg/yolov3.cfg");
		datacfg = darknet_dataconfig_load("cfg/coco.data");

		net = darknet_network_create(cfg);
		darknet_network_load_weights(net, "yolov3.weights");
		d = darknet_detector_create(net, datacfg);

		printf("\ntime: %.2fs\n\n",
		       (clock() - c) * 1.0f / CLOCKS_PER_SEC);
		c = clock();
		darknet_detector_test(d, "img/dog.jpg", &dets);
		printf("\ntime: %.2fs\n\n",
		       (clock() - c) * 1.0f / CLOCKS_PER_SEC);
	}
	darknet_catch(err)
	{
		printf("error: %s\n", darknet_get_last_error_string());
		code = -1;
	}
	darknet_etry;

	print_detections(&dets);

	darknet_detections_destroy(&dets);
	darknet_config_destroy(cfg);
	darknet_dataconfig_destroy(datacfg);
	darknet_detector_destroy(d);
	darknet_network_destroy(net);
	return code;
}
int main(int argc, char *argv[])
{
	return detect();
}

运行结果:

猜你喜欢

转载自www.oschina.net/news/105080/darknetlib-0-1-0-released