Esempio n. 1
0
class Video_Reader:
    def __init__(self):
        self._mtcnn_file = '/media/nas2/Deepfakedeetectionchallenge/mtcnn-0.1.0-py3-none-any.whl'
        self._THRESHOLD = 0.9
        self._interpolate_FLAG = 1
        self._detector = MTCNN()
        self._output_path = '/media/nas2/Shengbang/DFDC_CNN_LSTM_DATA/'
           
    def extract_frames_from_video(self, filename, num_frame, display=False):
        pass

    def extract_faces_from_video(self, filename, interpolate_scale, num_frame, face_size=150, display=False, return_faces=True):
        # input: filename, interpolate scale you want, number of frame you want.
        # output: frame_list, output bounding box.
        #start_time = time.process_time()
        if(interpolate_scale!=1):
            self._interpolate_FLAG = interpolate_scale
        if(filename[-4:]!= '.mp4'):
            raise ValueError("Input must be a mp4 file.")
        cap = cv2.VideoCapture(filename)
        x_list = []
        y_list = []
        w_list = []
        h_list = []
        frame_list = []
        if(cap.isOpened()==False):
            return 0,0,0,0,0
        for ii in range(num_frame):          
            ret, old_frame = cap.read()
            if(ret == False):
                return 0,0,0,0,0
            frame = cv2.cvtColor(old_frame, cv2.COLOR_BGR2RGB)
            frame_list.append(frame)
            if(ii % int(num_frame / interpolate_scale) == 0 or ii == (num_frame - 1)):
                faces = self._detector.detect_faces(frame)      
                
                # Two situation that we don't want this video
                if (faces == []):
                    print('no face found')
                    #end_time = time.process_time()
                    #print('Time: ', end_time - start_time)
                    return 0,0,0,0,0
                sorted(faces, key=lambda i: i['confidence'], reverse=True)
                if faces[0]['confidence'] < self._THRESHOLD:
                    print('Fail to find a face')
                    #end_time = time.process_time()
                    #print('Time: ', end_time - start_time)
                    return 0,0,0,0,0
                
                x,y,w,h=faces[0]['box']
                x_list.append(x + w/2)
                y_list.append(y + h/2)
                w_list.append(w)
                h_list.append(h)
        cap.release()
        output_x = list(range(num_frame))
        output_y = list(range(num_frame))
        output_x = self.cubic_interpolation(x_list, num_frame)
        output_y = self.cubic_interpolation(y_list, num_frame)
        w = np.mean(w_list)
        h = np.mean(h_list)
              
        # Display faces:      
        if(return_faces==True):
            face_list = []
            for ii, frame in enumerate(frame_list):
                x = output_x[ii]
                y = output_y[ii]
                #face_list.append(frame[int(y-face_size/2):int(y+face_size/2), int(x-face_size/2):int(x+face_size/2)])
                face_reg = frame[int(y-h/2):int(y+h/2), int(x-h/2):int(x+h/2)]
            
                if(y<h/2 or x<h/2 or face_reg.shape[0]!=face_reg.shape[1] or face_reg.shape.count(0)>0):
                    print("ROI too big.", face_reg.shape)
                    return 0,0,0,0,0
                #face_reg = cv2.cvtColor(face_reg, cv2.COLOR_RGB2GRAY)
                face_reg = cv2.resize(face_reg, (299, 299))
                face_list.append(face_reg)
            
            if(display==True):
                print('face region size: w:',w,'h:',h)
                for face in face_list:
                    plt.figure()
                    plt.imshow(face)
                    plt.show()
                    plt.close()
            #end_time = time.process_time()
            #print('Time: ', end_time - start_time)
            return face_list, output_x, output_y, w, h
        else:            
            if(display==True):
                print('face region size: w:',w,'h:',h)
                for ii, frame in enumerate(frame_list):
                    x = output_x[ii]
                    y = output_y[ii]
                    cv2.rectangle(frame, (x-int(w/2), y-int(h/2)), (x+int(w/2), y+int(h/2)), (255, 255, 255), 10)    
                    plt.figure()
                    plt.imshow(frame)
                    plt.show()
                    plt.close()
            #end_time = time.process_time()
            #print('Time: ', end_time - start_time)
            return frame_list, output_x, output_y, w, h
        
    def cubic_interpolation(self, input_list, length, show_spline=False):
        # get scale info from output list.
        input_array = np.array(input_list)
        scale = int(length / (len(input_list) - 1))
        x = np.array([i * scale for i in range(len(input_list))])
        b_n = interpolate.make_interp_spline(x, input_array)
        output_list = []
        for ii in range(length):
            output_list.append(int(b_n(ii)))
        if(show_spline==True):
            plt.plot([i * scale for i in range(len(input_list))], input_list)
            plt.plot(range(len(output_list)), output_list)
            plt.show()
        return output_list
        
    def set_face_detector_threshold(self, threshold):
        self._THRESHOLD = threshold
        
    def get_output_path(self):
        return self._output_path
Esempio n. 2
0
TRAIN_FAKE_DMAP_FOLDER = MAIN_PATH + 'train_fake_dmap'

