相关文章推荐
笑点低的橙子  ·  django filter - CSDN文库·  2 年前    · 
一直单身的鸵鸟  ·  jquery ...·  3 年前    · 
沉着的太阳  ·  ios mui picker 滑动错位 ...·  3 年前    · 
CV_IMAGE_API mt_result_t mt_image_detect_create( const char * model_path, mt_handle_t* handle); CV_IMAGE_API mt_image_detect_destroy(mt_handle_t handle); CV_IMAGE_API mt_image_release_detect_result(detection_result_t * detection_result, int count); CV_IMAGE_API mt_result_t mt_image_detect_compact(mt_handle_t handle, const unsigned char * img, int format, int image_width, int image_height, int image_stride, detection_result_t** detect_info, int * count); CV_IMAGE_API mt_result_t mt_image_detect_reset(mt_handle_t handle);

结构体的映射:

from ctypes import *
import os
import shutil
class rect_t(Structure):
rect_t._fields_ = [
    ('left', c_int),
    ('top', c_int),
    ('right', c_int),
    ('bottom', c_int),
class point3f_t(Structure):
point3f_t._fields_ = [
    ('x', c_float),
    ('y', c_float),
    ('z', c_float),
class extra_info(Structure):
extra_info._fields_ = [
    ('mvp_mat', c_float*3*3),
    ('point_t', POINTER(point3f_t)),
    ('point_count', c_int),
class detection_result(Structure):
detection_result._fields_ = [
    ('rect', rect_t),
    ('score', c_float),
    ('label', c_int),
    ('orientation', c_int),
    ('extra_info', extra_info),
def movefile(srcpath, dstpath):
    if not os.path.isfile(srcpath):
        print(srcpath + ' is not exist!')
    else:
        fpath, fname = os.path.split(dstpath)
        if not os.path.exists(fpath):
            os.makedirs(fpath)
        shutil.copy(srcpath, dstpath)
        print('copy ' + srcpath + '->' + dstpath)

接口映射:

import ctypes
import os
class MtLibrary:
    def __init__(self, path):
        self.path = path
        self.lib = None
        self.hasInit = False
    def load_library(self):
        dl = ctypes.cdll.LoadLibrary
        print('load_library lib is Exist : ' + str(os.path.exists(self.path)))
        print(os.getcwd())
        lib = dl(self.path)
        self.lib = lib
        self.hasInit = True
    def init_license(self, licence):
        if not self.hasInit:
            print('lib has not init!!')
            return False
        licence_context = bytes(licence, "utf8")
        return self.lib.mt_image_detect_init_config(licence_context)
    def create_handle(self, path, handle):
        if not self.hasInit:
            print('lib has not init!!')
            return None
        return self.lib.mt_image_detect_create(path, handle)
    def reset_handle(self, handle):
        return self.lib.mt_image_detect_reset(handle)
    def detect_image(self, handle, image, format, width, height, stride, detect_info, count):
        if not self.hasInit:
            print('lib has not init!!')
            return None
        return self.lib.mt_image_detect_compact(handle, image, format, width, height, stride, detect_info, count)
    def release_result(self, detect_result, count):
        if not self.hasInit:
            print("lib has not init!!")
            return None
        return self.lib.mt_image_release_detect_result(detect_result, count)
    def destroy_handle(self, handle):
        if not self.hasInit:
            print("lib has not init!!")
            return None
        return self.lib.mt_image_detect_destroy(handle)
extra_info._fields_ = [ ( ' mvp_mat ' , c_float*3*3 ), ( ' point_t ' , POINTER(point3f_t)), ( ' point_count ' , c_int), class detection_result(Structure): detection_result._fields_ = [ ( ' rect ' , rect_t), ( ' score ' , c_float), ( ' label ' , c_int), ( ' orientation ' , c_int), ( ' extra_info ' , extra_info), float mvp_mat[3][3] --> c_float 3 3

point_t *points_ori --> POINTER(point3f_t)

  • 调用时指针(二级指针)的映射
  • CV_IMAGE_API
    mt_result_t
    mt_image_detect_compact(mt_handle_t handle, const unsigned char* img, int format, int image_width,
            int image_height, int image_stride, detection_result_t** detect_info, int* count);

    python调用:

    TARGETPOINTER_t = POINTER(detection_result)
    result_handle = TARGETPOINTER_t()
    print('result_handle: ' + str(result_handle))
    count = c_int(0)
    status = mt_image_detect.detect_image(handle, byref(image_data), 0, width, height, width * 3, byref(result_handle), pointer(count))
    print('detect_image status: ' + str(status) + " count : " + str(count.value))
    detect_content = result_handle.contents

    针对于二级指针,必须POINTER(detection_result)生成T*,然后创建result_handle = TARGETPOINTER_t(),然后通过byref(result_handle)得到二级指针

  • byref(n)返回的相当于C的指针右值&n,本身没有被分配空间;
  • pointer返回的相当于指针左值T* p=&n,可以改变,可以取地址; POINTER得到是类;
  • /home/sensetime/miniconda3/envs/pythonPIL/bin/python /home/sensetime/jayzwang/workspace/clion_workspace/PyImageTest/image_test.py
    copy ../CvImageTest/build/libmtimage.so->./extents/libs/libmtimage.so
    copy ../CvImageTest/mt_image_common.h->./extents/include/mt_image_common.h
    copy ../CvImageTest/mt_image_detect.h->./extents/include/mt_image_detect.h
    test license
    load_library lib is Exist : True
    /home/sensetime/jayzwang/workspace/clion_workspace/PyImageTest
    mt_image_detect_init_config.14:  in
    init_license : 0
    mt_image_detect_create.24:  in
    create_handle : 0 handle : c_long(94128605088976)
    pil image : 768 height : 576
    width : 768 height : 576 format : None
    image pointer : <cparam 'P' (0x559c061f1960)> image_date [-1] : 255
    result_handle: <__main__.LP_detection_result object at 0x7fd92de1d1e0>
    mt_image_detect_compact.62:  in
    mt_image_detect_compact.75: mt_image_detect_compact : 0x559c060ce080
    detect_image status: 0 count : 1
    detect result left : 20
    detect result label: 1
    detect result points: 1
    mt_image_detect_reset.82:  in
    reset_handle status: 0
    mt_image_release_detect_result.46:  in
    mt_image_detect_destroy.34:  in
    destroy_handle status: 0 handle : c_long(94128605088976)
    
    def movefile(srcpath, dstpath):
        if not os.path.isfile(srcpath):
            print(srcpath + ' is not exist!')
        else:
            fpath, fname = os.path.split(dstpath)
            if not os.path.exists(fpath):
                os.makedirs(fpath)
            shutil.copy(srcpath, dstpath)
            print('copy ' + srcpath + '->' + dstpath)
    hand_image = Image.open('./extents/test_image/timg.jpeg')
    hand_image = hand_image.convert('RGB')
    width, height = hand_image.size
    image_format = hand_image.format
    image_data = (c_ubyte * (width * height * 3))()
    print('pil image : ' + str(width) + " height : " + str(height))
    # hand_image.show()
    for x in range(height):
        for y in range(width):
            r, g, b = hand_image.getpixel((y, x))
            #bgr = b, g, r
            image_data[(x * width + y)*3] = b
            image_data[(x * width + y)*3 + 1] = g
            image_data[(x * width + y)*3 + 2] = r