NCS2をラズベリーパイにつけて遊ぶメモ
OpenVINOをインストールしてサンプルプログラムで静止画の顔検出をした後の環境で動く。
静止画じゃなくて、リアルタイムで顔検出をしたとこのメモ
下のface_detection.pyを実行すると、OpenVINOのサンプルプログラムみたいな顔検出が、ラズベリーパイのカメラを使ってリアルタイムにできる。
【とりあえず買うもの】
@ラズベリーパイ3B+
ANCS2
Bラズベリーパイのカメラ
【NCS2の準備と静止画のサンプルプログラムを動かすとこまではこれ】
Install OpenVINO? toolkit for Raspbian* OS
https://docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_raspbian.html
【カメラの設定】
ラズベリーパイのカメラを付けて、/etc/video0にする
-モジュールのインストール--
sudo modprobe bcm2835-v4l2
/etc/modules にbcm2835-v4l2を追加してリブートすると/etc/video0ができる。
【リアルタイムで顔検出するプログラム】
-コード--
face_detection.py
------------------ import cv2 as cv import signal signal.signal(signal.SIGINT, signal.SIG_DFL) # Load the model. net = cv.dnn.readNet('face-detection-adas-0001.xml', 'face-detection-adas-0001.bin') # Specify target device. net.setPreferableTarget(cv.dnn.DNN_TARGET_MYRIAD) cap = cv.VideoCapture(0) while True: ret, frame = cap.read() blob = cv.dnn.blobFromImage(frame, size=(672, 384), ddepth=cv.CV_8U) net.setInput(blob) out = net.forward() # Draw detected faces on the frame. for detection in out.reshape(-1, 7): 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]) if confidence > 0.5: cv.rectangle(frame, (xmin, ymin), (xmax, ymax),color=(0, 255, 0)) cv.imshow('camera', frame) if cv.waitKey(1) > 0: break ---------------実行コマンド--
[ただの3B+のOpenCV] ------------------ 0.12894725799560547 0.17392373085021973 0.22026371955871582 0.21504449844360352 0.2494657039642334 0.23791289329528809 0.317767858505249 0.33171939849853516 0.3946549892425537 0.39640021324157715 0.38958096504211426 0.39200735092163086 0.4024956226348877 0.4194817543029785 0.41040968894958496 0.44701504707336426 0.4360315799713135 0.4048893451690674 0.40892791748046875 0.4139289855957031 0.41609764099121094 0.42462754249572754 0.408825159072876 0.4350752830505371 0.42213916778564453 -------------- [NCS2] ------------------ 7.149125576019287 ←1回目が遅いのでこれは対象外にした 0.12077951431274414 0.11380410194396973 0.11299777030944824 0.11402320861816406 0.11387085914611816 0.11324763298034668 0.11333894729614258 0.11316442489624023 0.11353659629821777 0.11388278007507324 0.1133580207824707 0.1132516860961914 0.11375117301940918 0.11354947090148926 0.11405372619628906 0.11321592330932617 0.11355710029602051 0.11284542083740234 0.11385655403137207 0.11326956748962402 0.11314940452575684 0.11264204978942871 0.11304712295532227 0.11284518241882324 0.1135244369506836 0.11256742477416992 0.1128699779510498 0.11285257339477539 0.11302304267883301 0.11344552040100098 0.11316156387329102 0.11319494247436523 0.11320996284484863 0.11323380470275879 0.11369013786315918 0.11309957504272461 0.11323952674865723 0.11301112174987793 0.11358094215393066 0.11409568786621094 0.11528587341308594 0.11307764053344727 --------------
------------------ import cv2 import time import signal signal.signal(signal.SIGINT, signal.SIG_DFL) ts = time.time() face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') cap = cv2.VideoCapture(0) while True: ret, img = cap.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) t1 = time.time() faces = face_cascade.detectMultiScale(gray) t2 = time.time() print(t2-t1) if(t2-ts) > 10: break for (x,y,w,h) in faces: cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0)) cv2.imshow('img',img) if cv2.waitKey(1) > 0: break cv2.destroyAllWindows() ---------------実行コマンド--
------------------ import cv2 as cv import time import signal signal.signal(signal.SIGINT, signal.SIG_DFL) ts = time.time() # Load the model. net = cv.dnn.readNet('face-detection-adas-0001.xml', 'face-detection-adas-0001.bin') # Specify target device. net.setPreferableTarget(cv.dnn.DNN_TARGET_MYRIAD) cap = cv.VideoCapture(0) while True: ret, frame = cap.read() blob = cv.dnn.blobFromImage(frame, size=(672, 384), ddepth=cv.CV_8U) net.setInput(blob) t1 = time.time() out = net.forward() t2 = time.time() print(t2-t1) if(t2-t1) > 5: ts = time.time() if(t2-ts) > 10: break # Draw detected faces on the frame. for detection in out.reshape(-1, 7): 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]) if confidence > 0.5: cv.rectangle(frame, (xmin, ymin), (xmax, ymax),color=(0, 255, 0)) cv.imshow('camera', frame) if cv.waitKey(1) > 0: break ---------------実行コマンド--