当前位置: 首页 > news >正文

Qt Creator 通过python解释器调用*.py

全是看了大佬们的帖子,结合chatGPT才揉出来。在此做个记录。

  1. 安装python
  2. 在Qt Creator *.pro 文件中配置好环境
  3. 来个简单的example.py
  4. 调用代码
  5. 安装pip
  6. 添加opencv等库
  7. 调用包含了opencv库的py代码
  8. 成功

*.pro配置:

INCLUDEPATH += C:\Users\xuanm\AppData\Local\Programs\Python\Python312\include
LIBS      += -LC:\Users\xuanm\AppData\Local\Programs\Python\Python312\libs  -lpython312

example.py源码:

# example.py
def add(a, b):return a + b

Qt Creator里面调用example.py的C++函数:


int MainWindow::testPy()
{const wchar_t * pythonHome = L"C:/Users/xuanm/AppData/Local/Programs/Python/Python312";Py_SetPythonHome(pythonHome);// 初始化 Python 解释器Py_Initialize();// 添加目录到 Python 模块搜索路径PyObject* sysPath = PySys_GetObject("path");PyObject* pPath = PyUnicode_FromString("C:/temp");PyList_Append(sysPath, pPath);Py_DECREF(pPath);  // 释放引用// 导入 Python 脚本PyObject* pModule = PyImport_ImportModule("example");if (!pModule) {PyErr_Print();std::cerr << "Failed to load module." << std::endl;return 1;}// 获取函数对象PyObject* pFunc = PyObject_GetAttrString(pModule, "add");if (!pFunc || !PyCallable_Check(pFunc)) {PyErr_Print();std::cerr << "Failed to get function." << std::endl;return 1;}// 准备参数PyObject* pArgs = PyTuple_Pack(2, PyLong_FromLong(5), PyLong_FromLong(3));// 调用函数PyObject* pValue = PyObject_CallObject(pFunc, pArgs);if (pValue) {// 打印结果std::cout << "Result: " << PyLong_AsLong(pValue) << std::endl;Py_DECREF(pValue);} else {PyErr_Print();std::cerr << "Function call failed." << std::endl;}// 清理Py_XDECREF(pFunc);Py_XDECREF(pModule);Py_XDECREF(pArgs);// 结束 Python 解释器Py_Finalize();
}

含opencv库的*.py:

#-*- coding: utf-8 -*-import os.path
import copy
import cv2
import numpy as np# 椒盐噪声
def SaltAndPepper(src, percetage):SP_NoiseImg = src.copy()SP_NoiseNum = int(percetage * src.shape[0] * src.shape[1])for i in range(SP_NoiseNum):randR = np.random.randint(0, src.shape[0] - 1)randG = np.random.randint(0, src.shape[1] - 1)randB = np.random.randint(0, 3)if np.random.randint(0, 1) == 0:SP_NoiseImg[randR, randG, randB] = 0else:SP_NoiseImg[randR, randG, randB] = 255return SP_NoiseImg# 高斯噪声
def addGaussianNoise(image, percetage):G_Noiseimg = image.copy()w = image.shape[1]h = image.shape[0]G_NoiseNum = int(percetage * image.shape[0] * image.shape[1])for i in range(G_NoiseNum):temp_x = np.random.randint(0, h)temp_y = np.random.randint(0, w)G_Noiseimg[temp_x][temp_y][np.random.randint(3)] = np.random.randn(1)[0]return G_Noiseimg# 昏暗
def darker(image, percetage=0.9):image_copy = image.copy()w = image.shape[1]h = image.shape[0]# get darkerfor xi in range(0, w):for xj in range(0, h):image_copy[xj, xi, 0] = int(image[xj, xi, 0] * percetage)image_copy[xj, xi, 1] = int(image[xj, xi, 1] * percetage)image_copy[xj, xi, 2] = int(image[xj, xi, 2] * percetage)return image_copy# 亮度
def brighter(image, percetage=1.5):image_copy = image.copy()w = image.shape[1]h = image.shape[0]# get brighterfor xi in range(0, w):for xj in range(0, h):image_copy[xj, xi, 0] = np.clip(int(image[xj, xi, 0] * percetage), a_max=255, a_min=0)image_copy[xj, xi, 1] = np.clip(int(image[xj, xi, 1] * percetage), a_max=255, a_min=0)image_copy[xj, xi, 2] = np.clip(int(image[xj, xi, 2] * percetage), a_max=255, a_min=0)return image_copy# 旋转
def rotate(image, angle, center=None, scale=1.0):(h, w) = image.shape[:2]# If no rotation center is specified, the center of the image is set as the rotation centerif center is None:center = (w / 2, h / 2)m = cv2.getRotationMatrix2D(center, angle, scale)rotated = cv2.warpAffine(image, m, (w, h))return rotated# 翻转
def flip(image):flipped_image = np.fliplr(image)return flipped_imagedef work(file_dir):
# 图片文件夹路径for img_name in os.listdir(file_dir):img_path = file_dir + '\\' + img_nameimg = cv2.imread(img_path)# cv2.imshow("1",img)# cv2.waitKey(5000)# 旋转rotated_90 = rotate(img, 90)cv2.imwrite(file_dir +'\\'+ img_name[0:-4] + '_r90.jpg', rotated_90)# rotated_180 = rotate(img, 180)# cv2.imwrite(file_dir +'\\'+ img_name[0:-4] + '_r180.jpg', rotated_180)# for img_name in os.listdir(file_dir):#     img_path = file_dir + img_name#     img = cv2.imread(img_path)#     镜像#     flipped_img = flip(img)#     cv2.imwrite(file_dir +'\\' + img_name[0:-4] + '_fli.jpg', flipped_img)## # 增加噪声# img_salt = SaltAndPepper(img, 0.3)#   cv2.imwrite(file_dir + '\\' + img_name[0:-4] + '_salt.jpg', img_salt)#  img_gauss = addGaussianNoise(img, 0.3)#  cv2.imwrite(file_dir +'\\' +  img_name[0:-4] + '_noise.jpg', img_gauss)## # 变亮、变暗# img_darker = darker(img)# cv2.imwrite(file_dir +'\\' + img_name[0:-4] + '_darker.jpg', img_darker)# img_brighter = brighter(img)# cv2.imwrite(file_dir + '\\' + img_name[0:-4] + '_brighter.jpg', img_brighter)## blur = cv2.GaussianBlur(img, (7, 7), 1.5)# #      cv2.GaussianBlur(图像,卷积核,标准差)# cv2.imwrite(file_dir + img_name[0:-4] + '_blur.jpg', blur)

