繁体   English   中英

使用Redis在C++和python之间共享Opencv图像

[英]Sharing Opencv image between C++ and python using Redis

我想使用 Redis 在 C++ 和 python 之间共享图像。 现在我已经成功地分享了数字。 在C++中,我使用hiredis作为客户端; 在 python 中,我只做import redis 我现在的代码如下:

cpp 设置值:

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代码:

import redis

R = redis.Redis(host='127.0.0.1', port='6379')

while 1:
    print(R.get('hiredisWord'))

现在我想共享 opencv 图像而不是数字。 我该怎么办? 请帮忙,谢谢!

这里不是hiredis ,而是使用cpp_redis的示例(可在https://github.com/cpp-redis/cpp_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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM