尝试通过 Python 脚本发布 C 数据



我一直在尝试发布一些边界框数据(来自 YOLOv2 神经网络(,并且在将一些值发布到 python 脚本时遇到了一些问题。

图片.c 代码:

#include "image.h"
#include "utils.h"
#include "blas.h"
#include "cuda.h"
#include <stdio.h>
#include <math.h>
#include <Python.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
int windows = 0;
int counter = 0;
char *pyArgs[] = {"../networktables/", "run_network.py", "init", "put"};
PyObject* pyModule;
void initialize(){
//Setting the Path
    PySys_SetPath(pyArgs[0]);
//Importing pynetworktables
PyObject* pyName = PyString_FromString(pyArgs[1]);
pyModule = PyImport_Import(pyName);
//Constructing the arguments
//Calling init()
pyFunc = pyObject_CallMethod(pyModule, pyArgs[2], NULL);
}
void draw_detections(image im, int num, float thresh, box *boxes, float **probs, float **masks, char **names, image **alphabet, int classes){

这里有一些代码

        int left  = (b.x-b.w/2.)*im.w;
        int right = (b.x+b.w/2.)*im.w;
        int top   = (b.y-b.h/2.)*im.h;
        int bot   = (b.y+b.h/2.)*im.h;
        /**
         * @Author Sam Schwartz
         * So this part is my  code that may or may not work good luck to you my friend :)
         */
    Py_Initialize();
        //Running initialize
    if(counter < 1){
        initialize();
    }
        //Converting left, right, top, bot to Python integers
        PyObject* pyLeft  = PyInt_FromSize_t(left);
        PyObject* pyRight = PyInt_FromSize_t(right);
        PyObject* pyTop   = PyInt_FromSize_t(top);
        PyObject* pyBot   = PyInt_FromSize_t(bot);
        PyObject* pyVal[4] = {pyLeft, pyRight, pyTop, pyBot};
        //Setting the four arguments for the put() function
        PyObject* pythonArgument;
        pythonArgument = PyTuple_New(4);
        PyTuple_SetItem(pythonArgument, 0, pyVal[0]);
        PyTuple_SetItem(pythonArgument, 1, pyVal[1]);
        pyTuple_SetItem(pythonArgument, 2, pyVal[2]);
        pyTuple_SetItem(pythonArgument, 3, pyVal[3]);
        //Calling put()
        pyFunc = PyObject_CallMethod(pyModule, pyArgs[3], pythonArgument);
    Py_Finalize();
        /**
         * End of my code. Everything else works.
         */
        if(left < 0) left = 0;
        if(right > im.w-1) right = im.w-1;
        if(top < 0) top = 0;
        if(bot > im.h-1) bot = im.h-1;
        draw_box_width(im, left, top, right, bot, width, red, green, blue);
        if (alphabet) {
            image label = get_label(alphabet, labelstr, (im.h*.03)/10);
            draw_label(im, top + width, left, label, rgb);
            free_image(label);
        }
        if (masks){
            image mask = float_to_image(14, 14, 1, masks[i]);
            image resized_mask = resize_image(mask, b.w*im.w, b.h*im.h);
            image tmask = threshold_image(resized_mask, .5);
            embed_image(tmask, im, left, top);
            free_image(mask);
            free_image(resized_mask);
            free_image(tmask);
        }
    }
}

本质上,我正在尝试将左、右、上和机器人整数发布到 python 脚本中。

以下是 python 脚本,run_network.py:

from networktables import NetworkTables
def init():
    NetworkTables.initialize('10.13.39.2')
    table = NetworkTables.getTable('Root')
def put(right, left, top, bot):
    table.putNumber(right)
    table.putNumber(left)
    table.putNumber(top)
    table.putNumber(bot)

Makefile可能存在一些问题,所以这里是文件:

GPU=1
CUDNN=1
OPENCV=1
OPENMP=0
DEBUG=0
PYTHON=1
ARCH= -gencode arch=compute_30,code=sm_30 
      -gencode arch=compute_35,code=sm_35 
      -gencode arch=compute_50,code=[sm_50,compute_50] 
      -gencode arch=compute_52,code=[sm_52,compute_52]
#      -gencode arch=compute_20,code=[sm_20,sm_21]  This one is deprecated?
# This is what I use, uncomment if you know your arch and want to specify
# ARCH= -gencode arch=compute_52,code=compute_52
VPATH=./src/:./examples
SLIB=libdarknet.so
ALIB=libdarknet.a
EXEC=darknet
OBJDIR=./obj/
CC=gcc
NVCC=nvcc 
AR=ar
ARFLAGS=rcs
OPTS=-Ofast
LDFLAGS= -lm -pthread 
COMMON= -Iinclude/ -Isrc/
CFLAGS=-Wall -Wno-unknown-pragmas -Wfatal-errors -fPIC
ifeq ($(OPENMP), 1) 
CFLAGS+= -fopenmp
endif
ifeq ($(DEBUG), 1) 
OPTS=-O0 -g
endif
CFLAGS+=$(OPTS)
ifeq ($(OPENCV), 1) 
COMMON+= -DOPENCV
CFLAGS+= -DOPENCV
LDFLAGS+= `pkg-config --libs opencv` 
COMMON+= `pkg-config --cflags opencv` 
endif
ifeq ($(GPU), 1) 
COMMON+= -DGPU -I/usr/local/cuda/include/
CFLAGS+= -DGPU
LDFLAGS+= -L/usr/local/cuda/lib64 -lcuda -lcudart -lcublas -lcurand
endif
ifeq ($(CUDNN), 1) 
COMMON+= -DCUDNN 
CFLAGS+= -DCUDNN
LDFLAGS+= -lcudnn
endif
ifeq ($(PYTHON), 1)
CFLAGS+= -I/usr/include/python2.7 -I/usr/include/aarch64-linux-gnu/python2.7  -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security  -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes
LDFLAGS+= -L/usr/include/python2.7 -L/usr/lib/python2.7/config-aarch64-linux-gnu -L/usr/lib -lpython2.7 -lpthread -ldl  -lutil -lm  -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions
endif

OBJ=gemm.o utils.o cuda.o deconvolutional_layer.o convolutional_layer.o list.o image.o activations.o im2col.o col2im.o blas.o crop_layer.o dropout_layer.o maxpool_layer.o softmax_layer.o data.o matrix.o network.o connected_layer.o cost_layer.o parser.o option_list.o detection_layer.o route_layer.o box.o normalization_layer.o avgpool_layer.o layer.o local_layer.o shortcut_layer.o activation_layer.o rnn_layer.o gru_layer.o crnn_layer.o demo.o batchnorm_layer.o region_layer.o reorg_layer.o tree.o  lstm_layer.o
EXECOBJA=captcha.o lsd.o super.o art.o tag.o cifar.o go.o rnn.o segmenter.o regressor.o classifier.o coco.o yolo.o detector.o nightmare.o attention.o darknet.o
ifeq ($(GPU), 1) 
LDFLAGS+= -lstdc++ 
OBJ+=convolutional_kernels.o deconvolutional_kernels.o activation_kernels.o 
im2col_kernels.o col2im_kernels.o blas_kernels.o crop_layer_kernels.o dropout_layer_kernels.o maxpool_layer_kernels.o avgpool_layer_kernels.o
endif
EXECOBJ = $(addprefix $(OBJDIR), $(EXECOBJA))
OBJS = $(addprefix $(OBJDIR), $(OBJ))
DEPS = $(wildcard src/*.h) Makefile include/darknet.h
#all: obj backup results $(SLIB) $(ALIB) $(EXEC)
all: obj  results $(SLIB) $(ALIB) $(EXEC)

$(EXEC): $(EXECOBJ) $(ALIB)
    $(CC) $(COMMON) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(ALIB)
$(ALIB): $(OBJS)
    $(AR) $(ARFLAGS) $@ $^
$(SLIB): $(OBJS)
    $(CC) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS)
$(OBJDIR)%.o: %.c $(DEPS)
    $(CC) $(COMMON) $(CFLAGS) -c $< -o $@
$(OBJDIR)%.o: %.cu $(DEPS)
    $(NVCC) $(ARCH) $(COMMON) --compiler-options "$(CFLAGS)" -c $< -o $@
obj:
    mkdir -p obj
backup:
    mkdir -p backup
results:
    mkdir -p results
.PHONY: clean
clean:
    rm -rf $(OBJS) $(SLIB) $(ALIB) $(EXEC) $(EXECOBJ)

有几个编译错误,指出"对'pyTuple_SetItem'的未定义引用"提前感谢您的帮助!

重新制作项目,还有更多错误!

gcc -Iinclude/ -Isrc/ -DOPENCV `pkg-config --cflags opencv`  -DGPU -I/usr/local/cuda/include/ -DCUDNN  -Wall -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DOPENCV -DGPU -DCUDNN -c ./examples/darknet.c -o obj/darknet.o
gcc -Iinclude/ -Isrc/ -DOPENCV `pkg-config --cflags opencv`  -DGPU -I/usr/local/cuda/include/ -DCUDNN  -Wall -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DOPENCV -DGPU -DCUDNN obj/captcha.o obj/lsd.o obj/super.o obj/art.o obj/tag.o obj/cifar.o obj/go.o obj/rnn.o obj/segmenter.o obj/regressor.o obj/classifier.o obj/coco.o obj/yolo.o obj/detector.o obj/nightmare.o obj/attention.o obj/darknet.o libdarknet.a -o darknet -lm -pthread  `pkg-config --libs opencv`  -L/usr/local/cuda/lib64 -lcuda -lcudart -lcublas -lcurand -lcudnn -lstdc++  libdarknet.a
libdarknet.a(image.o): In function `initialize':
image.c:(.text+0x1b058): undefined reference to `PySys_SetPath'
image.c:(.text+0x1b060): undefined reference to `PyString_FromString'
image.c:(.text+0x1b070): undefined reference to `PyImport_Import'
image.c:(.text+0x1b088): undefined reference to `PyObject_CallMethod'
libdarknet.a(image.o): In function `draw_detections':
image.c:(.text+0x222a8): undefined reference to `Py_Initialize'
image.c:(.text+0x222c8): undefined reference to `PyInt_FromSize_t'
image.c:(.text+0x222d8): undefined reference to `PyInt_FromSize_t'
image.c:(.text+0x222e8): undefined reference to `PyInt_FromSize_t'
image.c:(.text+0x222f8): undefined reference to `PyInt_FromSize_t'
image.c:(.text+0x22308): undefined reference to `PyTuple_New'
image.c:(.text+0x22318): undefined reference to `PyTuple_SetItem'
image.c:(.text+0x2232c): undefined reference to `PyTuple_SetItem'
image.c:(.text+0x22340): undefined reference to `pyTuple_SetItem'
image.c:(.text+0x22354): undefined reference to `pyTuple_SetItem'
image.c:(.text+0x22370): undefined reference to `PyObject_GetAttrString'
image.c:(.text+0x22378): undefined reference to `PyObject_CallObject'
image.c:(.text+0x2237c): undefined reference to `Py_Finalize'
collect2: error: ld returned 1 exit status
Makefile:82: recipe for target 'darknet' failed
make: *** [darknet] Error 1

您的问题是-lpython2.7出现在链接行中的-ldarknet之前。 由于暗网依赖于python2.7,因此暗网必须在链接器命令中排在第一位。

最新更新