コンテンツにスキップ

Jetson Inference

豊富な実装サンプルがあるDustin-NVのRepoを参考にする。

https://github.com/dusty-nv/jetson-inference

git clone --recursive https://github.com/dusty-nv/jetson-inference
cd jetson-inference
docker/run.sh

Sample code

Recognition

import jetson.inference
import jetson.utils

net = jetson.inference.imageNet("googlenet")
font = jetson.utils.cudaFont()
camera = jetson.utils.gstCamera(1280, 720, "/dev/video0")
display = jetson.utils.glDisplay()

while display.IsOpen():
        img, width, height = camera.CaptureRGBA()
        class_idx, confidence = net.Classify(img, width, height)
        class_desc = net.GetClassDesc(class_idx)     
        font.OverlayText(img, width, height, "{:05.2f}% {:s}".format(confidence * 100, class_desc), 5, 5, font.White, font.Gray40)
        display.RenderOnce(img, width, height)
        display.SetTitle("{:s} | Network {:.0f} FPS".format(net.GetNetworkName(), net.GetNetworkFPS()))
        net.PrintProfilerTimes()

Segmentation

import jetson.inference
import jetson.utils
import ctypes

width = 1280
height = 720
cam = "/dev/video0"
net = jetson.inference.segNet("fcn-resnet18-voc")
net.SetOverlayAlpha(175.0)
img_overlay = jetson.utils.cudaAllocMapped(width * height * 4 * ctypes.sizeof(ctypes.c_float))
img_mask = jetson.utils.cudaAllocMapped(width/2 * height/2 * 4 * ctypes.sizeof(ctypes.c_float))

camera = jetson.utils.gstCamera(width, height, cam)
display = jetson.utils.glDisplay()

while display.IsOpen():
    img, width, height = camera.CaptureRGBA()
    net.Process(img, width, height, "void")
    net.Overlay(img_overlay, width, height, "linear")
    net.Mask(img_mask, width/2, height/2, "linear")

    display.BeginRender()
    display.Render(img_overlay, width, height)
    display.Render(img_mask, width/2, height/2, width)
    display.EndRender()

    display.SetTitle("Segmentation | Network {:.0f} FPS".format(net.GetNetworkFPS()))

Object detection

import jetson.inference
import jetson.utils

net = jetson.inference.detectNet("ssd-mobilenet-v2", threshold=0.5)
camera = jetson.utils.gstCamera(1280, 720, "/dev/video0")  # using V4L2
display = jetson.utils.glDisplay()

while display.IsOpen():
    img, width, height = camera.CaptureRGBA()
    detections = net.Detect(img, width, height)
    display.RenderOnce(img, width, height)
    display.SetTitle("Object Detection | Network {:.0f} FPS".format(net.GetNetworkFPS()))