嗨,我正在做一个AI项目,我编写了一个python代码,它将训练和加载模型并给出预测,出于性能考虑,我用c++编写了后端的其余部分。
我想把整个python代码重写到c++中,但没有成功,所以我想如果让python代码作为服务器运行,而c++代码将作为客户端运行,c++客户端将向python服务器提供图像,服务器将返回带有预测图像的c++客户端,那么整个过程将在同一个系统中进行。
通常我会选择socket,但由于服务器和客户端都在同一个系统中,我认为一定有更好的solution.Iam可以在ubuntu上运行。
在同一系统(ubuntu OS)中的两个程序之间交换数据(图像)的最快方法是什么?
发布于 2021-05-29 05:20:47
一种选择是使用内存中的数据库,例如Redis,在两个应用程序之间交换图像。例如,以下C++程序使用OpenCV从文件中读取图像,并将其写入Redis中的key image:
#include <opencv4/opencv2/opencv.hpp>
#include <cpp_redis/cpp_redis>
int main(int argc, char** argv)
{
cv::Mat image = cv::imread("input.jpg");
std::vector<uchar> buf;
cv::imencode(".jpg", image, buf);
cpp_redis::client client;
client.connect();
client.set("image", {buf.begin(), buf.end()});
client.sync_commit();
}然后,您的Python程序可以访问该图像:
import cv2
import numpy as np
import redis
store = redis.Redis()
image = store.get('image')
array = np.frombuffer(image, np.uint8)
decoded = cv2.imdecode(array, flags=1)
cv2.imshow('hello', decoded)
cv2.waitKey()上面的C++示例使用了https://github.com/cpp-redis/cpp_redis上提供的cpp_redis库。
https://stackoverflow.com/questions/59451447
复制相似问题