File size: 13,741 Bytes
f496f54 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# # USAGE
# # python mask_detect_video.py --video your_video.mp4
# import tensorflow as tf
# tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
# from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
# from tensorflow.keras.preprocessing.image import img_to_array
# from tensorflow.keras.models import load_model
# import numpy as np
# import argparse
# import cv2
# import os
# def mask_video():
# # construct the argument parser and parse the arguments
# parser = argparse.ArgumentParser()
# parser.add_argument("-f", "--face", type=str, default="face_detector",
# help="Path to face detector model directory")
# parser.add_argument("-m", "--model", type=str, default="mask_detector.model",
# help="Path to trained face mask detector model")
# parser.add_argument('-s', '--size', type=int, default=64,
# help="Size of face image")
# parser.add_argument("-c", "--confidence", type=float, default=0.5,
# help="Minimum probability to filter weak detections")
# parser.add_argument("-v", "--video", type=str,
# help="Path to input video file")
# args = parser.parse_args()
# # Suppress TensorFlow INFO-level messages
# import tensorflow as tf
# tf.get_logger().setLevel('ERROR') # or 'WARNING' or 'INFO'
# # load our serialized face detector model from disk
# prototxtPath = os.path.sep.join([args.face, "deploy.prototxt"])
# weightsPath = os.path.sep.join([args.face, "res10_300x300_ssd_iter_140000.caffemodel"])
# net = cv2.dnn.readNet(prototxtPath, weightsPath)
# # load the face mask detector model from disk
# model = load_model(args.model)
# # initialize the video stream
# if args.video:
# vs = cv2.VideoCapture(args.video)
# else:
# print("[ERROR] No video file provided.")
# return
# while True:
# # grab the frame from the video stream
# (grabbed, frame) = vs.read()
# # if the frame was not grabbed, then we have reached the end
# # of the stream
# if not grabbed:
# break
# # detect faces in the frame
# detect_and_draw(frame, net, model, args)
# # show the output frame
# cv2.imshow("Frame", frame)
# # break the loop if the 'q' key is pressed
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
# # release the video stream and close any open windows
# vs.release()
# cv2.destroyAllWindows()
# def detect_and_draw(frame, net, model, args):
# (h, w) = frame.shape[:2]
# blob = cv2.dnn.blobFromImage(frame, scalefactor=1.0, mean=(104.0, 177.0, 123.0))
# net.setInput(blob)
# detections = net.forward()
# for i in range(0, detections.shape[2]):
# confidence = detections[0, 0, i, 2]
# if confidence < args.confidence:
# # Drop low confidence detections
# continue
# box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
# (startX, startY, endX, endY) = box.astype("int")
# (startX, startY) = (max(0, startX), max(0, startY))
# (endX, endY) = (min(w - 1, endX), min(h - 1, endY))
# try:
# face = frame[startY:endY, startX:endX]
# face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
# face = cv2.resize(face, (args.size, args.size))
# face = img_to_array(face)
# face = preprocess_input(face)
# face = np.expand_dims(face, axis=0)
# mask = model.predict(face)[0]
# label = "Mask" if mask < 0.5 else "No Mask"
# color = (0, 255, 0) if label == "Mask" else (0, 0, 255)
# # display the label and bounding box rectangle on the output frame
# cv2.putText(frame, label, (startX, startY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 1)
# cv2.rectangle(frame, (startX, startY), (endX, endY), color, 2)
# except Exception as e:
# print(e)
# if __name__ == "__main__":
# mask_video()
# # USAGE
# # python mask_detect_video.py --video your_video.mp4
# import tensorflow as tf
# tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
# from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
# import numpy as np
# import argparse
# import cv2
# import os
# def mask_video():
# # construct the argument parser and parse the arguments
# parser = argparse.ArgumentParser()
# parser.add_argument("-f", "--face", type=str, default="face_detector",
# help="Path to face detector model directory")
# parser.add_argument("-m", "--model", type=str, default="mask_detector.model",
# help="Path to trained face mask detector model")
# parser.add_argument('-s', '--size', type=int, default=64,
# help="Size of face image")
# parser.add_argument("-c", "--confidence", type=float, default=0.5,
# help="Minimum probability to filter weak detections")
# parser.add_argument("-v", "--video", type=str,
# help="Path to input video file")
# args = parser.parse_args()
# # Suppress TensorFlow INFO-level messages
# import tensorflow as tf
# tf.get_logger().setLevel('ERROR') # or 'WARNING' or 'INFO'
# # load our serialized face detector model from disk
# prototxtPath = os.path.sep.join([args.face, "deploy.prototxt"])
# weightsPath = os.path.sep.join([args.face, "res10_300x300_ssd_iter_140000.caffemodel"])
# net = cv2.dnn.readNet(prototxtPath, weightsPath)
# # initialize the video stream
# if args.video:
# vs = cv2.VideoCapture(args.video)
# else:
# print("[ERROR] No video file provided.")
# return
# while True:
# # grab the frame from the video stream
# (grabbed, frame) = vs.read()
# # if the frame was not grabbed, then we have reached the end
# # of the stream
# if not grabbed:
# break
# # detect faces in the frame
# detect_and_draw(frame, net, args)
# # show the output frame
# cv2.imshow("Frame", frame)
# # break the loop if the 'q' key is pressed
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
# # release the video stream and close any open windows
# vs.release()
# cv2.destroyAllWindows()
# def detect_and_draw(frame, net, args):
# (h, w) = frame.shape[:2]
# blob = cv2.dnn.blobFromImage(frame, scalefactor=1.0, mean=(104.0, 177.0, 123.0))
# net.setInput(blob)
# detections = net.forward()
# for i in range(0, detections.shape[2]):
# confidence = detections[0, 0, i, 2]
# if confidence < args.confidence:
# # Drop low confidence detections
# continue
# box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
# (startX, startY, endX, endY) = box.astype("int")
# (startX, startY) = (max(0, startX), max(0, startY))
# (endX, endY) = (min(w - 1, endX), min(h - 1, endY))
# # draw a dark pink solid rectangle around the detected face
# frame[startY:endY, startX:endX, :] = (136, 28, 238)
# if __name__ == "__main__":
# mask_video()
# import tensorflow as tf
# tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
# from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
# import numpy as np
# import argparse
# import cv2
# import os
# def mask_video():
# # construct the argument parser and parse the arguments
# parser = argparse.ArgumentParser()
# parser.add_argument("-f", "--face", type=str, default="face_detector",
# help="Path to face detector model directory")
# parser.add_argument("-m", "--model", type=str, default="mask_detector.model",
# help="Path to trained face mask detector model")
# parser.add_argument('-s', '--size', type=int, default=64,
# help="Size of face image")
# parser.add_argument("-c", "--confidence", type=float, default=0.5,
# help="Minimum probability to filter weak detections")
# parser.add_argument("-v", "--video", type=str,
# help="Path to input video file")
# parser.add_argument("-o", "--output", type=str, default="output.mp4",
# help="Path to save the output video file with .mp4 extension")
# args = parser.parse_args()
# # Suppress TensorFlow INFO-level messages
# import tensorflow as tf
# tf.get_logger().setLevel('ERROR') # or 'WARNING' or 'INFO'
# # load our serialized face detector model from disk
# prototxtPath = os.path.sep.join([args.face, "deploy.prototxt"])
# weightsPath = os.path.sep.join([args.face, "res10_300x300_ssd_iter_140000.caffemodel"])
# net = cv2.dnn.readNet(prototxtPath, weightsPath)
# # initialize the video stream
# if args.video:
# vs = cv2.VideoCapture(args.video)
# else:
# print("[ERROR] No video file provided.")
# return
# # Define the codec and create a VideoWriter object
# fourcc = cv2.VideoWriter_fourcc(*"mp4v")
# out = cv2.VideoWriter(args.output, fourcc, 20.0, (int(vs.get(3)), int(vs.get(4))))
# while True:
# # grab the frame from the video stream
# (grabbed, frame) = vs.read()
# # if the frame was not grabbed, then we have reached the end
# # of the stream
# if not grabbed:
# break
# # detect faces in the frame
# detect_and_draw(frame, net, args)
# # write the output frame to the video file
# out.write(frame)
# # release the video stream and close any open windows
# vs.release()
# out.release()
# cv2.destroyAllWindows()
# def detect_and_draw(frame, net, args):
# (h, w) = frame.shape[:2]
# blob = cv2.dnn.blobFromImage(frame, scalefactor=1.0, mean=(104.0, 177.0, 123.0))
# net.setInput(blob)
# detections = net.forward()
# for i in range(0, detections.shape[2]):
# confidence = detections[0, 0, i, 2]
# if confidence < args.confidence:
# # Drop low confidence detections
# continue
# box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
# (startX, startY, endX, endY) = box.astype("int")
# (startX, startY) = (max(0, startX), max(0, startY))
# (endX, endY) = (min(w - 1, endX), min(h - 1, endY))
# # draw a dark pink solid rectangle around the detected face
# frame[startY:endY, startX:endX, :] = (136, 28, 238)
# if __name__ == "__main__":
# mask_video()
# import tensorflow as tf
# from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
# import numpy as np
# import cv2
# import os
# from werkzeug.datastructures import FileStorage # Use this if you're using Flask for handling file uploads
# def mask_video(video, output_path="output.mp4"):
# # Suppress TensorFlow INFO-level messages
# # tf.get_logger().setLevel('ERROR') # or 'WARNING' or 'INFO'
# face_path="privacy/util/face_detect/face_detector"
# model_path="privacy/util/face_detect/results/Xception-size-64-bs-32-lr-0.0001.h5"
# size=64
# confidence=0.5
# # Load our serialized face detector model from disk
# prototxtPath = os.path.sep.join([face_path, "deploy.prototxt"])
# weightsPath = os.path.sep.join([face_path, "res10_300x300_ssd_iter_140000.caffemodel"])
# net = cv2.dnn.readNet(prototxtPath, weightsPath)
# print("STARTED")
# # If video is a FileStorage object, save it to a temporary file
# if isinstance(video, FileStorage):
# video_path = "temp_video.mp4"
# video.save(video_path)
# else:
# video_path = video # Assume it's already a file path
# # Initialize the video stream
# vs = cv2.VideoCapture(video_path)
# # Define the codec and create a VideoWriter object
# fourcc = cv2.VideoWriter_fourcc(*"mp4v")
# out = cv2.VideoWriter(output_path, fourcc, 20.0, (int(vs.get(3)), int(vs.get(4))))
# while True:
# # Grab the frame from the video stream
# (grabbed, frame) = vs.read()
# # If the frame was not grabbed, we have reached the end of the stream
# if not grabbed:
# break
# # Detect faces in the frame
# detect_and_draw(frame, net, size, confidence)
# # Write the output frame to the video file
# out.write(frame)
# # Release the video stream and close any open windows
# vs.release()
# out.release()
# cv2.destroyAllWindows()
# # If a temporary video file was created, remove it
# if isinstance(video, FileStorage):
# os.remove(video_path)
# def detect_and_draw(frame, net, size, confidence):
# (h, w) = frame.shape[:2]
# blob = cv2.dnn.blobFromImage(frame, scalefactor=1.0, mean=(104.0, 177.0, 123.0))
# net.setInput(blob)
# detections = net.forward()
# for i in range(0, detections.shape[2]):
# conf = detections[0, 0, i, 2]
# if conf < confidence:
# # Drop low confidence detections
# continue
# box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
# (startX, startY, endX, endY) = box.astype("int")
# (startX, startY) = (max(0, startX), max(0, startY))
# (endX, endY) = (min(w - 1, endX), min(h - 1, endY))
# # Draw a dark pink solid rectangle around the detected face
# frame[startY:endY, startX:endX, :] = (136, 28, 238)
|