IMG_PERCENT_PIXEL_THRESHOLD = 0.2
IMG_NOISE_THRESHOLD = 20

IMG_FORMAT = '.png'

#width = 300
#height = 300
ex = 0
video_frame_jump_fake = 10
video_frame_jump_real = 10
video_frame_jump_original = 10

# load detector
detector = MTCNN()

# Train Main Path

for k in range(1):

    TRAIN_SUB_FOLDER = 'dfdc_train_part_' + str(k)
    print(
        f"Train samples: {len(os.listdir(os.path.join(TRAIN_FOLDER,TRAIN_SUB_FOLDER)))}"
    )

    train_list = list(os.listdir(os.path.join(TRAIN_FOLDER, TRAIN_SUB_FOLDER)))
    ext_dict = []
    for file in train_list:
        file_ext = file.split('.')[1]
        if (file_ext not in ext_dict):
Esempio n. 3
0
from keras.preprocessing.image import img_to_array
import cv2
from keras.models import load_model
import numpy as np
from mtcnn import MTCNN
import json
detector = MTCNN()
# parameters for loading data and images
emotion_model_path = 'model/_mini_XCEPTION.102-0.66.hdf5'

# loading models
emotion_classifier = load_model(emotion_model_path, compile=False)
#emotion
EMOTIONS = [
    "angry", "disgust", "scared", "happy", "sad", "surprised", "neutral"
]


def predict_emotion(face):
    roi = cv2.resize(face, (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 dict((key, float(value)) for (key, value) in zip(EMOTIONS, preds))


def get_emotion(image):
    r = detector.detect_faces(image)
Esempio n. 4
0
                    "--update",
                    help="whether perform update the facebank",
                    action="store_true")
parser.add_argument("-tta",
                    "--tta",
                    help="whether test time augmentation",
                    action="store_true")
parser.add_argument("-c",
                    "--score",
                    help="whether show the confidence score",
                    action="store_true")
args = parser.parse_args()

conf = get_config(False)

mtcnn = MTCNN()
print('arcface loaded')

learner = face_learner(conf, True)
learner.threshold = args.threshold
if conf.device.type == 'cpu':
    learner.load_state(conf, 'cpu_final.pth', True, True)
else:
    learner.load_state(conf, 'final.pth', True, True)
learner.model.eval()
print('learner loaded')

if args.update:
    targets, names = prepare_facebank(conf, learner.model, mtcnn, tta=args.tta)
    print('facebank updated')
else:
Esempio n. 5
0
class FaceDAndR:
    def __init__(self):
        self.detector = MTCNN()
        self.vggface = VGGFace(model='resnet50',
                               include_top=False,
                               input_shape=(224, 224, 3),
                               pooling='avg')

    def getCentroid(self, bbox):
        cX = int((bbox[0] + bbox[2]) / 2.0)
        cY = int((bbox[1] + bbox[3]) / 2.0)
        return np.array((cX, cY))

    def getDist(self, p1, p2):
        return math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)

    def rectInsideRect(self, r1, r2, thresh=32):
        return r1[0] + thresh > r2[0] and r1[2] - thresh < r2[
            2] and r1[1] + thresh > r2[1] and r1[3] - thresh < r2[3]

    def extractFacesAndAssignToPeople(self,
                                      frame,
                                      bboxes,
                                      ids,
                                      cents,
                                      requiredSize=(224, 224),
                                      drawOnFrame=True):
        results = self.detector.detect_faces(frame)
        bboxes = bboxes.copy()  #poping from these to improve performance
        ids = ids.copy()  #poping
        cents = cents.copy()  #poping

        faces = []
        for result in results:
            x1, y1, width, height = result['box']
            x2, y2 = x1 + width, y1 + height
            face = frame[y1:y2, x1:x2]
            #image = Image.fromarray(face)

            #calculate distance
            #find the closest
            #check if it fits
            #if it fits, remove and assign this face id of the person
            centroid = self.getCentroid([x1, y1, x2, y2])
            dist = [self.getDist(c, centroid) for c in cents]
            if (len(dist) <= 0):
                continue

            ix = dist.index(min(dist))
            _rect = bboxes[ix]
            if not self.rectInsideRect([x1, y1, x2, y2], _rect):
                continue

            try:
                image = cv2.resize(face, requiredSize)
                _id = ids[ix]
                face_array = np.asarray(image)
                #adding embds too
                embd = self.getEmbedding(face_array)

                faces.append((face_array, (x1, y1, x2, y2), _id, embd))
                ids.pop(ix)
                cents.pop(ix)
                bboxes.pop(ix)
            except:
                print("face size isn't fit for the model")

            #draw
            if drawOnFrame:
                cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 255, 0), 3)
                text = "ID:" + str(_id)
                cv2.putText(frame, text, (x1, y1), cv2.FONT_HERSHEY_SIMPLEX,
                            0.5, (0, 255, 0), 2)

            #image = image.resize(requiredSize)

        return faces

    def extractFaces(self, frame, requiredSize=(224, 224), drawOnFrame=True):
        results = self.detector.detect_faces(frame)
        faces = []
        for result in results:
            x1, y1, width, height = result['box']
            x2, y2 = x1 + width, y1 + height
            face = frame[y1:y2, x1:x2]
            #image = Image.fromarray(face)

            #draw
            if drawOnFrame:
                cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 255, 0), 3)

            #image = image.resize(requiredSize)
            try:
                image = cv2.resize(face, requiredSize)
                face_array = np.asarray(image)
                faces.append((face_array, (x1, y1, x2, y2)))
            except:
                print("face size isn't fit for the model")

        return faces

    def getEmbedding(self, face):
        samples = np.expand_dims(face, 0)
        samples = (samples.astype('float64'))
        # prepare the face for the model, e.g. center pixels
        samples = preprocess_input(samples, version=2)
        yhat = self.vggface.predict(samples)
        return yhat

    def is_match(self, emb1, emb2, thresh=0.39):
        score = cosine(emb1, emb2)
        #print(score)
        if score <= thresh:
            #print('>face is a Match (%.3f <= %.3f)' % (score, thresh))
            return True
        else:
            #print('>face is NOT a Match (%.3f > %.3f)' % (score, thresh))
            return False
