gpt4 book ai didi

python - 我正在使用 Redis 来替换 ROS,这很奇怪吗?

转载 作者:可可西里 更新时间:2023-11-01 11:23:18 27 4
gpt4 key购买 nike

我正在使用 Redis 在两个 Python 程序之间交换数据。详细方式如下。我有两个 python 文件,cam.pyprocess.pycam.py从摄像头读取图片并存储到Redis,process.py从Redis读取图片并处理。
cam.py:

"""
This file read the camera and save images into Redis
"""
import cv2
import redis, struct


def imToRedis(r, a, n):
"""Store given Numpy array 'a' in Redis under key 'n'"""
h, w = a.shape[:2]
shape = struct.pack('>II',h,w)
encoded = shape + a.tobytes()

# Store encoded data in Redis
r.set(n,encoded)


RedisPool = redis.Redis(host='localhost', port=6379, db=0)

cap = cv2.VideoCapture(0)
key = 0
while key != 27:
ret, img = cap.read()
imToRedis(RedisPool, img, 'img')
flagProcessed = RedisPool.set('flagProcessed', 0)

cv2.imshow('cam', img)
key = cv2.waitKey(1) & 0xFF

进程.py:

"""
This file read image from redis and process
"""
import cv2
import redis, struct
import numpy as np


def imFromRedis(r, n):
"""Read image from redis"""
encoded = r.get(n)
h, w = struct.unpack('>II', encoded[:8])
data = np.frombuffer(encoded, dtype=np.uint8, offset=8).reshape(h, w, 3)

return data

RedisPool = redis.Redis(host='localhost', port=6379, db=0)

key = 0
while key != 27:
flagProcessed = int(RedisPool.get('flagProcessed'))
if not flagProcessed:
img = imFromRedis(RedisPool, 'img')

cv2.imshow('process', img)

RedisPool.set('flagProcessed', 1)
key = cv2.waitKey(1) & 0xFF

据我所知,还有一个名为 Robot Operating System 的软件包在做同样的事情。但我现在正在使用 Redis 来做这件事。
我的问题是:

  1. 这样使用Redis合适吗?
  2. 我应该直接使用 ROS 还是在 python 下有更好的方法吗?

最佳答案

我在github上找到了一个项目:jpolin/redisBenchmark .在那里,作者比较了 Redis 和 ROS 中发布/订阅功能的延迟,结果是 Redis 比 ROS 快。
所以我得出结论,我们可以使用 Redis 中的发布/订阅功能来替代 ROS。

关于python - 我正在使用 Redis 来替换 ROS,这很奇怪吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57404555/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com