def face_detection(self, img, scale_step=20):

        # this training accuracy should be the same as your training process,
        ##################################################################################
        train_predicts = []
        for idx in range(self.data.shape[0]):
            train_predicts.append(self.sc_function(self.data[idx, ...]))
        print('Check training accuracy is: ',
              np.mean(np.sign(train_predicts) == self.labels))
        ##################################################################################

        scales = 1 / np.linspace(1, 8, scale_step)
        patches, patch_xyxy = image2patches(scales, img)
        print('Face Detection in Progress ..., total %d patches' %
              patches.shape[0])
        predicts = [self.sc_function(patch) for patch in tqdm(patches)]
        print(np.mean(np.array(predicts) > 0), np.sum(np.array(predicts) > 0))
        pos_predicts_xyxy = np.array([
            patch_xyxy[idx] + [score] for idx, score in enumerate(predicts)
            if score > 0
        ])
        if pos_predicts_xyxy.shape[0] == 0:
            return
        xyxy_after_nms = nms(pos_predicts_xyxy, 0.01)

        print('after nms:', xyxy_after_nms.shape[0])
        for idx in range(xyxy_after_nms.shape[0]):
            pred = xyxy_after_nms[idx, :]
            cv2.rectangle(img, (int(pred[0]), int(pred[1])),
                          (int(pred[2]), int(pred[3])), (0, 255, 0),
                          2)  #gree rectangular with line width 3

        return img
Esempio n. 2
0
	def get_hard_negative_patches(self, img, name, scale_step = 10):

		scales = 1 / np.linspace(1, 8, scale_step)
		if os.path.exists('patches_%s.npy' %name):
			patches = np.load('patches_%s.npy' %name)
			patch_xyxy = np.load('patch_position_%s.npy' %name)
			print('Patches loaded')
		else:
			patches, patch_xyxy = image2patches(scales, img)
			print('Face Detection in Progress ..., total %d patches' % patches.shape[0])
			np.save('patches_%s.npy' %name, patches)
			np.save('patch_position_%s.npy' %name, patch_xyxy)
			print('Patches saved')

		if os.path.exists('patches_score%s.pkl' %name):
			print('[Find cached Patches Scores, patches_score%s.pkl loading...]' % name)
			with open('patches_score%s.pkl' %name, 'rb') as f:
				predicts = pickle.load(f)
			print('Patches Scores loaded')
		else:
			predicts = [self.sc_function(patch) for patch in tqdm(patches)]
			pickle.dump(predicts, open('patches_score%s.pkl' %name, 'wb'))
			print('Patches Scores saved')

		predicts = np.array(predicts)
		wrong_patches = patches[np.where(predicts > 0), ...]

		return wrong_patches
	def get_hard_negative_patches(self, img, scale_step = 10):
		scales = 1 / np.linspace(1, 8, scale_step)
		patches, patch_xyxy = image2patches(scales, img)
		print('Get Hard Negative in Progress ..., total %d patches' % patches.shape[0])
		predicts = [self.sc_function(patch) for patch in tqdm(patches)]
		wrong_patches = patches[np.where(np.array(predicts) > 0), ...]
		print(wrong_patches.shape)
		return wrong_patches
Esempio n. 4
0
 def get_hard_negative_patches(self, img, scale_step=10):
     scales = 1 / np.linspace(1, 8, scale_step)
     patches, patch_xyxy = image2patches(scales, img)
     print('Get Hard Negative in Progress ..., total %d patches' %
           patches.shape[0])
     predicts = [self.sc_function(patch) for patch in tqdm(patches)]
     predicts = np.array(predicts)
     wrong_patches = patches[np.where(predicts > 0), ...]
     pos_predicts_xyxy = np.array([
         patch_xyxy[idx] + [score] for idx, score in enumerate(predicts)
         if score > 0
     ])
     patches_after_nms, nms_patches_idx = nms(pos_predicts_xyxy, 0.01)
     return wrong_patches[0], wrong_patches[0][nms_patches_idx]
    def get_hard_negative_patches(self, img, scale_step=10):
        scales = 1 / np.linspace(1, 8, scale_step)
        patches, patches_xyxy = image2patches(scales, img)
        print('Get Hard Negative in Progress..., total %d patches' %
              patches.shape[0])

        preds = np.array([self.sc_function(patch) for patch in tqdm(patches)])
        wrong_patches = patches[np.where(preds > 0), ...]

        # apply nms
        # hard_negatives_xyxy = np.array([patches_xyxy[i] + [score] for i, score in enumerate(preds) if score > 0])
        # print('num hard negative detections before nms: ', hard_negatives_xyxy.shape[0])
        # hard_negatives_xyxy = nms(hard_negatives_xyxy, 0.01)
        # print('num hard negative detections after nms: ', hard_negatives_xyxy.shape[0])
        # wrong_patches = np.delete(hard_negatives_xyxy, 4, 1)

        return wrong_patches
