みらいテックラボ

音声・画像認識や機械学習など, 週末プログラマである管理人が興味のある技術の紹介や実際にトライしてみた様子などメモしていく.

Jetson AGX Xavierを試してみる(2)

NVIDIA社のJetson AGX Xavierを触る機会を得たので, 環境設定していろいろと試し始めている.

今回は, jetson-inferenceのサンプルを試していて, 一部正常に動作しないところがあったので, 暫定対応の方法などをメモしておく.


[関連記事]
Jetson AGX Xavierを試してみる(1)
・Jetson AGX Xavierを試してみる(2)


1. 不具合の内容
不具合の内容ですが, jetson-inference/python/exsamplesのサンプルを動かしていて, 物体検出結果を画面に表示しようとしたが, 表示されないというもの.
前回ファイルへの出力は試していて, そちらは特に問題なさそうだった.

$ ./detectnet.py data/video/Cars-1280x720.mp4 display://0

注) データは, 以下のサイトからダウンロードしたものを使用している.
600以上の無料車&道路動画、HD&4Kクリップ - Pixabay

ログを確認すると, X11 Windowの作成で失敗しているようだ.
[ログ]

  (省略)  
  -- zeroCopy:   true
  -- flipMethod: none
  -- loop:       0
  -- rtspLatency 2000
------------------------------------------------
[OpenGL] glDisplay -- X screen 0 resolution:  1920x1080
[OpenGL] glDisplay -- X window resolution:    1920x1080
[OpenGL] failed to create X11 Window.
jetson.utils -- no output streams, creating fake null output
[gstreamer] opening gstDecoder for streaming, transitioning pipeline to GST_STATE_PLAYING
[gstreamer] gstreamer changed state from NULL to READY ==> mysink
[gstreamer] gstreamer changed state from NULL to READY ==> capsfilter1
   :

※ Jetson Nanoでも同様の確認をしたら, こちらは問題なく動作していた!!


2. 暫定対応

jetson-inferece/python/examples/my-detection.pyをベースに, 暫定対応の方法を2つ紹介する.
元のコードは以下の通り.
[コード]

import jetson.inference
import jetson.utils

net = jetson.inference.detectNet("ssd-mobilenet-v2", threshold=0.5)
camera = jetson.utils.videoSource("./data/videos/Cars-1280x720.mp4")      # '/dev/video0' for V4L2
display = jetson.utils.videoOutput("display://0") # 'my_video.mp4' for file

while display.IsStreaming():
    img = camera.Capture()
    detections = net.Detect(img)
    display.Render(img)
    display.SetStatus("Object Detection | Network {:.0f} FPS".format(net.GetNetworkFPS()))


2. 1 方法1
まずは, OpenCVを併用する方法.
画面への表示ができないので, 画像を扱う部分はOpenCVを使い, 物体検出の部分はそのまま利用する方法である.

[コード]

import numpy as np
import cv2

import jetson.inference
import jetson.utils

net = jetson.inference.detectNet('ssd-mobilenet-v2', threshold=0.5)
cap = cv2.VideoCapture("./data/videos/Cars-1280x720.mp4")
while cap.isOpened():
    ret, frame = cap.read()
    h, w, _ = frame.shape
    img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2RGBA).astype(np.float32)
    img = jetson.utils.cudaFromNumpy(img)        
    detections = net.Detect(img)
    img = jetson.utils.cudaToNumpy(img, w, h, 4)
    img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB).astype(np.uint8)
    frame = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    cv2.imshow('Object Detection', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

[実行結果]


2. 2 方法2[1]
OpenCVで暫定対応した後も, ネットで不具合について調査していたら, jetson-inferenceのissues #619に暫定対応の方法が記載されていた.

非常に簡単な方法で, "import jetsoon.inference"を"jetson.utils.videoOutput()の後ろで行うというもの.

[コード]

import jetson.utils

camera = jetson.utils.videoSource("./data/videos/Cars-1280x720.mp4")      # '/dev/video0' for V4L2
display = jetson.utils.videoOutput("display://0") # 'my_video.mp4' for file

# import位置を変更
import jetson.inference
net = jetson.inference.detectNet("ssd-mobilenet-v2", threshold=0.5)
# ここまで

while display.IsStreaming():
    img = camera.Capture()
    detections = net.Detect(img)
    display.Render(img)
    display.SetStatus("Object Detection | Network {:.0f} FPS".format(net.GetNetworkFPS()))

[実行結果]

"jetson.inference"のimport位置及び機種によって発生する不具合ということもあり, 根本的な対応は簡単にはできそうにない.
当面は, 方法2で回避するのがよさそうだ!!

----
参照URL:
[1] DEV Branch | [OpenGL]failed to create X11 Window. | jetson.utils -- no output streams, creating fake null output | videoSource API #619