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

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

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

dlibは、機械学習のC++の画像処理ライブラリの一つ。性能の高い顔の器官検出が簡単にでき、Pythonバインドもあります。オープンソースで無料で使用でき、機械学習以外の様々な機能も搭載されています。

OpenCV

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

コンパイルエラー

コンパイルのフェーズで生成されるエラーです。よく無効なシンタックスやタイプが含まれているとき発生します。

保存

保存(save)とは、特定のファイルを、ハードディスク等の外部記憶装置に記録する行為を指します。

Python

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

Q&A

解決済

3回答

2541閲覧

コンパイルが通りません。泣

haruyuzu

総合スコア6

dlib

dlibは、機械学習のC++の画像処理ライブラリの一つ。性能の高い顔の器官検出が簡単にでき、Pythonバインドもあります。オープンソースで無料で使用でき、機械学習以外の様々な機能も搭載されています。

OpenCV

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

コンパイルエラー

コンパイルのフェーズで生成されるエラーです。よく無効なシンタックスやタイプが含まれているとき発生します。

保存

保存(save)とは、特定のファイルを、ハードディスク等の外部記憶装置に記録する行為を指します。

Python

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

0グッド

0クリップ

投稿2019/11/25 14:54

Opencvとdlibを用いて口を検出し、切り出し保存するコードなのですが

File "1122.py", line 56, in <module> main() File "1122.py", line 38, in main frame, roi = face_shape_detector_dlib(frame) File "1122.py", line 10, in face_shape_detector_dlib dets, scores, idx = detector.run(img_rgb, 0) NameError: name 'detector' is not defined

とエラーが出てしまいます。

以下コードになります。コンパイルできるようにしたいです。
よろしくお願いいたします。

python

1import cv2 2import dlib 3import numpy as np 4import imutils 5from imutils import face_utils 6 7def face_shape_detector_dlib(img): 8 img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 9 # frontal_face_detectorクラスは矩形, スコア, サブ検出器の結果を返す 10 dets, scores, idx = detector.run(img_rgb, 0) 11 if len(dets) > 0: 12 for i, rect in enumerate(dets): 13 shape = predictor(img_rgb, rect) 14 shape = face_utils.shape_to_np(shape) 15 clone = img.copy() 16 cv2.putText(clone, "mouth", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) 17 # landmarkを画像に書き込む 18 for (x, y) in shape[48:68]: 19 cv2.circle(clone, (x, y), 1, (0, 0, 255), -1) 20 # shapeで指定した個所の切り取り画像(ROI)を取得 21 (x, y, w, h) = cv2.boundingRect(np.array([shape[48:68]])) #口の部位のみ切り出し 22 roi = img[y:y + h, x:x + w] 23 roi = cv2.resize(roi,(100,100)) 24 return clone, roi 25 else : 26 return img, None 27 28def main(): 29 predictor_path = "./shape_predictor_68_face_landmarks.dat" 30 predictor = dlib.shape_predictor(predictor_path) 31 detector = dlib.get_frontal_face_detector() 32 cap = cv2.VideoCapture(0) 33 count = 0 34 35 while True: 36 ret, frame = cap.read() 37 frame = imutils.resize(frame, width=500) 38 frame, roi = face_shape_detector_dlib(frame) 39 cv2.imshow('img', frame) 40 if roi is not None : 41 cv2.imshow('roi', roi) 42 else : 43 cv2.destroyWindow('roi') 44 c = cv2.waitKey(1) 45 if c == 27:#ESCを押してウィンドウを閉じる 46 break 47 if c == 32:#spaceで保存 48 count += 1 49 cv2.imwrite('./filename%03.f'%(count)+'.jpg', roi) #001~連番で保存 50 print('save done') 51 cap.release() 52 cv2.destroyAllWindows() 53 54if __name__ == '__main__': 55 56 main() 57

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

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

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

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

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

guest

回答3

0

ベストアンサー

関数内で定義した変数は、その関数内でのみ参照できます。
なので、main() 内で定義した detector という変数は face_shape_detector_dlib() からはアクセスできません。
次のように引数として渡してあげるように修正してはどうでしょうか。

定義

diff

1- def face_shape_detector_dlib(img, detector): 2+ def face_shape_detector_dlib(img):

呼び出し側

diff

1- face_shape_detector_dlib(img, detector): 2+ face_shape_detector_dlib(img):

追記

python

