python - 使用 ctypes 从 python 调用暗网 API(图像作为参数)
问题描述
我想使用 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 的使用,这反过来又让我注意到了这样一个事实,即暗网 API 实际上带有一个显示如何使用共享库的 python 脚本!darknet 中的图像类型是一个在 python 中定义如下的类:
class IMAGE(Structure):
_fields_ = [("w", c_int),
("h", c_int),
("c", c_int),
("data", POINTER(c_float))]
在 python 中定义此类允许直接用作暗网 API 的函数参数。
推荐阅读
- css - Bootstrap 4 - 容器填充整个屏幕,滚动后显示页脚
- git - Invoke-Expression,并非所有输出都返回到变量
- amazon-web-services - 如何一起使用 Fn::Include 和 Fn::Sub?
- c - 将联合传递给函数无法识别参数名称
- java - 在控制器方法签名处处理异常
- javascript - 页面重新加载后如何使代码在已挂载的生命周期挂钩中运行
- java - Spring Boot 不需要 CSRF 令牌
- flutter - 将小部件的底部动态设置到另一个小部件的中间
- numpy - 如何使用与行相关的数组索引多维数组?
- typescript - 赛普拉斯 - 运行电子浏览器时要求更新到最新 Chrome 的警报消息未能通过涵盖元素的测试