You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Jetson Nano上如何用Python OpenCV结合nvvidconv同时执行两种帧翻转?

问题

根据《Accelerated_GStreamer_User_Guide_Release_24.2.1》文档,nvvidconv提供7种帧翻转方法。我希望同时执行nvvidconv flip-method=4和nvvidconv flip-method=6操作,请问在Jetson Nano上如何用Python结合cv2编写对应的GStreamer管道?

现有代码:

import cv2

width = 640
height = 480
framerate = 30

gs_pipeline = f"v4l2src device=/dev/video0 io-mode=2 " \
              f"! image/jpeg, width={width}, height={height}, framerate={framerate}/1, format=MJPG " \
              f"! nvv4l2decoder mjpeg=1 " \
              f"! nvvidconv flip-method=[4, 6]  " \
              f"! video/x-raw, format=BGRx " \
              f"! videoconvert " \
              f"! video/x-raw, format=BGR " \
              f"! appsink drop=1"

gs_pipeline = gs_pipeline
print(f"gst-launch-1.0 {gs_pipeline}\n")

v_cap = cv2.VideoCapture(gs_pipeline, cv2.CAP_GSTREAMER)
if not v_cap.isOpened():
    print("failed to open video capture")
    v_cap.release()
    exit(-1)

while v_cap.isOpened():
    ret_val, frame = v_cap.read()
    if not ret_val:
        break

    cv2.imshow('', frame)

    input_key = cv2.waitKey(1)
    if input_key != -1:
        print(f"input key = {input_key}")

    if input_key == ord('q'):
        break

v_cap.release()
cv2.destroyAllWindows()
解答

nvvidconv的flip-method参数一次只能设置一个值,无法通过[4,6]这种方式同时执行两种翻转操作。要实现同时应用两种翻转,需要在GStreamer管道中串联两个nvvidconv元素,分别设置对应的flip-method参数。

修改后的管道说明

  • 第一个nvvidconv flip-method=4:先执行第一种翻转(对应文档中"旋转180度")
  • 第二个nvvidconv flip-method=6:再执行第二种翻转(对应文档中"逆时针旋转90度并垂直翻转")
  • 后续的格式转换和appsink部分保持不变,确保输出能被OpenCV正常读取。

完整代码

import cv2

width = 640
height = 480
framerate = 30

# 修改后的GStreamer管道:串联两个nvvidconv分别执行翻转
gs_pipeline = f"v4l2src device=/dev/video0 io-mode=2 " \
              f"! image/jpeg, width={width}, height={height}, framerate={framerate}/1, format=MJPG " \
              f"! nvv4l2decoder mjpeg=1 " \
              f"! nvvidconv flip-method=4 " \
              f"! nvvidconv flip-method=6 " \
              f"! video/x-raw, format=BGRx " \
              f"! videoconvert " \
              f"! video/x-raw, format=BGR " \
              f"! appsink drop=1"

print(f"gst-launch-1.0 {gs_pipeline}\n")

v_cap = cv2.VideoCapture(gs_pipeline, cv2.CAP_GSTREAMER)
if not v_cap.isOpened():
    print("failed to open video capture")
    v_cap.release()
    exit(-1)

while v_cap.isOpened():
    ret_val, frame = v_cap.read()
    if not ret_val:
        break

    cv2.imshow('', frame)

    input_key = cv2.waitKey(1)
    if input_key != -1:
        print(f"input key = {input_key}")

    if input_key == ord('q'):
        break

v_cap.release()
cv2.destroyAllWindows()

注意事项

  • 翻转的顺序会影响最终效果,若需要调整,交换两个nvvidconv的顺序即可。
  • 确保Jetson Nano的L4T版本与文档版本匹配,避免nvvidconv参数兼容性问题。

内容的提问来源于stack exchange,提问作者Jet C.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 05:27:25