2

Hi iam working on an AI project, i have coded a python code which will train and load the model and give prediction, for performance sake i coded the rest of back-end with c++.

I thought of re-write the whole python code into c++ but havent managed to do it correctly, so i figured out that it would be way easier if i let the python code run as a server, and c++ code will run as client, c++ client will feed python server an image and server will return c++ client with prediction image, the whole process will be in the same system.

Normally i would go for socket but since both server and client is in the same system, i think there must be a better solution.Iam working on ubuntu.

TLDT;What is the fastest approach for data (images) exchange between two programs in the same system (ubuntu OS)

Minh Cht
  • 305
  • 2
  • 5
  • 13
  • You’ll have to define what “best” means here because all the different methods have their pros and cons. Nothing wrong with using sockets but pipes might be “better” in some situations. – Sami Kuhmonen Dec 23 '19 at 07:18
  • Okay you are right, i will edit my question, thank you for making the question better – Minh Cht Dec 23 '19 at 07:24
  • Consider using Redis, then you can have multiple clients and servers working in parallel across many machines. It is very simple and very fast. You can also easily have insights into it via the command-line for debugging. Clients are available for Python, C++, PHP, Ruby, bash... – Mark Setchell Dec 23 '19 at 07:32
  • Examples... https://stackoverflow.com/a/58521903/2836621 and https://stackoverflow.com/a/57910157/2836621 and https://stackoverflow.com/a/55313342/2836621 – Mark Setchell Dec 23 '19 at 07:34
  • Thank you very much for the examples i will look into it – Minh Cht Dec 23 '19 at 07:35

1 Answers1

0

One option is to use an in-memory database, such as Redis, to exchange images between the two applications. For example, the following C++ program reads an image from a file using OpenCV, and writes it to key image in Redis:

#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();
}

Then your Python program can access the image:

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()

The C++ example above uses the cpp_redis library available at https://github.com/cpp-redis/cpp_redis.

Velimir Mlaker
  • 10,664
  • 4
  • 46
  • 58