質問編集履歴
1
コード削除
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,156 +30,6 @@
|
|
30
30
|
|
31
31
|
```python3.7
|
32
32
|
|
33
|
-
import cv2
|
34
|
-
|
35
|
-
import threading
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
face_cascade_path = 'C:/Users/User/anaconda3/Lib/site-packages/cv2/data/haarcascade_frontalface_alt2.xml' #顔用cascade分類器
|
40
|
-
|
41
|
-
upperbody_cascade_path = 'C:/Users/User/anaconda3/Lib/site-packages/cv2/data/haarcascade_upperbody.xml' #上半身用cascade分類器
|
42
|
-
|
43
|
-
lowerbody_cascade_path = 'C:/Users/User/anaconda3/Lib/site-packages/cv2/data/haarcascade_lowerbody.xml' #下半身用cascade分類器
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
#全身だけHOG特徴量を使って検出
|
48
|
-
|
49
|
-
global hog
|
50
|
-
|
51
|
-
hog = cv2.HOGDescriptor()
|
52
|
-
|
53
|
-
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
face_cascade = cv2.CascadeClassifier(face_cascade_path)
|
58
|
-
|
59
|
-
#fullbody_cascade = cv2.CascadeClassifier(fullbody_cascade_path)
|
60
|
-
|
61
|
-
upperbody_cascade = cv2.CascadeClassifier(upperbody_cascade_path)
|
62
|
-
|
63
|
-
lowerbody_cascade = cv2.CascadeClassifier(lowerbody_cascade_path)
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
#顔検出用矩形描画スレッド
|
68
|
-
|
69
|
-
def face_rectangle(img, img_gray, faces):
|
70
|
-
|
71
|
-
for x, y, w, h in faces:
|
72
|
-
|
73
|
-
cv2.rectangle(img, (x,y), (x+w, y+h), (0, 0, 255), 2)
|
74
|
-
|
75
|
-
face = img[y:y+h, x:x+w]
|
76
|
-
|
77
|
-
face_gray = img_gray[y:y+h, x:x+w]
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
#全身検出用矩形描画スレッド
|
82
|
-
|
83
|
-
def fullbody_rectangle(img, fullbodys):
|
84
|
-
|
85
|
-
for x, y, w, h in fullbodys:
|
86
|
-
|
87
|
-
cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2)
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
#上半身検出用矩形描画スレッド
|
92
|
-
|
93
|
-
def upperbody_rectangle(img, img_gray, upperbodys):
|
94
|
-
|
95
|
-
for x, y, w, h in upperbodys:
|
96
|
-
|
97
|
-
cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2)
|
98
|
-
|
99
|
-
upperbody = img[y:y+h, x:x+w]
|
100
|
-
|
101
|
-
upperbody_gray = img_gray[y:y+h, x:x+w]
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
#下半身検出用矩形描画スレッド
|
106
|
-
|
107
|
-
def lowerbody_rectangle(img, img_gray, lowerbodys):
|
108
|
-
|
109
|
-
for x, y, w, h in lowerbodys:
|
110
|
-
|
111
|
-
cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2)
|
112
|
-
|
113
|
-
lowerbody = img[y:y+h, x:x+w]
|
114
|
-
|
115
|
-
lowerbody_gray = img_gray[y:y+h, x:x+w]
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
#カメラキャプチャ
|
120
|
-
|
121
|
-
cap = cv2.VideoCapture(0)
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
if __name__ == '__main__':
|
126
|
-
|
127
|
-
while(cap.isOpened()):
|
128
|
-
|
129
|
-
ret, frame = cap.read()
|
130
|
-
|
131
|
-
cv2.resize(frame, (320, 240)) #画像縮小(ここが機能していない)
|
132
|
-
|
133
|
-
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
faces = face_cascade.detectMultiScale(frame_gray, 1.11, 3)
|
138
|
-
|
139
|
-
fullbodys, features = hog.detectMultiScale(frame, winStride = (8, 8), padding = (32, 32), scale = 1.05)
|
140
|
-
|
141
|
-
upperbodys = upperbody_cascade.detectMultiScale(frame_gray, 1.11, 3)
|
142
|
-
|
143
|
-
lowerbodys = lowerbody_cascade.detectMultiScale(frame_gray, 1.11, 3)
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
t1 = threading.Thread(target = face_rectangle, args = (frame, frame_gray, faces))
|
148
|
-
|
149
|
-
t2 = threading.Thread(target = fullbody_rectangle, args = (frame, fullbodys))
|
150
|
-
|
151
|
-
t3 = threading.Thread(target = upperbody_rectangle, args = (frame, frame_gray, upperbodys))
|
152
|
-
|
153
|
-
t4 = threading.Thread(target = lowerbody_rectangle, args = (frame, frame_gray, lowerbodys))
|
154
|
-
|
155
|
-
t1.start()
|
156
|
-
|
157
|
-
t2.start()
|
158
|
-
|
159
|
-
t3.start()
|
160
|
-
|
161
|
-
t4.start()
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
#画像の表示
|
166
|
-
|
167
|
-
cv2.imshow('detect', frame)
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
key_input = cv2.waitKey(1)
|
172
|
-
|
173
|
-
if key_input > 0:
|
174
|
-
|
175
|
-
break
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
cap.release()
|
180
|
-
|
181
|
-
cv2.destroyAllWindows()
|
182
|
-
|
183
33
|
```
|
184
34
|
|
185
35
|
|