Esempio n. 6
0
class MxnetController:
    def __init__(self):
        self.detector = MTCNN()
        self.embedding_model = self.get_embedding_model(
            INSIGHT_MODEL_PATH, "fc1")
        print('Embedding model loaded')

    def calculate_score(self, emb1, emb2):
        total_score = CosineSimilarity(emb1, emb2)
        total_score -= MIN_COSINE_SIMILARITY
        total_score /= (MAX_COSINE_SIMILARITY - MIN_COSINE_SIMILARITY)
        total_score *= 100
        total_score = 100 - total_score
        return total_score

    def calculate_embedding(self, image):
        #detect for face
        detect_result = self.detector.detect_faces(image)
        for result in detect_result:
            # print(type(result))
            if result['confidence'] > DETECTION_SCORE_THRESHOLD:
                # print(result)
                bbox = np.array(result['box'])
                bbox[bbox < 0] = 0

                landmarks = format_landmarks(result['keypoints'])
                image = preprocess(image,
                                   bbox,
                                   landmarks,
                                   image_size=IMAGE_SIZE_STR)
                embedding = self.get_embedding(image)
        return embedding

    def get_embedding(self, nimg):
        nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
        nimg = np.transpose(nimg, (2, 0, 1))
        embedding = self.get_feature(self.embedding_model, nimg).reshape(1, -1)

        return embedding

    def get_feature(self, emb_model, aligned):
        input_blob = np.expand_dims(aligned, axis=0)
        data = mx.nd.array(input_blob)
        db = mx.io.DataBatch(data=(data, ))
        emb_model.forward(db, is_train=False)
        embedding = emb_model.get_outputs()[0].asnumpy()
        embedding = preprocessing.normalize(embedding).flatten()

        return embedding

    def get_embedding_model(self,
                            model_str,
                            layer,
                            ctx=mx.cpu(),
                            image_size=(112, 112)):
        _vec = model_str.split(',')
        prefix = _vec[0]
        epoch = int(_vec[1])
        print('loading', prefix, epoch)
        sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch)
        all_layers = sym.get_internals()
        sym = all_layers[layer + '_output']
        emb_model = mx.mod.Module(symbol=sym, context=ctx, label_names=None)
        emb_model.bind(data_shapes=[('data', (1, 3, image_size[0],
                                              image_size[1]))])
        emb_model.set_params(arg_params, aux_params)

        return emb_model
Esempio n. 7
0
class MTCNN_tf:
    def __init__(self, threshold=[0.7], verbose=0):
        self.threshold = threshold
        self.detector = MTCNN()
        self.verbose = verbose

    def detect_faces_cv2(self, img):
        pixels = np.uint16(img)

        results = self.detector.detect_faces(pixels)

        faces = []
        bounds = []
        confidences = []
        for result in results:
            if result['confidence'] >= self.threshold[-1]:
                x1, y1, width, height = result['box']
                x2, y2 = x1 + width, y1 + height
                x1 = max(x1, 0)
                y1 = max(y1, 0)
                x2 = min(x2, pixels.shape[1] - 1)
                y2 = min(y2, pixels.shape[0] - 1)
                face = pixels[y1:y2, x1:x2].copy()

                if face.shape[0] > 0 and face.shape[1] > 0:
                    faces.append(face)
                    bounds.append((x1, x2, y1, y2))
                    confidences.append(result['confidence'])
                    pixels = cv2.rectangle(pixels, (x1, y1), (x2, y2),
                                           (255, 0, 0), 5)

        return pixels, bounds, confidences, faces

    def detect_faces_polys(self, path):
        img = cv2.imread(path)
        cv2_image, bounds, confidences, faces = self.detect_faces_cv2(img)

        if self.verbose > 0:
            plt.imshow(cv2.cvtColor(cv2_image, cv2.COLOR_BGR2RGB))
            plt.show()

        eq_bounds = []
        for bound in bounds:
            x1, x2, y1, y2 = bound
            x1, x2 = min([x1, x2]), max([x1, x2])
            y1, y2 = min([y1, y2]), max([y1, y2])
            points = []

            points.append((int(x1), int(y1)))
            points.append((int(x2), int(y1)))
            points.append((int(x2), int(y2)))
            points.append((int(x1), int(y2)))

            #points = adjust_bounds(points, equ._img.shape[1])

            eq_bounds = eq_bounds + [points]

        adj_bounds = [
            adjust_bounds(eq_bound.copy(), img.shape[1])
            for eq_bound in eq_bounds
        ]
        polys = [
            geometry.Polygon(adj_bound).buffer(0) for adj_bound in adj_bounds
        ]

        return eq_bounds, adj_bounds, polys, confidences, faces
