質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Jupyter

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

810閲覧

openCVで写真の顔認証を行いたい

mokomokoyouyou

総合スコア7

Jupyter

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/01/09 07:57

前提・実現したいこと

openCVで顔認証を行いたい。

発生している問題・エラーメッセージ

cascade file が上手く起動しない

エラーメッセージ

error Traceback (most recent call last)
<ipython-input-40-f635a3ba6ff3> in <module>
----> 1 result = detect_face(face)
2 plt.imshow(result,cmap='gray')

<ipython-input-39-152d8294bf59> in detect_face(img)
3 face_img = img.copy()
4
----> 5 face_rects = face_cascade.detectMultiScale(face_img)
6
7 for (x,y,w,h) in face_rects:

error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

ソースコード 全文

import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
face=cv2.imread('face.jpg',0)
plt.imshow(face,cmap='gray')

cascade_file_path = 'C:/Users/satoshi/opencv-master/data/haarcascades_frontalface_default.xml'
face_cascade = cv2.CascadeClassifier('cascade_file_path')
def detect_face(img):

face_img = img.copy() face_rects = face_cascade.detectMultiScale(face_img) for (x,y,w,h) in face_rects: cv2.rectangle(face_img, (x,y), (x+w,y+h), (255,255,255), 10) return face_img

result = detect_face(face)
plt.imshow(result,cmap='gray')

該当のソースコード

cascade_file_path = 'C:/Users/satoshi/opencv-master/data/haarcascades_frontalface_default.xml'
face_cascade = cv2.CascadeClassifier('cascade_file_path')
def detect_face(img):

face_img = img.copy() face_rects = face_cascade.detectMultiScale(face_img) for (x,y,w,h) in face_rects: cv2.rectangle(face_img, (x,y), (x+w,y+h), (255,255,255), 10) return face_img

result = detect_face(face)
plt.imshow(result,cmap='gray')

python

試したこと

xmlへのディレクトリが違うのかと思い、パスの修正確認を行ったがダメでした

補足情報(FW/ツールのバージョンなど)

。jupyter notebook

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tiitoi

2020/01/09 08:03

C:/Users/satoshi/opencv-master/data/haarcascades_frontalface_default.xml は存在していることは確認しましたか?
mokomokoyouyou

2020/01/09 08:08

はい、確認しました。 haarcascade_frontalface_defaultを選んだのは参考にしたHPがそうしてあったためです。
tiitoi

2020/01/09 08:10

face=cv2.imread('face.jpg',0) の返り値 face は None でないことも確認しましたか?パスが間違っている等で読み込みに失敗している場合は None になりますので、念の為確認して見てください。
mokomokoyouyou

2020/01/09 08:44

助言ありがとうございます。 print(face) で 返り値は [[105 133 157 ... 155 155 155] [104 134 159 ... 155 155 155] [104 132 157 ... 156 156 156] ... [161 161 160 ... 162 164 162] [161 160 160 ... 162 164 162] [160 160 160 ... 162 164 162]] とありました。 また print(face_cascade) では <CascadeClassifier 000001DF41729250> とメッセージがありました。
guest

回答1

0

ベストアンサー

よくみたら、CascadeClassifier にわたしているのが、cascade_file_path という変数ではなく、'cascade_file_path' という文字列になっていますね。
これが原因ではないでしょうか。

python

1cv2.CascadeClassifier('cascade_file_path')

追記

問題切り分けのために、assert を入れたので、これを実行して見てください

python

1import cv2 2import numpy as np 3import matplotlib.pyplot as plt 4import os 5 6face = cv2.imread("face.jpg", 0) 7assert face is not None, "画像ファイルの読み込みに失敗" 8 9plt.imshow(face, cmap="gray") 10 11cascade_file_path = ( 12 "C:/Users/satoshi/opencv-master/data/haarcascades_frontalface_default.xml" 13) 14face_cascade = cv2.CascadeClassifier(cascade_file_path) 15assert os.path.exists(cascade_file_path), f"{os.path.abspath(cascade_file_path)} が存在しない" 16 17def detect_face(img): 18 face_img = img.copy() 19 face_rects = face_cascade.detectMultiScale(face_img) 20 21 for (x, y, w, h) in face_rects: 22 cv2.rectangle(face_img, (x, y), (x + w, y + h), (255, 255, 255), 10) 23 24 return face_img 25 26 27result = detect_face(face) 28plt.imshow(result, cmap="gray")

投稿2020/01/09 08:46

編集2020/01/09 08:58
tiitoi

総合スコア21956

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mokomokoyouyou

2020/01/09 08:51

再度 face_cascade = cv2.CascadeClassifier(cascade_file_path) として試しましたが結果は変わりませんでした。
tiitoi

2020/01/09 08:59 編集

問題切り分けのために、assert を入れたコードを追記したので、これを実行して見てください assert の部分で止まった場合はパスが間違っています
mokomokoyouyou

2020/01/09 09:08

試してみました。 下の文が表示されました AssertionError Traceback (most recent call last) <ipython-input-1-83a70c7e2a9b> in <module> 13 ) 14 face_cascade = cv2.CascadeClassifier(cascade_file_path) ---> 15 assert os.path.exists(cascade_file_path), f"{os.path.abspath(cascade_file_path)} が存在しない" 16 17 def detect_face(img): AssertionError: C:\Users\satoshi\opencv-master\data\haarcascades_frontalface_default.xml が存在しない
mokomokoyouyou

2020/01/09 09:10

試しにjupyternotebook のカレントディレクトリにhaarcascades_frontalface_default.xml を異動したところ、正常に動作しました。
tiitoi

2020/01/09 09:10

エラーがでたということはやはりその `C:\Users\satoshi\opencv-master\data` というディレクリに haarcascades_frontalface_default.xml が存在しないのではないでしょうか
tiitoi

2020/01/09 09:11

入れ違いになりました。動作したのであれば、よかったです。
mokomokoyouyou

2020/01/09 09:15

この度は短時間でこのような手厚い回答を頂けて感謝に堪えません。バックグランドとしておもちの知識と経験を尊敬します。昼過ぎから格闘していたので非常に助かりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問