使用c++调用yolov5完成推理工作

背景:使用c++调用python,完成yolov5的部署(遇到的问题以及解决方法见最后)

c++ 调用yolov5代码

#include<Python.h>
#include<iostream>
#include<string.h>
using namespace std;
int main(){
   
Py_SetPythonHome((wchar_t*)L"D:\\python\\py37\\"); //python env dir
Py_Initialize(); //before using python Call this function,for Initialize
PyRun_SimpleString("xxx"); //Import the libraries, which used in python.py
PyRun_SimpleString("xxx");
PyRun_SimpleString("xxx");
PyRun_SimpleString("sys.path.append(r'./')");//Set the location of the .py file (absolute path)

//声明变量
PyObject* pModule = NULL; //.py file
PyObject* pFunc = NULL;  //the function in .py file
PyObject* pParams = NULL; //the parameter of function
PyObject* pResult = NULL;  //Results returned by the function
// import .py file
pModule = PyImport_Import(PyUnicode_FromString("ttt")); //ttt is the .py's name
if (!pModule) {
   
		if (pModule == nullptr)
		{
   
			PyErr_Print();
			exit(1);
		}
		printf("Import Module failed!\n");
	}
pFunc = PyObject_GetAttrString(pModule, "run");   //Call the function(run) and return the calculation result
pParams = Py_BuildValue("(s)", "D:/data/images/bus.jpg");  //Assign the parameter to the Python function
//pParams = Py_BuildValue("(ii)", 1, 1);  // set the parameter for function, i means int, ii means two parmeters(int), s means string
//设置函数参数,i表示int整型,两个i表示有两个参数,s表示字符串等
pResult = PyObject_CallObject(pFunc, pParams);  //Call the function and return the calculation result

int res=0;
PyArg_Parse(pResult, "i", &res);   //Convert the return result to C ++ type
cout << "res:" << res << endl;
Py_Finalize();
}
# -*- coding: utf-8 -*-
#6.1
import sys
sys.path.insert(0, 'D:/project/yolov5-6.0/')
import cv2,torch,torchvision, time,os,random
import numpy as np

def plot_one_box(x, img, color=None, label=None, line_thickness=3):
    # Plots one bounding box on image img
    tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1  # line/font thickness
    color = color or [random.randint(0, 255) for _ in range(3)]
    c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
    cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
    if label:
        tf = max(tl - 1, 1)  # font thickness
        t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
        c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
        cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA)  # filled
        cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)
def xywh2xyxy(x):
    # Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right
    y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
    y[:, 0] = x[:, 0] - x[:, 2] / 2  # top left x
    y[:, 1] = x[:, 1] - x[:, 3] / 2  # top left y
    y[:, 2] = x[:, 0
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值