python - 使用 ctypes 从 python 调用 darknet API(图像作为参数)

标签 python c ctypes darknet

我想使用 ctypes 将图像从 python 传递到 C。

我正在使用 AlexeyAB 的暗网 API 实现:https://github.com/AlexeyAB/darknet我想在 python 中捕获图像(可能进行更多处理)并在 C 中执行预测。

我已编译为共享库(makefile 中的“LIBSO=1”)。 这个想法是在“detector.c”脚本中有两个自定义函数。第一个函数初始化网络并将其保存为全局变量(到目前为止有效),第二个函数获取图像并使用该网络进行预测。

我似乎不知道如何将图像从 python 传递到 C。我想我更愿意传递一个指向图像的指针,因为我认为这可能是最快的解决方案。到目前为止,我的搜索尚未产生任何结果。

这些是我在“detector.c”中的自定义函数:

void init_network(char *datacfg, char *cfgfile, char *weightfile, float thresh,
    float hier_thresh)
{
    options = read_data_cfg(datacfg);
    name_list = option_find_str(options, "names", "data/names.list");
    names_size = 0;
    names = get_labels_custom(name_list, &names_size); //get_labels(name_list);
    thr = thresh;
    hier_thr = hier_thresh;
    alphabet = load_alphabet();
    net = parse_network_cfg_custom(cfgfile, 1, 1); // set batch=1
    if (weightfile) {
        load_weights(&net, weightfile);
    }
    fuse_conv_batchnorm(net);
    calculate_binary_weights(net);
    if (net.layers[net.n - 1].classes != names_size) {
        printf(" Error: in the file %s number of names %d that isn't equal to classes=%d in the file %s \n",
            name_list, names_size, net.layers[net.n - 1].classes, cfgfile);
        if (net.layers[net.n - 1].classes > names_size) getchar();
    }

}

和预测:

detection * predict_network(image im, int img_width, int img_height, int image_channels, int dont_show){
    float nms = .45;    // 0.4F
    image sized;
    sized = resize_image(im, net.w, net.h);
    layer l = net.layers[net.n - 1];

    float *X = sized.data;

    double time = get_time_point();
    network_predict(net, X);

    printf("Predicted in %lf milli-seconds.\n", ((double)get_time_point() - time) / 1000);

    int nboxes = 0;
    detection *dets = get_network_boxes(&net, im.w, im.h, thr, hier_thr, 0, 1, &nboxes, 0);
    if (nms) do_nms_sort(dets, nboxes, l.classes, nms);

    draw_detections_v3(im, dets, nboxes, thr, names, alphabet, l.classes, 0);
    save_image(im, "predictions");

    free_image(im);
    free_image(sized);

    if (!dont_show) {
        wait_until_press_key_cv();
        destroy_all_windows_cv();
    }
    return dets;

}

这是我到目前为止的 python 脚本:

import ctypes
import time
import cv2

testlib = ctypes.cdll.LoadLibrary('/home/aut/joax/github/AlexeyAB_darknet/libdarknet.so')

testlib.init_network(ctypes.create_string_buffer(b"fire.data"), ctypes.create_string_buffer(b"yolov3-tiny_fire.cfg"), ctypes.create_string_buffer(b"backup/yolov3-tiny_fire_best.weights"), ctypes.create_string_buffer(b"data/fire/img00063.png"), ctypes.c_float(0.25), ctypes.c_float(0.50), 0, 0, 0, "0", 0)
img = cv2.imread("data/fire/img00063.png")

height, width, channels = img.shape
testlib.predict_network(hex(id(img)), width, height, channels, 1)
testlib.release_network()

这些并不完全是“最小可重现示例”,但很难给出这样的示例,因为暗网 API 太大了......

问候

最佳答案

非常感谢您的回答@mark-tolonen。你的回答让我更仔细地研究了 ctypes 的使用,这反过来又让我注意到这样一个事实:darknet API 实际上附带了一个 python 脚本,该脚本显示了如何使用共享库! darknet中的图像类型是python中定义如下的类:

class IMAGE(Structure):
    _fields_ = [("w", c_int),
                ("h", c_int),
                ("c", c_int),
                ("data", POINTER(c_float))]

在 python 中定义此类可以直接用作 darknet API 的函数参数。

关于python - 使用 ctypes 从 python 调用 darknet API(图像作为参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58453539/

相关文章:

Python - 用于从 CSV 文件中的整行中删除引号的正则表达式

python - 用另一个值替换 Pandas 数据框列中的几个值

python-2.7 - 如何使用 python ctypes 访问另一个进程的 PEB

使用 ctypes 在 C 中动态访问 Python 变量

python - 在 Ctypes 中传递结构

对 Coindesk 的 Python API 调用返回 403 或 405。如何排除故障?

python - 在 pyspark 中,为什么 `limit` 后跟 `repartition` 创建完全相等的分区大小?

复制文件并向后写入文件信息

多项式求和程序的 C 运行时错误

c - strlen 给出意外的输出 C