def main(): align = align_dlib.AlignDlib(os.path.expanduser(FLAGS.dlib_face_predictor)) image_paths = [FLAGS.image1, FLAGS.image2] landmarkIndices = align_dlib.AlignDlib.OUTER_EYES_AND_NOSE with tf.Graph().as_default(): with tf.Session() as sess: # Load the model print('Loading model "%s"' % FLAGS.model_file) facenet.load_model(FLAGS.model_file) # Get input and output tensors images_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") image_size = int(images_placeholder.get_shape()[1]) # Run forward pass to calculate embeddings images = load_and_align_data(image_paths, image_size, align, landmarkIndices) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb = sess.run(embeddings, feed_dict=feed_dict) dist = np.sqrt( np.mean(np.square(np.subtract(emb[0, :], emb[1, :])))) print('Distance between the embeddings: %3.6f' % dist)
def main(): align = align_dlib.AlignDlib(os.path.expanduser(FLAGS.dlib_face_predictor)) batch_size = 2 image_paths = [FLAGS.image1, FLAGS.image2] landmarkIndices = align_dlib.AlignDlib.OUTER_EYES_AND_NOSE with tf.Graph().as_default(): # Placeholder for input images images_placeholder = tf.placeholder(tf.float32, shape=(batch_size, FLAGS.image_size, FLAGS.image_size, 3), name='input') # Placeholder for phase_train phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') # Build the inference graph embeddings = network.inference(images_placeholder, FLAGS.pool_type, FLAGS.use_lrn, 1.0, phase_train=phase_train_placeholder) # Create a saver for restoring variable averages ema = tf.train.ExponentialMovingAverage(1.0) saver = tf.train.Saver(ema.variables_to_restore()) with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(os.path.expanduser(FLAGS.model_dir)) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) else: raise ValueError('Checkpoint not found') images = load_and_align_data(image_paths, FLAGS.image_size, align, landmarkIndices) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb = sess.run(embeddings, feed_dict=feed_dict) dist = np.sqrt(np.mean(np.square(np.subtract(emb[0,:], emb[1,:])))) print('Distance between the embeddings: %3.6f' % dist)
def crop(name): align = align_dlib.AlignDlib(os.path.expanduser(dlib_face_predictor)) landmarkIndices = align_dlib.AlignDlib.OUTER_EYES_AND_NOSE base_dir = os.path.join(os.path.expanduser(input_dir),name) scale = float(face_size) / image_size if os.path.exists(base_dir): bb_filename = os.path.join(base_dir, 'bb.json') # if os.path.exists(bb_filename): # continue bb_list = [] images = os.listdir(base_dir) for image_path in sorted(map(lambda x: os.path.join(base_dir, x), images)): if(os.path.splitext(image_path)[1] == ".png"): filename = os.path.splitext(os.path.split(image_path)[1])[0] # find the bounding box... print("finding face in " + image_path) img = misc.imread(image_path) bb = align.getLargestFaceBoundingBox(img, False) if bb is None: continue print("found!") bb = [ bb.left(), bb.top(), bb.width(), bb.height() ] bb_list.append( { 'name' : filename+'.png', 'bb' : bb } ) continue with open(bb_filename, 'w') as f: json.dump(bb_list,f)
def main(): align = align_dlib.AlignDlib(os.path.expanduser(FLAGS.dlib_face_predictor)) landmarkIndices = align_dlib.AlignDlib.OUTER_EYES_AND_NOSE dataset = facenet.get_dataset(FLAGS.input_dir) # Scale the image such that the face fills the frame when cropped to crop_size scale = float(FLAGS.face_size) / FLAGS.image_size for cls in dataset: output_class_dir = os.path.join(os.path.expanduser(FLAGS.output_dir), cls.name) if not os.path.exists(output_class_dir): os.makedirs(output_class_dir) for image_path in cls.image_paths: filename = os.path.splitext(os.path.split(image_path)[1])[0] output_filename = os.path.join(output_class_dir, filename + '.png') if not os.path.exists(output_filename): print(image_path) try: img = misc.imread(image_path) except (IOError, ValueError, IndexError) as e: errorMessage = '{}: {}'.format(image_path, e) print(errorMessage) else: if img.ndim == 2: img = facenet.to_rgb(img) aligned = align.align(FLAGS.image_size, img, landmarkIndices=landmarkIndices, skipMulti=True, scale=scale) if aligned is not None: misc.imsave(output_filename, aligned)
def __init__(self, model_file, dlib_face_predictor): ''' inital function :param model_file:the path point to chekpoint file (now using the model.ckpt-500000) :type basestring :param dlib_face_predictor: the path point to shape_predictor_68_face_landmarks.dat :type basestring ''' self.__align = align_dlib.AlignDlib( os.path.expanduser(dlib_face_predictor)) self.__landmarkIndices = align_dlib.AlignDlib.OUTER_EYES_AND_NOSE with tf.Graph().as_default(): self.__sess = tf.Session() self.__images_placeholder = tf.placeholder(tf.float32, shape=(None, image_size, image_size, 3), name='input') self.__phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') self.__embeddings = network.inference( self.__images_placeholder, pool_type, use_lrn, keep_probability, phase_train=self.__phase_train_placeholder) self.__image_size = int(self.__images_placeholder.get_shape()[1]) self.__saver = tf.train.Saver(tf.all_variables(), max_to_keep=0) self.__saver.restore(self.__sess, model_file) return
def main(argv=None): align = align_dlib.AlignDlib(os.path.expanduser(FLAGS.dlib_face_predictor)) landmarkIndices = align_dlib.AlignDlib.OUTER_EYES_AND_NOSE output_dir = os.path.expanduser(FLAGS.output_dir) if not os.path.exists(output_dir): os.makedirs(output_dir) # Store some git revision info in a text file in the log directory src_path, _ = os.path.split(os.path.realpath(__file__)) store_revision_info(src_path, output_dir, ' '.join(argv)) dataset = facenet.get_dataset(FLAGS.input_dir) random.shuffle(dataset) # Scale the image such that the face fills the frame when cropped to crop_size scale = float(FLAGS.face_size) / FLAGS.image_size nrof_images_total = 0 nrof_prealigned_images = 0 nrof_successfully_aligned = 0 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) 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') if not os.path.exists(output_filename): try: img = misc.imread(image_path) except (IOError, ValueError, IndexError) as e: errorMessage = '{}: {}'.format(image_path, e) print(errorMessage) else: if img.ndim == 2: img = facenet.to_rgb(img) if FLAGS.use_new_alignment: aligned = align.align_new( FLAGS.image_size, img, landmarkIndices=landmarkIndices, skipMulti=False, scale=scale) else: aligned = align.align(FLAGS.image_size, img, landmarkIndices=landmarkIndices, skipMulti=False, scale=scale) if aligned is not None: print(image_path) nrof_successfully_aligned += 1 misc.imsave(output_filename, aligned) elif FLAGS.prealigned_path: # Face detection failed. Use center crop from pre-aligned dataset class_name = os.path.split(output_class_dir)[1] image_path_without_ext = os.path.join( os.path.expanduser(FLAGS.prealigned_path), class_name, filename) # Find the extension of the image exts = ('jpg', 'png') for ext in exts: temp_path = image_path_without_ext + '.' + ext image_path = '' if os.path.exists(temp_path): image_path = temp_path break try: img = misc.imread(image_path) except (IOError, ValueError, IndexError) as e: errorMessage = '{}: {}'.format(image_path, e) print(errorMessage) else: scaled = misc.imresize(img, FLAGS.prealigned_scale, interp='bilinear') sz1 = scaled.shape[1] / 2 sz2 = FLAGS.image_size / 2 cropped = scaled[(sz1 - sz2):(sz1 + sz2), (sz1 - sz2):(sz1 + sz2), :] print(image_path) nrof_prealigned_images += 1 misc.imsave(output_filename, cropped) else: print('Unable to align "%s"' % image_path) print('Total number of images: %d' % nrof_images_total) print('Number of successfully aligned images: %d' % nrof_successfully_aligned) print('Number of pre-aligned images: %d' % nrof_prealigned_images)
if result: with open(filename, mode='w+b') as f: n.tofile(f) return True else: return False except Exception as e: print(e) return False # Create a HOG face detector using the built-in dlib class predictor_model = "shape_predictor_68_face_landmarks.dat" face_detector = dlib.get_frontal_face_detector() face_pose_predictor = dlib.shape_predictor(predictor_model) face_aligner = openface.AlignDlib(predictor_model) # Take the image file name from the command line file_name = sys.argv[1] img_name = file_name.split('\\')[1] img_name_1 = img_name.split('.')[0] #파일저장명 alignfile = os.path.split(file_name) alignfile = alignfile[1] #파일이름으로 폴더생성 file_path = os.path.splitext(file_name) file_path = os.path.split(file_path[0]) folder_path = file_path[1]
def main(args): #MOON feature extractor; not sure how to make this a modular component symbol = lightened_moon_feature(num_classes=40, use_fuse=True) #the detector passed in from the command line; requires files in facenet/data/ detector = align_dlib.AlignDlib( os.path.expanduser(args.dlib_face_predictor)) landmarkIndices = align_dlib.AlignDlib.OUTER_EYES_AND_NOSE if args.landmarkIndices is not None: landmarkIndices = args.landmarkIndices video = cv2.VideoCapture(args.input_video) devs = mx.cpu() #begin to iterate over the frames and process them ret, frame = video.read() #a list of dictionaries containing face_output for each frame total_output = [] #maps encoding matrix to id number known_faces_dict = dict() known_faces_encoding = [] id_count = 0 while ret is True: face_boxes = detector.getAllFaceBoundingBoxes(frame) id_attr, known_faces_dict, known_faces_encoding, id_count = processFrame( args, frame, known_faces_dict, known_faces_encoding, id_count, symbol, detector, landmarkIndices, devs, face_boxes) total_output.append(id_attr) ret, frame = video.read() #==========CONVERT TO JSON FILE=============== print('done processing; converting to json') #print(total_output) #ith element represents the ith frame frame_num = 0 json_output = '{\r\n"video":\r\n{\r\n"frames":\r\n[\r\n' for frame_info in total_output: #begin the num-faces entry json_output += '{\r\n"num": ' + str(frame_num) + ',\r\n' if len(frame_info.keys()) == 0: #if this still isnt valid, try doing "faces": 0 and closing the field # remove last occurrence of comma k = json_output.rfind(',') json_output = json_output[:k] + json_output[k + 1:] json_output += '},\r\n' #close the num-faces entry; no faces field frame_num += 1 continue json_output += '"faces":\r\n[\r\n' # process the face information in frame_info in a loop for face in frame_info.keys(): #get actual content, which is a list #content shouldnt ever be empty, because there exists a key #TODO may be a bug bc of this assumption content = frame_info[face] pid = content[0] #check if content is length > 1 #there may be an individual with 0 yes-attributes if len(content) == 3: #attributes will contain the topleft,bottomright coordinates, #followed by the attributes themselves attributes = content[1:len(content) - 1] attributes.extend(['Negatives']) d = {pid: attributes} #looks like 0:[] json_output += json.dumps(d) + ',\r\n' continue #attributes will contain the topleft, bottomright coordinates #followed by the attributes themselves attributes = content[1:len(content) - 1] d = {pid: attributes} #now we have the proper split json_output += json.dumps(d) + ',\r\n' #outside of loop # remove last occurrence of comma k = json_output.rfind(',') json_output = json_output[:k] + json_output[k + 1:] json_output += ']\r\n' #close the faces array json_output += '},\r\n' #close the num-faces entry frame_num += 1 # remove last occurrence of comma k = json_output.rfind(',') json_output = json_output[:k] + json_output[k + 1:] json_output += '\r\n]\r\n}\r\n}' d = json.loads(json_output) json_output = json.dumps(d, indent=4, separators=(',', ': ')) #write out to file print('done converting to json; writing to file') f = open('output.json', 'wb') f.write(json_output) f.close() print('done!')
import sys import dlib import cv2 import align_dlib # You can download the required pre-trained face detection model here: # http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2 predictor_model = "shape_predictor_68_face_landmarks.dat" # Take the image file name from the command line file_name = sys.argv[1] # Create a HOG face detector using the built-in dlib class face_detector = dlib.get_frontal_face_detector() face_pose_predictor = dlib.shape_predictor(predictor_model) face_aligner = align_dlib.AlignDlib(predictor_model) # Load the image image = cv2.imread(file_name) # Run the HOG face detector on the image data detected_faces = face_detector(image, 1) print("Found {} faces in the image file {}".format(len(detected_faces), file_name)) # Loop through each face we found in the image for i, face_rect in enumerate(detected_faces): # Detected faces are returned as an object with the coordinates of the top, left, right and bottom edges print("- Face #{} found at Left: {} Top: {} Right: {} Bottom: {}".format(
def __init__(self, predictor, dims): #self.predictor_model = predictor self.face_detector = dlib.get_frontal_face_detector() self.face_pose_predictor = dlib.shape_predictor(predictor) self.face_aligner = align_dlib.AlignDlib(predictor_model) self.output_dimensions = dims
def startDetection(fileName): shape_predictor = "shape_predictor_68_face_landmarks.dat" # construct the argument parse and parse the arguments ''' ap = argparse.ArgumentParser() ap.add_argument("-c", "--cascade", default="haarcascade_frontalface_default.xml", help="path to face detector haar cascade") # ap.add_argument("-p", "--shape-predictor", required=True, # help="path to facial landmark predictor") args = vars(ap.parse_args()) ''' # load the input image and convert it to grayscale image = cv2.imread(fileName) heightO, widthO, channelsO = image.shape print("original width,height=" , widthO,heightO) #image = resize(image, width=500,height= 500) #image = cv2.resize(image, (100, 100)) # Image to array (Biz ekledik) # savefig('rihanna.jpg') # gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) image_return = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image_return = Image.fromarray(image_return) image_return = ImageTk.PhotoImage(image_return) # load the cat detector Haar cascade, then detect cat faces # in the input image detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") #default scale factor = 1.3,1.04 de iyi rects = detector.detectMultiScale(gray, scaleFactor=1.2,minNeighbors=10, minSize=(75, 75)) i=0.01 while True: if len (rects)!=0: print(1.2-i) break if 1.2-i <= 1: print("Couldn't find a face") break else: rects = detector.detectMultiScale(gray, scaleFactor=1.2-i, minNeighbors=10, minSize=(75, 75)) i=i+0.01; #rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=10, minSize=(75, 75)) predictor = dlib.shape_predictor(shape_predictor) ##fa = FaceAligner.FaceAligner(predictor, desiredFaceWidth=96) fa = align_dlib.AlignDlib(shape_predictor) # loop over the face detections faceAligned_RGB = None faceAligned_return = None for (i, rect) in enumerate(rects): # determine the facial landmarks for the face region, then # convert the facial landmark (x, y)-coordinates to a NumPy # array print(rect) x = rect[0] y = rect[1] w = rect[2] h = rect[3] rect = dlib.rectangle(x, y, x + w, y + h) print(rect) shape = predictor(gray, rect) shape = shape_to_np(shape) #cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2) #cv2.putText(image, "Face #{}".format(i + 1), (x, y - 10), #cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 255), 2) # loop over the (x, y)-coordinates for the facial landmarks # and draw them on the image # Face Normalization ##faceAligned = fa.align(image, gray, rect) faceAligned = fa.align(96, image) if faceAligned is None: continue height, width, channels = faceAligned.shape print("aligned width,height=" , width,height) faceAligned_return = cv2.cvtColor(faceAligned, cv2.COLOR_BGR2RGB) faceAligned_RGB = faceAligned_return cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2) cv2.putText(image, "Face #{}".format(i + 1), (x, y - 10),cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 255), 2) ##ADAMIN FACEALIGNED PENCERESINDE BOX YANA KAYIYOR X VE Y DEGISTIGI ICIN #cv2.rectangle(faceAligned, (x, y), (x + w, y + h), (0, 0, 255), 2) #cv2.putText(faceAligned, "Face #{}".format(i + 1), (x, y - 10),cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 255), 2) i = 1 for (x, y) in shape: #print((i, (x, y))) # cv2.putText(image, "{}".format(i), (x, y - 10), # cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 200, 255), 1) i = i + 1 cv2.circle(image, (x, y), 1, (0, 0, 255), -1) # show the detected faces #cv2.imshow("Aligned", faceAligned) #cv2.imshow("Face", image) if faceAligned_return is not None: faceAligned_return = Image.fromarray(faceAligned_return) faceAligned_return = ImageTk.PhotoImage(faceAligned_return) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = Image.fromarray(image) image = ImageTk.PhotoImage(image) #cv2.waitKey(0) return (image_return,faceAligned_return,image,faceAligned_RGB)