def imshow(image_path): img = misc.imread(image_path) img_size = np.asarray(img.shape)[0:2] bounding_boxes, bounding_points = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold,factor) # img_color = cv2.imread(image_path) crop_faces = [] for i in range(bounding_boxes.shape[0]): # 人脸数目: draw = img.copy() det = np.squeeze(detect_face.rerec(bounding_boxes.copy())[i, 0:4]) bounding_point = bounding_points[:, i] # 按i获取多个人脸的point print(det) print(bounding_point) bb = np.zeros(4, dtype=np.int32) # 坐标 bb[0] = np.maximum(det[0] - 2 / 2, 0) bb[1] = np.maximum(det[1] - 2 / 2, 0) bb[2] = np.minimum(det[2] + 2 / 2, img_size[1]) bb[3] = np.minimum(det[3] + 2 / 2, img_size[0]) for ii in range(5): cv2.circle(draw, (bounding_point[ii], bounding_point[ii + 5]), 1, (255, 0, 0), 2) # 绘制特征点 cropped = draw[bb[1]:bb[3], bb[0]:bb[2], :] aligned = misc.imresize(cropped, (160, 160), interp='bilinear') # 缩放图像 print(aligned.shape) crop_faces.append(aligned) # plt.imshow(aligned) plt.imshow(draw) plt.show()
def getFace(img): faces = [] img_size = np.asarray(img.shape)[0:2] bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) if not len(bounding_boxes) == 0: for face in bounding_boxes: if face[4] > 0.50: det = np.squeeze(face[0:4]) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - margin / 2, 0) bb[1] = np.maximum(det[1] - margin / 2, 0) bb[2] = np.minimum(det[2] + margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + margin / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] resized = cv2.resize(cropped, (input_image_size, input_image_size), interpolation=cv2.INTER_CUBIC) prewhitened = facenet.prewhiten(resized) faces.append({ 'face': resized, 'rect': [bb[0], bb[1], bb[2], bb[3]], 'embedding': getEmbedding(prewhitened) }) return faces
def detect_face_and_align(self, img): img_size = np.asarray(img.shape)[0:2] #face_minsize = int(min(img_size[0], img_size[1]) * 0.15) #face_minsize = max(face_minsize, self.face_minsize) bounding_boxes, _ = detect_face.detect_face(img, self.face_minsize, self.pnet, self.rnet, self.onet, self.three_threshold, self.factor) if len(bounding_boxes) < 1: return np.array([]) bb_list = [] for i in range(len(bounding_boxes)): #det = np.squeeze(bounding_boxes[i, 0:4]) det = bounding_boxes[i, 0:4] bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - self.margin / 2, 0) bb[1] = np.maximum(det[1] - self.margin / 2, 0) bb[2] = np.minimum(det[2] + self.margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + self.margin / 2, img_size[0]) bb_list.append(bb) face_list = [] for i in range(len(bb_list)): bb = bb_list[i] cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] aligned = misc.imresize(cropped, (self.image_size, self.image_size), interp='bilinear') prewhitened = facenet.prewhiten(aligned) face_list.append(prewhitened) face_array = np.stack(face_list) return face_array
def find_faces(self, image): faces = [] bounding_boxes, _ = detect_face.detect_face(image, self.minsize, self.pnet, self.rnet, self.onet, self.threshold, self.factor) for bb in bounding_boxes: face = Face() face.container_image = image face.bounding_box = np.zeros(4, dtype=np.int32) img_size = np.asarray(image.shape)[0:2] face.bounding_box[0] = np.maximum( bb[0] - self.face_crop_margin / 2, 0) face.bounding_box[1] = np.maximum( bb[1] - self.face_crop_margin / 2, 0) face.bounding_box[2] = np.minimum( bb[2] + self.face_crop_margin / 2, img_size[1]) face.bounding_box[3] = np.minimum( bb[3] + self.face_crop_margin / 2, img_size[0]) cropped = image[face.bounding_box[1]:face.bounding_box[3], face.bounding_box[0]:face.bounding_box[2], :] face.image = cv2.resize(cropped, (self.face_crop_size, self.face_crop_size), interpolation=cv2.INTER_LINEAR) faces.append(face) return faces
def detect(self, img): """ img: rgb 3 channel """ minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor bounding_boxes, _ = FaceDet.detect_face(img, minsize, self.pnet, self.rnet, self.onet, threshold, factor) area = (bounding_boxes[:, 2] - bounding_boxes[:, 0]) * ( bounding_boxes[:, 3] - bounding_boxes[:, 1]) face_idx = area.argmax() bbox = bounding_boxes[face_idx][:4] # xy,xy margin = 32 x0 = np.maximum(bbox[0] - margin // 2, 0) y0 = np.maximum(bbox[1] - margin // 2, 0) x1 = np.minimum(bbox[2] + margin // 2, img.shape[1]) y1 = np.minimum(bbox[3] + margin // 2, img.shape[0]) x0, y0, x1, y1 = bbox = [int(k + 0.5) for k in [x0, y0, x1, y1]] cropped = img[y0:y1, x0:x1, :] scaled = cv2.resize(cropped, (160, 160), interpolation=cv2.INTER_LINEAR) return scaled, bbox
def detect_face(img): minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor margin = 32 image_size = 160 with tf.Graph().as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): pnet, rnet, onet = df.create_mtcnn(sess, None) img_size = np.asarray(img.shape)[0:2] bounding_boxes, _ = df.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) det = np.squeeze(bounding_boxes[0, 0:4]) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - margin / 2, 0) bb[1] = np.maximum(det[1] - margin / 2, 0) bb[2] = np.minimum(det[2] + margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + margin / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] aligned = misc.imresize(cropped, (image_size, image_size), interp='bilinear') face_token = str(uuid.uuid4()) output_filename = os.path.join(align_path, face_token + '.png') misc.imsave(output_filename, aligned) prewhitened = facenet.prewhiten(aligned) aligned_image = prewhitened aligned_pictures = [aligned_image] return aligned_pictures
def load_and_align_data(image_paths, image_size=160, margin=44): minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor print('Creating networks and loading parameters') with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): pnet, rnet, onet = detect_face.create_mtcnn(sess, None) img_list = [] nrof_samples = len(image_paths) success_paths = [] for img_path in image_paths: img = misc.imread(img_path, mode='RGB') img_size = np.asarray(img.shape)[0:2] bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) if len(bounding_boxes) < 1: print("no face found in {}".format(img_path)) continue det = np.squeeze(bounding_boxes[0, 0:4]) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - margin / 2, 0) bb[1] = np.maximum(det[1] - margin / 2, 0) bb[2] = np.minimum(det[2] + margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + margin / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] aligned = misc.imresize(cropped, (image_size, image_size), interp='bilinear') prewhitened = prewhiten(aligned) img_list.append(prewhitened) success_paths.append(img_path) images = np.stack(img_list) return success_paths, images
def _detectSingleFace(path, minsize=50, threshold=[0.6, 0.7, 0.7], factor=0.709, pad=5): I = cv2.imread(path) I = I[:, :, ::-1] box, point = detect_face(I, minsize, pnet_fun, rnet_fun, onet_fun, threshold, factor) if len(box) == 0: return None x1, y1, x2, y2, acc = box[0] x1, y1, x2, y2 = max(x1 - pad, 0), max(y1 - pad, 0), max(x2 + pad, 0), max(y2 + pad, 0) x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) h, w = y2 - y1, x2 - x1 # print(h,w) newI = I[y1:y2, x1:x2] newI = cv2.resize(newI, (96, 96)) # zeroI=np.zeros((96,96,3),dtype=np.uint8)+255 # c=zeroI.shape[1] # c1=newI.shape[1] # s=(c-c1)//2 # zeroI[:,s:s+c1,:]=newI # # a,b=os.path.split(path) # pyplot.imsave('test/'+b,newI) return np.round(newI / 255.0, decimals=12)
def find_faces(self, image): image = image[:, :, 0:3] faces = [] bounding_boxes, _ = detect_face.detect_face(image, self.minsize, self.pnet, self.rnet, self.onet, self.threshold, self.factor) for bb in bounding_boxes: face = Face() face.bounding_box = np.zeros(4, dtype=np.int32) img_size = np.asarray(image.shape)[0:2] face.bounding_box[0] = np.maximum( bb[0] - self.face_crop_margin / 2, 0) face.bounding_box[1] = np.maximum( bb[1] - self.face_crop_margin / 2, 0) face.bounding_box[2] = np.minimum( bb[2] + self.face_crop_margin / 2, img_size[1]) face.bounding_box[3] = np.minimum( bb[3] + self.face_crop_margin / 2, img_size[0]) cropped = image[face.bounding_box[1]:face.bounding_box[3], face.bounding_box[0]:face.bounding_box[2], :] # face.image = misc.imresize(cropped, (self.face_crop_size, self.face_crop_size), interp='bilinear') image_cropped = cv2.resize( cropped, (self.face_crop_size, self.face_crop_size), interpolation=cv2.INTER_LINEAR) # image_cropped = cv2.bitwise_not(image_cropped) # face.data_image = cv2.imencode('.jpg',image_cropped) # print(image_cropped.shape) face.data_image = self.encode_jpeg(image_cropped) faces.append(face) return faces
def detectFace(path='facedb', sess=sess, minsize=50, threshold=[0.6, 0.7, 0.7], factor=0.709): ''' :param path: :param sess: :return:a list of Image,None stand for can not detectFace ''' ret = [] for f in os.listdir(path): I = cv2.imread(os.path.join(path, f)) I = I[:, :, ::-1] box, point = detect_face(I, minsize, pnet_fun, rnet_fun, onet_fun, threshold, factor) if len(box) == 0: ret.append(None) continue x1, y1, x2, y2, acc = box[0] x1, y1, x2, y2 = max(x1, 0), max(y1, 0), max(x2, 0), max(y2, 0) x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) newI = I[y1:y2, x1:x2] newI = cv2.resize(newI, (96, 96)) ret.append((f, newI)) return ret
def align(self,image_path, margin=44, image_size=160): img = misc.imread(image_path) img = img[:,:,0:3] bounding_boxes, _ = detect_face.detect_face(img, self.minsize, self.pnet, self.rnet, self.onet, self.threshold, self.factor) nrof_faces = bounding_boxes.shape[0] bb = np.zeros(4, dtype=np.int32) print("Number of Faces: ", nrof_faces) if nrof_faces>0: det = bounding_boxes[:,0:4] img_size = np.asarray(img.shape)[0:2] if nrof_faces>1: 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 = (det[index,:]) det = np.squeeze(det) bb[0] = np.maximum(det[0]-margin/2, 0) bb[1] = np.maximum(det[1]-margin/2, 0) bb[2] = np.minimum(det[2]+margin/2, img_size[1]) bb[3] = np.minimum(det[3]+margin/2, img_size[0]) cropped = img[bb[1]:bb[3],bb[0]:bb[2],:] scaled = misc.imresize(cropped, (image_size, image_size), interp='bilinear') misc.imsave("./temp.png", scaled) return bb else: return None
def mtcnn_detect(self, bgr_img, detect_scale=1.0): self.minsize = 20 / detect_scale detect_scale = 1. im = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB) im_scale = cv2.resize(im, (int(im.shape[1] * detect_scale), int(im.shape[0] * detect_scale))) bboxes, points = detect_face.detect_face( im_scale, self.minsize, self.pnet, self.rnet, self.onet, self.threshold, self.factor) if len(bboxes) == 0: bboxes = np.array([]) points = np.array([]) else: bboxes /= detect_scale w = bboxes[:,2] - bboxes[:,0] h = bboxes[:,3] - bboxes[:,1] bboxes[:,2] = w bboxes[:,3] = h points = np.array(points) / detect_scale sort_index = np.argsort(-bboxes[:, 2]) bboxes = bboxes[sort_index] points = points[:, sort_index] points_out = [] for point in points.T: points_out += [np.array([[point[i], point[i+5]] for i in range(5)])] points = np.array(points_out) # bboxes = bboxes.astype(int) return bboxes, points
def makeFaceList(): basepath = 'F:/computervision/facematch-master/Ramesh/images/' for root, dirs, files in os.walk(basepath): for filena in files: print(filena) img = cv2.imread(basepath + filena) name = filena.split('.')[0] img_size = np.asarray(img.shape)[0:2] bounding_boxes, _ = detect_face.detect_face( img, minsize, pnet, rnet, onet, threshold, factor) if not len(bounding_boxes) == 0: for face in bounding_boxes: if face[4] > 0.95: det = np.squeeze(face[0:4]) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - margin / 2, 0) bb[1] = np.maximum(det[1] - margin / 2, 0) bb[2] = np.minimum(det[2] + margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + margin / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] resized = cv2.resize( cropped, (input_image_size, input_image_size), interpolation=cv2.INTER_CUBIC) prewhitened = facenet.prewhiten(resized) faces.append({ 'face': resized, 'rect': [bb[0], bb[1], bb[2], bb[3]], 'embedding': getEmbedding(prewhitened), 'name': name }) return faces
def detect(self, img): """ img: rgb 3 channel """ minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor bounding_boxes, points = FaceDet.detect_face(img, minsize, self.pnet, self.rnet, self.onet, threshold, factor) num_face = bounding_boxes.shape[0] # assert num_face == 1, num_face if num_face != 1: bbox = [0, 0, 112, 112] else: bbox = bounding_boxes[0][:4] # xy,xy margin = 32 x0 = np.maximum(bbox[0] - margin // 2, 0) y0 = np.maximum(bbox[1] - margin // 2, 0) x1 = np.minimum(bbox[2] + margin // 2, img.shape[1]) y1 = np.minimum(bbox[3] + margin // 2, img.shape[0]) x0, y0, x1, y1 = bbox = [int(k + 0.5) for k in [x0, y0, x1, y1]] cropped = img[y0:y1, x0:x1, :] scaled = misc.imresize(cropped, (160, 160), interp='bilinear') return scaled, bbox, points
def align_image(input_image, output_image, pnet, rnet, onet, image_size=182, margin=44, random_order=True, gpu_memory_fraction=1.0, debug=False): minsize = 20 # minimum size of face threshold = [0.7, 0.7, 0.9] # three steps's threshold factor = 0.709 # scale factor if not os.path.exists(output_image): try: img = scp_misc.imread(input_image) except (IOError, ValueError, IndexError) as e: errorMessage = '{}: {}'.format(input_image, e) if debug: print(errorMessage) else: if img.ndim < 2: if debug: print('Unable to align "%s"' % image_path) if img.ndim == 2: img = facenet.to_rgb(img) img = img[:, :, 0:3] bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) nrof_faces = bounding_boxes.shape[0] if nrof_faces > 0: det = bounding_boxes[:, 0:4] img_size = np.asarray(img.shape)[0:2] if nrof_faces > 1: det = np.squeeze(det) counter = 0 scaled_list = [] for d in det: bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(d[0] - margin / 2, 0) bb[1] = np.maximum(d[1] - margin / 2, 0) bb[2] = np.minimum(d[2] + margin / 2, img_size[1]) bb[3] = np.minimum(d[3] + margin / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] scaled = scp_misc.imresize(cropped, (image_size, image_size), interp='bilinear') filename = "{}_{}.jpg".format(output_image.split(".")[0] + "image", str(counter)) scp_misc.imsave(filename, scaled) scaled_list.append(scaled) counter = counter +1 return True, scaled_list if nrof_faces == 1: det = np.squeeze(det) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - margin / 2, 0) bb[1] = np.maximum(det[1] - margin / 2, 0) bb[2] = np.minimum(det[2] + margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + margin / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] scaled = scp_misc.imresize(cropped, (image_size, image_size), interp='bilinear') scp_misc.imsave(output_image, scaled) return True, scaled else: if debug: print('Unable to align "%s"' % image_path) return False, 1
def process_celebrity(img): minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor print('Creating networks and loading parameters') with tf.Graph().as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1.0) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): pnet, rnet, onet = detect_face.create_mtcnn(sess, None) img_size = np.asarray(img.shape)[0:2] img_list = [] bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) count = len(bounding_boxes) for i in range(count): det = np.squeeze(bounding_boxes[i, 0:4]) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - margin / 2, 0) bb[1] = np.maximum(det[1] - margin / 2, 0) bb[2] = np.minimum(det[2] + margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + margin / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] aligned = misc.imresize(cropped, (160, 160), interp='bilinear') prewhitened = facenet.prewhiten(aligned) # prewhitened = np.array(prewhitened).reshape(160, 160, 3) img_list.append(prewhitened) prewhitened = np.stack(img_list) with tf.Session() as sess: facenet.load_model(celeb_model) images_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0") phase_train_placeholder = tf.get_default_graph().get_tensor_by_name( "phase_train:0") feed_dict = { images_placeholder: prewhitened, phase_train_placeholder: False } emb = sess.run(embeddings, feed_dict=feed_dict) classifier_filename_exp = os.path.expanduser(classifier) with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile, encoding='latin1') print('Loaded classifier model from file "%s"\n' % classifier_filename_exp) predictions = model.predict_proba(emb) best_class_indices = np.argmax(predictions, axis=1) best_class_probabilities = predictions[ np.arange(len(best_class_indices)), best_class_indices] result_string = "" for i in range(count): result_string += class_names[best_class_indices[i]] + ', ' print(result_string) return result_string
def eyes(im): img = misc.imread(im) bounding_boxes, face_points = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) if bounding_boxes.shape[0] == 0: return -1 eye_dis = abs(face_points[0] - face_points[1]) eye_mid = (face_points[0] + face_points[1])/2 score1 = abs(face_points[2]-eye_mid)/eye_dis return round(score1[0], 2)
def getFace(img): faces = [] img_size = np.asarray(img.shape)[0:2] n = 0 bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) if not len(bounding_boxes) == 0: for face in bounding_boxes: n = n + 1 return n
def load_align_image(image_path, sess, graph, pnet, rnet, onet): img = misc.imread(os.path.expanduser(image_path)) with graph2.as_default(): # sess2 = tf.Session(config=tf.ConfigProto(log_device_placement=False)) with sess2.as_default(): bounding_boxes, bounding_points = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) nrof_faces = bounding_boxes.shape[0] # 人脸数目 print('The number of faces detected: {}'.format(nrof_faces))
def detect(self, img_path): try: img = misc.imread(os.path.expanduser(img_path)) bounding_boxes, _ = detect_face.detect_face( img, minsize, self.pnet, self.rnet, self.onet, threshold, factor) return len(bounding_boxes) except Exception as e: print(e) return 0
def detect(self, img): """ img: rgb 3 channel """ minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor _, points = FaceDet.detect_face( img, minsize, self.pnet, self.rnet, self.onet, threshold, factor) return points
def main(args): # MTCNN with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): pnet, rnet, onet = detect_face.create_mtcnn(sess, None) threshold = [0.6, 0.7, 0.7] factor = 0.709 # Output dirs creation if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) images = [] for path in sorted(os.listdir(args.input_dir)): if not os.path.exists(os.path.join(args.output_dir, path)): os.mkdir(os.path.join(args.output_dir, path)) for name in sorted(os.listdir(os.path.join(args.input_dir, path))): images.append(os.path.join(path, name)) # Alignment procedure for path in tqdm(images): img = io.imread(os.path.join(args.input_dir, path)) if img.ndim == 2: img = to_rgb(img) img = img[:, :, 0:3] _minsize = min(min(img.shape[0] // 5, img.shape[1] // 5), 80) bounding_boxes, points = detect_face.detect_face( img, _minsize, pnet, rnet, onet, threshold, factor) if bounding_boxes.size > 0: bindex = -1 nrof_faces = bounding_boxes.shape[0] if nrof_faces > 0: det = bounding_boxes[:, 0:4] img_size = np.asarray(img.shape)[0:2] bindex = 0 if nrof_faces > 1: 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) bindex = np.argmax(bounding_box_size - offset_dist_squared * 2.0) points = points[:, bindex] landmark = points.reshape((2, 5)).T warped = preprocess(img, landmark) io.imsave(os.path.join(args.output_dir, path), warped) else: print(path + ' was skipped')
def image_array_align_data(self, image_arr, img_name, image_size=160, margin=32, detect_multiple_faces=True): minsize = 20 threshold = [0.6, 0.7, 0.7] factor = 0.709 img = image_arr bounding_boxes, _ = detect_face.detect_face(img, minsize, self.pnet, self.rnet, self.onet, threshold, factor) nrof_faces = bounding_boxes.shape[0] if nrof_faces > 0: det = bounding_boxes[:, 0:4] det_arr = [] img_size = np.asarray(img.shape)[0:2] if nrof_faces > 1: if 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) 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] - margin / 2, 0) bb[1] = np.maximum(det[1] - margin / 2, 0) bb[2] = np.minimum(det[2] + margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + margin / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] # 进行图片缩放 cv2.resize(img,(w,h)) scaled = imresize(cropped, (image_size, image_size), interp='bilinear') #保存人脸对齐、裁剪后的人脸图片 cv2.imwrite(self.FLAGS.img_dir + img_name + '.jpg', scaled)
def process_one(self, img): # print('aligning > start ', str(datetime.datetime.now())) img = img[:, :, 0:3] bounding_boxes, _ = detect_face.detect_face(img, MIN_SIZE, self.pnet, self.rnet, self.onet, THRESHOLD, FACTOR) nrof_faces = bounding_boxes.shape[0] if nrof_faces <= 0: print('**-*-*-*-* No face detected *-*-*----*') return img if nrof_faces > 0: det = bounding_boxes[:, 0:4] det_arr = [] img_size = np.asarray(img.shape)[0:2] if nrof_faces == 1: 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] - MARGIN / 2, 0) bb[1] = np.maximum(det[1] - MARGIN / 2, 0) bb[2] = np.minimum(det[2] + MARGIN / 2, img_size[1]) bb[3] = np.minimum(det[3] + MARGIN / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] scaled = misc.imresize(cropped, (IMAGE_SIZE, IMAGE_SIZE), interp='bilinear') # print('aligning > finish', str(datetime.datetime.now())) # imagen = Image.fromarray(scaled) # imagen.save('./tito.jpg') return scaled elif nrof_faces > 1: if 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, :])
def detection_face(img): minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor print('Creating networks and loading parameters') with tf.Graph().as_default(): # gpu_memory_fraction = 1.0 # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_memory_fraction) # sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) sess = tf.Session() with sess.as_default(): pnet, rnet, onet = detect_face.create_mtcnn(sess, None) bounding_boxes, points = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) return bounding_boxes, points
def detect_face(self, img_rgb): """ Input: R-G-B image matrix. Output: bounding_boxes (N x 5), landmarks (10 x N) bounding_box: [left, up, right, down, score] """ bounding_boxes, landmarks = detect_face.detect_face( img_rgb, self.minsize, self.pnet, self.rnet, self.onet, self.threshold, self.factor) return bounding_boxes, landmarks
def save_align_image(image_path): img = misc.imread(os.path.expanduser(image_path)) img_size = np.asarray(img.shape)[0:2] with graph2.as_default(): # sess2 = tf.Session(config=tf.ConfigProto(log_device_placement=False)) with sess2.as_default(): bounding_boxes, bounding_points = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) nrof_faces = bounding_boxes.shape[0] # 人脸数目 width = img_size[1] height = img_size[0] for i in range(nrof_faces): # 遍历所有faces draw = img.copy() print('nrof_faces: {}'.format(i)) # det = np.squeeze(bounding_boxes[i, 0:4]) det = np.squeeze(detect_face.rerec(bounding_boxes.copy())[i, 0:4]) bounding_point = bounding_points[:, i] # 按i获取多个人脸的point bb = np.zeros(4, dtype=np.int32) # 坐标 bb[0] = np.maximum(det[0] - 2 / 2, 0) bb[1] = np.maximum(det[1] - 2 / 2, 0) bb[2] = np.minimum(det[2] + 2 / 2, img_size[1]) bb[3] = np.minimum(det[3] + 2 / 2, img_size[0]) # for ii in range(5): # cv2.circle(draw, (bounding_point[ii], bounding_point[ii + 5]), 1, (255, 0, 0), 2) # 绘制特征点 cropped = draw[bb[1]:bb[3], bb[0]:bb[2], :] # 裁剪 aligned = misc.imresize(cropped, (160, 160), interp='bilinear') # 缩放图像 # plt.imshow(aligned) # plt.show() # # Need to detect if face is too blury to be detected # gray_face = cv2.cvtColor(aligned, cv2.COLOR_BGR2GRAY) # blury_value = cv2.Laplacian(gray_face, cv2.CV_64F).var() # if blury_value < 4: # print('A blur face (%d) captured, avoid it.' %blury_value) # continue # else: # print('Blur Value: %d, good'%blury_value) # base_path, old_dir_name, img_name = image_path.rsplit('/', 2) new_dir_name = old_dir_name + '_' new_dir_path = os.path.join(base_path, new_dir_name) new_img_path = os.path.join(new_dir_path, img_name) if not os.path.exists(new_dir_path): os.mkdir(new_dir_path) misc.imsave(new_img_path, aligned) # 保存为图像
def get_face_img(img_paths): # input a list of paths of images # return # (1) close-ups of faces # (2) source # (3) locations face_closeups = list() face_source = list() face_locations = list() with tf.Graph().as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.25) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): pnet, rnet, onet = detect_face.create_mtcnn(sess, None) for path in img_paths: img = img_read(path) bounding_boxes, _ = detect_face.detect_face( img, minsize, pnet, rnet, onet, threshold, factor) nrof_faces = bounding_boxes.shape[0] if nrof_faces > 0: det = bounding_boxes[:, 0:4] img_size = np.asarray(img.shape)[0:2] for det_no in range(nrof_faces): each_det = np.squeeze(det[det_no]) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(each_det[0] - args_margin / 2, 0) # left Bound bb[1] = np.maximum(each_det[1] - args_margin / 2, 0) # upper Bound bb[2] = np.minimum(each_det[2] + args_margin / 2, img_size[1]) # right Bound bb[3] = np.minimum(each_det[3] + args_margin / 2, img_size[0]) # lower Bound cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] scaled = misc.imresize(cropped, (args_image_size, args_image_size), interp='bilinear') face_closeups.append(scaled) face_source.append(path) face_locations.append(bb) return face_closeups, face_source, face_locations
def detect_folder(folder, threshold): face_counts = 0 n_time = time.time() total_set = set() for img_path in os.listdir(folder): img_path = os.path.join(folder, img_path) img = misc.imread(os.path.expanduser(img_path)) # load_align_image(img, sess, graph, pnet, rnet, onet) bounding_boxes, bounding_points = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) nrof_faces = bounding_boxes.shape[0] # 人脸数目 face_counts += nrof_faces total_set.add((img_path, nrof_faces)) print('{}, The number of faces detected: {}'.format(img_path, nrof_faces)) print('Total faces: {}; cotst: {} '.format(face_counts, time.time()-n_time)) return total_set
def convert_to_embedding(self, single=False, img_path=None): extracted = [] npy='' with tf.Graph().as_default(): with tf.Session() as sess: self.sess = sess # Load the model facenet.load_model(self.model_dir) pnet, rnet, onet = detect_face.create_mtcnn(sess, npy) minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor margin = 32 frame_interval = 3 batch_size = 1000 image_size = 160 input_image_size = 160 # Get input and output tensors images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0") self.images_placeholder = tf.image.resize_images(images_placeholder,(self.image_size, self.image_size)) self.embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0") self.phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0") self.embedding_size = self.embeddings.get_shape()[1] if not single: for filename in os.listdir(self.data_path): img = cv2.imread(self.data_path+"/"+filename, 1) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #cv2.imshow('img', img) #cv2.waitKey(0) bounding_boxes, points = self.alignMTCNN.get_bounding_boxes(image=img) bounding_boxes,points = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) #detector = MTCNN() #image = cv2.imread() #result = detector.detect_faces(img) #bounding_box = result[0]['box'] #keypoints = result[0]['keypoints'] faces = self.get_faces(img, bounding_boxes, points, filename) extracted.append(faces) with open('extracted_embeddings.pickle','wb') as f: pickle.dump(extracted,f) return None else: img = cv2.imread(img_path, 1) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) bounding_boxes, points = self.alignMTCNN.get_bounding_boxes(image=img) faces = self.get_faces(img, bounding_boxes, points, img_path) return faces