Memo

メモ > サーバ > 各論: プログラミング > OpenCV(画像処理・画像解析・顔認識)を使う

■OpenCV(画像処理・画像解析・顔認識)を使う
■OpenCVのインストール
# git clone https://github.com/jayrambhia/Install-OpenCV # cd Install-OpenCV # cd RedHat # chmod +x * # ./opencv_latest.sh
2時間くらい?で
OpenCV 3.2.0 ready to be used
と表示された ■Facedetectのインストール
# yum -y install php-devel … phpizeを使えるようにする # git clone https://github.com/infusion/PHP-Facedetect # cd PHP-Facedetect # phpize && ./configure && make # make install # vi /etc/php.ini
extension=facedetect.so … 追加
# service httpd restart
■エラー回避 プログラムからface_detectを呼び出すと、以下のようなエラーが表示された OpenCV Error: Unspecified error (The node does not represent a user object (unknown type?)) in cvRead, file /root/Install-OpenCV/RedHat/OpenCV/opencv-3.2.0/modules/core/src/persistence.cpp, line 6628 https://sourceforge.net/projects/opencvlibrary/ からライブラリをダウンロードし、 opencv\sources\data\haarcascades opencv\sources\data\haarcascades_cuda を取り出して haarcascades_cuda/haarcascade_frontalface_alt2.xml を読み込めば認識できた Vagrantで実行する場合、画像ファイルのキャッシュに注意
<pre><?php // 顔認識 $facefile = 'images/yukata2.jpg'; $facedb = 'haarcascades_cuda/haarcascade_frontalface_alt2.xml'; $faces = face_detect($facefile, $facedb); // 顔認識の結果を画像に書き込む $im = imagecreatefromjpeg($facefile); $red = imagecolorallocate($im, 255, 0, 0); foreach ($faces as $i => $face) { $x = $face["x"]; $y = $face["y"]; $w = $face["w"]; imagerectangle($im, $x, $y, $x + $w, $y + $w, $red); echo "[$i] (x:$x, y:$y, w:$w)\n"; } // 画像を出力 imagejpeg($im, "face-out.jpg"); echo "<img src='face-out.jpg'>";

Advertisement