Example #1
0
def create_manual_data():
    
    FRGraph = FaceRecGraph();
    MTCNNGraph = FaceRecGraph();
    aligner = AlignCustom();
    extract_feature = FaceFeature(FRGraph)
    face_detect = MTCNNDetect(MTCNNGraph, scale_factor=2); #scale_factor, rescales image for faster detection
    vs = cv2.VideoCapture(0); #get input from webcam
#     print("Please input new user ID:")
    new_name = request.form['input_name']; #ez python input()
    f = open('./facerec_128D.txt','r');
    data_set = json.loads(f.read());
    person_imgs = {"Left" : [], "Right": [], "Center": []};
    person_features = {"Left" : [], "Right": [], "Center": []};
    print("Please start turning slowly. Press 'q' to save and add this new user to the dataset");
    while True:
        _, frame = vs.read();
        rects, landmarks = face_detect.detect_face(frame, 80);  # min face size is set to 80x80
        for (i, rect) in enumerate(rects):
            aligned_frame, pos = aligner.align(160,frame,landmarks[:,i]);
            if len(aligned_frame) == 160 and len(aligned_frame[0]) == 160:
                person_imgs[pos].append(aligned_frame)
                cv2.imshow("Captured face", aligned_frame)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break

    for pos in person_imgs: #there r some exceptions here, but I'll just leave it as this to keep it simple
        person_features[pos] = [np.mean(extract_feature.get_features(person_imgs[pos]),axis=0).tolist()]
    data_set[new_name] = person_features;
    f = open('./facerec_128D.txt', 'w');
    f.write(json.dumps(data_set))

    return render_template('home.html')
Example #2
0
 def __init__(self):
     super(Window, self).__init__()
     self.detector = cv2.CascadeClassifier(
         "haarcascade_frontalface_default.xml")
     self.FRGraph = FaceRecGraph()
     self.MTCNNGraph = FaceRecGraph()
     self.aligner = AlignCustom()
     self.extract_feature = FaceFeature(self.FRGraph)
     self.face_detect = MTCNNDetect(self.MTCNNGraph, scale_factor=2)
     #scale_factor, rescales image for faster detection
     #        self.face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
     self.InitWindow()
    def __init__(self):
        # Using OpenCV to capture from device 0. If you have trouble capturing
        # from a webcam, comment the line below out and use a video file
        # instead.
        FRGraph = FaceRecGraph();
        self.aligner = AlignCustom();
        self.extract_feature = FaceFeature(FRGraph)
        self.face_detect = MTCNNDetect(FRGraph, scale_factor=2); #scale_factor, rescales image for faster detection

        self.person_imgs = {"Left" : [], "Right": [], "Center": []};
        self.person_features = {"Left" : [], "Right": [], "Center": []};
        self.video = cv2.VideoCapture(2)
Example #4
0
def startRec():
    FrameCounter = 0
    #parser = argparse.ArgumentParser()
    #parser.add_argument("--mode", type=str, help="Run camera recognition", default="camera")
    #args = parser.parse_args(sys.argv[1:]);
    FRGraph = FaceRecGraph()
    MTCNNGraph = FaceRecGraph()
    aligner = AlignCustom()
    extract_feature = FaceFeature(FRGraph)
    face_detect = MTCNNDetect(MTCNNGraph, scale_factor=1)
    #scale_factor, rescales image for faster detection
    #mode = args.mode
    return (FrameCounter, aligner, extract_feature, face_detect)
Example #5
0
def addNewPerson(capture, newLabel):
    tensorGraph = tf.Graph()
    aligner = AlignCustom()
    allFeatures = FaceFeature(tensorGraph)
    face_detect = MTCNNDetect(tensorGraph, scale_factor=2)
    f = open('./facerec_128D.txt', 'r')
    data_set = json.loads(f.read())
    person_imgs = {
        "Left": [],
        "Right": [],
        "Center": []
    }
    person_features = {
        "Left": [],
        "Right": [],
        "Center": []
    }
    print(
        "Please start turning slowly. Press 'q' to save and add this new user to the dataset"
    )
    while True:
        _, frame = capture.read()
        rects, landmarks = face_detect.detect_face(frame, 80)
        # min face size is set to 80x80
        for (i, rect) in enumerate(rects):
            aligned_frame, pos = aligner.align(160, frame, landmarks[i])
            if len(aligned_frame) == 160 and len(aligned_frame[0]) == 160:
                person_imgs[pos].append(aligned_frame)
                cv2.imshow("Captured face", aligned_frame)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break

    cv2.destroyAllWindows()

    print("Capturing new face has ended")
    for pos in person_imgs:
        person_features[pos] = [
            np.mean(allFeatures.get_features(person_imgs[pos]),
                    axis=0).tolist()
        ]
    data_set[newLabel] = person_features
    f = open('./facerec_128D.txt', 'w')
    f.write(json.dumps(data_set))
    print("new face saved")