Esempio n. 8
0
from mtcnn import MTCNN
import cv2

detector = MTCNN()

resl=(1280,720)

# cam = cv2.VideoCapture(0)
cam = cv2.VideoCapture('cropped.mp4')
out = cv2.VideoWriter('out.mp4',cv2.VideoWriter_fourcc(*'mp4v'), 25,resl)


while cam.isOpened():
	ret, img = cam.read()
	try:
		img = cv2.resize(img, resl)
		rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
	except:
		break
	faces = detector.detect_faces(rgb)
	for idx,bx in enumerate(faces):
		x,y,w,h = bx['box']
		cv2.rectangle(img,(x,y),(w+x,h+y),(0,255,0),2)
		tp = y-15 if y-15 > 15 else y+15
		cv2.putText(img,str(idx+1),(x,tp),cv2.FONT_HERSHEY_SIMPLEX,0.75,(0,255,0),2,cv2.LINE_AA)

	cv2.putText(img,"Count: "+str(len(faces)),(10,60),cv2.FONT_HERSHEY_SIMPLEX,2.5,(0,0,255),4,cv2.LINE_AA)
	cv2.imshow('webcam', img)
	out.write(img)
	if cv2.waitKey(1) & 0xff == 27:
		break
                        help="whether perform update the facebank",
                        action="store_true")
    parser.add_argument("-tta",
                        "--tta",
                        help="whether test time augmentation",
                        action="store_true")
    parser.add_argument("-c",
                        "--score",
                        help="whether show the confidence score",
                        action="store_true")
    args = parser.parse_args()

    conf = get_config(False)
    server = "http://127.0.0.1:5000/"

    mtcnn = MTCNN()
    print('mtcnn loaded')

    # inital camera
    img = []

    img.append(cv2.imread('data/input/evans/evans_p.jpg'))
    img.append(cv2.imread('data/input/hermsworth/hermsworth_p.jpg'))
    img.append(cv2.imread('data/input/jeremy/jeremy.jpg'))
    img.append(cv2.imread('data/input/mark/mark.jpg'))
    img.append(cv2.imread('data/input/olsen/olsen.jpg'))

    faces = []
    re_img = Image.fromarray(img[0][..., ::-1])
    re_img = mtcnn.align(re_img)
    tolist_face = np.array(re_img).tolist()
def main(args):

    # Create folder
    now = datetime.now()
    dt_string = now.strftime("%d%m%Y%H%M%S")
    output_loc = os.path.join(args.output_loc, dt_string)
    os.mkdir(output_loc)

    with tf.device('/GPU:0'):
        with tf.Graph().as_default():
            with tf.Session() as sess:
                # Load facenet model
                print('Loading feature extraction model')
                facenet.load_model(args.model_path)

                # Create mtcnn model
                detector = MTCNN()

                # Create model classifer
                model, class_names = load_model_classfier(
                    args.model_classfier_path)

                # Read video
                cap = cv2.VideoCapture(args.input_video)

                # Get fpt from video
                fps = cap.get(cv2.CAP_PROP_FPS)

                # Get frame information
                if args.frame_skip == 0:
                    frame_skip = int(fps)
                else:
                    frame_skip = args.frame_skip
                frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
                frame_number = 0

                # Not finding an exact face

                # Create folder images for export video
                if args.export_video:
                    images_video_dir = os.path.join(output_loc, 'images')
                    os.mkdir(images_video_dir)
                count = frame_skip
                print('Processing video...')
                with tqdm(total=(frame_count), file=sys.stdout) as pbar:
                    while cap.isOpened():
                        # Extract the frame
                        ret, frame = cap.read()
                        if ret == True:
                            frame_number += 1
                            if frame_number == 1 or frame_number == count:
                                # Face Detection
                                detect_face(frame, args.id, frame_number,
                                            frame_count, fps,
                                            (model, class_names), output_loc,
                                            detector, sess, args.export_video,
                                            args.threshold)
                                count += frame_skip
                            elif args.export_video:
                                img_path = f'{images_video_dir}/{frame_number}.jpg'
                                cv2.imwrite(img_path, frame)
                        else:
                            break
                        pbar.update(1)
                cap.release()
                print('Successful write .txt file')

                # Export video
                if args.export_video:
                    print('Rendering video...')
                    out_video(images_video_dir, args.input_video, output_loc,
                              fps)
                    print('Successful export .mp4 file: ')
