+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

python和ctypes,ctypes从嵌套结构返回错误的float值

发布于2021-04-12 03:21     阅读(975)     评论(0)     点赞(27)     收藏(4)


0

1

2

3

4

5

6

7

我有一个名为DetectorResult的结构,它内部还有另一个结构称为detector_predictions_t。该嵌套结构是指向结构的指针,而不是结构的值:

/// detector result structures
typedef struct _detector_predictions_t {
    float x1;
    float x2;
    float y1;
    float y2;
    float score;
} detector_predictions_t, *pdetector_predictions_t;

typedef struct _DetectorResult {
    int detect_num;
    int class_idx;
    pdetector_predictions_t preds; ///< all face detector predictions
} DetectorResult, *pDetectorResult;

我有python ctypes代码,可以正确定义上述c结构:

# prediction structs
class CDetectorPredictionsT(ctypes.Structure):
    _fields_ = [("x1", ctypes.c_float), ("x2", ctypes.c_float), ("y1", ctypes.c_float), ("y2", ctypes.c_float), ("score", ctypes.c_float)]

class CDetectorResult(ctypes.Structure):
    _fields_ = [("detect_num", ctypes.c_int), ("class_idx", ctypes.c_int), ("preds", ctypes.POINTER(CDetectorPredictionsT))]

我有一个函数,它返回一个指向DetectorResult结构的指针:

pDetectorResult p_model_getresult(uint8_t* data)
{
    printf("entering\n");
    //pDetectorResult resultData = (pDetectorResult)malloc(sizeof(*resultData));
    //resultData->preds = (pdetector_predictions_t)malloc(sizeof(*resultData->preds));
    pDetectorResult resultData = (pDetectorResult)calloc(1, sizeof(*resultData));
    resultData->preds = (pdetector_predictions_t)calloc(1, sizeof(*resultData->preds));
    if(resultData == NULL) {
        printf("resultdata malloc failed");
    } else if(resultData->preds == NULL) {
        printf("preds malloc failed");
    } else {
        printf("about to memset");
        //memset(resultData, 0, sizeof(*resultData));
        printf("memset 2");
        //memset(resultData->preds, 0, sizeof(*resultData->preds));
        printf("finished memset");
        printf("successfully made det result\n");
        model_getresult(resultData, data);
        printf("the first bb is %f %f %f %f %f\n", resultData->preds->x1, resultData->preds->x2, resultData->preds->y1, resultData->preds->y2, resultData->preds->score);
        printf("returning now\n");
    }
    return resultData;
}

而且我有调用此函数的python代码:

self.aml_lib.p_model_getresult.argtypes = [c_uint_p_type(self.width * self.height * self.channel)]
self.aml_lib.p_model_getresult.restype = ctypes.POINTER(CDetectorResult)

# ...

    def model_getresult(self, data, format):
        input_data = self.model_setinput(data, format)
        print("data s ", input_data[0], input_data[1], input_data[2])
        return self.aml_lib.p_model_getresult(input_data)

最后,我有python代码检查结果:

    ret = aml_object.model_getresult(data, PIXEL_FORMAT).contents
    a = ret.preds.contents.x1

其中ret是直接来自c的DetectorResult结构。

a的值是某个随机数,例如1.25 ^ 25。当我在c中打印值时(例如printf(“%f”,resultData-> preds-> x1)),结果是不同的,介于0和1之间的数字。

如何获得与在c中的printf中打印的值相同的a值?

我在linux上运行它。


解决方案


暂无回答

0

1

2

3

4

5

6

7

8

9



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接: https://www.pythonheidong.com/blog/article/934682/1ae58309b8b6aab43bc0/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

27 0
收藏该文
已收藏

评论内容:(最多支持255个字符)