コード例 #1
0
    def translatevideo(self, cap, videoout=__videoout):
        print("output dir is {}".format(videoout))
#        if not os.path.exists(video):
#            print("file is not exit, and use camera")
#            video = 0
        ret, frame = cap.read()
        sub = 0
        top = 0
        while ret:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            box, pot = detect_face.detect_face(frame, self.__minsize, self.__pnet, self.__rnet, self.__onet, self.__threshold, self.__factor)
            try:
                length = np.shape(pot)[1]
            except:
                length = 0
            for i in range(length):
                prefix = ''.join(random.sample(string.ascii_letters + string.digits, 8))
                det = box[i, 0:4]
                img = frame[int(det[1]):int(det[3]), int(det[0]):int(det[2])]
                savepath = os.path.join(videoout + "/" + prefix + str(sub) + ".jpg")
                misc.imsave(savepath, img)
                sub = sub + 1
                print("top save the number of photo is {}".format(sub))

            ret, frame = cap.read()
            top = top + 1
            print("top to progress the number of fps is {}".format(top))
コード例 #2
0
    def translateface(self, input=__input, output=__out):
        criter = 249
        dataset = facenet.get_dataset(input)
        for cls in dataset:
            assert (len(cls.image_paths)>0, 'There must be at least one image for each class in the dataset')
        paths, labels = facenet.get_image_paths_and_labels(dataset)
        try:
            [os.mkdir(os.path.join(output, label)) for label in set(labels)]
        except:
            print('Please detect the folder under the {} first and try again'.format(output))
            return 0
        for i in range(len(paths)):
            path = paths[i]
            img = cv2.imread(path)
            w, h, d = img.shape
            if w > criter:
                img = cv2.resize(img, (int(h*criter/w), criter))
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#            cv2.imshow("input", cv2.cvtColor(img, cv2.COLOR_RGB2BGR))
#            cv2.waitKey(0)

            box,_ = detect_face.detect_face(img, self.__minsize, self.__pnet, self.__rnet, self.__onet, self.__threshold, self.__factor)
            det = box[:, 0:4]
            img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
            for j in range(np.shape(box)[0]):
                res = img[int(det[j][1]):int(det[j][3]), int(det[j][0]):int(det[j][2])]
#                cv2.imshow("result",res)
#                cv2.waitKey(0)
                savepath = os.path.join(output + "/" + labels[i], path.split('/')[-1])
                #print(savepath)
                cv2.imwrite(savepath, res)
コード例 #3
0
def load_and_align_data(image_path,
                        image_size=160,
                        margin=44,
                        gpu_memory_fraction=1.0):
    '''
    :param image_path: 图片的地址
    :param image_size: 对齐后图片的大小
    :param margin: 图片界限
    :param gpu_memory_fraction: GPU设定

    :return: images: 对齐后的人脸集合,list
             bounding_boxes:人脸框集合 ndarray,n*5,[x1 x2 y1 y2 p], float32
             face_exist:是否存在人脸,布尔值,True:存在;False:不存在
             img:读取的图片
    '''
    minsize = 20  # minimum size of face
    threshold = [0.6, 0.7, 0.7]  # three steps's threshold
    factor = 0.709  # scale factor
    face_exist = True

    with tf.Graph().as_default():
        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))
        with sess.as_default():
            pnet, rnet, onet = detect_face.create_mtcnn(sess, None)
    images = []
    img = cv2.imread(image_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # BGR to 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:
        image_path.remove(image_path)
        print("没检测到人脸")
        face_exist = False
    else:  # 检测到人脸
        for per_box in bounding_boxes:
            box = per_box[0:4]
            bb = np.zeros(4, dtype=np.int32)
            bb[0] = np.maximum(box[0] - margin / 2, 0)
            bb[1] = np.maximum(box[1] - margin / 2, 0)
            bb[2] = np.minimum(box[2] + margin / 2, img_size[1])
            bb[3] = np.minimum(box[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 = facenet.prewhiten(aligned)
            images.append(prewhitened)
    return images, img, bounding_boxes, face_exist
コード例 #4
0
filename = './test/Benedict Cumberbatch_001.png'
'''
Mtcnn 检测人脸
'''
minsize = 20
threshold = [0.6, 0.7, 0.7]  # three steps's threshold
factor = 0.709  # scale factor

graph = tf.Graph()  # tf图
sess = tf.Session(graph=graph)
with graph.as_default():
    pnet, rnet, onet = detect_face.create_mtcnn(sess, None)
image = cv2.imread(filename)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)  # BGR to RGB
bounding_boxes, _ = detect_face.detect_face(image, minsize, pnet, rnet, onet,
                                            threshold, factor)
box = bounding_boxes[0:3].astype(int)
for number in range(box.shape[0]):
    cv2.rectangle(image, (box[number, 0], box[number, 1]),
                  (box[number, 2], box[number, 3]), (0, 255, 255), 1)
    face = image[box[number, 1]:box[number, 3], box[number, 0]:box[number, 2]]
'''
进行性别/年龄检测
年龄检测有点不准,建议不要使用人脸输入,人体输入似乎更好, 也可能和性别/年龄检测模型有关
'''
ag = age_and_gender(device_id='/gpu:0', GENDER_LIST=['Male', 'Female'])
best_choice, second_choice = ag.predict(
    mode=1,  # 0: 年龄;1:性别
    image_file=filename,
    image_bound=face,
    use_tf_to_read=True,  # 整张图为True,优先级高于人脸图
コード例 #5
0
	def align(self, path_input, path_output):
		# get dataset 
		dataset = facenet.get_dataset(path_input)
		# create output dir 
		if not os.path.exists(path_output):
			os.makedirs(path_output)
        # 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(path_output, 'bounding_boxes_%05d.txt' % random_key)
		with open(bounding_boxes_filename, "w") as text_file:
			# number of images 
			nrof_images_total = 0
			# number of successfully alinged faces 
			nrof_successfully_aligned = 0
			for cls in dataset:
				path_output_class = os.path.join(path_output, cls.name)
				if not os.path.exists(path_output_class):
					os.makedirs(path_output_class)
				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(path_output_class, filename + '.png')
					print(image_path)
					if not os.path.exists(output_filename):
					    try:
					        img = misc.imread(image_path)
					        print('read data dimension: ', img.ndim)
					    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 = facenet.to_rgb(img)
					        	print('to_rgb data dimension: ', img.ndim)
					        img = img[:, :, 0:3]
					        # bounding boxes 
					        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]
					        print('detected_face: %d' % 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_temp = np.zeros(4, dtype=np.int32)
						        bb_temp[0] = np.maximum(det[0], 0)
						        bb_temp[1] = np.maximum(det[1], 0)
						        bb_temp[2] = np.minimum(det[2], img_size[1])
						        bb_temp[3] = np.minimum(det[3], img_size[0])
						        cropped_temp = img[bb_temp[1]:bb_temp[3], bb_temp[0]:bb_temp[2], :]
						        scaled_temp = misc.imresize(cropped_temp, (self.image_size, self.image_size), interp='bilinear')
						        nrof_successfully_aligned += 1
						        misc.imsave(output_filename, scaled_temp)
						        text_file.write('%s %d %d %d %d\n' % (output_filename, bb_temp[0], bb_temp[1], bb_temp[2], bb_temp[3]))
					        else:
					        	print('No faces 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)
コード例 #6
0
 def detetface(self, img):
     faceBound, point = detect_face.detect_face(img, self.__minsize, self.__pnet, self.__rnet, self.__onet, self.__threshold, self.__factor)
     return faceBound, point