Esempio n. 6
0
	def face_detection(self, img, name, scale_step = 20):
		
		# this training accuracy should be the same as your training process,
		##################################################################################
		#train_predicts = []
		#for idx in range(self.data.shape[0]):
		#	train_predicts.append(self.sc_function(self.data[idx, ...]))
		#print('Check training accuracy is: ', np.mean(np.sign(train_predicts) == self.labels))
		##################################################################################
		print(scale_step)
		scales = 1 / np.linspace(1, 8, scale_step)
		if os.path.exists('patches_%s.npy' %name):
			patches = np.load('patches_%s.npy' %name)
			patch_xyxy = np.load('patch_position_%s.npy' %name)
			print('Patches loaded')
		else:
			patches, patch_xyxy = image2patches(scales, img)
			print('Face Detection in Progress ..., total %d patches' % patches.shape[0])
			np.save('patches_%s.npy' %name, patches)
			np.save('patch_position_%s.npy' %name, patch_xyxy)
			print('Patches saved')

		if os.path.exists('patches_score%s.pkl' %name):
			print('[Find cached Patches Scores, patches_score%s.pkl loading...]' % name)
			with open('patches_score%s.pkl' %name, 'rb') as f:
				predicts = pickle.load(f)
			print('Patches Scores loaded')
		else:
			predicts = [self.sc_function(patch) for patch in tqdm(patches)]
			pickle.dump(predicts, open('patches_score%s.pkl' %name, 'wb'))
			print('Patches Scores saved')

		print(np.mean(np.array(predicts) > 0), np.sum(np.array(predicts) > 0))
		pos_predicts_xyxy = np.array([np.hstack((patch_xyxy[idx], np.array(score))) for idx, score in enumerate(predicts) if score > 0])
		if pos_predicts_xyxy.shape[0] == 0:
			return
		xyxy_after_nms = nms(pos_predicts_xyxy, 0.01)
		
		print('after nms:', xyxy_after_nms.shape[0],xyxy_after_nms.shape[1])
		for idx in range(xyxy_after_nms.shape[0]):
			pred = xyxy_after_nms[idx, :]
			cv2.rectangle(img, (int(pred[0]), int(pred[1])), (int(pred[2]), int(pred[3])), (0, 255, 0), 2) #gree rectangular with line width 3

		return img
    def face_detection(self, img, scale_step=20):
        train_preds = []
        for i in range(self.data.shape[0]):
            train_preds.append(self.sc_function(self.data[i, ...]))
        tr_acc = np.mean(np.sign(train_preds) == self.labels)
        print('Check on training accuracy is: ', tr_acc)

        pp = 'pos_preds_xyxy_face_1_with_negatives.pkl'  # CHANGE FILE
        if os.path.exists(pp):
            pos_preds_xyxy = get_data(pp)
        else:
            scales = 1 / np.linspace(1, 8, scale_step)
            patches, patches_xyxy = image2patches(scales, img)
            print('Face Detection in Progress..., total %d patches' %
                  patches.shape[0])

            preds = [self.sc_function(patch) for patch in tqdm(patches)]
            pos_preds = np.array(preds) > 0
            print('positive detections mean and sum: ', np.mean(pos_preds),
                  np.sum(pos_preds))

            pos_preds_xyxy = np.array([
                patches_xyxy[i] + [score] for i, score in enumerate(preds)
                if score > 0
            ])
            save_data(
                pos_preds_xyxy,
                'pos_preds_xyxy_face_1_with_negatives.pkl')  # CHANGE FILE
        if pos_preds_xyxy.shape[0] == 0:
            return

        print('num positive detections before nms: ', pos_preds_xyxy.shape[0])
        pos_preds_xyxy = nms(pos_preds_xyxy, 0.01)
        print('num positive detections after nms: ', pos_preds_xyxy.shape[0])

        for i in range(pos_preds_xyxy.shape[0]):
            pred = pos_preds_xyxy[i, :]
            xy1 = (int(pred[0]), int(pred[1]))
            xy2 = (int(pred[2]), int(pred[3]))
            cv2.rectangle(img, xy1, xy2, (0, 255, 0), 2)

        return img