def live_detection(database, threshold): device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') print('Running on device: {}'.format(device)) mtcnn = MTCNN(image_size=160, margin=0, min_face_size=20, thresholds=[0.6, 0.7, 0.7], factor=0.709, post_process=True, device=device) resnet = InceptionResnetV1(pretrained='vggface2').eval().to(device) with open(database, "rb") as pkl_in: database = pickle.load(pkl_in) embeddings_set, id_to_name = database cap = cv2.VideoCapture(0) # Check if the webcam is opened correctly if not cap.isOpened(): raise IOError("Cannot open webcam") while True: ret, frame = cap.read() rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) try: face = mtcnn(rgb) c, _ = mtcnn.detect(rgb) c = c.flatten().tolist() embeddings = resnet(face.unsqueeze(0).to(device)).detach() if embeddings is not None: index = (embeddings_set - embeddings).norm(dim=-1).argmin().item() dist = (embeddings_set - embeddings).norm(dim=-1).min().item() if dist < threshold: name = id_to_name[index] else: name = "Unknown" cv2.rectangle(frame, (int(c[0]), int(c[1])), (int(c[2]), int(c[3])), (0, 0, 255), 2) cv2.putText(frame, name, (int(c[0]) - 120, int(c[1]) - 10), cv2.FONT_HERSHEY_TRIPLEX, 1.0, [0, 0, 255], 2, 1) except Exception: pass cv2.imshow('Input', frame) c = cv2.waitKey(1) if c == 27: break cap.release() cv2.destroyAllWindows()
def test(): model = FECNet() model.load_state_dict(torch.load('data/FECNet.pt')) model.eval() with torch.no_grad(): test_path = "/data00/chenriwei/PublicData/FaceData/RAF/RAF" with open("feature2.txt", 'w') as fout: idx = 1 for filename in os.listdir(test_path): full_path = os.path.join(test_path, filename) try: img = Image.open(full_path) channels = img.split() if len(channels) != 3: continue r, g, b = channels img = Image.merge("RGB", (b, g, r)) mtcnn = MTCNN(image_size=224) except: # ignore all exception continue face, prob = mtcnn(img, return_prob=True) idx += 1 face = np.array(face) if face.any(): face = torch.Tensor(face).view(1, 3, 224, 224) Embedding = model(face.cuda()).cpu() fout.write("{}\t{}\n".format(filename, list( Embedding[0, :].numpy())))
def embedding(file_name): import PIL.Image as Image from models.mtcnn import MTCNN from models.inception_resnet_v1 import InceptionResnetV1 workers = 10 #device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') device = torch.device('cpu') mtcnn = MTCNN(image_size=160, margin=0, min_face_size=20, thresholds=[0.6,0.7,0.7], factor=0.709, post_process=True, device=device) resnet = InceptionResnetV1(pretrained='casia_webface').eval().to(device) crop_imgs = [] labels = [] with open (file_name, 'rt') as file: for line in file: l = line.strip('\n').split(' ') path = l[0] labels.append(l[1]) x = Image.open(path) x_aligned, prob = mtcnn(x, return_prob=True) crop_imgs.append(x_aligned) print('total', len(crop_imgs),'images read!') embeddings = [] if len(crop_imgs)%100==0 and len(crop_imgs)>=100: for i in range(len(crop_imgs)//100): em = resnet(torch.stack(crop_imgs[i*100:100*(i+1)]).to(device)).detach().cpu() embeddings+=em else: for i in range(len(crop_imgs)): em = resnet(torch.stack(crop_imgs).to(device)).detach().cpu() embeddings+=em return embeddings, labels
import sys from PIL import Image import torch sys.path.append('./') from models.mtcnn import MTCNN # noqa if __name__ == "__main__": mtcnn = MTCNN() # print('mtcnn:{}'.format(mtcnn)) img_path = './data/predict_images/02huangxuan/timg.jpg' save_path = './data/predict_images/02huangxuan/timg_cropped.jpg' # img_path = './data/CASIA-FaceV5/test/009_0.bmp' # save_path = './data/CASIA-FaceV5/test/009_0_cropped.bmp' resnet = torch.load( './data/test_data/test_model/vggface2_finetune.pt').eval() # print('resnet:{}'.format(resnet)) img = Image.open(img_path) # Get cropped and prewhitened image tensor img_cropped = mtcnn(img, save_path=save_path) # Calculate embedding (unsqueeze to add batch dimension) img_embedding = resnet(img_cropped.unsqueeze(0)) # Or, if using for VGGFace2 classification resnet.classify = True img_probs = resnet(img_cropped.unsqueeze(0)) probs = torch.nn.functional.softmax(img_probs, dim=-1) print('probs:{}'.format(probs)) print('img_probs:{}'.format(probs.max(dim=1))) # dataset.classes:['01jay', '02huangxuan', '03liudehua', '04zhangxueyou', '05zengxiaoxian', 'angelina_jolie', 'bradley_cooper', 'kate_siegel', 'paul_rudd', 'shea_whigham'] # dataset.class_to_idx:{'01jay': 0, '02huangxuan': 1, '03liudehua': 2, '04zhangxueyou': 3, '05zengxiaoxian': 4, 'angelina_jolie': 5, 'bradley_cooper': 6, 'kate_siegel': 7, 'paul_rudd': 8, 'shea_whigham': 9}
def get_image(path, trans): img = Image.open(path) img = trans(img) return img trans = transforms.Compose([transforms.Resize(512)]) trans_cropped = transforms.Compose( [np.float32, transforms.ToTensor(), prewhiten]) dataset = datasets.ImageFolder('data/test_images', transform=trans) dataset.idx_to_class = {k: v for v, k in dataset.class_to_idx.items()} loader = DataLoader(dataset, collate_fn=lambda x: x[0]) mtcnn_pt = MTCNN(device=torch.device('cpu')) names = [] aligned = [] aligned_fromfile = [] for img, idx in loader: name = dataset.idx_to_class[idx] start = time() img_align = mtcnn_pt( img, save_path='data/test_images_aligned/{}/1.png'.format(name)) print('MTCNN time: {:6f} seconds'.format(time() - start)) if img_align is not None: names.append(name) aligned.append(img_align) aligned_fromfile.append(
from torchvision import datasets <<<<<<< HEAD from annoy import AnnoyIndex ======= >>>>>>> 30e0e67433fc362377061a0234b20899c571efe5 from tqdm import tqdm import torch import json # Define device device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') # Define Model mtcnn = MTCNN( image_size=160, margin=0, min_face_size=20, thresholds=[0.6, 0.7, 0.7], factor=0.709, post_process=True, device=device ) resnet = InceptionResnetV1(pretrained='vggface2').eval().to(device) # Define annoy dimention tree = AnnoyIndex(512, 'euclidean') # Extrac fetrue from images dataset = datasets.ImageFolder('./data/images') dataset.idx_to_class = {i:c for c, i in dataset.class_to_idx.items()} faces_aligned = [] names = [] for x, y in tqdm(dataset):
img = Image.open(path) img = trans(img) return img trans = transforms.Compose([ transforms.Resize(512) ]) trans_cropped = transforms.Compose([ np.float32, transforms.ToTensor(), prewhiten ]) mtcnn_pt = MTCNN() resnet_pt = InceptionResnetV1(pretrained='vggface2').eval() dataset = datasets.ImageFolder('data/test_images', transform=trans) dataset.idx_to_class = {k: v for v, k in dataset.class_to_idx.items()} loader = DataLoader(dataset, num_workers=8, collate_fn=lambda x: x[0]) names = [] aligned = [] aligned_fromfile = [] for img, idx in loader: name = dataset.idx_to_class[idx] names.append(name) start = time() img_align = mtcnn_pt(img, save_path='data/test_images_aligned/{}/1.png'.format(name)) print('MTCNN time: {:6f} seconds'.format(time() - start))
import tornado.ioloop import tornado.web import json from tornado.escape import json_decode from models.mtcnn import MTCNN from models.inception_resnet_v1 import InceptionResnetV1 from FaceRecognition import cosSimilarity, getVec from utils import getConnection define("port", default=8080, help="run on the given port", type=int) tornado.options.parse_command_line() BASE_DIR = os.path.dirname(__file__) # FaceNet mtcnn = MTCNN(image_size=160, margin=10) resnet = InceptionResnetV1().eval() class MainHandler(tornado.web.RequestHandler): def get(self): self.render("index.html") def post(self): self.render("index.html") class FaceRecognitionHandler(tornado.web.RequestHandler): def get(self): pass
def main(args): device = 'cuda' sleep(random.random()) output_dir = os.path.expanduser(args.output_dir) if not os.path.exists(output_dir): os.makedirs(output_dir) dataset = get_dataset(args.input_dir) print('Creating networks and loading parameters') mtcnn = MTCNN(min_face_size=20, thresholds=[0.6, 0.7, 0.7], factor=0.709, device=device) # Add a random key to the filename to allow alignment using multiple processes random_key = np.random.randint(0, high=99999) bounding_boxes_filename = os.path.join( output_dir, 'bounding_boxes_%05d.txt' % random_key) with open(bounding_boxes_filename, "w") as text_file: nrof_images_total = 0 nrof_successfully_aligned = 0 if args.random_order: random.shuffle(dataset) for cls in dataset: output_class_dir = os.path.join(output_dir, cls.name) if not os.path.exists(output_class_dir): os.makedirs(output_class_dir) if args.random_order: random.shuffle(cls.image_paths) for image_path in cls.image_paths: nrof_images_total += 1 filename = os.path.splitext(os.path.split(image_path)[1])[0] output_filename = os.path.join(output_class_dir, filename + '.png') print(image_path) if not os.path.exists(output_filename): try: # img = misc.imread(image_path) img = Image.open(image_path) img = np.asarray(img) except (IOError, ValueError, IndexError) as e: errorMessage = '{}: {}'.format(image_path, e) print(errorMessage) else: if img.ndim < 2: print('Unable to align "%s"' % image_path) text_file.write('%s\n' % (output_filename)) continue if img.ndim == 2: img = to_rgb(img) img = img[:, :, 0:3] bounding_boxes, _ = mtcnn.detect(img, landmarks=False) if bounding_boxes is not None: nrof_faces = bounding_boxes.shape[0] det = bounding_boxes[:, 0:4] det_arr = [] img_size = np.asarray(img.shape)[0:2] if nrof_faces > 1: # if there are multiple faces, it will choose the face that closed to center if args.detect_multiple_faces: for i in range(nrof_faces): det_arr.append(np.squeeze(det[i])) else: bounding_box_size = ( det[:, 2] - det[:, 0]) * (det[:, 3] - det[:, 1]) img_center = img_size / 2 offsets = np.vstack([ (det[:, 0] + det[:, 2]) / 2 - img_center[1], (det[:, 1] + det[:, 3]) / 2 - img_center[0] ]) offset_dist_squared = np.sum( np.power(offsets, 2.0), 0) index = np.argmax( bounding_box_size - offset_dist_squared * 2.0 ) # some extra weight on the centering det_arr.append(det[index, :]) else: det_arr.append(np.squeeze(det)) for i, det in enumerate(det_arr): det = np.squeeze(det) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - args.margin / 2, 0) bb[1] = np.maximum(det[1] - args.margin / 2, 0) bb[2] = np.minimum(det[2] + args.margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + args.margin / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] scaled = Image.fromarray(cropped).resize( (args.image_size, args.image_size), Image.BILINEAR) nrof_successfully_aligned += 1 filename_base, file_extension = os.path.splitext( output_filename) if args.detect_multiple_faces: output_filename_n = "{}_{}{}".format( filename_base, i, file_extension) else: output_filename_n = "{}{}".format( filename_base, file_extension) scaled.save(output_filename_n) text_file.write('%s %d %d %d %d\n' % (output_filename_n, bb[0], bb[1], bb[2], bb[3])) else: print('Unable to align "%s"' % image_path) text_file.write('%s\n' % (output_filename)) print('Total number of images: %d' % nrof_images_total) print('Number of successfully aligned images: %d' % nrof_successfully_aligned)
img = Image.open(path) img = trans(img) return img trans = transforms.Compose([transforms.Resize(512)]) trans_cropped = transforms.Compose( [np.float32, transforms.ToTensor(), fixed_image_standardization]) dataset = datasets.ImageFolder('data/test_images', transform=trans) dataset.idx_to_class = {k: v for v, k in dataset.class_to_idx.items()} loader = DataLoader(dataset, collate_fn=lambda x: x[0]) mtcnn_pt = MTCNN(device=torch.device('cpu')) names = [] aligned = [] aligned_fromfile = [] for img, idx in loader: name = dataset.idx_to_class[idx] start = time() img_align = mtcnn_pt( img, save_path='data/test_images_aligned/{}/1.png'.format(name)) print('MTCNN time: {:6f} seconds'.format(time() - start)) if img_align is not None: names.append(name) aligned.append(img_align) aligned_fromfile.append(
# # point = points[0] # aligned_face = face_aligner(face_image, landmark=point, image_size='112,112') # aligned_face = Image.fromarray(aligned_face) # aligned_face.save(save_path) # # except: # print('failed to extract:', file) if __name__ == '__main__': # aligned_dir = './data/aligned/lfw' # raw_dir = './data/lfw/*/*.jpg' # align_face(raw_dir, aligned_dir) mtcnn = MTCNN() aligned_dir = './data/aligned/CASIA' for file in glob.glob("./data/raw/CASIA/*/*.jpg"): file_splits = file.split(os.path.sep) class_name = file_splits[-2] file_name = file_splits[-1] class_dir = os.path.join(aligned_dir, class_name) save_path = os.path.join(class_dir, file_name) if not os.path.exists(class_dir): os.makedirs(class_dir) face_image = Image.open(file) face_image = np.asarray(face_image)
# face_image = np.asarray(face_image) # # try: # print('extracting:', file) # boxes, _, points = mtcnn.detect(face_image, landmarks=True) # # point = points[0] # aligned_face = face_aligner(face_image, landmark=point, image_size='112,112') # aligned_face = Image.fromarray(aligned_face) # aligned_face.save(save_path) # # except: # print('failed to extract:', file) if __name__ == '__main__': mtcnn = MTCNN(device='cuda') aligned_dir = './data/aligned/CASIA-maxpy-clean' for file in glob.glob( "./data/CASIA-maxpy-clean/CASIA-maxpy-clean/*/*.jpg"): file_splits = file.split(os.path.sep) class_name = file_splits[-2] file_name = file_splits[-1] class_dir = os.path.join(aligned_dir, class_name) save_path = os.path.join(class_dir, file_name) if not os.path.exists(class_dir): os.makedirs(class_dir) face_image = Image.open(file)
def get_image(path, trans): img = Image.open(path) img = trans(img) return img trans = transforms.Compose([transforms.Resize(512)]) trans_cropped = transforms.Compose( [np.float32, transforms.ToTensor(), fixed_image_standardization]) dataset = datasets.ImageFolder('data/test_images', transform=trans) dataset.idx_to_class = {k: v for v, k in dataset.class_to_idx.items()} mtcnn_pt = MTCNN(device=torch.device('cpu')) names = [] aligned = [] aligned_fromfile = [] for img, idx in dataset: name = dataset.idx_to_class[idx] start = time() img_align = mtcnn_pt( img, save_path='data/test_images_aligned/{}/1.png'.format(name)) print('MTCNN time: {:6f} seconds'.format(time() - start)) # Comparison between types img_box = mtcnn_pt.detect(img)[0] assert (img_box - mtcnn_pt.detect(np.array(img))[0]).sum() < 1e-2 assert (img_box -
from models.mtcnn import MTCNN from pydantic import BaseModel from annoy import AnnoyIndex from typing import Optional from detect import detect import torch import io # Looking for device device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') # Face detection model MTCNN mtcnn = MTCNN(image_size=160, margin=0, min_face_size=20, keep_all=True, thresholds=[0.6, 0.7, 0.7], factor=0.709, post_process=True, device=device) # InceptionResnetV1 for extract feature to make embedding vector resnet = InceptionResnetV1(pretrained='vggface2').eval().to(device) # Annoy for faster serach rather than loop tree = AnnoyIndex(512, 'euclidean') tree.load('./data/annoy_vector512.ann') tags_metadata = [ { "name": "detection",
import torch import torch.nn as nn import numpy as np from PIL import Image from datetime import datetime import torchvision.transforms as transforms from alignfaces import align_face from models.mtcnn import MTCNN from models.mobilefacenet import MobileFacenet device = "cuda" if torch.cuda.is_available() else "cpu" dt = datetime.now().isoformat(' ', 'seconds') print("[INFO {}] Running on device: {}".format(dt, device)) # MTCNN model to face detection model = MTCNN(device=device) embeddings = MobileFacenet() state_dict = torch.load("./weights/068.ckpt", map_location="cpu") embeddings.load_state_dict(state_dict["net_state_dict"]) embeddings.eval() embeddings.to(device) cos = nn.CosineSimilarity(dim=0, eps=1e-6) test_trainsforms = transforms.Compose([ transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ])
if not os.path.exists(outPath): os.makedirs(outPath) if args.video_path != 0: countFileName = '{}/{}.csv'.format( outPath, args.video_path.split('/')[-1].split('.mp4')[0]) else: countFileName = '{}/results_live_cam.csv'.format(outPath) countFile = open(countFileName, 'w') countFile.close() print('Running on: {}'.format(device)) # Prepare detection model mtcnn = MTCNN(keep_all=True, device=device, ov=args.ov) # Load video cap = cv2.VideoCapture(args.video_path) num_images = int(cap.get( cv2.CAP_PROP_FRAME_COUNT)) if args.video_path != 0 else 10000 if args.le: skip_frames = 10 cumulative = 0 if args.le: skip_frames = 10 id = 0 #fake (tracking) id for fr in tqdm(range(num_images)): start = time.time()
from models.mtcnn import MTCNN, PNet, RNet, ONet, prewhiten, fixed_image_standardization from models.utils.detect_face import extract_face import torch import numpy as np import cv2 import os from tqdm import tqdm device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') mtcnn = MTCNN(keep_all=True, device=device) # ------------------------ export ----------------------------- print("==> Exporting model to ONNX format") input_names = ['images'] output_names = ['scores'] inputs_pnet = torch.randn(1, 3, 649, 1153).to(device) #inputs_pnet = torch.randn(1, 3, 460, 817).to(device) inputs_rnet = torch.randn(1, 3, 24, 24).to(device) inputs_onet = torch.randn(1, 3, 48, 48).to(device) #state_dict_path = os.path.join(os.path.dirname(__file__), 'pnet.pt') #state_dict = torch.load(state_dict_path) #self.load_state_dict(state_dict) #dynamic_axes = {'images': {2:'h',3:'w'}} torch_out = torch.onnx._export(mtcnn.pnet, inputs_pnet,
from models.mtcnn import MTCNN from models.inception_resnet_v1 import InceptionResnetV1 device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') print('Running on device: {}'.format(device)) # Define MTCNN module # Default params shown for illustration, but not needed # Note that, since MTCNN is a collection of neural nets and other code, the # device must be passed in the following way to enable copying of objects when # needed internally. # See `help(MTCNN)` for details. mtcnn = MTCNN(image_size=160, margin=0, min_face_size=20, thresholds=[0.6, 0.7, 0.7], factor=0.709, prewhiten=True, device=device) # Define Inception Resnet V1 module # Set classify=True for pretrained classifier # See `help(InceptionResnetV1)` for details resnet = InceptionResnetV1(pretrained='vggface2').eval().to(device) # Define a dataset and data loader dataset = datasets.ImageFolder('data/test_images') dataset.idx_to_class = {i: c for c, i in dataset.class_to_idx.items()} loader = DataLoader(dataset, collate_fn=lambda x: x[0]) # Perfom MTCNN facial detection
from PIL import Image, ImageDraw import torch from torch.utils.data import DataLoader from torchvision import transforms, datasets import numpy as np import pandas as pd from time import time import sys, os import glob from models.mtcnn import MTCNN, fixed_image_standardization from models.inception_resnet_v1 import InceptionResnetV1, get_torch_home mtcnn = MTCNN(keep_all=True) img = [ Image.open( 'C:/Users/mmlab/PycharmProjects/facenet-pytorch-master-s/data/abc.jpg' ), Image.open( 'C:/Users/mmlab/PycharmProjects/facenet-pytorch-master-s/data/abc.jpg') ] batch_boxes, batch_probs = mtcnn.detect(img) mtcnn( img, save_path=[ 'C:/Users/mmlab/PycharmProjects/facenet-pytorch-master-s/data/tmp1.png', 'C:/Users/mmlab/PycharmProjects/facenet-pytorch-master-s/data/tmp1.png' ]) tmp_files = glob.glob('data/tmp*') for f in tmp_files: os.remove(f)