def predFrame(frame): gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_detection.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE) if len(faces) > 0: with session.as_default(): with session.graph.as_default(): faces = sorted(faces, reverse=True, key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))[0] (fX, fY, fW, fH) = faces roi = gray[fY:fY + fH, fX:fX + fW] roi = cv2.resize(roi, (64, 64)) roi = roi.astype("float") / 255.0 roi = img_to_array(roi) roi = np.expand_dims(roi, axis=0) preds = emotion_classifier.predict(roi)[0] return preds else: return [0, 0, 0, 0, 0, 0, 0]
def load_image_into_numpy_array(path_to_image, imgdim=(96, 96, 1), grayScale=True): if None != imgdim: img = image.load_img(path_to_image, grayscale=grayScale, target_size=(imgdim[0], imgdim[1])) else: img = image.load_img(path_to_image, grayscale=grayScale, target_size=None) x = image.img_to_array(img).astype(np.uint8) return x
def load_image_for_classification(path_to_image, imgdim=(96, 96, 1), expandDims=True, grayScale=True): if imgdim != None: img = image.load_img(path_to_image, grayscale=grayScale, target_size=(imgdim[0], imgdim[1])) else: img = image.load_img(path_to_image, grayscale=grayScale) x = image.img_to_array(img).astype(np.uint8) if expandDims is True: x = np.expand_dims(x, axis=0) x = x / 255 return x
ret, frame = video_capture.read() faces = faceCascade.detectMultiScale(frame, scaleFactor=1.1, minNeighbors=5, minSize=(60, 60), flags=cv2.CASCADE_SCALE_IMAGE) faces_list = [] faces_list = np.array(faces_list).reshape(-1, 64, 64, 3) preds = [] for (x, y, w, h) in faces: face_frame = frame[y:y + h, x:x + w] face_frame = cv2.resize(face_frame, (64, 64)) plt.imshow(face_frame) face_frame = img_to_array(face_frame) face_frame = np.expand_dims(face_frame, axis=0) face_frame = preprocess_input(face_frame) # faces_list.append(face_frame) faces_list = np.append(faces_list, face_frame, axis=0) if len(faces_list) > 0: preds = model.predict(faces_list) for pred in preds: (withoutMask, mask, incorrectMask) = pred if mask > withoutMask and mask > incorrectMask: label = "Mask" elif withoutMask > mask and withoutMask > incorrectMask: label = "NoMask" elif incorrectMask > mask and incorrectMask > withoutMask: label = "IncorrectMask" if label == "Mask":
def load_img_4d(path): img = load_img(path) img = img_to_array(img) / 255 img = np.expand_dims(img, 0) return img
def generate_advs_same_bpp(orig_img_dir, compression_model, checkpoint_dir, result_dir, epsilon=1 / 255, num_steps=100, step_size=0.0001, use_grad_sign=False, img_size=(512, 768, 3), reconstruction_metric='mse', kappa=1e3, tau=0., bpp_offset=0.): # create result_dir if the dir does not exist yet if not os.path.exists(result_dir): os.makedirs(result_dir) # prepare compression model if compression_model == 'ICLR2017': from adv_compression.model_iclr2017 import load_model sess, keras_compression_model = load_model(checkpoint_dir) input_placeholder = None elif compression_model == 'ICLR2018': from adv_compression.model_iclr2018 import load_model sess, keras_compression_model, input_placeholder = load_model( checkpoint_dir) else: raise ValueError("%s is not a supported compression model" % compression_model) # prepare input examples files = os.listdir(orig_img_dir) img_files = [f for f in files if f.endswith(".png")] # create SubstituteGenerator object adv_generator = AdvGenerator( keras_compression_model=keras_compression_model, epsilon=epsilon, num_steps=num_steps, step_size=step_size, use_grad_sign=use_grad_sign, img_size=img_size, reconstruct_metric=reconstruction_metric, bpp_target=True, kappa=kappa, tau=tau, compression_model=compression_model, input_placeholder=input_placeholder) # generate perburbations on images orig_images = [] adv_images = [] for f in img_files: # keras load image x_image = load_img(orig_img_dir + f) x_image = img_to_array(x_image) x_image = x_image / 255 x_image = np.expand_dims(x_image, 0) orig_bpp = adv_generator.get_eval_bpp(sess, x_image) print('targeting bpp: %.4f' % orig_bpp) adv_image = adv_generator.perturb(sess, orig_image=x_image, bpp_target=orig_bpp + bpp_offset) # save generated image save_path = result_dir + 'adv_' + f save_img(save_path, array_to_img(adv_image[0])) orig_images.append(x_image) adv_images.append(adv_image) return orig_images, adv_images
def embed_croped_face(model, cropped): img = cv2.resize(cropped, (160, 160)) img = img_to_array(img) img = np.expand_dims(img, axis=0) vector = model.predict(img) return vector