2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ラズパイ&OpenCVで顔検知

Last updated at Posted at 2020-08-01

#カメラ
使ったカメラはラズパイ用のカメラ
使い方は
https://www.itmedia.co.jp/news/articles/1907/13/news009.html
などを参考にしました。

#ラズパイにOpenCVをインストール

sudo apt-get update
sudo apt-get install libopencv-dev python-opencv

#Python用のライブラリをインストール

sudo pip install picamera
sudo pip3 install opencv-python

OpenCV git

git clone https://github.com/opencv/opencv.git

でダウンロードしたファイルの中にhaarcascadesというフォルダがあるので、それを今から作るpythonファイルと同じフォルダ内に移動させておく

#こんなコードを書いてみた

import picamera
import time
import cv2 as cv


while True:
    with picamera.PiCamera() as camera:
        camera.resolution = (512,384)
        time.sleep(2)
        camera.capture('test.jpg')
        print('capture')
        img = cv.imread('test.jpg')
        grayimg = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
        
        face_cascade = cv.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')
        facerect = face_cascade.detectMultiScale(grayimg,scaleFactor=1.2,minNeighbors=2,minSize=(1,1))
        print(facerect)
        
        if len(facerect)>0:
            print('face')

        time.sleep(2)

ラズパイのカメラモジュールは逆光に弱いようで、逆光をさけるようにすれば、カメラに顔が写れば'face'と反応しました。

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?