Example #6
0
    def __init__(self, parent=None):
        super(Second, self).__init__(parent)
        loadUi('addImage.ui', self)

        self.face_detector = cv2.CascadeClassifier(
            'haarcascade_frontalface_default.xml')
        self.FRGraph = FaceRecGraph()
        self.MTCNNGraph = FaceRecGraph()
        self.aligner = AlignCustom()
        self.extract_feature = FaceFeature(self.FRGraph)
        self.face_detect = MTCNNDetect(self.MTCNNGraph, scale_factor=2)
        self.person_imgs = {
            "Left": [],
            "Right": [],
            "Center": []
        }
        self.person_features = {
            "Left": [],
            "Right": [],
            "Center": []
        }
        self.init_ui()
        self.count = 0
Example #7
0
def camera_recog():
    FRGraph = FaceRecGraph();
    MTCNNGraph = FaceRecGraph();
    aligner = AlignCustom();
    extract_feature = FaceFeature(FRGraph)
    face_detect = MTCNNDetect(MTCNNGraph, scale_factor=2); #scale_factor, rescales image for faster detection
    print("[INFO] camera sensor warming up...")
    vs = cv2.VideoCapture(0); #get input from webcam
    detect_time = time.time()
    while True:
        _,frame = vs.read();
        #u can certainly add a roi here but for the sake of a demo i'll just leave it as simple as this
        rects, landmarks = face_detect.detect_face(frame,80);#min face size is set to 80x80
        aligns = []
        positions = []

        for (i, rect) in enumerate(rects):
            aligned_face, face_pos = aligner.align(160,frame,landmarks[:,i])
            if len(aligned_face) == 160 and len(aligned_face[0]) == 160:
                aligns.append(aligned_face)
                positions.append(face_pos)
            else: 
                print("Align face failed") #log        
        if(len(aligns) > 0):
            features_arr = extract_feature.get_features(aligns)
            recog_data = findPeople(features_arr,positions)
            for (i,rect) in enumerate(rects):
                cv2.rectangle(frame,(rect[0],rect[1]),(rect[2],rect[3]),(255,0,0)) #draw bounding box for the face
                cv2.putText(frame,recog_data[i][0]+" - "+str(recog_data[i][1])+"%",(rect[0],rect[1]),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),1,cv2.LINE_AA)


        cv2.imshow("Frame",frame)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break
            
    return render_template('home.html')
Example #8
0
def identifyPeople(capture):
    tensorGraph = tf.Graph()
    aligner = AlignCustom()
    allFeatures = FaceFeature(tensorGraph)
    face_detect = MTCNNDetect(tensorGraph, scale_factor=2)
    print("[INFO] camera sensor warming up...")
    while True:
        _, frame = capture.read()
        rects, landmarks = face_detect.detect_face(frame, 80)
        # min face size is set to 80x80
        aligns = []
        positions = []
        for (i, rect) in enumerate(rects):
            aligned_face, face_pos = aligner.align(160, frame, landmarks[i])
            if len(aligned_face) == 160 and len(aligned_face[0]) == 160:
                aligns.append(aligned_face)
                positions.append(face_pos)
            else:
                print("Align face failed")
        if (len(aligns) > 0):
            features_arr = allFeatures.get_features(aligns)
            recog_data = getKnownPeople(features_arr, positions)
            for (i, rect) in enumerate(rects):
                cv2.rectangle(frame, (rect[0], rect[1]),
                              (rect[0] + rect[2], rect[1] + rect[3]),
                              (255, 0, 0))  # draw bounding box for the face
                cv2.putText(
                    frame,
                    recog_data[i][0] + " - " + str(recog_data[i][1]) + "%",
                    (rect[0], rect[1]), cv2.FONT_HERSHEY_SIMPLEX, 1,
                    (255, 255, 255), 1, cv2.LINE_AA)

        cv2.imshow("Frame", frame)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break
    cv2.destroyAllWindows()
Example #9
0
    def addface(self):
        parser = argparse.ArgumentParser()
        parser.add_argument("--mode",
                            type=str,
                            help="Run camera recognition",
                            default="camera")
        args = parser.parse_args(sys.argv[1:])
        FRGraph = FaceRecGraph()
        aligner = AlignCustom()
        extract_feature = FaceFeature(FRGraph)
        face_detect = MTCNNDetect(FRGraph, scale_factor=2)
        # scale_factor, rescales image for faster detection

        #这个print需要界面化
        print("Please input new user ID:")
        #new_name = raw_input("input ID:");  # ez python input()
        new_name = unicode(self.lineEdit.text().toUtf8(), 'utf8', 'ignore')

        print("ce shi dai ma")
        f = open('./facerec_128D.txt', 'r')
        data_set = json.loads(f.read())
        person_imgs = {
            "Left": [],
            "Right": [],
            "Center": []
        }
        person_features = {
            "Left": [],
            "Right": [],
            "Center": []
        }
        print(
            "Please start turning slowly. Press 'q' to save and add this new user to the dataset"
        )

        vs = cv2.VideoCapture(
            "rtsp://*****:*****@192.168.2.131/cam/realmonitor?channel=1&subtype=0"
        )

        while True:
            _, frame = vs.read()
            rects, landmarks = face_detect.detect_face(frame, 20)
            # min face size is set to 80x80
            for (i, rect) in enumerate(rects):
                aligned_frame, pos = aligner.align(182, frame, landmarks[i])
                person_imgs[pos].append(aligned_frame)
                cv2.imshow("Captured face", aligned_frame)

            cv2.cvtColor(frame, cv2.COLOR_BGR2RGB, frame)
            img = QtGui.QImage(frame.data, frame.shape[1], frame.shape[0],
                               QtGui.QImage.Format_RGB888)
            pixmap = QtGui.QPixmap(img)
            self.label.setPixmap(pixmap)

            key = cv2.waitKey(1) & 0xFF
            #if key == ord("q"):
            #   break
            if self.video_flag == False:
                break

        vs.release()
        cv2.destroyAllWindows()

        for pos in person_imgs:  # there r some exceptions here, but I'll just leave it as this to keep it simple
            print("ceshi")
            print(person_imgs[pos])
            person_features[pos] = [
                np.mean(extract_feature.get_features(person_imgs[pos]),
                        axis=0).tolist()
            ]
        data_set[new_name] = person_features

        print("done done done done")
        f = open('./facerec_128D.txt', 'w')
        f.write(json.dumps(data_set))
        exit(0)
def camera_recog(frame):
	global frameList
	global FrameCounter
	FrameCounter=FrameCounter+1
	global conn1
	global add1
	detect_time = time.time()

	rects, landmarks = face_detect.detect_face(frame,80);#min face size is set to 80x80
	aligns = []
	positions = []
	try:
		for (i) in range(len(rects)):
			rect= rects[i]
			#print('niche vala')
			aligned_face, face_pos = aligner.align(160,frame,landmarks[:,i])
			if len(aligned_face) == 160 and len(aligned_face[0]) == 160:
				aligns.append(aligned_face)
				positions.append(face_pos)
			else: 
				print("Align face failed") #log        
		if(len(aligns) > 0):
			features_arr = extract_feature.get_features(aligns)
			recog_data = findPeople(features_arr,positions)
			for (i) in range(len(rects)):
				rect= rects[i]
				cv2.rectangle(frame,(rect[0],rect[1]),(rect[2],rect[3]),(255,0,0)) #draw bounding box for the face
				cv2.putText(frame,recog_data[i][0]+" - "+str(recog_data[i][1])+"%",(rect[0],rect[1]),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),1,cv2.LINE_AA)
		#cv2.imshow("Frame",frame)
		time.sleep(0.01)
		frameList.append(frame) 
		if(len(frameList)>999:
			del frameList
			print("deleted")
			initiliaze()
		
	except Exception as e:
		print(e)

def recPiFrame():
	global frameList
	global server_socket
	frameList=[]	
	conn1,add1= server_socket.accept()
	print("conection has been established with pi| ip "+add1[0]+" port "+str(add1[1]))

	data = b""
	payload_size = struct.calcsize(">L")
	print("payload_size: {}".format(payload_size))
	mode = args.mode
	while True:
		time.sleep(0.01)
		while len(data) < payload_size:
			data += conn1.recv(4096)
		packed_msg_size = data[:payload_size]
		data = data[payload_size:]
		msg_size = struct.unpack(">L", packed_msg_size)[0]
		while len(data) < msg_size:
			data += conn1.recv(4096)
		frame_data = data[:msg_size]
		data = data[msg_size:]
		frame=pickle.loads(frame_data, fix_imports=True, encoding="bytes")
		frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
		if(mode == "camera"):
			camera_recog(frame)
			time.sleep(0.01)
			
		elif mode == "input":
			create_manual_data(frame);
		cv2.waitKey(1)


def sendVideoFrame():
	global conn2
	time.sleep(2)
	print("waiting for Android to connect")
	conn2,add2= server_socket.accept()
	print("conection has been established with android | ip "+add2[0]+" port "+str(add2[1]))
	time.sleep(0.020)
	
	img_counter = 0
	pr=conn2.recv(9)
	encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
	print('sending')
	cmdRecThread=Thread(target=RecCommand)
	cmdRecThread.start()

	while True:
		
		if FrameCounter > 3:
			frame =frameList[FrameCounter-2]
			cv2.imshow('fcontroller'+str(len(frame)),frame)
			result, frame = cv2.imencode('.jpg', frame, encode_param)
			#data = zlib.compress(pickle.dumps(frame, 0))
			data = pickle.dumps(frame, 0)
			size = len(data)

			#print("frames:","{}: {}".format(img_counter, size))
			conn2.send(frame)
			time.sleep(0.06)
			
			img_counter += 1
			if cv2.waitKey(1) & 0xFF == ord('q'):
				break				
	
def RecCommand():
	time.sleep(1)
	while True:	
		global conn2
		global add2
		pr=conn2.recv(10)
		time.sleep(0.020)
		print(pr)

if __name__ == '__main__':

	parser = argparse.ArgumentParser()
	parser.add_argument("--mode", type=str, help="Run camera recognition", default="camera")
	args = parser.parse_args(sys.argv[1:]);
	FRGraph = FaceRecGraph();
	MTCNNGraph = FaceRecGraph();
	aligner = AlignCustom();
	extract_feature = FaceFeature(FRGraph)
	face_detect = MTCNNDetect(MTCNNGraph, scale_factor=1); #scale_factor, rescales image for faster detection		
	mode = args.mode
	'''if(mode == "camera"):
		camera_recog()
	elif mode == "input":
		create_manual_data();
	'''
	createSocket()
	RecPiFrameThread=Thread(target=recPiFrame)
	FrameSenderThread=Thread(target=sendVideoFrame)
	RecPiFrameThread.start()
	FrameSenderThread.start()
	FrameSenderThread.join()
	RecPiFrameThread.join()
	
	
Example #11
0
from align_custom import AlignCustom
from face_feature import FaceFeature
from mtcnn_detect import MTCNNDetect
import argparse
import sys, time
import json
import numpy as np
from PIL import Image
import StringIO
import threading
import codecs


aligner = AlignCustom();
extract_feature = FaceFeature()
face_detect = MTCNNDetect(scale_factor=2); #scale_factor, rescales image for faster detection
feature_data_set = {}
person_images = {}



def training_start(name):
    global person_images,feature_data_set
    if (name in feature_data_set or name in person_images):
        return False

    person_images[name] = {"Left" : [], "Right": [], "Center": []}
    return True

def training_proframe(name, frame):
    global person_images,feature_data_set
            cv2.destroyAllWindows()
            break
    for pos in image_matrix:
        #this line is for multiple channel image :- to convert 128D vector
        feature_128D[pos] = [
            np.mean(extract_feature.get_features(image_matrix[pos]),
                    axis=0).tolist()
        ]
    dataset[name_id] = feature_128D
    file = open(r'.\face_feature_dataset_128D.txt', 'w')
    file.write(json.dumps(dataset))


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--mode",
                        type=str,
                        help="Run camera for face recognition",
                        default='camera')
    args = parser.parse_args(sys.argv[1:])
    default_graph = FaceRecGraph(
    )  #it take info of data flow in tensorflow's layer
    aligner = AlignCustom()
    extract_feature = FaceFeature(default_graph)
    face_detect = MTCNNDetect(
        default_graph,
        scale_factor=2)  #rescale image for fast detection of image
    main(args)

#-- the end--#
Example #13
0
            person_imgs[pos].append(aligned_frame)
            cv2.imshow("Captured face", aligned_frame)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break

    for pos in person_imgs:  #there r some exceptions here, but I'll just leave it as this to keep it simple
        person_features[pos] = [
            np.mean(extract_feature.get_features(person_imgs[pos]),
                    axis=0).tolist()
        ]
    data_set[new_name] = person_features
    f = open('./facerec_128D.txt', 'w')
    f.write(json.dumps(data_set))


save_part = False
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--mode",
                        type=str,
                        help="Run camera recognition",
                        default="camera")
    args = parser.parse_args(sys.argv[1:])
    FRGraph = FaceRecGraph(save_part=save_part)
    aligner = AlignCustom()
    extract_feature = FaceFeature(FRGraph, save_part=save_part)
    face_detect = MTCNNDetect(FRGraph, scale_factor=2, save_part=save_part)
    #scale_factor, rescales image for faster detection
    main(args)
Example #14
0
    PERCENT_THRES = read_cfg(CFG_PATH, 'percent_thres')
    ERROR_PATH = read_cfg(CFG_PATH, 'error_path')

    # print(model_dir, db_path)
    temp = input('请选择操作:')
    if temp == '':
        print('不能选择空,请重新选择操作!\n')
    else:
        pick = int(temp)
        if pick == 1:
            if isload:
                isload = False
                face_rec_graph = FaceRecognizeGraph()
                aligner = AlignCustom()
                extract_feature = FaceFeature(face_rec_graph, MODEL_DIR)
                face_detect = MTCNNDetect(face_rec_graph, 2, MODEL_DIR)
                print('MTCNNDetect:' + str(face_detect))
            create_cam_data(DB_PATH, MIN_CREATE_SIZE)
        elif pick == 2:
            if isload:
                isload = False
                face_rec_graph = FaceRecognizeGraph()
                aligner = AlignCustom()
                extract_feature = FaceFeature(face_rec_graph, MODEL_DIR)
                face_detect = MTCNNDetect(face_rec_graph, 2, MODEL_DIR)
                print('MTCNNDetect:' + str(face_detect))
            create_lcl_data(DB_PATH, FACES_DIR, MIN_CREATE_SIZE)
        elif pick == 3:
            if isload:
                isload = False
                face_rec_graph = FaceRecognizeGraph()
Example #15
0
 def __init__(self,FRGraph,aligner,extract_feature):
     self.FRGraph = FRGraph
     self.aligner = aligner
     self.extract_feature = extract_feature
     self.face_detect = MTCNNDetect(FRGraph, scale_factor=2)
Example #16
0
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break

    for pos in person_imgs:  #yeni kişi bir diziye atılır ,atılmadan önce bazı işlemler yapılır
        person_features[pos] = [
            np.mean(extract_feature.get_features(person_imgs[pos]),
                    axis=0).tolist()
        ]
    data_set[new_name] = person_features
    f = open('./facerec_128D.txt', 'w')
    f.write(
        json.dumps(data_set)
    )  #diziye atılan kişi daha sonra yüzme bulma işleminde kullanılmak üzere dosyaya yazdırılır


if __name__ == '__main__':  #main fonksiyon
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--mode", type=str, help="Run camera recognition", default="camera"
    )  #program çalışırken mode okundu ona göre işlem yapılıcak
    args = parser.parse_args(sys.argv[1:])
    FRGraph = FaceRecGraph()
    #tüm .py'ler tanımlandı
    MTCNNGraph = FaceRecGraph()
    aligner = AlignCustom()
    extract_feature = FaceFeature(FRGraph)
    face_detect = MTCNNDetect(MTCNNGraph, scale_factor=2)
    #scale_factor daha hızlı algılama için görüntüyü yeniden ölçeklendirir
    main(args)
Example #17
0
    def findpeopleface(self):

        self.video_flag = True

        parser = argparse.ArgumentParser()
        parser.add_argument("--mode",
                            type=str,
                            help="Run camera recognition",
                            default="camera")
        args = parser.parse_args(sys.argv[1:])
        FRGraph = FaceRecGraph()
        aligner = AlignCustom()
        extract_feature = FaceFeature(FRGraph)
        face_detect = MTCNNDetect(FRGraph, scale_factor=2)
        # scale_factor, rescales image for faster detection
        print("[INFO] camera sensor warming up...")

        vs = cv2.VideoCapture(
            'rtsp://*****:*****@192.168.2.131/cam/realmonitor?channel=1&subtype=0'
        )
        # get input from webcam
        #vs = cv2.VideoCapture('test.mp4')

        c = 0
        _, frame = vs.read()

        message = ''

        result_dict = {}

        while True:
            timeF = frame_interval

            if (c % timeF == 0):
                # u can certainly add a roi here but for the sake of a demo i'll just leave it as simple as this
                rects, landmarks = face_detect.detect_face(frame, 20)
                # min face size is set to 80x80
                aligns = []

                positions = []
                for (i, rect) in enumerate(rects):
                    aligned_face, face_pos = aligner.align(
                        182, frame, landmarks[i])
                    aligns.append(aligned_face)
                    positions.append(face_pos)
                features_arr = extract_feature.get_features(aligns)

                recog_data = findPeople(features_arr, positions)

                for (i, rect) in enumerate(rects):
                    cv2.rectangle(frame, (rect[0], rect[1]),
                                  (rect[0] + rect[2], rect[1] + rect[3]),
                                  (0, 255, 0),
                                  2)  # draw bounding box for the face
                    cv2.putText(
                        frame,
                        recog_data[i][0] + " - " + str(recog_data[i][1]) + "%",
                        (rect[0], rect[1]), cv2.FONT_HERSHEY_SIMPLEX, 1,
                        (255, 255, 255), 1, cv2.CV_AA)

                    if result_dict.has_key(recog_data[i][0]):
                        result_dict[recog_data[i][0]][1] += 1
                        result_dict[recog_data[i][0]][0] = (
                            result_dict[recog_data[i][0]][0] *
                            (result_dict[recog_data[i][0]][1] - 1) +
                            float(recog_data[i][1])
                        ) / result_dict[recog_data[i][0]][1]
                    else:
                        result_dict[recog_data[i][0]] = [
                            float(recog_data[i][1]), 1
                        ]

                cv2.cvtColor(frame, cv2.COLOR_BGR2RGB, frame)
                img = QtGui.QImage(frame.data, frame.shape[1], frame.shape[0],
                                   QtGui.QImage.Format_RGB888)
                pixmap = QtGui.QPixmap(img)
                self.label.setPixmap(pixmap)

                #result_dict是用来保存名称和精确度的字典,将它的按值排序给result_list并定义一个字符串message来保存result_list的内容并显示message
                result_list = sorted(result_dict.items(),
                                     key=lambda item: item[1][1],
                                     reverse=True)
                message = ''
                for i in result_list:
                    message += i[0]
                    message += ': \n'
                    message += str(i[1][0])[0:10]
                    message += '%\n'
                    message += str(i[1][1])[0:7]
                    message += ' times\n\n'
                self.plainTextEdit.setPlainText(message)

                key = cv2.waitKey(1) & 0xFF
                if self.video_flag == False:
                    break

            _, frame = vs.read()
            c += 1

        vs.release()
        cv2.destroyAllWindows()
Example #18
0
        # min face size is set to 80x80
        for (i, rect) in enumerate(rects):
            aligned_frame, pos = aligner.align(160, frame, landmarks[i])
            person_imgs[pos].append(aligned_frame)
            cv2.imshow("Captured face", aligned_frame)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break

    for pos in person_imgs:  #there r some exceptions here, but I'll just leave it as this to keep it simple
        person_features[pos] = [
            np.mean(extract_feature.get_features(person_imgs[pos]),
                    axis=0).tolist()
        ]
    data_set[new_name] = person_features
    f = open('./facerec_128D.txt', 'w')
    f.write(json.dumps(data_set))


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--mode",
                        type=str,
                        help="Run camera recognition",
                        default="camera")
    args = parser.parse_args(sys.argv[1:])
    FRSession = FaceRecSession()
    aligner = AlignCustom()
    extract_feature = FaceFeature(FRSession)
    face_detect = MTCNNDetect(FRSession, scale_factor=2)
    main(args)
import cv2
from align_custom import AlignCustom
from face_feature import FaceFeature
from mtcnn_detect import MTCNNDetect
import argparse
import sys, time
import json
import numpy as np
from PIL import Image
import StringIO

#FRGraph = FaceRecGraph();
aligner = AlignCustom()
extract_feature = FaceFeature()
face_detect = MTCNNDetect(scale_factor=3)
#scale_factor, rescales image for faster detection
feature_data_set = None
'''
Description:
Images from Video Capture -> detect faces' regions -> crop those faces and align them 
    -> each cropped face is categorized in 3 types: Center, Left, Right 
    -> Extract 128D vectors( face features)
    -> Search for matching subjects in the dataset based on the types of face positions. 
    -> The preexisitng face 128D vector with the shortest distance to the 128D vector of the face on screen is most likely a match
    (Distance threshold is 0.6, percentage threshold is 70%)
    
'''


def recog_process_frame(frame):
Example #20
0
        if key == ord("q"):
            break

    for pos in person_imgs:  #there r some exceptions here, but I'll just leave it as this to keep it simple
        person_features[pos] = [
            np.mean(extract_feature.get_features(person_imgs[pos]),
                    axis=0).tolist()
        ]
    data_set[new_name] = person_features
    f = open('./facerec_128D.txt', 'w')
    f.write(json.dumps(data_set))


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--mode",
                        type=str,
                        help="Run camera recognition",
                        default="camera")
    parser.add_argument("--type", type=str, help="camera type", default=0)
    parser.add_argument("--rec", type=bool, help="record", default=False)
    args = parser.parse_args(sys.argv[1:])
    FRGraph = FaceRecGraph()
    MTCNNGraph = FaceRecGraph()
    aligner = AlignCustom()
    extract_feature = FaceFeature(FRGraph)
    face_detect = MTCNNDetect(
        MTCNNGraph,
        scale_factor=2)  #scale_factor, rescales image for faster detection
    main(args)
Example #21
0
class MainClass:
    global conn1
    global add1
    global frameList
    global FrameCounter
    global server_socket
    global conn2
    global add2

    def connectPi():
        global conn1
        global add1

        conn1, add1 = server_socket.accept()
        time.sleep(0.050)
        print("conection has been established | ip " + add1[0] + " port " +
              str(add1[1]))
        return conn1

    def connectController():
        global conn2
        global add2
        global FrameCounter
        FrameCounter = 0

        conn2, add2 = server_socket.accept()
        time.sleep(0.050)
        print("conection has been established | ip " + add2[0] + " port " +
              str(add2[1]))
        return conn2

    def create_manual_data(frame):
        print("Please input new user ID:")
        new_name = input()
        #ez python input()
        f = open('./facerec_128D.txt', 'r')
        data_set = json.loads(f.read())
        person_imgs = {
            "Left": [],
            "Right": [],
            "Center": []
        }
        person_features = {
            "Left": [],
            "Right": [],
            "Center": []
        }
        print(
            "Please start turning slowly. Press 'q' to save and add this new user to the dataset"
        )

        data = b""
        payload_size = struct.calcsize(">L")
        while True:
            time.sleep(0.050)
            while len(data) < payload_size:
                data += conn.recv(4096)
            packed_msg_size = data[:payload_size]
            data = data[payload_size:]
            msg_size = struct.unpack(">L", packed_msg_size)[0]
            while len(data) < msg_size:
                data += conn.recv(4096)
            frame_data = data[:msg_size]
            data = data[msg_size:]
            frame = pickle.loads(frame_data,
                                 fix_imports=True,
                                 encoding="bytes")
            frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
            rects, landmarks = face_detect.detect_face(frame, 80)
            # min face size is set to 80x80
            for (i, rect) in enumerate(rects):
                aligned_frame, pos = aligner.align(160, frame, landmarks[:, i])
                if len(aligned_frame) == 160 and len(aligned_frame[0]) == 160:
                    person_imgs[pos].append(aligned_frame)
                    cv2.imshow("Captured face", aligned_frame)
            key = cv2.waitKey(1) & 0xFF
            if key == ord("q"):
                break

        for pos in person_imgs:  #there r some exceptions here, but I'll just leave it as this to keep it simple
            person_features[pos] = [
                np.mean(extract_feature.get_features(person_imgs[pos]),
                        axis=0).tolist()
            ]
        data_set[new_name] = person_features
        f = open('./facerec_128D.txt', 'w')
        f.write(json.dumps(data_set))

    def findPeople(features_arr, positions, thres=0.6, percent_thres=70):
        '''
        :param features_arr: a list of 128d Features of all faces on screen
        :param positions: a list of face position types of all faces on screen
        :param thres: distance threshold
        :return: person name and percentage
        '''
        f = open('./facerec_128D.txt', 'r')
        data_set = json.loads(f.read())
        returnRes = []
        for (i, features_128D) in enumerate(features_arr):
            result = "Unknown"
            smallest = sys.maxsize
            for person in data_set.keys():
                person_data = data_set[person][positions[i]]
                for data in person_data:
                    distance = np.sqrt(np.sum(np.square(data - features_128D)))
                    if (distance < smallest):
                        smallest = distance
                        result = person
            percentage = min(100, 100 * thres / smallest)
            if percentage <= percent_thres:
                result = "Unknown"
            returnRes.append((result, percentage))
        return returnRes

    def camera_recog(frame):
        global frameList
        global FrameCounter
        global conn1
        global add1

        detect_time = time.time()
        rects, landmarks = face_detect.detect_face(frame, 80)
        #min face size is set to 80x80
        aligns = []
        positions = []

        for (i, rect) in enumerate(rects):
            aligned_face, face_pos = aligner.align(160, frame, landmarks[:, i])
            if len(aligned_face) == 160 and len(aligned_face[0]) == 160:
                aligns.append(aligned_face)
                positions.append(face_pos)
            else:
                print("Align face failed")  #log
        if (len(aligns) > 0):
            features_arr = extract_feature.get_features(aligns)
            recog_data = findPeople(features_arr, positions)
            for (i, rect) in enumerate(rects):
                cv2.rectangle(frame, (rect[0], rect[1]), (rect[2], rect[3]),
                              (255, 0, 0))  #draw bounding box for the face
                cv2.putText(
                    frame,
                    recog_data[i][0] + " - " + str(recog_data[i][1]) + "%",
                    (rect[0], rect[1]), cv2.FONT_HERSHEY_SIMPLEX, 1,
                    (255, 255, 255), 1, cv2.LINE_AA)
        cv2.imshow("Frame", frame)
        frameList.append(frame)
        FrameCounter = FrameCounter + 1
        if FrameCounter > 1020:
            frameList.clear()
            FrameCounter = 0

    def recPiFrame():
        global conn1
        global add1
        global frame
        global frameList
        global FrameCounter
        FrameCounter = 0

        conn1, add1 = server_socket.accept()
        time.sleep(0.050)
        print("connection has been established | ip " + add1[0] + " port " +
              str(add1[1]))

        data = b""
        payload_size = struct.calcsize(">L")
        print("payload_size: {}".format(payload_size))
        #mode = args.mode
        while True:
            time.sleep(0.050)
            while len(data) < payload_size:
                data += conn1.recv(4096)
            packed_msg_size = data[:payload_size]
            data = data[payload_size:]
            msg_size = struct.unpack(">L", packed_msg_size)[0]
            while len(data) < msg_size:
                data += conn1.recv(4096)
            frame_data = data[:msg_size]
            data = data[msg_size:]
            frame = pickle.loads(frame_data,
                                 fix_imports=True,
                                 encoding="bytes")
            frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)

            camera_recog(frame)

            cv2.waitKey(1)

    def sendVideoFrame():
        global conn2
        global add2
        global frameList
        global FrameCounter

        conn2 = connectController()
        img_counter = 0
        pr = conn2.recv(1024)
        encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
        print('sending')

        while True:
            time.sleep(0.050)
            if FrameCounter > 3:
                frame = frameList[FrameCounter - 2]
                #cv2.imshow('fcontroller',frame)
                result, frame = cv2.imencode('.jpg', frame, encode_param)
                #data = zlib.compress(pickle.dumps(frame, 0))
                data = pickle.dumps(frame, 0)
                size = len(data)

                print("frames:", "{}: {}".format(img_counter, size))
                conn2.send(frame)

                img_counter += 1
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
        cam.release()

    def RecCommand():

        while True:
            global conn2
            global add2
            pr = conn2.recv(1024)
            time.sleep(0.050)
            print(pr)

    if __name__ == '__main__':
        global FrameCounter
        global server_socket
        parser = argparse.ArgumentParser()
        parser.add_argument("--mode",
                            type=str,
                            help="Run camera recognition",
                            default="camera")
        args = parser.parse_args(sys.argv[1:])
        FRGraph = FaceRecGraph()
        MTCNNGraph = FaceRecGraph()
        aligner = AlignCustom()
        extract_feature = FaceFeature(FRGraph)
        face_detect = MTCNNDetect(MTCNNGraph, scale_factor=1)
        #scale_factor, rescales image for faster detection

        server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        server_socket.bind(('', 9000))
        print('Socket bind complete')
        server_socket.listen(10)
        print('Socket now listening')

        RecPiFrameThread = Thread(target=recPiFrame)
        RecPiFrameThread.start()

        FrameCounter = 0
        FrameSenderThread = Thread(target=sendVideoFrame)
        cmdRecThread = Thread(target=RecCommand)

        if FrameCounter > 3:
            FrameSenderThread.start()
            cmdRecThread.start()
Example #22
0
            if len(aligned_frame) == 160 and len(aligned_frame[0]) == 160:
                person_imgs[pos].append(aligned_frame)
                cv2.imshow("Captured face", aligned_frame)
        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            break

    for pos in person_imgs:  #there r some exceptions here, but I'll just leave it as this to keep it simple
        person_features[pos] = [
            np.mean(extract_feature.get_features(person_imgs[pos]),
                    axis=0).tolist()
        ]
    data_set[new_name] = person_features
    f = open('./facerec_128D.txt', 'w')
    f.write(json.dumps(data_set))


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--mode",
                        type=str,
                        help="Run camera recognition",
                        default="camera")
    args = parser.parse_args(sys.argv[1:])
    FRGraph = FaceRecGraph()
    aligner = AlignCustom()
    extract_feature = FaceFeature(FRGraph)
    face_detect = MTCNNDetect(FRGraph, scale_factor=1)
    #scale_factor, rescales image for faster detection
    main(args)
Example #23
0
import cv2
import mtcnn_detect
from mtcnn_detect import MTCNNDetect

face_detect = MTCNNDetect()
#scale_factor, rescales image for faster detection

rects, landmarks = face_detect.detect_face(frame, 20)
# min face size is set to 20x20

cv2.rectangle(frame, (rect[0], rect[1]),
              (rect[0] + rect[2], rect[1] + rect[3]),
              (255, 0, 0))  #draw bounding box for the face
for j in range(0, 5):
    cv2.circle(frame, (int(landmarks[i][j]), int(landmarks[i][j + 5])), 2,
               (0, 0, 255))
    cv2.namedWindow("Image")
    cv2.imshow("Image", frame)
    cv2.waitKey(1)