soma0sd

코딩 & 과학 & 교육

NVIDIA Jetson 동영상 객체인식 - Python

반응형

이 예제는 기본 제공 모델에 포함되어 있는 ssd-mobilenet-v2를 함께 설치하는 젯슨 추론도구 설치 과정을 마친 후에 실행할 수 있습니다.

작업 공간에 샘플 비디오 다운로드

우선 작업을 위한 디렉토리를 하나 생성하고 그곳에서 인텔이 제공하는 AI 추론 테스트를 위한 샘플 비디오를 작업 공간에 내려받습니다.

git clone https://github.com/intel-iot-devkit/sample-videos

VSCode를 이용하여 원격 개발을 하는 경우 새로운 디렉토리를 열 때 code ~/Work와 같은 방식으로 터미널 명령을 통해서 열 수 있습니다.

Python 스크립트

"""Jetson Detectnet Test

샘플 비디오의 객체인식 결과를 출력하는 프로그램
"""
import glob
import logging
import os

# jetson-inference 모듈  
import jetson.inference
import jetson.utils

# 결과를 보여줄 창을 초기화
display = jetson.utils.videoOutput("display://0")
# 객체인식을 수행할 DetectNet을 초기화
net = jetson.inference.detectNet("ssd-mobilenet-v2", threshold=0.4)

running = True  # 사용자의 의도로 종료했는지 판단하는 플래그
for path in glob.glob("sample-videos/*.mp4"):
    # 샘플 비디오 디렉토리 내부에 있는 mp4 파일을 비디오 소스로 초기화
    source = jetson.utils.videoSource(f"{os.path.abspath(path)}")
    source.Open()
    if not running:
        break
    while True:
        if not display.IsStreaming():
            logging.info("display has not streaming")
            running = False
            break
        if not source.IsStreaming():
            logging.info("source has not streaming")
            break
        # 소스로부터 프레임 이미지를 추출
        cuda_img = source.Capture()
        # 이미지 객체인식 수행
        detections = net.Detect(cuda_img)
        # 결과창에 이미지 표시
        display.Render(cuda_img)
        # 결과창의 제목표시줄을 업데이트
        display.SetStatus(
            "Object Detection | Network {:.0f} FPS".format(net.GetNetworkFPS())
        )
    source.Close()
display.Close()

스크립트에 대한 설명은 주석을 참고하세요.

반응형
태그:

댓글

End of content

No more pages to load