1
0

More than 3 years have passed since last update.

Jetson Nanoを使って画像解析に挑戦

Posted at

Jetson Nanoについて

jetson Nanoは画像の分類、オブジェクトの検出、セグメンテーション、および音声処理のようなアプリケーションにおいて複数のニューラル ネットワークを並列に実行できる、小型なコンピューターとなっている。また、様々な機能がわずか20ワットの消費電力で稼働が可能となっている。

目標

Jetson Nanoにカメラを取り付け、そのカメラから取得した画像を解析しその結果をWebページに表示することを目標とする。

使用するもの

  • Jetson Nano
  • keres 2.2.5
  • django 3.0.4
  • tensorflow 1.14.0
  • OpenCV v3

実装

簡単に実装してみる
先ずテキトーにモデル作成
Colaboratoryで学習させておく

import os

from keras.applications.mobilenet_v2 import MobileNetV2
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import EarlyStopping, ModelCheckpoint

SIZE = 224
BATCH_SIZE = 256
EPOCH = 300

generator = ImageDataGenerator(
    rescale=1.0/255,
    horizontal_flip=True,
    brightness_range=(0.8, 1.0))
train_generator = generator.flow_from_directory(
    directory="resized",
    target_size=(SIZE, SIZE),
    batch_size=BATCH_SIZE,
    seed=0
)
num_classes = train_generator.num_classes

model = MobileNetV2(
    input_shape=(SIZE, SIZE, 3),
    alpha=0.5,
    weights=None,
    classes=num_classes)
model.compile(
    loss='categorical_crossentropy', optimizer="Adam", metrics=['accuracy'])

es = EarlyStopping(monitor="loss", patience=10)
best_path = "best.hdf5"
mc = ModelCheckpoint(best_path, monitor="loss", save_best_only=True)

history = model.fit_generator(
    train_generator,
    epochs=EPOCH,
    verbose=1,
    callbacks=[es, mc]
)
model.load_weights(best_path)

output_model_path = "/content/drive/My Drive/Colab Notebooks/best.hdf5"
model.save(output_model_path)

サンプルの人物準備
ここからjetson nanoのdjangoプロジェクト内の処理(views.py)

CLASSES = [
    'Aaron Eckhart',
    'Abhishek Bachan',
    'Adam Sandler',
    'Adriana Lima',
    'Alberto Gonzales',
    'Alec Baldwin',
    'Alex Rodriguez',
     .....
    ]

画像処理

from django.shortcuts import render
from keras.applications.mobilenet_v2 import MobileNetV2
import cv2
import numpy as np

def index(request):
    # モデルの読み込み
    model = MobileNetV2(
        input_shape=(224, 224, 3),
        alpha=0.5,
        weights=None,
        classes=201
    )
    model.load_weights("/home/hogehoge/django_prj/django_app/deep_learning/best_weights.hdf5")

    # 画像取得とリサイズ
    capture = cv2.VideoCapture(0)
    ret, frame = capture.read()
    frame = np.array(frame)[:, 280:1000]
    img = cv2.resize(frame, (224, 224))
    array = np.array(img).reshape(1, 224, 224, 3)

    # 予測
    pred = model.predict(array)
    alike = CLASSES[np.argmax(pred)]

    # 取得画像の保存
    cv2.imwrite("/home/hogehoge/django_prj/django_app/static/face_cnn/result.jpg", img)

    context = {"result":alike}
    return render(request, "face_cnn/index.html", context)

結果

カメラから画像取得
IMG_9597.JPG

結果画面
ノートpcのwebブラウザで確認
無題.png

Faith Hillと判定、、、
あれ、Faith Hill?

今後は精度を上げていきたいなー

参考

1
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
1
0