python-32位调用ncnn模型

import time
from ctypes import *
import cv2
import numpy as np
import numpy.ctypeslib as npct
import os

class Detector():
    def __init__(self, par,bin, dll_path):
        self.ncnn = CDLL(dll_path)
        self.ncnn.Detect.argtypes = [POINTER(c_ubyte), c_int, c_int, c_int,c_int,
                                       npct.ndpointer(dtype=np.float32, ndim=2, shape=(50, 6), flags="C_CONTIGUOUS")]
        self.ncnn.Init.argtypes = [c_void_p,c_void_p]
        self.ncnn.Init(par,bin)

    def predict(self, img):
        rows, cols = img.shape[0], img.shape[1]
        res_arr = np.zeros((50, 6), dtype=np.float32)
        self.ncnn.Detect(img.ctypes.data_as(POINTER(c_ubyte)), c_int(rows), c_int(cols), 352, 4, res_arr)
        self.bbox_array = res_arr[~(res_arr == 0).all(1)]
        return self.bbox_array

    def free(self):
        self.ncnn.ncnn_clear()


def visualize(img, bbox_array):
    for temp in bbox_array:
        bbox = [temp[0], temp[1], temp[2], temp[3]]  # xywh
        clas = int(temp[4])
        score = temp[5]
        cv2.rectangle(img, (int(temp[0]), int(temp[1])), (int(temp[2]), int(temp[3])),
                      (105, 237, 249), 2)
        img = cv2.putText(img, "class:" + str(clas) + " " + str(round(score, 2)), (int(temp[0]), int(temp[1]) - 5),
                          cv2.FONT_HERSHEY_SIMPLEX, 0.5, (105, 237, 249), 1)
    return img

p = os.getcwd()
det = Detector(par=str.encode(p) + b"\\FastestDet.param",bin=str.encode(p) + b"\\FastestDet.bin", dll_path=p + "\\fastDet.dll")  # b'' is needed

while True:

    img = cv2.imread("2.jpg")
    t1 = time.time()
    result = det.predict(img)
    img = visualize(img, result)
    print(time.time() - t1)
    cv2.imshow("img", img)
    cv2.waitKey(1)

    # det.free()


文件项目 会传资源下载  请自己查找

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微笑 ❶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值