I want to share image between C++ and python using Redis. Now I have succeed in sharing numbers. In C++, I use hiredis as the client; in python, I just do import redis
. The code I have now is as below:
cpp to set value:
VideoCapture video(0);
Mat img;
int key = 0;
while (key != 27)
{
video >> img;
imshow("img", img);
key = waitKey(1) & 0xFF;
reply = (redisReply*)redisCommand(c, "SET %s %d", "hiredisWord", key);
//printf("SET (binary API): %s\n", reply->str);
freeReplyObject(reply);
img.da
}
python code to get value:
import redis
R = redis.Redis(host='127.0.0.1', port='6379')
while 1:
print(R.get('hiredisWord'))
Now I want to share opencv image instead of numbers. What should I do? Please help, thank you!
Instead of hiredis
, here's an example using cpp_redis
(available at https://github.com/cpp-redis/cpp_redis ).
The following C++ program reads an image from a file on disk using OpenCV, and stores the image in Redis under the 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();
}
Then in Python you grab the image from the database:
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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.