#author("2020-01-22T14:45:46+00:00","default:f-lab","f-lab")
#author("2020-01-22T14:51:58+00:00","default:f-lab","f-lab")
**detectクラスの改造 [#p9623751]
-検出をOpenCVではなくdlibでやってみる
***Detector.py [#aabc3230]
 import cv2
 import dlib
 import numpy as np
 import imutils
 from imutils import face_utils
 
 #顔と口を検出するクラス
 #引数:detector 顔判別器  predictor 口などを検出するときに使うやつ
 #  - detect()は画像のパスを入力すると顔検出と口検出をしてくれる.
 #    flag = 1 だと画像に検出結果を描画して表示する. flag = 0 なら描画しない
 #    出力は検出された顔と口の矩形領域
 #    形式 face = [[左上のx座標, y座標, 右下のx座標,y座標]...[]]
 #         mouth = [[x,y]...] (1つの口につき20点ずつ)
 #detect()は画像のパスを入力すると顔検出と口検出をしてくれる.
 #flag = 1 だと画像に検出結果を描画して表示する. flag = 0 なら描画しない
 #出力は検出された顔と口の矩形領域
 #形式 face = [[左上のx座標, y座標, 右下のx座標,y座標]...[]]
 #     mouth = [[x,y]...] (1つの口につき20点ずつ)
 class Detector():
     def __init__(self,detector,predictor):
         self.detector = detector
         self.predictor = predictor
         
         
     def detect(self,img_path, plot_flag):
         img = cv2.imread(img_path)
         img = imutils.resize(img, width=500)
         img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
         mouth=[]
         face = []
         temp = []
         # frontal_face_detectorクラスは矩形, スコア, サブ検出器の結果を返す
         dets, scores, idx = self.detector.run(img_rgb, 1)
         if len(dets) > 0:
             clone = img.copy()
             for det in dets:
                 if plot_flag == 1:
                     cv2.rectangle(clone, (det.left(), det.top()), (det.right(), det.bottom()), (0, 0, 255))
                 buf = [det.left(),det.top(),det.right(),det.bottom()]
                 face.append(buf)
             j = 0
             #口検出
             for i, rect in enumerate(dets):
                 shape = self.predictor(img_rgb, rect)
                 shape = face_utils.shape_to_np(shape)
                 #口検出結果の処理
                 for (x, y) in shape[48:68]:
                     if plot_flag == 1 :                      
                         cv2.circle(clone, (x, y), 1, (0, 0, 255), -1)
                         temp.append([x,y])
                 mouth.append(temp[j*20 : (j+1)*20])
                 j+=1
             if plot_flag == 1 :
                 cv2.imshow('img', clone)
                 c = cv2.waitKey(0)
             return face ,mouth 
             
         else :
             return None,None
             
**test.py [#a113b0de]
 from Detector import Detector
 import cv2
 import dlib
 import numpy as np
 import imutils
 from imutils import face_utils
 
 
 img = "image/frame_098.jpg"
 predictor_path = "./shape_predictor_68_face_landmarks.dat"
 predictor = dlib.shape_predictor(predictor_path)
 detector = dlib.get_frontal_face_detector()
 det = Detector(detector,predictor)
 face, mouth = det.detect(img,1)

***結果 [#o02fb14e]
#ref("out.jpg");


[[seminar-personal/fukumoto2017]]



トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS