def run(args):
	global averages, running
	averages, running = True, []
	start_time = time.clock()
	video = cv2.VideoCapture()
	q = []

	detector = dlib.get_frontal_face_detector()
	front = frontalization.Front(args)

	classifiers = []
	classifiers.append(Classifier(args, start_time, args.classes, args.resource_dir, (150,150,3), colour=True, local=False))
	classifiers.append(Classifier(args, start_time, args.classes, args.resource_dir, (150, 150), colour=False, local=False))
	classifiers.append(Classifier(args, start_time, args.classes, args.resource_dir, (150,150,3), colour=True, local=False))
	classifiers.append(Classifier(args, start_time, args.classes, args.resource_dir, (150,150,3), colour=True, local=True))
	classifiers.append(Classifier(args, start_time, args.classes, args.resource_dir, (150, 150), colour=False, local=False))
	classifiers.append(Classifier(args, start_time, args.classes, args.resource_dir, (150, 150), colour=False, local=True))

	if video.grab():
		while running:
			_, frame = video.read()
			image = cv2.resize(frame, (150, 150, frame.shape[2]))
			detection = detector(image, 1)
			if detection:
				rgb_image = frame[detection.top():detection.bottom(), detection.left():detection.right()]
				lbp_image = feature.local_binary_pattern(cv2.cvtColor(rgb_image, cv2.COLOR_BGR2GRAY).astype(np.float64), 8, 1, 'uniform')
				frgb_image = front.frontalized(image)
				flbp_image = feature.local_binary_pattern(cv2.cvtColor(frgb_image, cv2.COLOR_BGR2GRAY).astype(np.float64), 8, 1, 'uniform')

				classifications = []
				classifiers[0].classify(rgb_image, args.log + 'rgb')
				classifiers[1].classify(lbp_image, (args.log + 'lbp'))
				classifiers[2].classify(frgb_image, (args.log + 'frgb'))
				classifiers[3].classify(frgb_image, (args.log + 'lfrgb'))
				classifiers[4].classify(flbp_image, (args.log + 'flbp'))
				classifiers[5].classify(flbp_image, (args.log + 'lflbp'))

				result = np.zeros(args.classes)
				for classification in classifications:
					for i in range(len(classification)):
						result[i] += classification[i] / 6

				temp = []
				if len(q) < 10:
					q.insert(0, result)
				else:
					q.pop()
					q.insert(0, result)
				for i in range(len(q[0])):
					average = 0
					for j in range(len(q)):
						average += q[j][i] / 10
					temp.append(average)
				averages = temp
				main.log(args, averages)
			else:
				main.log(args, 'No Face Found', True)
			if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
				raw_input()
				break
def run(args):
    detector = dlib.get_frontal_face_detector()
    front = frontalization.Front(args)
    global predictions, running, arguments
    predictions, running = [], True
    start_time = time.clock()
    q = []

    video = cv2.VideoCapture()

    classifier = Classifier(args, start_time, 3, args.resource_dir,
                            [88, 88, 3], args.scope, '/' + args.model + '/')
    classifier.load_model()

    if video.grab():
        while running:
            _, image = video.read()
            detection = detector(image, 1)
            if detection:
                rgb_image = image[detection.top():detection.bottom(),
                                  detection.left():detection.right()]
                rgb_image = cv2.resize(rgb_image, (88, 88))
                front_image = front.frontalized(rgb_image)
                prediction = classifier.classify(front_image)
                predictions.append(prediction)
                main.log(
                    args, '\n{:.5f}s'.format(time.clock() - start_time) +
                    ' Prediction: ' + str(prediction))
                main.log(
                    args, '\n{:.5f}s'.format(time.clock() - start_time) +
                    ' Averages: ' + str(predictions))
def run(args):
	detector = dlib.get_frontal_face_detector()
	front = frontalization.Front(args)
	global predictions, running, arguments
	predictions, running = [], True
	start_time = time.clock()
	q = []

	videoDevice = ALProxy('ALVideoDevice', args.ip, args.port)
	captureDevice = videoDevice.subscribeCamera('Camera', 0, 2, 13, 10)
	image = np.zeros((480, 640, 3), np.uint8)

	classifier = Classifier(args, start_time, 3, args.resource_dir, [88, 88, 3], args.scope, '/'+args.model+'/')
	classifier.load_model()

	while running:
		image = stream2cv(image, videoDevice.getImageRemote(captureDevice))
		if not image:
			detection = detector(image, 1)
			if detection:
				rgb_image = image[detection.top():detection.bottom(), detection.left():detection.right()]
				rgb_image = cv2.resize(rgb_image, (88, 88))
				front_image = front.frontalized(rgb_image)
				prediction = classifier.classify(front_image)
				predictions.append(prediction)
				main.log(args, '\n{:.5f}s'.format(time.clock() - start_time) + ' Prediction: ' + str(prediction))
				main.log(args, '\n{:.5f}s'.format(time.clock() - start_time) + ' Averages: ' + str(predictions))
def save_image(args, start_time, save, data, type):
    detector, count = dlib.get_frontal_face_detector(), 0
    front = frontalization.Front(args)
    for image_file in data:
        image = cv2.imread(image_file[0], cv2.IMREAD_COLOR)
        detections = detector(image, 1)
        for _, detection in enumerate(detections):
            #try:
            face = image[detection.top():detection.bottom(),
                         detection.left():detection.right()]
            if face.shape[0] == 0:
                continue
            face = cv2.resize(face, (88, 88))
            images = []
            images.append(face)
            images.append(hue(face, 5))
            images.append(hue(face, -5))
            images.append(noisy('sp', images[0]))
            images.append(noisy('gauss', images[0]))
            images.append(hue(noisy('sp', images[0]), 5))
            images.append(hue(noisy('sp', images[0]), -5))
            images.append(hue(noisy('gauss', images[0]), 5))
            images.append(hue(noisy('gauss', images[0]), -5))
            for _image in images:
                cv2.imwrite(
                    save + '/rgb/' + str(image_file[1]) + '/' + str(count) +
                    '.jpg', _image)
                #lbp_image = feature.local_binary_pattern(cv2.cvtColor(_image, cv2.COLOR_BGR2GRAY).astype(np.float64), 8, 1, 'uniform').astype(np.uint8)
                #lbp_image *= (255 / lbp_image.max())
                #cv2.imwrite(save + '/lbp/' + str(image_file[1]) + '/' + str(count + 1) + '.jpg', lbp_image)
                frgb_image = front.frontalized(image)
                if frgb_image:
                    cv2.imwrite(
                        save + '/frgb/' + str(image_file[1]) + '/' +
                        str(count + 2) + '.jpg',
                        cv2.resize(frgb_image, (88, 88)))
                #flbp_image = feature.local_binary_pattern(cv2.cvtColor(frgb_image, cv2.COLOR_BGR2GRAY).astype(np.float64), 8, 1, 'uniform').astype(np.uint8)
                #flbp_image *= (255 / flbp_image.max())
                #cv2.imwrite(save + '/flbp/' + str(image_file[1]) + '/' + str(count + 3) + '.jpg', flbp_image)
                count += 4
                if count % 100 == 0:
                    main.log(
                        args, '{:.5f}'.format(time.clock() - start_time) +
                        's ' + str(count) + ' ' + type + ' images extracted')
            #except:
            #pass
    main.log(args,
             str(time.clock() - start_time) + ' ' + type + ' Images Extracted')
Example #5
0
def classify(args):
    start_time = time.clock()
    detector = dlib.get_frontal_face_detector()
    front = frontalization.Front(args)
    image = cv2.resize(cv2.imread(args.image, cv2.IMREAD_COLOR), (150, 150))
    detection = detector(image, 1)
    for _, detection in enumerate(detection):
        rgb_image = image[detection.top():detection.bottom(),
                          detection.left():detection.right()]
        lbp_image = feature.local_binary_pattern(
            cv2.cvtColor(rgb_image, cv2.COLOR_BGR2GRAY).astype(np.float64), 8,
            1, 'uniform').astype(np.uint8)
        frgb_image = front.frontalized(image)
        flbp_image = feature.local_binary_pattern(
            cv2.cvtColor(frgb_image, cv2.COLOR_BGR2GRAY).astype(np.float64), 8,
            1, 'uniform')

        main.log(
            args,
            str(time.clock() - start_time) +
            ' Image Representations Extracted')

        classifications = []

        rgb_classifier = Classifier(args,
                                    start_time,
                                    args.classes,
                                    args.resource_dir,
                                    rgb_image.shape,
                                    'rgb',
                                    local=False)
        classifications.append(
            rgb_classifier.classify(rgb_image, (args.model_name + '/rgb/')))

        lbp_classifier = Classifier(args,
                                    start_time,
                                    args.classes,
                                    args.resource_dir,
                                    lbp_image.shape,
                                    'lbp',
                                    local=False)
        classifications.append(
            lbp_classifier.classify(lbp_image, (args.model_name + '/lbp/')))

        frgb_classifier = Classifier(args,
                                     start_time,
                                     args.classes,
                                     args.resource_dir,
                                     frgb_image.shape,
                                     'frgb',
                                     local=False)
        classifications.append(
            frgb_classifier.classify(frgb_image, (args.model_name + '/frgb/')))

        lfrgb_classifier = Classifier(args,
                                      start_time,
                                      args.classes,
                                      args.resource_dir,
                                      frgb_image.shape,
                                      'lfrgb',
                                      local=True)
        classifications.append(
            lfrgb_classifier.classify(frgb_image,
                                      (args.model_name + '/lfrgb/')))

        flbp_classifier = Classifier(args,
                                     start_time,
                                     args.classes,
                                     args.resource_dir,
                                     flbp_image.shape,
                                     'flbp',
                                     local=False)
        classifications.append(
            flbp_classifier.classify(flbp_image, (args.model_name + '/flbp/')))

        lflbp_classifier = Classifier(args,
                                      start_time,
                                      args.classes,
                                      args.resource_dir,
                                      flbp_image.shape,
                                      'lflbp',
                                      local=True)
        classifications.append(
            lflbp_classifier.classify(flbp_image,
                                      (args.model_name + '/lflbp/')))

        result = np.zeros(args.classes)
        for classification in classifications:
            for i in range(len(classification)):
                result[i] += classification[i] / 6

        main.log(
            args, '\n' + str(time.clock() - start_time) + ' ' + args.image +
            ' classified as ' + str(result), True)
        return True
    main.log(
        args, '\n' + str(time.clock() - start_time) +
        ' Could not detect face in inputted image', True)