【Python】OpenCV を使って顔画像を検出してみた。
Contents
OpenCV のインストール
1 2 3 4 5 |
$ sudo add-apt-repository ppa:orangain/opencv $ sudo apt-get update $ sudo apt-get install -y python3-opencv opencv-data |
Numpy のインストール
1 |
$ sudo python3 -m pip install numpy |
以下のコマンドを実行して、エラーが出なければインストール成功です。
1 |
$ python -c 'import cv2' |
画像の中から顔部分を検出
Wikipedia から顔を含む画像をダウンロードします。
1 |
$ wget https://upload.wikimedia.org/wikipedia/commons/5/55/Grace_Hopper.jpg |
以下のスクリプトを作成して、実行します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import cv2 # 分類器オブジェクトを作成する # OpenCV に付属している classifier = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml') # 画像ファイルを読み込む image = cv2.imread('Grace_Hopper.jpg') # 顔を検出する faces = classifier.detectMultiScale(image) for x, y, w, h in faces: cv2.rectangle(image, (x,y), (x+w, y+h), color=(255,255,255), thickness=2) cv2.imwrite('faces.jpg', image) cv2.imshow('Faces', image) |
参考書籍
これらの内容は、以下の書籍を参照しました。
関連記事
-
-
【Python】Wikipedia のデータセットを取得し、文章を抽出する方法。
Wikipedia のデータセットを取得し、Python のライブラリを用いて文章を抽出する方法を紹
-
-
【Python】機械学習のために SciPy・Matplotlib・scikit-learn をインストール。
Python で機械学習を行うために、Windows OS の python 2 系にライブラリ「S
-
-
python 2.7 を Windows 64bit OS にインストールした。
python 2.7 を Windows にインストールしたときのメモです。 Python に
-
-
【Atomエディタ】Python 開発用にインストールしてみた。
Python でプログラミングするときの エディタ を探していたのですが、とりあえず Atom とい
-
-
【Pandas】 loc・ilocで1行のみ Series ではなく DataFrame で抽出する方法。
Python の Pandas で DataFrame から loc や iloc を使って行を抽出
-
-
【Spyder】引数のあるスクリプトを実行する方法。
Python の統合開発環境(IDE)である Spyder では、簡単にスクリプトを実行できます。
-
-
【Pandas】 DataFrame のある列の最大値を含む行のインデックス値を取得する方法。
今回は、Pandas の DataFrame において、ある列で最大値を求めて、その最大値をもつ行に
-
-
【Python】pip3 で「cannot import name ‘main’」エラーが出たときの対処法。
Python でライブラリをインストールする際に、pipを使います。 Python2 と Py
-
-
【Pandas】 DataFrame と Series のデータ構造について【Python】
今回は、Python でデータ分析を行っていると必ず使う Pandas の DataFrame と
-
-
【OpenCV】検出した顔画像部分を切り出す方法【Python】
OpenCV を使って、Python で画像の中から顔部分を切り出したときのメモです。 顔部分