1import cv2 2import dlib 3import numpy as np 4import imutils 5from imutils import face_utils 6 7def face_shape_detector_dlib(img): 8 img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 9 # frontal_face_detectorクラスは矩形, スコア, サブ検出器の結果を返す 10 dets, scores, idx = detector.run(img_rgb, 0) 11 if len(dets) > 0: 12 for i, rect in enumerate(dets): 13 shape = predictor(img_rgb, rect) 14 shape = face_utils.shape_to_np(shape) 15 clone = img.copy() 16 cv2.putText(clone, "mouth", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) 17 # landmarkを画像に書き込む 18 for (x, y) in shape[48:68]: 19 cv2.circle(clone, (x, y), 1, (0, 0, 255), -1) 20 # shapeで指定した個所の切り取り画像(ROI)を取得 21 (x, y, w, h) = cv2.boundingRect(np.array([shape[48:68]])) #口の部位のみ切り出し 22 roi = img[y:y + h, x:x + w] 23 roi = cv2.resize(roi,(100,100)) 24 return clone, roi 25 else : 26 return img, None 27 28predictor_path = "./shape_predictor_68_face_landmarks.dat" 29predictor = dlib.shape_predictor(predictor_path) 30detector = dlib.get_frontal_face_detector() 31cap = cv2.VideoCapture(0) 32count = 0 33 34while True: 35 ret, frame = cap.read() 36 frame = imutils.resize(frame, width=500) 37 frame, roi = face_shape_detector_dlib(frame) 38 cv2.imshow('img', frame) 39 if roi is not None : 40 cv2.imshow('roi', roi) 41 else : 42 cv2.destroyWindow('roi') 43 c = cv2.waitKey(1) 44 if c == 27:#ESCを押してウィンドウを閉じる 45 break 46 if c == 32:#spaceで保存 47 count += 1 48 cv2.imwrite('./filename%03.f'%(count)+'.jpg', roi) #001~連番で保存 49 print('save done') 50cap.release() 51cv2.destroyAllWindows() 52

投稿2019/11/25 14:58

編集2019/11/25 16:49
tiitoi

総合スコア21956

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

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

haruyuzu

2019/11/25 16:10

tiitoi 様 回答ありがとうございます! 初心者なためわかりませんでした泣 それぞれどの部分に書き加えれば良いのでしょうか?
tiitoi

2019/11/25 16:17

赤の行を緑の行に2箇所変更するようにという意図でした。 修正後のコードを示すと追記したようになります。 他の部分に問題がないかどうかはわかりませんが、すくなくともname 'detector' is not defined のエラーはでなくなるはずです。
haruyuzu

2019/11/25 16:40

その意図だと思って変えて見たのですがまだコンパイルできず、 もう一度確認したら今度は以下のようなエラーが出てしまいました泣 File "1122.py", line 56, in <module> main() File "1122.py", line 38, in main frame, roi = face_shape_detector_dlib(frame, detector) File "1122.py", line 13, in face_shape_detector_dlib shape = predictor(img_rgb, rect) NameError: name 'predictor' is not defined predictorというのが定義されていないということでしょうか
tiitoi

2019/11/25 16:52 編集

関数内で使用する変数は、「関数内で定義する」「引数として渡す」「グローバル変数」のいずれかである必要があります。 例えば、質問のコードの detector、predictor という変数は main() で定義されていますが、その関数である face_shape_detector_dlib() という関数からは参照することはできません。 参照しようとすると、undefined reference エラーになります。 なので、引数で渡すかグローバル変数にする必要があります。 回答のコードを main() を消して、グローバル変数にしたのでこれで動くでしょうか?
haruyuzu

2019/11/25 16:54

detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") この2行を最初に足したらコンパイル通りました! ありがとうございました!
guest

0

python

1import cv2 2import dlib 3import numpy as np 4import imutils 5from imutils import face_utils 6 7detector = dlib.get_frontal_face_detector() 8predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") 9 10def face_shape_detector_dlib(img, detector): 11 img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 # frontal_face_detectorクラスは矩形, スコア, サブ検出器の結果を返す 13 dets, scores, idx = detector.run(img_rgb, 0) 14 if len(dets) > 0: 15 for i, rect in enumerate(dets): 16 shape = predictor(img_rgb, rect) 17 shape = face_utils.shape_to_np(shape) 18 clone = img.copy() 19 cv2.putText(clone, "mouth", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) 20 # landmarkを画像に書き込む 21 for (x, y) in shape[48:68]: 22 cv2.circle(clone, (x, y), 1, (0, 0, 255), -1) 23 # shapeで指定した個所の切り取り画像(ROI)を取得 24 (x, y, w, h) = cv2.boundingRect(np.array([shape[48:68]])) #口の部位のみ切り出し 25 roi = img[y:y + h, x:x + w] 26 roi = cv2.resize(roi,(80,50)) 27 return clone, roi 28 else : 29 return img, None 30 31def main(): 32 predictor_path = "./shape_predictor_68_face_landmarks.dat" 33 predictor = dlib.shape_predictor(predictor_path) 34 detector = dlib.get_frontal_face_detector() 35 cap = cv2.VideoCapture(0) 36 count = 0 37 38 while True: 39 ret, frame = cap.read() 40 frame = imutils.resize(frame, width=500) 41 frame, roi = face_shape_detector_dlib(frame, detector) 42 cv2.imshow('img', frame) 43 if roi is not None : 44 cv2.imshow('roi', roi) 45 else : 46 cv2.destroyWindow('roi') 47 c = cv2.waitKey(1) 48 if c == 27:#ESCを押してウィンドウを閉じる 49 break 50 if c == 32:#spaceで保存 51 count += 1 52 cv2.imwrite('./filename%03.f'%(count)+'.jpg', roi) #001~連番で保存 53 print('save done') 54 cap.release() 55 cv2.destroyAllWindows() 56 57if __name__ == '__main__': 58 59 main() 60 61

投稿2019/11/25 16:55

haruyuzu

総合スコア6

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

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

0

エラー・メッセージのとおり、'detector' ってのが存在してません
それをどーにかしましょう

投稿2019/11/25 14:58

y_waiwai

総合スコア87774

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問