私的AI研究会 > OpenVINO4
インテルの学習済みモデルを使用する。
face-detection-retail-0004
pi@raspberrypi:~ $ cd workspace/FP16 pi@raspberrypi:~/workspace/FP16 $ wget --no-check-certificate https://download.01.org/opencv/2021/openvinotoolkit/2021.2/open_model_zoo/models_bin/3/face-detection-retail-0004/FP16/face-detection-retail-0004.bin --2020-12-28 17:01:57-- https://download.01.org/opencv/2021/openvinotoolkit/2021.2/open_model_zoo/models_bin/3/face-detection-retail-0004/FP16/face-detection-retail-0004.bin download.01.org (download.01.org) をDNSに問いあわせています... 2600:140b:d400:1ad::4b21, 2600:140b:d400:188::4b21, 23.42.230.170 : 2020-12-28 17:01:59 (923 KB/s) - `face-detection-retail-0004.bin' へ保存完了 [1176544/1176544] pi@raspberrypi:~/workspace/FP16 $ wget --no-check-certificate https://download.01.org/opencv/2021/openvinotoolkit/2021.2/open_model_zoo/models_bin/3/face-detection-retail-0004/FP16/face-detection-retail-0004.xml --2021-01-02 15:23:33-- https://download.01.org/opencv/2021/openvinotoolkit/2021.2/open_model_zoo/models_bin/3/face-detection-retail-0004/FP16/face-detection-retail-0004.xml download.01.org (download.01.org) をDNSに問いあわせています... 2600:140b:d400:1ad::4b21, 2600:140b:d400:188::4b21, 23.42.230.170 : 2021-01-02 15:23:34 (324 KB/s) - `face-detection-retail-0004.xml' へ保存完了 [100995/100995] pi@raspberrypi:~/workspace/FP16 $ ls -l 合計 6144 -rw-r--r-- 1 pi pi 4965014 12月 11 02:12 emotions-recognition-retail-0003.bin -rw-r--r-- 1 pi pi 37985 12月 11 02:12 emotions-recognition-retail-0003.xml -rw-r--r-- 1 pi pi 1176544 12月 11 02:14 face-detection-retail-0004.bin -rw-r--r-- 1 pi pi 100995 12月 11 02:14 face-detection-retail-0004.xml
Inputs name: "input" , shape: [1x3x300x300] - An input image in the format [BxCxHxW], where: B - batch size C - number of channels H - image height W - image width Expected color order - BGR.
# 画像サイズを300x300にする img = cv2.resize(img, (300, 300)) # HWCからCHWに変更 img = img.transpose((2, 0, 1)) # 大きさ1の次元を追加し4次元にする。省略OK img = np.expand_dims(img, axis=0)
# -*- coding: utf-8 -*- import cv2 import numpy as np # モジュール読み込み from openvino.inference_engine import IECore # モデルの読み込み ie = IECore() net = ie.read_network(model='FP16/face-detection-retail-0004.xml', weights='FP16/face-detection-retail-0004.bin') exec_net = ie.load_network(network=net, device_name="MYRIAD") # 入力画像読み込み img_face = cv2.imread('./image/photo4.jpg') # 入力データフォーマットへ変換 img = cv2.resize(img_face, (300, 300)) # サイズ変更 img = img.transpose((2, 0, 1)) # HWC > CHW img = np.expand_dims(img, axis=0) # 次元合せ # 推論実行 out = exec_net.infer(inputs={'data': img}) # 出力 print(out)
pi@raspberrypi:~/workspace $ python3 detection1.py {'detection_out': array([[[[0. , 1. , 0.89990234, ..., 0.17883301, 0.7060547 , 0.44335938], [0. , 1. , 0.08251953, ..., 0.4831543 , 0.44506836, 0.6064453 ], [0. , 1. , 0.04394531, ..., 0.52197266, 0.42236328, 0.5913086 ], ..., [0. , 0. , 0. , ..., 0. , 0. , 0. ], [0. , 0. , 0. , ..., 0. , 0. , 0. ], [0. , 0. , 0. , ..., 0. , 0. , 0. ]]]], dtype=float32)}
Outputs The net outputs a blob with shape: [1, 1, N, 7], where N is the number of detected bounding boxes. For each detection, the description has the format: [image_id, label, conf, x_min, y_min, x_max, y_max], where: ・image_id - ID of the image in the batch ・¥label - predicted class ID ・conf - confidence for the predicted class ・(x_min, y_min) - coordinates of the top left bounding box corner ・(x_max, y_max) - coordinates of the bottom right bounding box corner.
# 出力から必要なデータのみ取り出し out = out['detection_out'] out = np.squeeze(out) #不要な次元の削減
# 検出されたすべての顔領域に対して1つずつ処理 for detection in out: # conf値の取得 confidence = float(detection[2]) # バウンディングボックス座標を入力画像のスケールに変換 xmin = int(detection[3] * frame.shape[1]) ymin = int(detection[4] * frame.shape[0]) xmax = int(detection[5] * frame.shape[1]) ymax = int(detection[6] * frame.shape[0]) # conf値が0.5より大きい場合のみバウンディングボックス表示 if confidence > 0.5: cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), color=(240, 180, 0), thickness=3)
vi detection2.py # -*- coding: utf-8 -*- import cv2 import numpy as np # モジュール読み込み from openvino.inference_engine import IECore # モデルの読み込み ie = IECore() net = ie.read_network(model='FP16/face-detection-retail-0004.xml', weights='FP16/face-detection-retail-0004.bin') exec_net = ie.load_network(network=net, device_name="MYRIAD") # 入力画像読み込み frame = cv2.imread('./image/photo4.jpg') # 入力データフォーマットへ変換 img = cv2.resize(frame, (300, 300)) # サイズ変更 img = img.transpose((2, 0, 1)) # HWC > CHW img = np.expand_dims(img, axis=0) # 次元合せ # 推論実行 out = exec_net.infer(inputs={'data': img}) # 出力から必要なデータのみ取り出し out = out['detection_out'] out = np.squeeze(out) #サイズ1の次元を全て削除 # 検出されたすべての顔領域に対して1つずつ処理 for detection in out: # conf値の取得 confidence = float(detection[2]) # バウンディングボックス座標を入力画像のスケールに変換 xmin = int(detection[3] * frame.shape[1]) ymin = int(detection[4] * frame.shape[0]) xmax = int(detection[5] * frame.shape[1]) ymax = int(detection[6] * frame.shape[0]) # conf値が0.5より大きい場合のみバウンディングボックス表示 if confidence > 0.5: cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), color=(240, 180, 0), thickness=3) # 画像表示 cv2.imshow('frame', frame) # キーが押されたら終了 cv2.waitKey(0) cv2.destroyAllWindows()
vi detection3.py # -*- coding: utf-8 -*- import cv2 import numpy as np # モジュール読み込み from openvino.inference_engine import IECore # モデルの読み込み ie = IECore() net = ie.read_network(model='FP16/face-detection-retail-0004.xml', weights='FP16/face-detection-retail-0004.bin') exec_net = ie.load_network(network=net, device_name="MYRIAD") # カメラ準備 cap = cv2.VideoCapture(0) # メインループ while True: ret, frame = cap.read() # Reload on error if ret == False: continue # 入力データフォーマットへ変換 img = cv2.resize(frame, (300, 300)) # サイズ変更 img = img.transpose((2, 0, 1)) # HWC > CHW img = np.expand_dims(img, axis=0) # 次元合せ # 推論実行 out = exec_net.infer(inputs={'data': img}) # 出力から必要なデータのみ取り出し out = out['detection_out'] out = np.squeeze(out) #サイズ1の次元を全て削除 # 検出されたすべての顔領域に対して1つずつ処理 for detection in out: # conf値の取得 confidence = float(detection[2]) # バウンディングボックス座標を入力画像のスケールに変換 xmin = int(detection[3] * frame.shape[1]) ymin = int(detection[4] * frame.shape[0]) xmax = int(detection[5] * frame.shape[1]) ymax = int(detection[6] * frame.shape[0]) # conf値が0.5より大きい場合のみバウンディングボックス表示 if confidence > 0.5: cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), color=(240, 180, 0), thickness=3) # 画像表示 cv2.imshow('frame', frame) # 何らかのキーが押されたら終了 key = cv2.waitKey(1) if key != -1: break # 終了処理 cap.release() cv2.destroyAllWindows()
カメラ画像とほとんど同じ処理で動画ファイルからも顔検出ができる。
pi@raspberrypi:~/workspace $ cp detection2.py detect2-video.py pi@raspberrypi:~/workspace $ vi detect2-video.py # -*- coding: utf-8 -*- import cv2 import numpy as np # モジュール読み込み from openvino.inference_engine import IECore # モデルの読み込み ie = IECore() net = ie.read_network(model='FP16/face-detection-retail-0004.xml', weights='FP16/face-detection-retail-0004.bin') exec_net = ie.load_network(network=net, device_name="MYRIAD") # カメラ準備 filepath = "/home/pi/Videos/video-test.mp4" cap = cv2.VideoCapture(filepath) # メインループ while True: ret, frame = cap.read() # Reload on error if ret == False: continue # 入力データフォーマットへ変換 img = cv2.resize(frame, (300, 300)) # サイズ変更 img = img.transpose((2, 0, 1)) # HWC > CHW img = np.expand_dims(img, axis=0) # 次元合せ # 推論実行 out = exec_net.infer(inputs={'data': img}) # 出力から必要なデータのみ取り出し out = out['detection_out'] out = np.squeeze(out) #サイズ1の次元を全て削除 # 検出されたすべての顔領域に対して1つずつ処理 for detection in out: # conf値の取得 confidence = float(detection[2]) # バウンディングボックス座標を入力画像のスケールに変換 xmin = int(detection[3] * frame.shape[1]) ymin = int(detection[4] * frame.shape[0]) xmax = int(detection[5] * frame.shape[1]) ymax = int(detection[6] * frame.shape[0]) # conf値が0.5より大きい場合のみバウンディングボックス表示 if confidence > 0.5: cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), color=(240, 180, 0), thickness=3) # 画像表示 cv2.imshow('frame', frame) # 何らかのキーが押されたら終了 key = cv2.waitKey(1) if key != -1: break # 終了処理 cap.release() cv2.destroyAllWindows()