質問編集履歴

3

リンクを追加しました

2021/12/22 01:05

投稿

nample
nample

スコア1

test CHANGED
File without changes
test CHANGED
@@ -6,6 +6,10 @@
6
6
 
7
7
 
8
8
 
9
+ ↓こちらからダウンロードしました。
10
+
11
+ [リンク内容](https://github.com/chandrikadeb7/Face-Mask-Detection)
12
+
9
13
 
10
14
 
11
15
  ### 該当のソースコード

2

Pythonと追加しました

2021/12/22 01:05

投稿

nample
nample

スコア1

test CHANGED
File without changes
test CHANGED
@@ -14,6 +14,14 @@
14
14
 
15
15
  ```Python
16
16
 
17
+ # USAGE
18
+
19
+ # python detect_mask_video.py
20
+
21
+
22
+
23
+ # import the necessary packages
24
+
17
25
  from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
18
26
 
19
27
  from tensorflow.keras.preprocessing.image import img_to_array
@@ -38,114 +46,166 @@
38
46
 
39
47
  def detect_and_predict_mask(frame, faceNet, maskNet):
40
48
 
41
- (h, w) = frame.shape[:2]
42
-
43
- blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300),
44
-
45
- (104.0, 177.0, 123.0))
46
-
47
-
48
-
49
- faceNet.setInput(blob)
50
-
51
- detections = faceNet.forward()
52
-
53
-
54
-
55
- faces = []
56
-
57
- locs = []
58
-
59
- preds = []
60
-
61
-
62
-
63
- for i in range(0, detections.shape[2]):
64
-
65
- confidence = detections[0, 0, i, 2]
66
-
67
-
68
-
69
- if confidence > args["confidence"]:
70
-
71
- box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
72
-
73
- (startX, startY, endX, endY) = box.astype("int")
74
-
75
-
76
-
77
- (startX, startY) = (max(0, startX), max(0, startY))
78
-
79
- (endX, endY) = (min(w - 1, endX), min(h - 1, endY))
80
-
81
-
82
-
83
- face = frame[startY:endY, startX:endX]
84
-
85
- if face.any():
86
-
87
- face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
88
-
89
- face = cv2.resize(face, (224, 224))
90
-
91
- face = img_to_array(face)
92
-
93
- face = preprocess_input(face)
94
-
95
-
96
-
97
- if len(faces) > 0:
98
-
99
- faces = np.array(faces, dtype="float32")
100
-
101
- preds = maskNet.predict(faces, batch_size=32)
102
-
103
-
104
-
105
- return (locs, preds)
106
-
107
-
49
+ # grab the dimensions of the frame and then construct a blob
50
+
51
+ # from it
52
+
53
+ (h, w) = frame.shape[:2]
54
+
55
+ blob = cv2.dnn.blobFromImage(frame, 1.0, (300, 300),
56
+
57
+ (104.0, 177.0, 123.0))
58
+
59
+
60
+
61
+ # pass the blob through the network and obtain the face detections
62
+
63
+ faceNet.setInput(blob)
64
+
65
+ detections = faceNet.forward()
66
+
67
+
68
+
69
+ # initialize our list of faces, their corresponding locations,
70
+
71
+ # and the list of predictions from our face mask network
72
+
73
+ faces = []
74
+
75
+ locs = []
76
+
77
+ preds = []
78
+
79
+
80
+
81
+ # loop over the detections
82
+
83
+ for i in range(0, detections.shape[2]):
84
+
85
+ # extract the confidence (i.e., probability) associated with
86
+
87
+ # the detection
88
+
89
+ confidence = detections[0, 0, i, 2]
90
+
91
+
92
+
93
+ # filter out weak detections by ensuring the confidence is
94
+
95
+ # greater than the minimum confidence
96
+
97
+ if confidence > args["confidence"]:
98
+
99
+ # compute the (x, y)-coordinates of the bounding box for
100
+
101
+ # the object
102
+
103
+ box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
104
+
105
+ (startX, startY, endX, endY) = box.astype("int")
106
+
107
+
108
+
109
+ # ensure the bounding boxes fall within the dimensions of
110
+
111
+ # the frame
112
+
113
+ (startX, startY) = (max(0, startX), max(0, startY))
114
+
115
+ (endX, endY) = (min(w - 1, endX), min(h - 1, endY))
116
+
117
+
118
+
119
+ # extract the face ROI, convert it from BGR to RGB channel
120
+
121
+ # ordering, resize it to 224x224, and preprocess it
122
+
123
+ face = frame[startY:endY, startX:endX]
124
+
125
+ if face.any():
126
+
127
+ face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
128
+
129
+ face = cv2.resize(face, (224, 224))
130
+
131
+ face = img_to_array(face)
132
+
133
+ face = preprocess_input(face)
134
+
135
+
136
+
137
+ # only make a predictions if at least one face was detected
138
+
139
+ if len(faces) > 0:
140
+
141
+ # for faster inference we'll make batch predictions on *all*
142
+
143
+ # faces at the same time rather than one-by-one predictions
144
+
145
+ # in the above `for` loop
146
+
147
+ faces = np.array(faces, dtype="float32")
148
+
149
+ preds = maskNet.predict(faces, batch_size=32)
150
+
151
+
152
+
153
+ # return a 2-tuple of the face locations and their corresponding
154
+
155
+ # locations
156
+
157
+ return (locs, preds)
158
+
159
+
160
+
161
+ # construct the argument parser and parse the arguments
108
162
 
109
163
  ap = argparse.ArgumentParser()
110
164
 
111
165
  ap.add_argument("-f", "--face", type=str,
112
166
 
113
- default="face_detector",
167
+ default="face_detector",
114
-
168
+
115
- help="path to face detector model directory")
169
+ help="path to face detector model directory")
116
170
 
117
171
  ap.add_argument("-m", "--model", type=str,
118
172
 
119
- default="mask_detector.model",
173
+ default="mask_detector.model",
120
-
174
+
121
- help="path to trained face mask detector model")
175
+ help="path to trained face mask detector model")
122
176
 
123
177
  ap.add_argument("-c", "--confidence", type=float, default=0.5,
124
178
 
125
- help="minimum probability to filter weak detections")
179
+ help="minimum probability to filter weak detections")
126
180
 
127
181
  args = vars(ap.parse_args())
128
182
 
129
183
 
130
184
 
185
+ # load our serialized face detector model from disk
186
+
131
187
  print("[INFO] loading face detector model...")
132
188
 
133
189
  prototxtPath = os.path.sep.join([args["face"], "deploy.prototxt"])
134
190
 
135
191
  weightsPath = os.path.sep.join([args["face"],
136
192
 
137
- "res10_300x300_ssd_iter_140000.caffemodel"])
193
+ "res10_300x300_ssd_iter_140000.caffemodel"])
138
194
 
139
195
  faceNet = cv2.dnn.readNet(prototxtPath, weightsPath)
140
196
 
141
197
 
142
198
 
199
+ # load the face mask detector model from disk
200
+
143
201
  print("[INFO] loading face mask detector model...")
144
202
 
145
203
  maskNet = load_model(args["model"])
146
204
 
147
205
 
148
206
 
207
+ # initialize the video stream and allow the camera sensor to warm up
208
+
149
209
  print("[INFO] starting video stream...")
150
210
 
151
211
  vs = VideoStream(src=0).start()
@@ -154,58 +214,92 @@
154
214
 
155
215
 
156
216
 
217
+ # loop over the frames from the video stream
218
+
157
219
  while True:
158
220
 
159
- frame = vs.read()
160
-
161
- frame = imutils.resize(frame, width=400)
162
-
163
-
164
-
165
- (locs, preds) = detect_and_predict_mask(frame, faceNet, maskNet)
166
-
167
-
168
-
169
- for (box, pred) in zip(locs, preds):
170
-
171
- (startX, startY, endX, endY) = box
172
-
173
- (mask, withoutMask) = pred
174
-
175
-
176
-
177
- label = "Mask" if mask > withoutMask else "No Mask"
178
-
179
- color = (0, 255, 0) if label == "Mask" else (0, 0, 255)
180
-
181
-
182
-
183
- label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100)
184
-
185
-
186
-
187
- cv2.putText(frame, label, (startX, startY - 10),
188
-
189
- cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2)
190
-
191
- cv2.rectangle(frame, (startX, startY), (endX, endY), color, 2)
192
-
193
-
194
-
195
- cv2.imshow("Frame", frame)
196
-
197
- key = cv2.waitKey(1) & 0xFF
198
-
199
-
200
-
201
- if key == ord("q"):
202
-
203
- break
204
-
205
-
221
+ # grab the frame from the threaded video stream and resize it
222
+
223
+ # to have a maximum width of 400 pixels
224
+
225
+ frame = vs.read()
226
+
227
+ frame = imutils.resize(frame, width=400)
228
+
229
+
230
+
231
+ # detect faces in the frame and determine if they are wearing a
232
+
233
+ # face mask or not
234
+
235
+ (locs, preds) = detect_and_predict_mask(frame, faceNet, maskNet)
236
+
237
+
238
+
239
+ # loop over the detected face locations and their corresponding
240
+
241
+ # locations
242
+
243
+ for (box, pred) in zip(locs, preds):
244
+
245
+ # unpack the bounding box and predictions
246
+
247
+ (startX, startY, endX, endY) = box
248
+
249
+ (mask, withoutMask) = pred
250
+
251
+
252
+
253
+ # determine the class label and color we'll use to draw
254
+
255
+ # the bounding box and text
256
+
257
+ label = "Mask" if mask > withoutMask else "No Mask"
258
+
259
+ color = (0, 255, 0) if label == "Mask" else (0, 0, 255)
260
+
261
+
262
+
263
+ # include the probability in the label
264
+
265
+ label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100)
266
+
267
+
268
+
269
+ # display the label and bounding box rectangle on the output
270
+
271
+ # frame
272
+
273
+ cv2.putText(frame, label, (startX, startY - 10),
274
+
275
+ cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 2)
276
+
277
+ cv2.rectangle(frame, (startX, startY), (endX, endY), color, 2)
278
+
279
+
280
+
281
+ # show the output frame
282
+
283
+ cv2.imshow("Frame", frame)
284
+
285
+ key = cv2.waitKey(1) & 0xFF
286
+
287
+
288
+
289
+ # if the `q` key was pressed, break from the loop
290
+
291
+ if key == ord("q"):
292
+
293
+ break
294
+
295
+
296
+
297
+ # do a bit of cleanup
206
298
 
207
299
  cv2.destroyAllWindows()
208
300
 
209
301
  vs.stop()
210
302
 
303
+
304
+
211
305
  ```

1

Pythonと追加しました

2021/12/21 23:29

投稿

nample
nample

スコア1

test CHANGED
File without changes
test CHANGED
@@ -12,6 +12,8 @@
12
12
 
13
13
 
14
14
 
15
+ ```Python
16
+
15
17
  from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
16
18
 
17
19
  from tensorflow.keras.preprocessing.image import img_to_array
@@ -205,3 +207,5 @@
205
207
  cv2.destroyAllWindows()
206
208
 
207
209
  vs.stop()
210
+
211
+ ```