質問編集履歴
2
書式変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,31 +2,45 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
```python
|
5
|
+
#OpenCVのインポート
|
5
6
|
import cv2
|
7
|
+
|
8
|
+
#カスケード型分類器に使用する分類器のデータ(xmlファイル)を読み込み
|
9
|
+
HAAR_FILE = "/content/drive/My Drive/haarcascade_frontalface_default.xml"
|
10
|
+
cascade = cv2.CascadeClassifier(HAAR_FILE)
|
11
|
+
|
12
|
+
#画像ファイルの読み込み
|
13
|
+
image_picture = "/content/drive/My Drive/a_resize.png"
|
14
|
+
|
15
|
+
img = cv2.imread(image_picture)
|
16
|
+
|
17
|
+
#グレースケールに変換する
|
18
|
+
img_g = cv2.imread(image_picture,0)
|
19
|
+
|
20
|
+
#カスケード型分類器を使用して画像ファイルから顔部分を検出する
|
21
|
+
face = cascade.detectMultiScale(img_g)
|
22
|
+
|
23
|
+
#顔の座標を表示する
|
24
|
+
print(face)
|
25
|
+
|
26
|
+
#顔部分を切り取る
|
27
|
+
for x,y,w,h in face:
|
28
|
+
face_cut = img[y:y+h, x:x+w]
|
29
|
+
|
30
|
+
#白枠で顔を囲む
|
31
|
+
for x,y,w,h in face:
|
32
|
+
cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,255),2)
|
33
|
+
|
34
|
+
#画像の出力
|
35
|
+
cv2.imwrite('face_cut.jpg', face_cut)
|
36
|
+
cv2.imwrite('face_rectangle.jpg', img)
|
37
|
+
```
|
38
|
+
```error
|
39
|
+
NameError Traceback (most recent call last)
|
40
|
+
<ipython-input-10-1b361f5a4955> in <module>()
|
41
|
+
24
|
42
|
+
25 #画像の出力
|
43
|
+
---> 26 cv2.imwrite('face_cut.jpg', face_cut)
|
6
44
|
|
7
|
-
cascade_path = "/content/drive/My Drive/haarcascade_frontalface_alt.xml"
|
8
|
-
origin_image_path = "/content/drive/My Drive/a_resize.png"
|
9
|
-
|
45
|
+
NameError: name 'face_cut' is not defined
|
10
|
-
|
11
|
-
i=0
|
12
|
-
|
13
|
-
image = cv2.imread(origin_image_path,0)
|
14
|
-
|
15
|
-
cascade = cv2.CascadeClassifier(cascade_path)
|
16
|
-
facerect = cascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=1, minSize=(10, 10))
|
17
|
-
|
18
|
-
if len(facerect) > 0:
|
19
|
-
for rect in facerect:
|
20
|
-
# 顔だけ切り出して保存
|
21
|
-
x = rect[0]
|
22
|
-
y = rect[1]
|
23
|
-
width = rect[2]
|
24
|
-
height = rect[3]
|
25
|
-
dst = image[y:y + height, x:x + width]
|
26
|
-
save_path = dir_path + '/' + 'image(' + str(i) + ')' + '.png'
|
27
|
-
#認識結果の保存
|
28
|
-
cv2.imwrite(save_path, dst)
|
29
|
-
print("save!")
|
30
|
-
i += 1
|
31
|
-
print("Finish")
|
32
46
|
```
|
1
書式変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
画像から顔を切り出して保存したいと考えています。
|
2
2
|
|
3
|
+
|
3
4
|
```python
|
4
5
|
import cv2
|
5
6
|
|
@@ -28,15 +29,4 @@
|
|
28
29
|
print("save!")
|
29
30
|
i += 1
|
30
31
|
print("Finish")
|
31
|
-
```
|
32
|
-
```error
|
33
|
-
SystemError Traceback (most recent call last)
|
34
|
-
<ipython-input-8-3cf9f5ef0f68> in <module>()
|
35
|
-
9 image = cv2.imread(origin_image_path,0)
|
36
|
-
10
|
37
|
-
---> 11 cascade = cv2.CascadeClassifier(cascade_path)
|
38
|
-
12 facerect = cascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=1, minSize=(10, 10))
|
39
|
-
13
|
40
|
-
|
41
|
-
SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set
|
42
32
|
```
|