Esempio n. 11
0
def main(_argv):
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu

    logger = tf.get_logger()
    logger.disabled = True
    logger.setLevel(logging.FATAL)
    set_memory_growth()

    cfg = load_yaml(FLAGS.cfg_path)

    model = ArcFaceModel(size=cfg['input_size'],
                         backbone_type=cfg['backbone_type'],
                         training=False)

    ckpt_path = tf.train.latest_checkpoint('./checkpoints/' + cfg['sub_name'])
    if ckpt_path is not None:
        print("[*] load ckpt from {}".format(ckpt_path))
        model.load_weights(ckpt_path)
    else:
        print("[*] Cannot find ckpt from {}.".format(ckpt_path))
        exit()

    if FLAGS.img_path:

        print("Check Start!!!")

        file_dir = "C:/Users/smgg/Desktop/dataset/superjunior/all3.jpeg"
        npy_dir = "/SCLab/newTWICE_id/*,npy"

        img_list = glob.glob(file_dir)
        npy_list = glob.glob(npy_dir)

        for img_name in img_list:
            img = cv2.cvtColor(cv2.imread(img_name), cv2.COLOR_BGR2RGB)
            detector = MTCNN()
            data_list = detector.detect_faces(img)

            for data in data_list:
                xmin, ymin, width, height = data['box']
                xmax = xmin + width
                ymax = ymin + height

                face_image = img[ymin:ymax, xmin: xmax, :]
                face_image = cv2.cvtColor(face_image, cv2.COLOR_RGB2BGR)

                # cv2.imshow('', face_image)
                # cv2.waitKey(0)

                img_resize = cv2.resize(face_image, (cfg['input_size'], cfg['input_size']))
                img_resize = img_resize.astype(np.float32) / 255.
                if len(img_resize.shape) == 3:
                    img_resize = np.expand_dims(img_resize, 0)
                embeds = l2_norm(model(img_resize))

                i = 0
                cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), 1)
                for npy_name in npy_list:

                    name_embeds = np.load(npy_name)
                    # print(1)

                    if distance(embeds, name_embeds,1) < 0.37:
                        i = i + 1
                        name = npy_name.split('/')[5].split('\\')[1].split('.npy')[0]

                        cv2.putText(img, name, (xmin, ymin - 15*(i)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1,cv2.LINE_AA)


                    # else:
                    #     cv2.putText(img, "Unknown", (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 1, cv2.LINE_AA)
                    #     cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), 1)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            cv2.imshow('', img)
            cv2.waitKey(0)
Esempio n. 12
0
        scaled.append((np.array((Image.fromarray(frame)).resize(
            (112, 96), Image.BILINEAR)) - 127.5) / 128)
    return scaled


parser = argparse.ArgumentParser(description='PyTorch sphereface video')
parser.add_argument('--img', default='images', type=str)
parser.add_argument('--model',
                    '-m',
                    default='sphere20a_20171020.pth',
                    type=str)
parser.add_argument('--video', '-v', default='sample.mp4', type=str)
parser.add_argument('--embed', '-e', default='false', type=str)
args = parser.parse_args()

detector = MTCNN()
model = sphere20a()
model.load_state_dict(torch.load(args.model))
model.feature = True

emb_path = "embeddings"
img_path = args.img
if (args.embed == "true"):
    embed_imgs(img_path, model, detector)
#resnet = InceptionResnetV1(pretrained='vggface2').eval()
#mtcnn = mtcn(keep_all=True)
v_cap = cv2.VideoCapture(args.video)
v_len = int(v_cap.get(cv2.CAP_PROP_FRAME_COUNT))
img_array = []

for count in range(v_len):
Esempio n. 13
0
from model import Backbone, Arcface, MobileFaceNet, Am_softmax, l2_norm
from torchvision import transforms as trans
import PIL.Image as Image
from mtcnn import MTCNN
import torch
import cv2
import os

img_root_dir = '../img_align_celeba'
save_path = '../celeba_64'

device = torch.device('cuda:0')
mtcnn = MTCNN()

model = Backbone(50, 0.6, 'ir_se').to(device)
model.eval()
model.load_state_dict(torch.load('./saved_models/model_ir_se50.pth'))

# threshold = 1.54
test_transform = trans.Compose(
    [trans.ToTensor(),
     trans.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])

# decoder = libnvjpeg.py_NVJpegDecoder()

ind = 0
embed_map = {}

for root, dirs, files in os.walk(img_root_dir):
    files_len = len(files)
    for idx, name in enumerate(files):
Esempio n. 14
0
 def __init__(self):
     self._mtcnn_file = '/media/nas2/Deepfakedeetectionchallenge/mtcnn-0.1.0-py3-none-any.whl'
     self._THRESHOLD = 0.9
     self._interpolate_FLAG = 1
     self._detector = MTCNN()
     self._output_path = '/media/nas2/Shengbang/DFDC_CNN_LSTM_DATA/'
Esempio n. 15
0
 def __init__(self, logger=None):
     self._logger = logger or getLogger(__name__)
     self._detector = MTCNN()
Esempio n. 16
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cv2
from mtcnn import MTCNN
import os
import glob

img_dir = "hkm/"
results_dir = img_dir + "results/"

detector = MTCNN()

files = []
for file in glob.glob(img_dir + "*.jpg"):
    files.append(os.path.basename(file))

for file in files:
    print("detecting files: " + file)
    image = cv2.cvtColor(cv2.imread(img_dir + file), cv2.COLOR_BGR2RGB)
    result = detector.detect_faces(image)

    for i in result:
        # Result is an array with all the bounding boxes detected. We know that for 'ivan.jpg' there is only one.
        bounding_box = i['box']
        keypoints = i['keypoints']

        cv2.rectangle(image, (bounding_box[0], bounding_box[1]),
                      (bounding_box[0] + bounding_box[2],
                       bounding_box[1] + bounding_box[3]), (0, 155, 255), 2)
"""
    This code will take care of surveillance.
"""
# pylint: disable=E1101
# pylint: disable=E0211
# pylint: disable=W0603
import datetime
import threading

import cv2
from mtcnn import MTCNN
from flask import Response, Flask, render_template

OUTPUT_IMAGE, LOCK, VIDEO_STREAM = None, threading.Lock(), cv2.VideoCapture(0)
DETECTOR = MTCNN()

APP = Flask(__name__, template_folder="frontend")


class Surveillance:
    """
        This class contains all the methods of surveillance.
    """
    @APP.route("/")
    def index():
        """
            Index page which will be hit when you open base URL.
        """
        return render_template("index.html")

    @classmethod
Esempio n. 18
0
import numpy as np
import matplotlib.pyplot as plt

from mtcnn import MTCNN
from tensorflow.keras import models

# read image
if len(sys.argv) > 1:
    img = plt.imread(sys.argv[1])
else:
    img = plt.imread("images/facemask1.jpg")

#
# Face Detection : MTCNN
#
detector = MTCNN()

faces = detector.detect_faces(img)

# find bounding box of each face (with confidence>90%)
bbox = []
for face in faces:
    box = face['box']
    print(box)
    keypoints = face['keypoints']
    print(keypoints)
    confidence = face['confidence']
    print(confidence)
    print()
    if confidence >= 0.9:  # check confidence > 90%
        bbox.append(box)
Esempio n. 19
0
 def __init__(self):
     self.detector = MTCNN()
     self.embedding_model = self.get_embedding_model(
         INSIGHT_MODEL_PATH, "fc1")
     print('Embedding model loaded')
Esempio n. 20
0
class FaceRecognition:
    def __init__(self,block_size):
        self.desc=Calculator(block_size)
        self.detector=MTCNN()

    def predictPath(self, path, model):
        image=cv2.imread(path,0)
        hist = self.desc.calc_hist(image[40:-70, 120:-180])
        hist = np.array(hist)
        prediction = model.predict(hist.reshape(1, -1))
        return prediction

    def predictImg(self,img,model):  # Input : Gray Image; Output : Face Prediction
        #image=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        hist = self.desc.calc_hist(img)
        hist = np.array(hist)
        prediction = model.predict(hist.reshape(1, -1))
        return prediction

    def find_face(self,img):          # Input : BGR Image; Output : GRAY Image with bounded faces and resized to 360x480
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        faces = self.detector.detect_faces(imgRGB)
        if len(faces)==0:
            return np.asarray([0,0,0])
        
        i = 0
        memo = {}
        for face in faces:
            key = "Face" + str (i)
            x1, y1, w1, h1 = face['box']
            # cv2.rectangle(imgRGB, (x1, y1), (x1 + w1, y1 + h1), (0, 255, 0), 3)
            memo[key] = [x1, y1, w1, h1]
            #memo.add (key, [x1, y1, w1, h1])
            #imgRGB = imgRGB[y1:y1 + h1, x1:x1 + w1, :]
            #imgfinal = cv2.cvtColor(imgRGB, cv2.COLOR_RGB2GRAY)
            i += 1    
            ##break
        #imgfinal=cv2.resize(imgfinal,(360,480))
        return memo
        #return imgfinal

    def captureData(self,path):
        counter=0
        i = 0
        cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
        while True:

            ret, img = cap.read()
            if counter%7==0:
                imgFinal = self.find_face(img)
                if not imgFinal is None:
                    filename=str(i)+'.jpeg'
                    cv2.imwrite(os.path.join(path,filename),imgFinal)
                    i += 1
                    print('No. of images : '+ str(i))
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            counter+=1

    def trainRecognizer(self, trainPath, printAcc, testPath = None):
        data = []
        labels = []
        print('Extracting Features.....')
        for imageFolder in os.listdir(trainPath):
            imagePath = os.path.join(trainPath, imageFolder)
            for trainImg in os.listdir(imagePath):
                image = cv2.imread(os.path.join(imagePath, trainImg))
                gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                #print(gray[50:-70,130:-170].shape)
                hist = self.desc.calc_hist(gray)
                labels.append(int(imageFolder[-1]))
                data.append(hist)
            print("Processed folder " + imageFolder)
            if imageFolder == "Subject09":
                break
        print('Completed Feature Extraction!')
        print('Training Classifier.......')
        temp = list(zip(data, labels))
        random.shuffle(temp)
        data, labels = zip(*temp)
        model = KNeighborsClassifier(n_neighbors=1)
        model.fit(data, labels)
        print('Done Training')
        if printAcc:
            correct = 0
            total = 0
            print('Calculating accuracy....')
            for imageFolder in os.listdir(testPath):
                imagePath = os.path.join(testPath, imageFolder)
                for testImg in os.listdir(imagePath):
                    image = cv2.imread(os.path.join(imagePath, testImg))
                    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
                    hist = self.desc.calc_hist(gray)
                    hist = np.array(hist)
                    prediction = model.predict(hist.reshape(1, -1))
                    total += 1
                    if prediction == int(imageFolder[-1]):
                        correct += 1
                print("Done "+imageFolder)
                tempacc = (correct/total)*100
                print("Total : "+str(total)+" Correct : "+str(correct)+" Accuracy : "+str(tempacc))
                if imageFolder == "Subject09":
                    break
            acc = (correct/total)*100
            print('Accuracy on test set is : '+str(acc)+'%')
        return model
Esempio n. 21
0
 def __init__(self, threshold=[0.7], verbose=0):
     self.threshold = threshold
     self.detector = MTCNN()
     self.verbose = verbose
Esempio n. 22
0
 def __init__(self,block_size):
     self.desc=Calculator(block_size)
     self.detector=MTCNN()
Esempio n. 23
0
import dlib
import numpy as np
import cv2
import os
import glob
import imutils
import shutil
from mtcnn import MTCNN

path = "/home/whirldata/Documents/19.10.2019/DSC_0502.JPG"
img = cv2.imread(path)
face_detector = MTCNN()
# img = adjust_gamma(img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# img = adjust_gamma(img)
# img = cv2.cvtColor(img,cv2.COLOR_YCrCb2RGB)

face_info = face_detector.detect_faces(img)
for i in face_info:
    x, y, width, height = i["box"]
    cv2.rectangle(img, (x, y), (x + width, y + height), (255, 0, 233), 3)

img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)

