def __init__(self, model: str = 'mobilenetv2', cuda: bool = True, model_path: str = "", use_face_detector: bool = True): """ Generate an autocropper instance :param model: str mobilenetv2 or shufflenetv2 :param cuda: bool set True if cuda if inference on GPU :param model_path: str :param use_face_detector: bool: default True """ self.cuda = True if cuda and torch.cuda.is_available() else False self.cropper = build_crop_model(model=model, cuda=self.cuda, model_path=model_path) self.face_detector = None if use_face_detector: self.face_detector = face_detection.build_detector( "DSFDDetector", confidence_threshold=0.5, nms_iou_threshold=0.3) if self.cuda: self.cropper = torch.nn.DataParallel(self.cropper) self.cropper.cuda()
def __init__(self): # Initialize a Face Detector # Confidence Threshold can be Adjusted, Greater values would Detect only Clear Faces self.detector = face_detection.build_detector("DSFDDetector", confidence_threshold=.5, nms_iou_threshold=.3) # Define Blurring Kernel Size Ranges, a Random Size would be chosen in the Specified Ranges # Greater the Size, Higher is the Blurring Effect (Adjustments can be made according to the needs) self.motion_blur_kernel_range = (4, 8) self.average_blur_kernel_range = (3, 7) self.gaussian_blur_kernel_range = (3, 8) # Set Blurring Kernels to Use and their associated Probabilities self.Blurring_Kernels = ["none", "motion", "gaussian", "average"] self.Probs = [0.75, 0.1, 0.05, 0.1] # Set the Hyper-Parameters self.alpha = 0.00001 self.n_epochs = 5 self.mini_batch_size = 32 self.MODEL_SAVE_PATH = "Models/ResNet50_Classifier.h5" # Set Path to the Dataset # Faces would be extracted and placed in the specified Directory after Processing self.Dataset_PATH = "Training_Data" self.Processed_Dataset_PATH = "Processed_Training_Data"
def __init__(self, face_detector_cfg: dict, simple_expand: bool, align_faces: bool, resize_background: bool, generator_imsize: int, *args, **kwargs): self.face_detector = face_detection.build_detector( **face_detector_cfg, device=torch_utils.get_device()) self.simple_expand = simple_expand self.align_faces = align_faces self.resize_background = resize_background self.generator_imsize = generator_imsize if self.__class__.__name__ == "BaseDetector": assert face_detector_cfg.name == "RetinaNetResNet50"
def get_models(): detector = face_detection.build_detector("DSFDDetector", confidence_threshold=.5, nms_iou_threshold=.3) mask_classifier = load_model("ResNet50_Classifier2.h5") net = cv2.dnn.readNet("Models/"+"yolov4_tiny.weights", "Models/"+"yolov4_tiny.cfg") classes = [] with open("Models/"+"coco.names", "r") as f: classes = [line.strip() for line in f.readlines()] layer_names = net.getLayerNames() output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] return detector, mask_classifier, classes, output_layers, net
def __init__(self, proxy_map, startup_check=False): super(SpecificWorker, self).__init__(proxy_map) self.Period = 50 if startup_check: self.startup_check() else: self.timer.timeout.connect(self.compute) self.timer.start(self.Period) self.bb_color = [0, 0, 255] self.bb_thickness = 3 self.text_color = (255, 255, 255) self.fps_text_color = (0, 0, 0) # Select face_detection = 1 for dlib and 2 for RetinaNetMobile self.face_detection = 2 if (self.face_detection == 2): self.detector = face_detection.build_detector( "RetinaNetMobileNetV1", max_resolution=1080)
def __init__(self, proxy_map): super(SpecificWorker, self).__init__(proxy_map) self.timer.timeout.connect(self.compute) self.Period = 50 self.timer.start(self.Period) self.bb_color = [0,0,255] self.bb_thickness = 3 self.text_color = (255,255,255) self.fps_text_color = (0,0,0) self.class_names = ['Angry', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral'] self.frame_count = np.zeros((6)) # Select face_detection = 1 for dlib and 2 for RetinaNetMobile self.face_detection_method = 2 if (self.face_detection_method == 2): self.detector = face_detection.build_detector("RetinaNetMobileNetV1", max_resolution=1080) self.fig = plt.gcf() self.fig.show() self.fig.canvas.draw()
def __init__(self, inpVidFilePath): # Path to the Working Environment # If using Google Colab (If on a Local Environment, no path required => set BASE_PATH = "") self.BASE_PATH = "../" # Path to Input Video File in the BASE_PATH self.FILE_PATH = inpVidFilePath # Initialize a Face Detector # Confidence Threshold can be Adjusted, Greater values would Detect only Clear Faces self.detector = face_detection.build_detector("DSFDDetector", confidence_threshold=.5, nms_iou_threshold=.3) # Load Pretrained Face Mask Classfier (Keras Model) self.mask_classifier = load_model("../Models/ResNet50_Classifier.h5") # Set the Safe Distance in Pixel Units (Minimum Distance Expected to be Maintained between People) # This Parameter would Affect the Results, Adjust according to the Footage captured by CCTV Camera self.threshold_distance = 150 # Try with different Values before Finalizing ##################################### Analyze the Video ################################################ # Load YOLOv3 self.net = cv2.dnn.readNet( self.BASE_PATH + "Models/" + "yolov3.weights", self.BASE_PATH + "Models/" + "yolov3.cfg") # Load COCO Classes classes = [] with open(self.BASE_PATH + "Models/" + "coco.names", "r") as f: self.classes = [line.strip() for line in f.readlines()] layer_names = self.net.getLayerNames() self.output_layers = [ layer_names[i[0] - 1] for i in self.net.getUnconnectedOutLayers() ]
if not os.path.exists(feature_path): os.makedirs(feature_path) if not os.path.exists(score_path): os.makedirs(score_path) if not os.path.exists(predictions_path): os.makedirs(predictions_path) #=========================================================== # get the croped face by use dsfd module imgs = os.listdir(old_path) temps = [int(img.split('.')[0]) for img in imgs] temps = sorted(temps) imgs = [old_path + str(temp) + '.jpg' for temp in temps] # file='bad_ones.txt' detector = face_detection.build_detector("DSFDDetector", confidence_threshold=.7, nms_iou_threshold=.3, max_resolution=1080) t0 = time.time() for img_name in imgs: img_cv2 = cv2.imread(img_name) img = Image.open(img_name) w, h = img.size dets = detector.detect(img_cv2[:, :, ::-1])[:, :4] if len(dets) == 0: final_box = [ int(w * 7 / 24), int(h * 7 / 24), int(w * 17 / 24), int(h * 17 / 24) ]
import os import cv2 import time import face_detection def draw_faces(im, bboxes): for bbox in bboxes: x0, y0, x1, y1 = [int(_) for _ in bbox] cv2.rectangle(im, (x0, y0), (x1, y1), (0, 0, 255), 2) if __name__ == "__main__": impaths = "images" impaths = glob.glob(os.path.join(impaths, "*.jpg")) detector = face_detection.build_detector("DSFDDetector", max_resolution=1080) for impath in impaths: if impath.endswith("out.jpg"): continue im = cv2.imread(impath) print("Processing:", impath) t = time.time() dets = detector.detect(im[:, :, ::-1])[:, :4] print(f"Detection time: {time.time()- t:.3f}") draw_faces(im, dets) imname = os.path.basename(impath).split(".")[0] output_path = os.path.join(os.path.dirname(impath), f"{imname}_out.jpg") cv2.imwrite(output_path, im)
import numpy as np import cv2 as cv import sys import face_detection 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 detector = face_detection.build_detector("RetinaNetMobileNetV1", confidence_threshold=.5, nms_iou_threshold=.3) mask_classifier = load_model("best.h5") imgNum = sys.argv[1] origimg = cv.imread(str(imgNum) + ".png") w, h = origimg.shape[:2] if w < 1000 and h < 1000: origimg = cv.resize(origimg, (h * 2, w * 2)) img = cv.cvtColor(origimg, cv.COLOR_BGR2RGB) detection = detector.detect(img) faces = [] locs = [] for i in range(detection.shape[0]): xmin, ymin, xmax, ymax = detection[i, :4].astype("int") face = origimg[ymin:ymax, xmin:xmax] face = cv.resize(face, (100, 100)) face = img_to_array(face)
f.write('%s\n' % image_name) f.write("%d\n" % len(bboxes)) for bbox in bboxes: x0, y0, x1, y1, score = [float(_) for _ in bbox] w, h = x1 - x0, y1 - y0 f.write('%d %d %d %d %f\n' % (x0, y0, w, h, score)) #cv2.rectangle(im, (x0, y0), (x1, y1), (0, 0, 255), 2) if __name__ == "__main__": args = parseArgs() impaths = "images" impaths = glob.glob(os.path.join(impaths, "*.jpg")) detector = face_detection.build_detector("DSFDDetector", max_resolution=2160, confidence_threshold=0.01, nms_iou_threshold=0.3) with open(args.val_file) as f: val_files = list(map(str.strip, f.readlines())) for val in tqdm(val_files): if args.correction != '': image_id = val.replace('jpg', 'txt').split('/')[-2:] output_file = os.path.join( '../CORRECTIONS', '%s_%s' % (args.noise, args.correction), "%s_%s" % (args.noise_param, args.correction_param), 'dsfd', '/'.join(image_id)) else: output_file = val.replace('images', 'detections') output_arr = output_file.split('/') output_arr.insert(-2, 'dsfd')
import cv2 import time import face_detection import tqdm if __name__ == "__main__": num = 1000 for detector in face_detection.available_detectors: detector = face_detection.build_detector(detector, fp16_inference=True) im = "images/0_Parade_Parade_0_873.jpg" im = cv2.imread(im)[:, :, ::-1] t = time.time() for i in tqdm.trange(num): dets = detector.detect(im) total_time = time.time() - t avg_time = total_time / num fps = 1 / avg_time ms = avg_time * 1000 print( f"Detector: {detector.__class__.__name__}. Average inference time over image shape: {im.shape} is:", f"{ms:.2f} ms, fps: {fps:.2f}")
def main(): cascPath = sys.argv[1] modelPath = sys.argv[2] faceCascade = cv2.CascadeClassifier(cascPath) modelPath = sys.argv[2] mask_classifier = load_model(modelPath) detector = face_detection.build_detector("DSFDDetector", confidence_threshold=.5, nms_iou_threshold=.3) video_capture = cv2.VideoCapture(0) FILE_PATH = "/Users/milly./Desktop/mask.jpg" img = cv2.imread(FILE_PATH) i = 0 masked_faces = [] unmasked_faces = [] while True: # Capture frame-by-frame ret, frame = video_capture.read() # frame = img gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = [] if i % 500 == 0: faces = detector.detect(frame[:, :, ::-1]) print(faces) if len(faces) > 0 and faces.shape[0] > 0: masked_faces = [] unmasked_faces = [] for i in range(faces.shape[0]): # Get Co-ordinates x1 = int(faces[i][0]) x2 = int(faces[i][2]) y1 = int(faces[i][1]) y2 = int(faces[i][3]) # Predict Output face_arr = cv2.resize(frame[y1:y2, x1:x2, ::-1], (224, 224), interpolation=cv2.INTER_NEAREST) face_arr = np.expand_dims(face_arr, axis=0) face_arr = resnet50.preprocess_input(face_arr) match = mask_classifier.predict(face_arr) if match[0][0] < 0.5: masked_faces.append([x1, y1, x2, y2]) else: unmasked_faces.append([x1, y1, x2, y2]) # Put Bounding Box on the Faces (Green:Masked,Red:Not-Masked) for f in range(len(masked_faces)): a, b, c, d = masked_faces[f] cv2.rectangle(frame, (a, b), (c, d), (0, 255, 0), 2) for f in range(len(unmasked_faces)): a, b, c, d = unmasked_faces[f] cv2.rectangle(frame, (a, b), (c, d), (0, 0, 255), 2) # Display the resulting frame cv2.imshow('Video', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break i += 1 # When everything is done, release the capture video_capture.release() cv2.destroyAllWindows()
def init_detector(model=DETECTOR_MODEL): detector = face_detection.build_detector(model, confidence_threshold=.99, max_resolution=1080) return detector
def model_fn(model_dir): return face_detection.build_detector( name=MODEL_NAME, confidence_threshold=CONFIDENCE_THRESHOLD, nms_iou_threshold=NMS_IOU_THRESHOLD)
import cv2 import time import face_detection import tqdm if __name__ == "__main__": num = 1000 for detector in face_detection.available_detectors: detector = face_detection.build_detector( detector ) im = "images/0_Parade_Parade_0_873.jpg" im = cv2.imread(im)[:, :, ::-1] t = time.time() for i in tqdm.trange(num): dets = detector.detect(im) total_time = time.time() - t avg_time = total_time / num print( f"Detector: {detector}. Average inference time over image shape: {im.shape} is:", f"{avg_time} s")
import numpy as np import cv2 as cv import sys import face_detection detector = face_detection.build_detector("DSFDDetector", confidence_threshold=.5, nms_iou_threshold=.3) imgNum = sys.argv[1] origimg = cv.imread(str(imgNum) + ".png") img = cv.cvtColor(origimg, cv.COLOR_BGR2RGB) detection = detector.detect(img) faces = [] for i in range(detection.shape[0]): xmin, ymin, xmax, ymax = detection[i, :4].astype("int") img = cv.rectangle(origimg, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2) cv.imshow('img', origimg) # cv.imwrite('haaraltex1.png',img) cv.waitKey(0) cv.destroyAllWindows()
model2.load_weights(weight_file2) print('Finished loading model 2.') weight_file3 = '/content/FSA-Net/pre-trained/300W_LP_models/fsanet_noS_capsule_3_16_2_192_5/fsanet_noS_capsule_3_16_2_192_5.h5' model3.load_weights(weight_file3) print('Finished loading model 3.') inputs = Input(shape=(64,64,3)) x1 = model1(inputs) #1x1 x2 = model2(inputs) #var x3 = model3(inputs) #w/o avg_model = Average()([x1,x2,x3]) model = Model(inputs=inputs, outputs=avg_model) detector = face_detection.build_detector( "RetinaNetResNet50", confidence_threshold=.5, nms_iou_threshold=.3) """ SORT: A Simple, Online and Realtime Tracker Copyright (C) 2016 Alex Bewley [email protected] This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. """