调用含opencv库的*.py C++函数:


int MainWindow::testYBZQ()
{const wchar_t * pythonHome = L"C:/Users/xuanm/AppData/Local/Programs/Python/Python312";Py_SetPythonHome(pythonHome);// 初始化 Python 解释器Py_Initialize();// 添加目录到 Python 模块搜索路径PyObject* sysPath = PySys_GetObject("path");PyObject* pPath = PyUnicode_FromString("C:/temp");PyList_Append(sysPath, pPath);Py_DECREF(pPath);  // 释放引用// 添加标准库路径PyObject* stdLibPath = PyUnicode_FromString("C:/Users/xuanm/AppData/Local/Programs/Python/Python312/libs");PyList_Append(sysPath, stdLibPath);Py_DECREF(stdLibPath);// 添加 site-packages 路径PyObject* sitePackagesPath = PyUnicode_FromString("C:/Users/xuanm/AppData/Local/Programs/Python/Python312/Lib/site-packages");PyList_Append(sysPath, sitePackagesPath);Py_DECREF(sitePackagesPath);// 导入 Python 脚本PyObject* pModule = PyImport_ImportModule("data_augmentation");if (!pModule) {PyErr_Print();std::cerr << "Failed to load module." << std::endl;return 1;}// 获取函数对象PyObject* pFunc = PyObject_GetAttrString(pModule, "work");if (!pFunc || !PyCallable_Check(pFunc)) {PyErr_Print();std::cerr << "Failed to get function." << std::endl;return 1;}// 准备参数std::string strParam = "C:/temp/sar_sample";PyObject* pStrParam = PyUnicode_FromString(strParam.c_str());PyObject* pArgs = PyTuple_Pack(1, pStrParam);// 调用函数PyObject* pValue = PyObject_CallObject(pFunc, pArgs);if (pValue) {// 打印结果std::cout << "Result: " << PyLong_AsLong(pValue) << std::endl;Py_DECREF(pValue);} else {PyErr_Print();std::cerr << "Function call failed." << std::endl;}// 清理Py_XDECREF(pFunc);Py_XDECREF(pModule);Py_XDECREF(pArgs);// 结束 Python 解释器Py_Finalize();
}

程序运行出了正确结果,报错:

Result: -1
TypeError: 'NoneType' object cannot be interpreted as an integer

再次感谢各位大佬的帖子,谢谢。


http://www.mrgr.cn/news/47105.html

相关文章:

  • 这几个被淘汰的Python库,请不要再用!
  • PostgreSQL学习笔记八:高级命令TRUNCATE TABLE
  • Harmony 线性布局对其方式说明
  • 【趣学Python算法100例】求车速
  • 如何选择适合网络安全工程师的编程语言进行深入学习?
  • 使用IDEA启动项目build时,解决Java编译时内存溢出问题:OutOfMemoryError深入解析
  • 编程题 7-17 爬动的蠕虫【PAT】
  • Tee Turtle 翻转玩偶和桌游火热维权,涉及上百张版权图
  • 美发店管理新思路:SpringBoot系统开发
  • 创建文件后未显示文件后缀
  • JVM发展历程
  • 小程序智能视频制作SDK解决方案,云端智能视频制作
  • 彻底理解TypeScript函数语法
  • AIStarter 3.0.2 全新发布:三大模式详解,共创共享共赢!
  • 基于 C# .NET Framework 4.0 开发实现 WCF 服务实例详解(二)——实现Windows服务内嵌WCF服务
  • 【Vue】Vue扫盲(二)指令:v-for 、v-if、v-else-if、v-else、v-show
  • 【力扣刷题实战】(顺序表)移除元素
  • 界面耻辱纪念堂--隐喻使用不当03
  • 国产长芯微LDC5791是一款单通道、20位、无缓冲电压输出DAC完全P2P替代AD5791
  • 如何提高LabVIEW编程效率