cv2.imwrite("face_detection.jpg", img)
print("Success")
Esempio n. 24
0
def load_mtcnn_model(model_path):
    mtcnn = MTCNN(model_path)
    return mtcnn
Esempio n. 25
0
 def __init__(self):
     self.detector = MTCNN()
     self.vggface = VGGFace(model='resnet50',
                            include_top=False,
                            input_shape=(224, 224, 3),
                            pooling='avg')
Esempio n. 26
0
def predict():
    input_shape = (160, 160, 3)
    mtcnn = MTCNN()
    # 영상 불러오기
    videos_path = 'test1.mp4'
    vid_name = videos_path.split('/')[-1].split('.')[0]
    # 프레임으로 나눠서 저장
    cap = cv2.VideoCapture(videos_path)
    frame = 0
    detect_face_num = 0

    heat_images = []

    # def get_face_coord(img):

    pred = 0
    while (cap.isOpened()):
        if frame == 10 or detect_face_num > 1:
            break

        ret, img = cap.read()
        if ret == False:
            break

        # 얼굴 detect
        face = mtcnn.detect_faces(img)

        # 얼굴 없으면 다음 프레임
        if not face:
            continue

        # 얼굴 위치
        x1, y1, w, h = face[0]['box']
        x2 = min(x1 + w, img.shape[1])
        y2 = min(y1 + h, img.shape[0])
        x1 = max(x1, 0)
        y1 = max(y1, 0)

        # 이미지 자르기
        crop_img = img[y1:y2, x1:x2]
        crop_img = cv2.resize(crop_img, (160, 160))

        # 얼굴위치 저장
        before_face_img_coord = [y1, y2, x1, x2]
        detect_face_num += 1

        # 이미지 전처리 및 예측
        img_tensor = (crop_img.flatten() / 255.0).reshape(-1, 160, 160, 3)
        #img_tensor = np.expand_dims(crop_img, axis=0)
        #print(img_tensor.shape)
        pred += model.predict(img_tensor)[0][1]

        # 프레임 가져와서 히트맵 표시
        conv_layer = model.get_layer("conv_7b")
        heatmap_model = models.Model([model.inputs],
                                     [conv_layer.output, model.output])
        # # Get gradient of the winner class w.r.t. the output of the (last) conv. layer
        with tf.GradientTape() as gtape:
            conv_output, predictions = heatmap_model(img_tensor)
            loss = predictions[:, np.argmax(predictions[0])]
            grads = gtape.gradient(loss, conv_output)
            pooled_grads = K.mean(grads, axis=(0, 1, 2))

        heatmap = tf.reduce_mean(tf.multiply(pooled_grads, conv_output),
                                 axis=-1)
        heatmap = np.maximum(heatmap, 0)
        max_heat = np.max(heatmap)
        if max_heat == 0:
            max_heat = 1e-10
        heatmap /= max_heat

        img = crop_img

        # print(img)

        heatmap2 = cv2.resize(heatmap, (img.shape[1], img.shape[0]))
        heatmap2 = np.uint8(255 * heatmap2)
        heatmap2 = cv2.applyColorMap(heatmap2, cv2.COLORMAP_JET)
        hif = .5

        superimposed_img = heatmap2 * hif + img

        # print(superimposed_img.shape, heatmap2.shape)
        type(superimposed_img)
        heat_images.append(superimposed_img.tolist())

        output = f'output_{vid_name}_{detect_face_num}.jpeg'
        cv2.imwrite(output, superimposed_img)

    # 정확도 판별(평균)
    if detect_face_num:
        acc = pred / detect_face_num
    else:
        acc = 0
    print(acc)

    # 정확도, 이미지 array -> json
    return jsonify(acc=acc)  # ,heat_images=heat_images
