質問編集履歴
2
プログラム削除
test
CHANGED
File without changes
|
test
CHANGED
@@ -34,203 +34,7 @@
|
|
34
34
|
|
35
35
|
```python
|
36
36
|
|
37
|
-
#人の全身、顔、上半身、下半身を検出し、矩形を描くプログラム
|
38
37
|
|
39
|
-
import cv2
|
40
|
-
|
41
|
-
import threading
|
42
|
-
|
43
|
-
import PySimpleGUI as sg
|
44
|
-
|
45
|
-
import os
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
#GUIのデザインテーマ設定
|
50
|
-
|
51
|
-
sg.theme('DarkPurple4')
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
#GUIのレイアウト設定
|
56
|
-
|
57
|
-
layout = [[sg.Button('終了', key = 'end')]]
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
#GUI生成
|
62
|
-
|
63
|
-
window = sg.Window('プログラム終了ボタン', layout)
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
#分類器のフォルダパス(顔、上半身、下半身はhaar-like特徴で検出)
|
68
|
-
|
69
|
-
face_cascade_path = '/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_frontalface_alt2.xml'
|
70
|
-
|
71
|
-
upperbody_cascade_path = '/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_upperbody.xml'
|
72
|
-
|
73
|
-
lowerbody_cascade_path = '/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_lowerbody.xml'
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
#全身のみHOG特徴で検出
|
78
|
-
|
79
|
-
global hog
|
80
|
-
|
81
|
-
hog = cv2.HOGDescriptor()
|
82
|
-
|
83
|
-
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
face_cascade = cv2.CascadeClassifier(face_cascade_path)
|
88
|
-
|
89
|
-
upperbody_cascade = cv2.CascadeClassifier(upperbody_cascade_path)
|
90
|
-
|
91
|
-
lowerbody_cascade = cv2.CascadeClassifier(lowerbody_cascade_path)
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
#顔検出用スレッド
|
96
|
-
|
97
|
-
def face_rectangle(img, img_gray, faces):
|
98
|
-
|
99
|
-
for x, y, w, h in faces:
|
100
|
-
|
101
|
-
cv2.rectangle(img, (x,y), (x+w, y+h), (0, 0, 255), 2)
|
102
|
-
|
103
|
-
face = img[y:y+h, x:x+w]
|
104
|
-
|
105
|
-
face_gray = img_gray[y:y+h, x:x+w]
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
#全身検出用スレッド
|
110
|
-
|
111
|
-
def fullbody_rectangle(img, fullbodys):
|
112
|
-
|
113
|
-
for x, y, w, h in fullbodys:
|
114
|
-
|
115
|
-
cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2)
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
#上半身検出用スレッド
|
120
|
-
|
121
|
-
def upperbody_rectangle(img, img_gray, upperbodys):
|
122
|
-
|
123
|
-
for x, y, w, h in upperbodys:
|
124
|
-
|
125
|
-
cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2)
|
126
|
-
|
127
|
-
upperbody = img[y:y+h, x:x+w]
|
128
|
-
|
129
|
-
upperbody_gray = img_gray[y:y+h, x:x+w]
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
#下半身検出用スレッド
|
134
|
-
|
135
|
-
def lowerbody_rectangle(img, img_gray, lowerbodys):
|
136
|
-
|
137
|
-
for x, y, w, h in lowerbodys:
|
138
|
-
|
139
|
-
cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2)
|
140
|
-
|
141
|
-
lowerbody = img[y:y+h, x:x+w]
|
142
|
-
|
143
|
-
lowerbody_gray = img_gray[y:y+h, x:x+w]
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
#カメラキャプチャON
|
148
|
-
|
149
|
-
cap = cv2.VideoCapture(0)
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
if __name__ == '__main__':
|
154
|
-
|
155
|
-
while(cap.isOpened()):
|
156
|
-
|
157
|
-
#画像の設定
|
158
|
-
|
159
|
-
ret, frame = cap.read()
|
160
|
-
|
161
|
-
frame = cv2.resize(frame, (320, 240))
|
162
|
-
|
163
|
-
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
164
|
-
|
165
|
-
frame_gray = cv2.resize(frame_gray, (320, 240))
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
#検出パラメータ設定
|
170
|
-
|
171
|
-
faces = face_cascade.detectMultiScale(frame_gray, 1.11, 3)
|
172
|
-
|
173
|
-
fullbodys, features = hog.detectMultiScale(frame, winStride = (8, 8), padding = (32, 32), scale = 1.05)
|
174
|
-
|
175
|
-
upperbodys = upperbody_cascade.detectMultiScale(frame_gray, 1.05, 2)
|
176
|
-
|
177
|
-
lowerbodys = lowerbody_cascade.detectMultiScale(frame_gray, 1.05, 2)
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
#検出&矩形描画スレッド起動(マルチスレッド)
|
182
|
-
|
183
|
-
t1 = threading.Thread(target = face_rectangle, args = (frame, frame_gray, faces))
|
184
|
-
|
185
|
-
t2 = threading.Thread(target = fullbody_rectangle, args = (frame, fullbodys))
|
186
|
-
|
187
|
-
t3 = threading.Thread(target = upperbody_rectangle, args = (frame, frame_gray, upperbodys))
|
188
|
-
|
189
|
-
t4 = threading.Thread(target = lowerbody_rectangle, args = (frame, frame_gray, lowerbodys))
|
190
|
-
|
191
|
-
t1.start()
|
192
|
-
|
193
|
-
t2.start()
|
194
|
-
|
195
|
-
t3.start()
|
196
|
-
|
197
|
-
t4.start()
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
#画像の表示
|
202
|
-
|
203
|
-
cv2.imshow('detect', frame)
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
#終了ボタンGUI読み込み
|
208
|
-
|
209
|
-
event, values = window.read()
|
210
|
-
|
211
|
-
#☓ボタンを押しても終了
|
212
|
-
|
213
|
-
if event == sg.WIN_CLOSED:
|
214
|
-
|
215
|
-
break
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
#GUIの処理
|
220
|
-
|
221
|
-
if event == 'end':
|
222
|
-
|
223
|
-
break
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
window.close()
|
228
|
-
|
229
|
-
cap.release()
|
230
|
-
|
231
|
-
cv2.destroyAllWindows()
|
232
|
-
|
233
|
-
```
|
234
38
|
|
235
39
|
|
236
40
|
|
1
質問内容が消えていたので再掲示
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,7 +2,11 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
|
5
|
+
opencvでwebカメラからキャプチャした画像を表示、
|
6
|
+
|
7
|
+
プログラムの終了(画像ウインドウの破棄)は
|
8
|
+
|
9
|
+
pysimpleguiで作成した終了ボタンで行いたい。
|
6
10
|
|
7
11
|
|
8
12
|
|
@@ -12,7 +16,13 @@
|
|
12
16
|
|
13
17
|
```
|
14
18
|
|
19
|
+
guiは出力されるが、画像が出力されない。
|
20
|
+
|
15
|
-
|
21
|
+
※プログラムを実行するとカメラはONになる
|
22
|
+
|
23
|
+
※gui機能追加前は画像出力されていた。
|
24
|
+
|
25
|
+
|
16
26
|
|
17
27
|
```
|
18
28
|
|
@@ -22,9 +32,203 @@
|
|
22
32
|
|
23
33
|
|
24
34
|
|
25
|
-
```
|
35
|
+
```python
|
36
|
+
|
26
|
-
|
37
|
+
#人の全身、顔、上半身、下半身を検出し、矩形を描くプログラム
|
38
|
+
|
39
|
+
import cv2
|
40
|
+
|
41
|
+
import threading
|
42
|
+
|
43
|
+
import PySimpleGUI as sg
|
44
|
+
|
45
|
+
import os
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
#GUIのデザインテーマ設定
|
50
|
+
|
51
|
+
sg.theme('DarkPurple4')
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
#GUIのレイアウト設定
|
56
|
+
|
57
|
+
layout = [[sg.Button('終了', key = 'end')]]
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
#GUI生成
|
62
|
+
|
63
|
+
window = sg.Window('プログラム終了ボタン', layout)
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
#分類器のフォルダパス(顔、上半身、下半身はhaar-like特徴で検出)
|
68
|
+
|
69
|
+
face_cascade_path = '/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_frontalface_alt2.xml'
|
70
|
+
|
71
|
+
upperbody_cascade_path = '/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_upperbody.xml'
|
72
|
+
|
73
|
+
lowerbody_cascade_path = '/usr/local/lib/python3.7/dist-packages/cv2/data/haarcascade_lowerbody.xml'
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
#全身のみHOG特徴で検出
|
78
|
+
|
79
|
+
global hog
|
80
|
+
|
81
|
+
hog = cv2.HOGDescriptor()
|
82
|
+
|
83
|
+
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
face_cascade = cv2.CascadeClassifier(face_cascade_path)
|
88
|
+
|
89
|
+
upperbody_cascade = cv2.CascadeClassifier(upperbody_cascade_path)
|
90
|
+
|
91
|
+
lowerbody_cascade = cv2.CascadeClassifier(lowerbody_cascade_path)
|
92
|
+
|
93
|
+
|
94
|
+
|
27
|
-
|
95
|
+
#顔検出用スレッド
|
96
|
+
|
97
|
+
def face_rectangle(img, img_gray, faces):
|
98
|
+
|
99
|
+
for x, y, w, h in faces:
|
100
|
+
|
101
|
+
cv2.rectangle(img, (x,y), (x+w, y+h), (0, 0, 255), 2)
|
102
|
+
|
103
|
+
face = img[y:y+h, x:x+w]
|
104
|
+
|
105
|
+
face_gray = img_gray[y:y+h, x:x+w]
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
#全身検出用スレッド
|
110
|
+
|
111
|
+
def fullbody_rectangle(img, fullbodys):
|
112
|
+
|
113
|
+
for x, y, w, h in fullbodys:
|
114
|
+
|
115
|
+
cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2)
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
#上半身検出用スレッド
|
120
|
+
|
121
|
+
def upperbody_rectangle(img, img_gray, upperbodys):
|
122
|
+
|
123
|
+
for x, y, w, h in upperbodys:
|
124
|
+
|
125
|
+
cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2)
|
126
|
+
|
127
|
+
upperbody = img[y:y+h, x:x+w]
|
128
|
+
|
129
|
+
upperbody_gray = img_gray[y:y+h, x:x+w]
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
#下半身検出用スレッド
|
134
|
+
|
135
|
+
def lowerbody_rectangle(img, img_gray, lowerbodys):
|
136
|
+
|
137
|
+
for x, y, w, h in lowerbodys:
|
138
|
+
|
139
|
+
cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2)
|
140
|
+
|
141
|
+
lowerbody = img[y:y+h, x:x+w]
|
142
|
+
|
143
|
+
lowerbody_gray = img_gray[y:y+h, x:x+w]
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
#カメラキャプチャON
|
148
|
+
|
149
|
+
cap = cv2.VideoCapture(0)
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
if __name__ == '__main__':
|
154
|
+
|
155
|
+
while(cap.isOpened()):
|
156
|
+
|
157
|
+
#画像の設定
|
158
|
+
|
159
|
+
ret, frame = cap.read()
|
160
|
+
|
161
|
+
frame = cv2.resize(frame, (320, 240))
|
162
|
+
|
163
|
+
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
164
|
+
|
165
|
+
frame_gray = cv2.resize(frame_gray, (320, 240))
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
#検出パラメータ設定
|
170
|
+
|
171
|
+
faces = face_cascade.detectMultiScale(frame_gray, 1.11, 3)
|
172
|
+
|
173
|
+
fullbodys, features = hog.detectMultiScale(frame, winStride = (8, 8), padding = (32, 32), scale = 1.05)
|
174
|
+
|
175
|
+
upperbodys = upperbody_cascade.detectMultiScale(frame_gray, 1.05, 2)
|
176
|
+
|
177
|
+
lowerbodys = lowerbody_cascade.detectMultiScale(frame_gray, 1.05, 2)
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
#検出&矩形描画スレッド起動(マルチスレッド)
|
182
|
+
|
183
|
+
t1 = threading.Thread(target = face_rectangle, args = (frame, frame_gray, faces))
|
184
|
+
|
185
|
+
t2 = threading.Thread(target = fullbody_rectangle, args = (frame, fullbodys))
|
186
|
+
|
187
|
+
t3 = threading.Thread(target = upperbody_rectangle, args = (frame, frame_gray, upperbodys))
|
188
|
+
|
189
|
+
t4 = threading.Thread(target = lowerbody_rectangle, args = (frame, frame_gray, lowerbodys))
|
190
|
+
|
191
|
+
t1.start()
|
192
|
+
|
193
|
+
t2.start()
|
194
|
+
|
195
|
+
t3.start()
|
196
|
+
|
197
|
+
t4.start()
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
#画像の表示
|
202
|
+
|
203
|
+
cv2.imshow('detect', frame)
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
#終了ボタンGUI読み込み
|
208
|
+
|
209
|
+
event, values = window.read()
|
210
|
+
|
211
|
+
#☓ボタンを押しても終了
|
212
|
+
|
213
|
+
if event == sg.WIN_CLOSED:
|
214
|
+
|
215
|
+
break
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
#GUIの処理
|
220
|
+
|
221
|
+
if event == 'end':
|
222
|
+
|
223
|
+
break
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
window.close()
|
228
|
+
|
229
|
+
cap.release()
|
230
|
+
|
231
|
+
cv2.destroyAllWindows()
|
28
232
|
|
29
233
|
```
|
30
234
|
|
@@ -42,4 +246,8 @@
|
|
42
246
|
|
43
247
|
|
44
248
|
|
249
|
+
環境:raspberrypi4
|
250
|
+
|
251
|
+
言語:python3.7
|
252
|
+
|
45
|
-
|
253
|
+
画像処理ライブラリ:opencv4.5.1
|