Ubuntu
Ubuntu OS에서 환경 설정 및 라이브러리 설치 방법을 알아봅니다.
Goal
- Ubuntu에서 NuFiStreamer 설치하기
- NuFiStreamer가 설치된 환경에서 제공된 example code 실행하기
Ubuntu에서 NuFiStreamer 설치하기
1. Gstreamer build를 위해 필요한 패키지들을 설치합니다.
sudo apt install --no-install-recommends libglib2.0-dev libgtk2.0-dev gir1.2-gstreamer-1.0
sudo apt build-dep python3-gst-1.0
2. Gstreamer를 build합니다.(apt로도 설치가 가능하나 해당 버전에 문제가 있어서 직접 빌드합니다.)
cd ~
git clone <https://github.com/GStreamer/gstreamer.git>
cd gstreamer
meson build -Dgst-plugins-ugly:x264=enabled -Dgpl=enabled
ninja -C build
2-1. 빌드가 실패할 경우 해당 Github 레포 참고 또는 설치가 안된 라이브러리 추가 후 재빌드
rm -rf build
meson build -Dgst-plugins-ugly:x264=enabled -Dgpl=enabled
ninja -C build
3. gstreamer 가상환경을 실행합니다.
cd ~/gstreamer
./gst-env.py
4. NuFiStreamer를 설치합니다.
python3 -m pip install nufi
5. Pipeline example code 실행하여 환경설정이 여부를 확인합니다.
- video_path와 결과를 저장할 result_file_path를 argument로 넣어 run_sdstreamer 함수를 실행해봅니다.
import logging as log
import os
import sys
from gi.repository import GLib, Gst
import nufi
from nufi.models import Yolo, BaseModel
nufi.init()
def bus_call(_, message, loop):
t = message.type
if t == Gst.MessageType.EOS:
log.info("End-of-stream")
loop.quit()
elif t == Gst.MessageType.WARNING:
err, debug = message.parse_warning()
log.warning("Warning: %s: %s", err, debug)
elif t == Gst.MessageType.ERROR:
err, debug = message.parse_error()
log.error("Error: %s: %s", err, debug)
loop.quit()
return True
def run_sdstreamer(video_path, result_file_path):
# 1. Elements 생성
filesrc = Gst.ElementFactory.make("filesrc", "filesrc")
decodebin = Gst.ElementFactory.make("decodebin", "decodebin")
videoconvert1 = Gst.ElementFactory.make("videoconvert", "videoconvert1")
queue = Gst.ElementFactory.make("queue", "queue")
# - nufi element 생성
nufi = Gst.ElementFactory.make("nufi", "yolo")
draw = Gst.ElementFactory.make("draw", "draw")
qos = Gst.ElementFactory.make("qos", "qos")
videoconvert2 = Gst.ElementFactory.make("videoconvert", "videoconvert2")
x264enc = Gst.ElementFactory.make("x264enc", "x264enc")
mp4mux = Gst.ElementFactory.make("mp4mux", "mp4mux")
sink = Gst.ElementFactory.make("filesink", "filesink")
elements = [
filesrc,
decodebin,
videoconvert1,
queue,
nufi,
draw,
qos,
videoconvert2,
x264enc,
mp4mux,
sink,
]
if not all(elements):
log.error("Unable to create Elements")
sys.exit(-1)
# 2. Elements properties 설정
filesrc.set_property("location", video_path)
videoconvert1.set_property("n-threads", 16)
videoconvert2.set_property("n-threads", 16)
sink.set_property("location", result_file_path)
# - nufi element models 설정
models = [Yolo() for _ in range(6)] # for using nufi element multi threading
nufi.set_property("model", models)
# 3. 파이프라인 생성
pipeline = Gst.Pipeline()
pipeline.add(*elements)
for i in range(len(elements) - 1):
if elements[i].name != "decodebin":
if not Gst.Element.link(elements[i], elements[i + 1]):
log.error(
"Elements %s and %s could not be linked.",
elements[i].get_name(),
elements[i + 1].get_name(),
)
sys.exit(-1)
loop = GLib.MainLoop()
bus = pipeline.get_bus()
bus.add_signal_watch()
bus.connect("message", bus_call, loop)
def on_demuxer_pad_added(_, pad):
if not pad.has_current_caps():
log.error("Pad %s has no caps, can't link", pad.get_name())
sys.exit(-1)
caps = pad.get_current_caps()
struct = caps.get_structure(0)
if struct.get_name().startswith("video"):
pad.link(videoconvert1.get_static_pad("sink"))
decodebin.connect("pad-added", on_demuxer_pad_added)
# 4. 파이프라인 시작
ret = pipeline.set_state(Gst.State.PLAYING)
if ret == Gst.StateChangeReturn.FAILURE:
log.error("Unable to set the pipeline to the playing state.")
sys.exit(-1)
try:
loop.run()
except Exception as e:
log.error("App finished : %s", e)
loop.quit()
pipeline.set_state(Gst.State.NULL)
return result_file_path