Esempio n. 27
0
def initialize_detector(detector_backend):
	
	global face_detector
	
	home = str(Path.home())
	
	if detector_backend == 'opencv':
		opencv_path = get_opencv_path()
		
		face_detector_path = opencv_path+"haarcascade_frontalface_default.xml"
		eye_detector_path = opencv_path+"haarcascade_eye.xml"
	
		if os.path.isfile(face_detector_path) != True:
			raise ValueError("Confirm that opencv is installed on your environment! Expected path ",face_detector_path," violated.")
		
		face_detector = cv2.CascadeClassifier(face_detector_path)
		
		global eye_detector
		eye_detector = cv2.CascadeClassifier(eye_detector_path)
	
	elif detector_backend == 'ssd':
	
		#check required ssd model exists in the home/.deepface/weights folder
		
		#model structure
		if os.path.isfile(home+'/.deepface/weights/deploy.prototxt') != True:
			
			print("deploy.prototxt will be downloaded...")
			
			url = "https://github.com/opencv/opencv/raw/3.4.0/samples/dnn/face_detector/deploy.prototxt"
			
			output = home+'/.deepface/weights/deploy.prototxt'
			
			gdown.download(url, output, quiet=False)
		
		#pre-trained weights
		if os.path.isfile(home+'/.deepface/weights/res10_300x300_ssd_iter_140000.caffemodel') != True:
			
			print("res10_300x300_ssd_iter_140000.caffemodel will be downloaded...")
			
			url = "https://github.com/opencv/opencv_3rdparty/raw/dnn_samples_face_detector_20170830/res10_300x300_ssd_iter_140000.caffemodel"
			
			output = home+'/.deepface/weights/res10_300x300_ssd_iter_140000.caffemodel'
			
			gdown.download(url, output, quiet=False)
		
		face_detector = cv2.dnn.readNetFromCaffe(
			home+"/.deepface/weights/deploy.prototxt", 
			home+"/.deepface/weights/res10_300x300_ssd_iter_140000.caffemodel"
		)
		
	elif detector_backend == 'dlib':
		import dlib #this is not a must library within deepface. that's why, I didn't put this import to a global level. version: 19.20.0
		
		global sp
		
		face_detector = dlib.get_frontal_face_detector()
		
		#check required file exists in the home/.deepface/weights folder
		if os.path.isfile(home+'/.deepface/weights/shape_predictor_5_face_landmarks.dat') != True:
			
			print("shape_predictor_5_face_landmarks.dat.bz2 is going to be downloaded") 
			
			url = "http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2"
			output = home+'/.deepface/weights/'+url.split("/")[-1]
			
			gdown.download(url, output, quiet=False)
			
			zipfile = bz2.BZ2File(output)
			data = zipfile.read()
			newfilepath = output[:-4] #discard .bz2 extension
			open(newfilepath, 'wb').write(data)
		
		sp = dlib.shape_predictor(home+"/.deepface/weights/shape_predictor_5_face_landmarks.dat")
		
	elif detector_backend == 'mtcnn':
		face_detector = MTCNN()
                        "--update",
                        help="whether perform update the facebank",
                        action="store_true")
    parser.add_argument("-tta",
                        "--tta",
                        help="whether test time augmentation",
                        action="store_true")
    parser.add_argument("-c",
                        "--score",
                        help="whether show the confidence score",
                        action="store_true")
    args = parser.parse_args()

    conf = get_config(False)

    mtcnn = MTCNN()
    print('mtcnn loaded')

    learner = face_learner(conf, True)
    learner.threshold = args.threshold
    if conf.device.type == 'cpu':
        learner.load_state(conf, 'cpu_final.pth', True, True)
    else:
        learner.load_state(conf, 'final.pth', True, True)
    learner.model.eval()
    print('learner loaded')

    if args.update:
        targets, names = prepare_facebank(conf,
                                          learner.model,
                                          mtcnn,
def get_detector():
    return MTCNN()
Esempio n. 30
0
from mtcnn import MTCNN
import cv2
import os
from PIL import ImageDraw, Image
import matplotlib.pyplot as plt

path = r"D:\BaiduNetdiskDownload\CelebA\Img\img_celeba.7z\img_celeba"
file = os.listdir(path)
img = cv2.cvtColor(cv2.imread("1.png"), cv2.COLOR_BGR2RGB)

detector = MTCNN()
data = detector.detect_faces(img)
print(data)
img = Image.fromarray(img, "RGB")
draw = ImageDraw.Draw(img)
#draw.rectangle([138, 70, 206, 260],outline="red",width=2)

draw.ellipse((176, 208, 186, 218), fill=(255, 0, 0), width=2)
draw.ellipse((295, 212, 305, 222), fill=(255, 0, 0), width=2)
plt.imshow(img)
plt.show()