0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Jetson Orin Nano SuperでYOLO11(v.8.3.200)を使い物体追跡(MOT)し、Flaskで配信する

Last updated at Posted at 2025-10-19
  • Jetson Orin Nano Super 8GB
  • logicool C270N
  • SanDisk Extreme PRO 64GB micro SDXC A2
  • JetPack 6.2.1

パッケージ一覧の更新、パッケージ操作の再設定、pipのインストール

sudo apt update
sudo dpkg --configure -a
sudo apt install python3-pip

torchのインストール

pip3 install --no-deps torch-2.3.0-cp310-cp310-linux_aarch64.whl 
pip3 install --no-deps torchaudio-2.3.0+952ea74-cp310-cp310-linux_aarch64.whl 
pip3 install --no-deps torchvision-0.18.0a0+6043bc2-cp310-cp310-linux_aarch64.whl 

その他ライブラリのインストール

pip3 install psutil==5.9.5
pip3 install typing_extensions==4.12.2
pip3 install tqdm==4.66.4

YOLO11をインストールと動作確認

pip3 install --no-deps ultralytics-8.3.200-py3-none-any.whl
reboot
yolo predict model=yolo11n.pt source="https://ultralytics.com/images/zidane.jpg"

snapdのダウングレード

cd /tmp
snap download snapd --revision=24724
sudo snap ack snapd_24724.assert 
sudo snap install snapd_24724.snap 
sudo snap refresh --hold snapd

Chromiumブラウザのインストール

sudo apt install -y chromium-browser

YOLO初回実行時にインストールされるnumpy 2.2.6を1.21.5にダウングレード

pip3 uninstall numpy
pip3 install numpy==1.21.5

FlaskとOpenCVのインストール

pip3 install flask
pip3 install opencv-python==4.8.0.74

配信プログラム

app.py
from flask import Flask, render_template, Response  # Flaskフレームワークのインポート
from camera import Camera

app = Flask(__name__)  # Flaskインスタンスの作成


@app.route("/")  # ルート アクセスの処理
def index():
    return "Hello World!"  # 文字列をブラウザへ返す


@app.route("/stream")  # streamアドレスへの アクセス処理
def stream():
    return render_template("stream.html")  # htmlを返す


def gen(camera):  # cameraを受け取り
    while True:
        frame = camera.get_frame()  # 1フレーム取得

        if frame is not None:  # フレームがあれば
            yield (b"--frame\r\n"  # 画像データを返す
                   b"Content-Type: image/jpeg\r\n\r\n" + frame.tobytes() +
                   b"\r\n")
        else:  # フレームが無ければ
            print("frame is none")


@app.route("/video_feed")
def video_feed():
    return Response(gen(Camera()),
                    mimetype="multipart/x-mixed-replace; boundary=frame")


if __name__ == "__main__":
    app.debug = True
    app.run(host="0.0.0.0", port=5000)  # どのIPの5000番からも受け付ける
camera.py
import cv2
from ultralytics import YOLO


class Camera(object):  # Cameraクラスの定義
    def __init__(self):  # インスタンス作成時
        global model
        model = YOLO('yolo11n.pt')  # 学習済みモデルをロード
        self.video = cv2.VideoCapture(0)  # カメラの起動

    def __del__(self):  # インスタンス削除時
        self.video.release()

    def get_frame(self):  # 1フレームの取得
        success, image = self.video.read()

        results = model.track(image, persist=True, verbose=False, conf=0.5)

        annotated_image = results[0].plot()  # トラッキング結果をフレームに描画

        items = results[0]  # 複数の物体情報を取得

        for item in items:  # 1つ取得し
            cls = int(item.boxes.cls)   # クラスIDを取得
            label = item.names[int(cls)]    # クラスIDからラベル名を取得

            score = item.boxes.conf.cpu().numpy()[0]    # 信頼度を取得

            x1, y1, x2, y2 = item.boxes.xyxy.cpu().numpy()[0]  # バウンディングボックスの座標を取得

            id_value = item.boxes.id  # トラッキングIDを取得 存在しない場合はNone
            if id_value is None:    # トラッキングIDが存在しないなら空文字
                track_ids = ''
            else:   # 存在すればIDを取得
                track_ids = item.boxes.id.int().cpu().tolist()[0]

            print(str(cls), str(label), str(score), str(track_ids), str(x1), str(y1), str(x2), str(y2))

        ret, frame = cv2.imencode('.jpg', annotated_image)  # jpg形式に変換
        return frame  # データを返す
templates/stream.html
<html>
  <body>
    <img src="{{ url_for('video_feed') }}">
  </body>
</html>

Webサーバを起動し、アクセスする

python3 app.py
http://127.0.0.1:5000/stream
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?