回答編集履歴

2

d

2019/11/25 16:49

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -48,7 +48,7 @@
48
48
 
49
49
 
50
50
 
51
- def face_shape_detector_dlib(img, detector):
51
+ def face_shape_detector_dlib(img):
52
52
 
53
53
  img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
54
54
 
@@ -90,62 +90,54 @@
90
90
 
91
91
 
92
92
 
93
- def main():
93
+ predictor_path = "./shape_predictor_68_face_landmarks.dat"
94
94
 
95
- predictor_path = "./shape_predictor_68_face_landmarks.dat"
95
+ predictor = dlib.shape_predictor(predictor_path)
96
96
 
97
- predictor = dlib.shape_predictor(predictor_path)
97
+ detector = dlib.get_frontal_face_detector()
98
98
 
99
- detector = dlib.get_frontal_face_detector()
99
+ cap = cv2.VideoCapture(0)
100
100
 
101
- cap = cv2.VideoCapture(0)
102
-
103
- count = 0
101
+ count = 0
104
102
 
105
103
 
106
104
 
107
- while True:
105
+ while True:
108
106
 
109
- ret, frame = cap.read()
107
+ ret, frame = cap.read()
110
108
 
111
- frame = imutils.resize(frame, width=500)
109
+ frame = imutils.resize(frame, width=500)
112
110
 
113
- frame, roi = face_shape_detector_dlib(frame, detector)
111
+ frame, roi = face_shape_detector_dlib(frame)
114
112
 
115
- cv2.imshow('img', frame)
113
+ cv2.imshow('img', frame)
116
114
 
117
- if roi is not None :
115
+ if roi is not None :
118
116
 
119
- cv2.imshow('roi', roi)
117
+ cv2.imshow('roi', roi)
120
118
 
121
- else :
119
+ else :
122
120
 
123
- cv2.destroyWindow('roi')
121
+ cv2.destroyWindow('roi')
124
122
 
125
- c = cv2.waitKey(1)
123
+ c = cv2.waitKey(1)
126
124
 
127
- if c == 27:#ESCを押してウィンドウを閉じる
125
+ if c == 27:#ESCを押してウィンドウを閉じる
128
126
 
129
- break
127
+ break
130
128
 
131
- if c == 32:#spaceで保存
129
+ if c == 32:#spaceで保存
132
130
 
133
- count += 1
131
+ count += 1
134
132
 
135
- cv2.imwrite('./filename%03.f'%(count)+'.jpg', roi) #001~連番で保存
133
+ cv2.imwrite('./filename%03.f'%(count)+'.jpg', roi) #001~連番で保存
136
134
 
137
- print('save done')
135
+ print('save done')
138
136
 
139
- cap.release()
137
+ cap.release()
140
138
 
141
- cv2.destroyAllWindows()
139
+ cv2.destroyAllWindows()
142
140
 
143
141
 
144
142
 
145
- if __name__ == '__main__':
146
-
147
-
148
-
149
- main()
150
-
151
143
  ```

1

d

2019/11/25 16:49

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -27,3 +27,125 @@
27
27
  + face_shape_detector_dlib(img):
28
28
 
29
29
  ```
30
+
31
+
32
+
33
+ ## 追記
34
+
35
+
36
+
37
+ ```python
38
+
39
+ import cv2
40
+
41
+ import dlib
42
+
43
+ import numpy as np
44
+
45
+ import imutils
46
+
47
+ from imutils import face_utils
48
+
49
+
50
+
51
+ def face_shape_detector_dlib(img, detector):
52
+
53
+ img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
54
+
55
+ # frontal_face_detectorクラスは矩形, スコア, サブ検出器の結果を返す
56
+
57
+ dets, scores, idx = detector.run(img_rgb, 0)
58
+
59
+ if len(dets) > 0:
60
+
61
+ for i, rect in enumerate(dets):
62
+
63
+ shape = predictor(img_rgb, rect)
64
+
65
+ shape = face_utils.shape_to_np(shape)
66
+
67
+ clone = img.copy()
68
+
69
+ cv2.putText(clone, "mouth", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
70
+
71
+ # landmarkを画像に書き込む
72
+
73
+ for (x, y) in shape[48:68]:
74
+
75
+ cv2.circle(clone, (x, y), 1, (0, 0, 255), -1)
76
+
77
+ # shapeで指定した個所の切り取り画像(ROI)を取得
78
+
79
+ (x, y, w, h) = cv2.boundingRect(np.array([shape[48:68]])) #口の部位のみ切り出し
80
+
81
+ roi = img[y:y + h, x:x + w]
82
+
83
+ roi = cv2.resize(roi,(100,100))
84
+
85
+ return clone, roi
86
+
87
+ else :
88
+
89
+ return img, None
90
+
91
+
92
+
93
+ def main():
94
+
95
+ predictor_path = "./shape_predictor_68_face_landmarks.dat"
96
+
97
+ predictor = dlib.shape_predictor(predictor_path)
98
+
99
+ detector = dlib.get_frontal_face_detector()
100
+
101
+ cap = cv2.VideoCapture(0)
102
+
103
+ count = 0
104
+
105
+
106
+
107
+ while True:
108
+
109
+ ret, frame = cap.read()
110
+
111
+ frame = imutils.resize(frame, width=500)
112
+
113
+ frame, roi = face_shape_detector_dlib(frame, detector)
114
+
115
+ cv2.imshow('img', frame)
116
+
117
+ if roi is not None :
118
+
119
+ cv2.imshow('roi', roi)
120
+
121
+ else :
122
+
123
+ cv2.destroyWindow('roi')
124
+
125
+ c = cv2.waitKey(1)
126
+
127
+ if c == 27:#ESCを押してウィンドウを閉じる
128
+
129
+ break
130
+
131
+ if c == 32:#spaceで保存
132
+
133
+ count += 1
134
+
135
+ cv2.imwrite('./filename%03.f'%(count)+'.jpg', roi) #001~連番で保存
136
+
137
+ print('save done')
138
+
139
+ cap.release()
140
+
141
+ cv2.destroyAllWindows()
142
+
143
+
144
+
145
+ if __name__ == '__main__':
146
+
147
+
148
+
149
+ main()
150
+
151
+ ```