设为首页收藏本站

Jetson开发者

搜索
查看: 1995|回复: 0

如何利用Python在Jetson上抓取和显示摄像头视频(代码)

[复制链接]

127

主题

163

帖子

2003

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2003
发表于 2017-11-20 17:09:11 | 显示全部楼层 |阅读模式
Jetson TX2
源代码作者是放在:https://gist.github.com/jkjung-a ... da19f7bfa3cb7d2690e
因为顾虑到很多用户网络访问不了,我将代码贴在这里:
tegra-cam.py
  1. # --------------------------------------------------------
  2. # Camera sample code for Tegra X2/X1
  3. #
  4. # This program could capture and display video from
  5. # IP CAM, USB webcam, or the Tegra onboard camera.
  6. # Refer to the following blog post for how to set up
  7. # and run the code:
  8. #   https://jkjung-avt.github.io/tx2-camera-with-python/
  9. #
  10. # Written by JK Jung <jkjung13@gmail.com>
  11. # --------------------------------------------------------

  12. import sys
  13. import argparse
  14. import cv2
  15. import numpy as np

  16. windowName = "CameraDemo"

  17. def parse_args():
  18.     """
  19.     Parse input arguments
  20.     """
  21.     parser = argparse.ArgumentParser(description=
  22.                                      "Capture and display live camera video on Jetson TX2/TX1")
  23.     parser.add_argument("--rtsp", dest="use_rtsp",
  24.                         help="use IP CAM (remember to also set --uri)",
  25.                         action="store_true")
  26.     parser.add_argument("--uri", dest="rtsp_uri",
  27.                         help="RTSP URI string, e.g. rtsp://192.168.1.64:554",
  28.                         default=None, type=str)
  29.     parser.add_argument("--latency", dest="rtsp_latency",
  30.                         help="latency in ms for RTSP [200]",
  31.                         default=200, type=int)
  32.     parser.add_argument("--usb", dest="use_usb",
  33.                         help="use USB webcam (remember to also set --vid)",
  34.                         action="store_true")
  35.     parser.add_argument("--vid", dest="video_dev",
  36.                         help="video device # of USB webcam (/dev/video?) [1]",
  37.                         default=1, type=int)
  38.     parser.add_argument("--width", dest="image_width",
  39.                         help="image width [1920]",
  40.                         default=1920, type=int)
  41.     parser.add_argument("--height", dest="image_height",
  42.                         help="image width [1080]",
  43.                         default=1080, type=int)
  44.     args = parser.parse_args()
  45.     return args

  46. def open_cam_rtsp(uri, width, height, latency):
  47.     gst_str = ("rtspsrc location={} latency={} ! rtph264depay ! h264parse ! omxh264dec ! "
  48.                "nvvidconv ! video/x-raw, width=(int){}, height=(int){}, format=(string)BGRx ! "
  49.                "videoconvert ! appsink").format(uri, latency, width, height)
  50.     return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)

  51. def open_cam_usb(dev, width, height):
  52.     # We want to set width and height here, otherwise we could just do:
  53.     #     return cv2.VideoCapture(dev)
  54.     gst_str = ("v4l2src device=/dev/video{} ! "
  55.                "video/x-raw, width=(int){}, height=(int){}, format=(string)RGB ! "
  56.                "videoconvert ! appsink").format(dev, width, height)
  57.     return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)

  58. def open_cam_onboard(width, height):
  59.     # On versions of L4T previous to L4T 28.1, flip-method=2
  60.     # Use Jetson onboard camera
  61.     gst_str = ("nvcamerasrc ! "
  62.                "video/x-raw(memory:NVMM), width=(int)2592, height=(int)1458, format=(string)I420, framerate=(fraction)30/1 ! "
  63.                "nvvidconv ! video/x-raw, width=(int){}, height=(int){}, format=(string)BGRx ! "
  64.                "videoconvert ! appsink").format(width, height)
  65.     return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)

  66. def open_window(windowName, width, height):
  67.     cv2.namedWindow(windowName, cv2.WINDOW_NORMAL)
  68.     cv2.resizeWindow(windowName, width, height)
  69.     cv2.moveWindow(windowName, 0, 0)
  70.     cv2.setWindowTitle(windowName, "Camera Demo for Jetson TX2/TX1")

  71. def read_cam(windowName, cap):
  72.     showHelp = True
  73.     showFullScreen = False
  74.     helpText = "'Esc' to Quit, 'H' to Toggle Help, 'F' to Toggle Fullscreen"
  75.     font = cv2.FONT_HERSHEY_PLAIN
  76.     while True:
  77.         if cv2.getWindowProperty(windowName, 0) < 0: # Check to see if the user closed the window
  78.             # This will fail if the user closed the window; Nasties get printed to the console
  79.             break;
  80.         ret_val, displayBuf = cap.read();
  81.         if showHelp == True:
  82.             cv2.putText(displayBuf, helpText, (11,20), font, 1.0, (32,32,32), 4, cv2.LINE_AA)
  83.             cv2.putText(displayBuf, helpText, (10,20), font, 1.0, (240,240,240), 1, cv2.LINE_AA)
  84.         cv2.imshow(windowName, displayBuf)
  85.         key = cv2.waitKey(10)
  86.         if key == 27: # ESC key: quit program
  87.             break
  88.         elif key == ord('H') or key == ord('h'): # toggle help message
  89.             showHelp = not showHelp
  90.         elif key == ord('F') or key == ord('f'): # toggle fullscreen
  91.             showFullScreen = not showFullScreen
  92.             if showFullScreen == True:
  93.                 cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
  94.             else:
  95.                 cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_NORMAL)

  96. if __name__ == "__main__":
  97.     args = parse_args()
  98.     print("Called with args:")
  99.     print(args)
  100.     print("OpenCV version: {}".format(cv2.__version__))

  101.     if args.use_rtsp:
  102.         cap = open_cam_rtsp(args.rtsp_uri, args.image_width, args.image_height, args.rtsp_latency)
  103.     elif args.use_usb:
  104.         cap = open_cam_usb(args.video_dev, args.image_width, args.image_height)
  105.     else: # by default, use the Jetson onboard camera
  106.         cap = open_cam_onboard(args.image_width, args.image_height)

  107.     if not cap.isOpened():
  108.         sys.exit("Failed to open camera!")

  109.     open_window(windowName, args.image_width, args.image_height)
  110.     read_cam(windowName, cap)
  111.    
  112.     cap.release()
  113.     cv2.destroyAllWindows()
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

快速回复 返回顶部 返回列表