如何进一步优化拥有tensorflow+opencv+pyrealsense2的python代码

2024-04-23 23:08:28 发布

您现在位置:Python中文网/ 问答频道 /正文

当前状态

现在,我有一个python代码,它使用pyrealsense2cv2tensorflow来读取tensorflow模型。我已经用cythonized它而没有将.py更改为.pyx我没有用cdef更改任何变量,因为大多数变量都依赖于指定的库。你知道吗

我有一项关于如何使用上的cdef部分的研究,例如从pyrealsense2,但是这些来源让我一无所获。我只能找到一些cv2包装,但对我来说还不够。此外,我是cython的新人。所以,我也很难把它转换成cython。你知道吗

问题是

是否真的有可能优化和加速具有tensorflow + pyrealsense2 + cv2的代码?我确信我可以为cv2部分做些什么,但最重要的是尽可能减少模型的推理时间。你知道吗

示例和预期结果

我的目标是转化例如

pipeline = rs.pipeline()
config_rs = rs.config()
config_rs.enable_stream(rs.stream.depth, 848, 480, rs.format.z16, FRAME_RATE)
config_rs.enable_stream(rs.stream.color, 848, 480, rs.format.bgr8, FRAME_RATE)
frames = pipeline.wait_for_frames()
try:
    while True:


            depth_frame = frames.get_depth_frame()
            color_frame = frames.get_color_frame()

            if not depth_frame or not color_frame:
                continue
            depth_image = np.asanyarray(depth_frame.get_data())
            color_image = np.asanyarray(color_frame.get_data())
            x, y = sess.run([x_, y_], feed_dict={input_img: [depth_img]})

变成这样:

cdef "type of this" pipeline = rs.pipeline()
cdef "type of this" config_rs = rs.config()
config_rs.enable_stream(rs.stream.depth, 848, 480, rs.format.z16, FRAME_RATE)
config_rs.enable_stream(rs.stream.color, 848, 480, rs.format.bgr8, FRAME_RATE)
cdef "type of this" frames = pipeline.wait_for_frames()
try:
    while True:


            cdef "type of this" depth_frame = frames.get_depth_frame()
            cdef "type of this" color_frame = frames.get_color_frame()

            if not depth_frame or not color_frame:
                continue
            cdef "type of this" depth_image = np.asanyarray(depth_frame.get_data())
            cdef "type of this" color_image = np.asanyarray(color_frame.get_data())
            cdef "type of them" x, y = sess.run([x_, y_], feed_dict={input_img: [depth_img]})

进一步

首先,感谢您的帮助,我倾向于优化tensorflow + pyrealsense2部分,但我想听听其他类型优化的建议(可能我还没有读到它们)。你知道吗

注意:有些人可能会建议我使用TensorRT,但遗憾的是我使用的是Windows机器。你知道吗


Tags: ofconfigstreamgetframespipelinetypethis