Exemplo n.º 1
0
def click2():
    faceRec = FaceRecognition(8)
    # trainmodelpath=modelpath.get()
    pickle_in = open("savedmodel.pickle", "rb")
    model = pickle.load(pickle_in)
    cap = cv2.VideoCapture("D:\\TestProject\\jitesh.mp4")
    while cap.isOpened():
        font = cv2.FONT_HERSHEY_SIMPLEX
        org = (50, 50)
        fontScale = 1
        color = (255, 0, 0)
        thickness = 2
        ret, img = cap.read()
        if ret:
            imgFaces = faceRec.find_face(img)
            if imgFaces.all() == 0:
                print('No Face Found!')
                image = cv2.putText(img, "None", org, font, fontScale, color,
                                    thickness, cv2.LINE_AA)
                cv2.imshow('frame', image)
                cv2.waitKey(1)
                continue
            prediction = faceRec.predictImg(imgFaces, model)

            image = cv2.putText(img, str(prediction), org, font, fontScale,
                                color, thickness, cv2.LINE_AA)
            cv2.imshow('frame', image)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    close_window()
Exemplo n.º 2
0
 def test_exception_raise(self):
     face_recognition_instance = FaceRecognition()
     with self.assertRaises(Exception) as assertRaises:
         face_recognition_instance.show_faces_in_image()
     self.assertEqual(
         str(assertRaises.exception),
         'The current object does not contain an image location.')
Exemplo n.º 3
0
def click():
    trainPath = train.get()
    testPath = test.get()
    print(trainPath, testPath)
    faceRec = FaceRecognition(8)
    model = faceRec.trainRecognizer(trainPath=trainPath,
                                    printAcc=printAcc,
                                    testPath=testPath)
    pickel_out = open("savedmodel.pickle", "wb")
    pickle.dump(model, pickel_out)
    pickel_out.close()
    # cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
    # while True:
    #     ret, img = cap.read()
    #     imgFaces = faceRec.find_face(img)
    #     if imgFaces.any()==None:
    #         break
    #     prediction=faceRec.predictImg(imgFaces, model)
    #     font = cv2.FONT_HERSHEY_SIMPLEX
    #     org = (50, 50)
    #     fontScale = 1
    #     color = (255, 0, 0)
    #     thickness = 2
    #     image = cv2.putText(img, str(prediction), org, font,
    #                         fontScale, color, thickness, cv2.LINE_AA)
    #     cv2.imshow('frame', image)
    #     if cv2.waitKey(1) & 0xFF == ord('q'):
    #         break
    close_window()
Exemplo n.º 4
0
def recognize():
    encoded_img = request.form.get('encodedImg')
    path = None

    face = FaceRecognition()
    image = face.facerecognition(encoded_img)
    if image == 0:
        return redirect(url_for('recognized_image'))

    return render_template('recognize.html', path=path)
Exemplo n.º 5
0
 def __init__(self, args):
     self.id_to_feat = OrderedDict()
     self.tr_pos=0
     self.tr_neg=0
     self.f_pos=0
     self.f_neg=0
     self.threshold = args.threshold
     self.wrong_list = []
     self.step_save_accuracy = args.step_accuracy
     self.accuracy = OrderedDict()
     self.features_model = FaceRecognition(args)
     self.wrong_matches = []
     self.wrong_not_matches = []
     self.save_one_in = args.save_one_in
Exemplo n.º 6
0
def click2():
    faceRec = FaceRecognition(8)
    # trainmodelpath=modelpath.get()
    pickle_in = open("savedmodel.pickle", "rb")
    model = pickle.load(pickle_in)
    cap = cv2.VideoCapture(0)

    while True:
        font = cv2.FONT_HERSHEY_SIMPLEX
        org = (50, 50)
        fontScale = 1
        color = (255, 0, 0)
        thickness = 2
        ret, img = cap.read()
        if ret:

            #dict length = num faces

            faceMemo = faceRec.find_face(img)
            faceCount = len(faceMemo.keys())
            #if imgFaces.all()==0:
            if faceCount == 0:
                print('No Face Found!')
                image = cv2.putText(img, "None", org, font, fontScale, color,
                                    thickness, cv2.LINE_AA)
                cv2.imshow('frame', image)
                cv2.waitKey(1)
                continue

            #iterate over length dict
            predictions = []
            for faceIndex in range(faceCount):
                imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                key = "Face" + str(faceIndex)
                x1, y1, w1, h1 = faceMemo[key]
                imageFace = imgGray[y1:y1 + h1, x1:x1 + w1]
                imageFace = cv2.resize(imageFace, (360, 480))
                predictions.append(faceRec.predictImg(imageFace, model))

                orgTemp = (x1, y1)
                image = cv2.putText(img, str(predictions[-1]), orgTemp, font,
                                    fontScale, color, thickness, cv2.LINE_AA)

            cv2.imshow('frame', image)

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

    close_window()
Exemplo n.º 7
0
    def clustering(self, face_input):
        for i in self.files:
            img_rgb = Fr.bgr2rgb(cv2.imread(str(self.img_path) + str(i)))
            _, shapes, _ = Fr.find_faces(img_rgb)
            n, descriptors = Fr.encode_faces(img_rgb, shapes)
            self.faces += descriptors
            self.faces_count.append(n)
        faces = np.array(self.faces)

        # clustering
        # clt = DBSCAN(eps=0.5, metric="euclidean")
        # clt.fit(faces)

        self.kmeans = KMeans(n_clusters=face_input, random_state=0).fit(faces)

        print("kmeans labels =", self.kmeans.labels_)
        print("face_counts =", self.faces_count)
Exemplo n.º 8
0
from FaceDetection import FaceDetection
from FaceRecognition import FaceRecognition
from Session import Session
from DialogService import DialogService

if __name__ == '__main__':
    # Create services instances
    faceDetection = FaceDetection()
    faceRecognition = FaceRecognition()

    # Train face recognizer
    faceRecognition.train()

    # Create session with timeout
    session = Session(5)

    def do(face):
        if session.is_expired():
            user = faceRecognition.recognize(face)
            session.authorize(user)

            # TODO run in other thread
            DialogService(session).start_new_dialog()
        else:
            session.renew()


    faceDetection.run_with_callback(do)
Exemplo n.º 9
0
class Worker:
    def __init__(self, args):
        self.id_to_feat = OrderedDict()
        self.tr_pos=0
        self.tr_neg=0
        self.f_pos=0
        self.f_neg=0
        self.threshold = args.threshold
        self.wrong_list = []
        self.step_save_accuracy = args.step_accuracy
        self.accuracy = OrderedDict()
        self.features_model = FaceRecognition(args)
        self.wrong_matches = []
        self.wrong_not_matches = []
        self.save_one_in = args.save_one_in

    def processing_dataset(self, name_all_images):
        idx_images = 1
        for name in name_all_images:
            label = name[1]
            img = cv2.imread(name[0])
            ### get features from image
            #print(img.shape)
            features = self.features_model.predict(img,1)
            if np.any(features):
                self._compare_feat(features, label, name)
                if idx_images%self.step_save_accuracy==0:
                    self._calculate_accuracy(idx_images)
                idx_images+=1
        self._calculate_accuracy(idx_images)
        #return self.accuracy, self.wrong_matches, self.wrong_not_matches

    def _compare_feat(self, features, label, name):
        id_to_min = OrderedDict({'id': None, 'min': 100000})
        for key, val in self.id_to_feat.items():
            #dist = distance.euclidean(val, features)
            dist = np.sqrt(np.sum(np.square(np.subtract(features,val[0])))) 
            if dist < id_to_min['min']:
                id_to_min['id'] = key
                id_to_min['min'] = dist
        for feat_wrong in self.wrong_list:
            dist = np.sqrt(np.sum(np.square(np.subtract(feat_wrong[0], features))))
            if dist < id_to_min['min']:
                id_to_min['id'] = None
                id_to_min['min'] = dist
        if id_to_min['min'] <= self.threshold:
            if label==id_to_min['id']:
                self.tr_pos+=1
            else:
                self.f_pos+=1
                if id_to_min['id']==None:
                    self.wrong_matches.append([name,None,id_to_min['min']])
                else:
                    self.wrong_matches.append([name,self.id_to_feat[id_to_min['id']][1],id_to_min['min']])
        else:
            if label not in self.id_to_feat:
                self.tr_neg+=1
                self.id_to_feat[label] = [features,name]
            else:
                self.f_neg+=1
                self.wrong_not_matches.append([name,self.id_to_feat[label][1],id_to_min['min']])
                self.wrong_list.append([features,name,id_to_min['min']])
    def _calculate_accuracy(self, idx_images):
        accuracy = (self.tr_neg+self.tr_pos)/(self.tr_pos+self.tr_neg+self.f_neg+self.f_pos)
        print('iteration - {}, accuracy -{}, fp - {}, fn - {}'.format(idx_images, accuracy, self.f_pos, self.f_neg))
        self.accuracy[idx_images] = accuracy
        if idx_images%self.save_one_in==0:
            with open('mxnet_1d_result.json', 'w') as fp:
                json.dump(self.accuracy, fp)
            np.save('wrong_matches.npy',self.wrong_matches)
            np.save('wrong_not_matches.npy',self.wrong_not_matches)
    def __init__(self):
        # GUI
        self.appName = 'Face Attendance'
        self.root = Tk()
        self.root.title(self.appName)
        self.root.geometry("1080x720")
        self.root.minsize(1080, 720)
        self.root.resizable(True, True)
        self.tabControl = ttk.Notebook(self.root)
        self.tabFaceRecog = ttk.Frame(self.tabControl)
        self.tabAddFace = ttk.Frame(self.tabControl)
        self.tabAttendance = ttk.Frame(self.tabControl)
        self.tabControl.add(self.tabFaceRecog, text='FaceRecog')
        self.tabControl.add(self.tabAddFace, text='AddFace')
        self.tabControl.add(self.tabAttendance, text='Attendance')
        self.tabControl.pack(expand=1, fill="both")
        self.tabControl.bind("<<NotebookTabChanged>>", self.on_tab_selected)
        self.root.protocol("WM_DELETE_WINDOW", self.on_closing)

        # Params
        self.is_record = False
        self.record_count = 0
        self.record_total = 10
        self.image_buffer = []
        self.data_folder = 'images/data/train'
        self.person_name = ''
        self.modified_folder = []
        self.name_cache = []

        self.is_running_fr = False
        self.stop_update_frame_facerecog = False
        self.stop_update_frame_addface = False
        self.name_last_frame = []
        self.last_boxes = []
        self.last_texts = []
        self.last_colors = []
        self.skip_list = []

        # Video Capture
        self.vid = MyVideoCapture()
        # Init face classifier
        self.fc = FaceClassifier()
        self.fc.load_pretrained_data('data.pt')
        # Init face recognition engine
        self.fr = FaceRecognition(self.vid.cap)
        for key, value in self.fr.class_names.items():
            self.name_cache.append(value)
        self.name_counter = [0] * len(self.fr.class_names)
        self.name_time_pause = [datetime.min] * len(self.fr.class_names)
        self.green_box_counter = [0] * len(self.fr.class_names)

        # Tab FaceRecog
        self.top_frame_facerecog = Frame(self.tabFaceRecog)
        self.bottom_frame_facerecog = Frame(self.tabFaceRecog)
        self.top_frame_facerecog.grid(row=0, column=0, sticky=W + E)
        self.bottom_frame_facerecog.grid(row=1, column=0, sticky=N + W + E + S)
        self.label = Label(self.top_frame_facerecog,
                           text=self.appName,
                           font=15,
                           bg='blue',
                           fg='white')
        self.label.grid(row=0, column=0, sticky=W)
        self.frame_canvas = Frame(self.bottom_frame_facerecog, bg='red')
        self.frame_canvas.grid(row=0, column=0, sticky=N + W + E + S)
        self.canvas_facerecog = Canvas(self.frame_canvas, bg='black')
        self.canvas_facerecog.pack(fill=BOTH, expand=YES)
        self.frame_notify = LabelFrame(self.bottom_frame_facerecog,
                                       text='Notify',
                                       bg='green')
        self.frame_notify.grid(row=0, column=1, sticky=N + W + E + S)
        self.scroll_notify = Scrollbar(self.frame_notify, orient=VERTICAL)
        self.scroll_notify.pack(side=RIGHT, fill=Y)
        self.txt_notify = Text(self.frame_notify,
                               bg='black',
                               fg='white',
                               width=20,
                               yscroll=self.scroll_notify.set)
        self.txt_notify.pack(fill=Y, expand=YES)
        self.scroll_notify.config(command=self.txt_notify.yview)

        self.bottom_frame_facerecog.rowconfigure(0, weight=1)
        self.bottom_frame_facerecog.columnconfigure(0, weight=1)

        # self.btnFaceRecog = Button(self.tabFaceRecog, text='Start', command=self.btn_FaceRecog_pressed)
        # self.btnFaceRecog.pack_forget()
        self.btnRefreshFR = Button(self.top_frame_facerecog,
                                   text='Refresh',
                                   command=self.btnRefreshFRPressed)
        self.btnRefreshFR.grid(row=0, column=1, sticky=W, padx=10)
        self.lblMinFace = Label(self.top_frame_facerecog,
                                text='Minimum face',
                                font=15)
        self.lblMinFace.grid(row=0, column=2, sticky=E)
        self.entrMinFace = Entry(self.top_frame_facerecog, width=10)
        self.entrMinFace.grid(row=0, column=3, sticky=E)
        self.entrMinFace.insert(END, str(self.fr.min_face))
        self.lblMinFace = Label(self.top_frame_facerecog,
                                text='Accuracy threshold',
                                font=15)
        self.lblMinFace.grid(row=0, column=4, sticky=E)
        self.entrAccuracyTH = Entry(self.top_frame_facerecog, width=10)
        self.entrAccuracyTH.grid(row=0, column=5, sticky=E)
        self.entrAccuracyTH.insert(END, str(self.fr.accuracy_th))

        self.top_frame_facerecog.rowconfigure(0, weight=1)
        self.top_frame_facerecog.columnconfigure(1, weight=1)

        self.tabFaceRecog.rowconfigure(1, weight=1)
        self.tabFaceRecog.columnconfigure(0, weight=1)

        # Tab AddFace
        self.left_frame_addface = LabelFrame(self.tabAddFace, text='Cam')
        self.right_frame_addface = LabelFrame(self.tabAddFace,
                                              text='Button',
                                              width=150)
        self.bottom_frame_addface = LabelFrame(self.tabAddFace, text='Console')
        self.left_frame_addface.grid(row=0, column=0, sticky=N + W + E + S)
        self.right_frame_addface.grid(row=0, column=1, sticky=N + W + E + S)
        self.bottom_frame_addface.grid(row=1,
                                       column=0,
                                       columnspan=2,
                                       sticky=W + E)
        self.tabAddFace.rowconfigure(0, weight=1)
        self.tabAddFace.columnconfigure(0, weight=1)
        self.canvas_addface = Canvas(self.left_frame_addface,
                                     width=300,
                                     height=300,
                                     bg='black')
        self.canvas_addface.pack(fill=BOTH, expand=YES)

        self.lblRecord = Label(self.right_frame_addface, text='Num of image')
        self.lblRecord.grid(row=0, column=0, sticky=E)
        self.entrRecord = Entry(self.right_frame_addface, width=4)
        self.entrRecord.insert(END, str(self.record_total))
        self.entrRecord.grid(row=0, column=1, sticky=N, padx=10, pady=10)
        self.btnRecord = Button(self.right_frame_addface,
                                text='Record',
                                command=self.btnRecordPressed)
        self.btnRecord.grid(row=1,
                            column=0,
                            sticky=N,
                            padx=10,
                            pady=10,
                            columnspan=2)
        self.btnTrain = Button(self.right_frame_addface,
                               text='Train',
                               command=self.btnTrainPressed)
        self.btnTrain.grid(row=2,
                           column=0,
                           sticky=S,
                           padx=10,
                           pady=10,
                           columnspan=2)
        self.btnReTrainAll = Button(self.right_frame_addface,
                                    text='Retrain all',
                                    command=self.btnReTrainAllPressed)
        self.btnReTrainAll.grid(row=3,
                                column=0,
                                sticky=S,
                                padx=10,
                                pady=10,
                                columnspan=2)

        self.scroll_console = Scrollbar(self.bottom_frame_addface,
                                        orient=VERTICAL)
        self.scroll_console.pack(side=RIGHT, fill=Y)
        self.txt_console = Text(self.bottom_frame_addface,
                                bg='black',
                                fg='white',
                                height=7,
                                yscroll=self.scroll_console.set)
        self.txt_console.pack(fill=BOTH, expand=YES)
        self.scroll_console.config(command=self.txt_console.yview)

        # Tab Attendance
        self.top_frame_attend = LabelFrame(self.tabAttendance)
        self.bottom_frame_attend = LabelFrame(self.tabAttendance, text='Table')
        self.top_frame_attend.grid(row=0, column=0, sticky=W + E)
        self.bottom_frame_attend.grid(row=1, column=0, sticky=N + W + E + S)
        self.btnShowAttend = Button(self.top_frame_attend,
                                    text='Show',
                                    command=self.btnShowAttendPressed)
        self.btnShowAttend.grid(row=0, column=0, sticky=W, padx=2)
        self.btnCleanAttend = Button(self.top_frame_attend,
                                     text='Clean',
                                     command=self.btnCleanAttendPressed)
        self.btnCleanAttend.grid(row=0, column=1, sticky=W)
        self.scroll_table = Scrollbar(self.bottom_frame_attend,
                                      orient=VERTICAL)
        self.scroll_table.pack(side=RIGHT, fill=Y)
        self.tblAttend = ttk.Treeview(self.bottom_frame_attend,
                                      yscroll=self.scroll_table.set)
        self.tblAttend['columns'] = ('ID', 'Name', 'Date', 'Time')
        self.tblAttend['show'] = 'headings'
        self.tblAttend.heading("#1", text="ID")
        self.tblAttend.heading("#2", text="Name")
        self.tblAttend.heading("#3", text="Date")
        self.tblAttend.heading("#4", text="Time")
        self.tblAttend.pack(fill=BOTH, expand=YES)
        self.scroll_table.config(command=self.tblAttend.yview)

        self.tabAttendance.rowconfigure(1, weight=1)
        self.tabAttendance.columnconfigure(0, weight=1)
class FaceAttendanceApp:
    def __init__(self):
        # GUI
        self.appName = 'Face Attendance'
        self.root = Tk()
        self.root.title(self.appName)
        self.root.geometry("1080x720")
        self.root.minsize(1080, 720)
        self.root.resizable(True, True)
        self.tabControl = ttk.Notebook(self.root)
        self.tabFaceRecog = ttk.Frame(self.tabControl)
        self.tabAddFace = ttk.Frame(self.tabControl)
        self.tabAttendance = ttk.Frame(self.tabControl)
        self.tabControl.add(self.tabFaceRecog, text='FaceRecog')
        self.tabControl.add(self.tabAddFace, text='AddFace')
        self.tabControl.add(self.tabAttendance, text='Attendance')
        self.tabControl.pack(expand=1, fill="both")
        self.tabControl.bind("<<NotebookTabChanged>>", self.on_tab_selected)
        self.root.protocol("WM_DELETE_WINDOW", self.on_closing)

        # Params
        self.is_record = False
        self.record_count = 0
        self.record_total = 10
        self.image_buffer = []
        self.data_folder = 'images/data/train'
        self.person_name = ''
        self.modified_folder = []
        self.name_cache = []

        self.is_running_fr = False
        self.stop_update_frame_facerecog = False
        self.stop_update_frame_addface = False
        self.name_last_frame = []
        self.last_boxes = []
        self.last_texts = []
        self.last_colors = []
        self.skip_list = []

        # Video Capture
        self.vid = MyVideoCapture()
        # Init face classifier
        self.fc = FaceClassifier()
        self.fc.load_pretrained_data('data.pt')
        # Init face recognition engine
        self.fr = FaceRecognition(self.vid.cap)
        for key, value in self.fr.class_names.items():
            self.name_cache.append(value)
        self.name_counter = [0] * len(self.fr.class_names)
        self.name_time_pause = [datetime.min] * len(self.fr.class_names)
        self.green_box_counter = [0] * len(self.fr.class_names)

        # Tab FaceRecog
        self.top_frame_facerecog = Frame(self.tabFaceRecog)
        self.bottom_frame_facerecog = Frame(self.tabFaceRecog)
        self.top_frame_facerecog.grid(row=0, column=0, sticky=W + E)
        self.bottom_frame_facerecog.grid(row=1, column=0, sticky=N + W + E + S)
        self.label = Label(self.top_frame_facerecog,
                           text=self.appName,
                           font=15,
                           bg='blue',
                           fg='white')
        self.label.grid(row=0, column=0, sticky=W)
        self.frame_canvas = Frame(self.bottom_frame_facerecog, bg='red')
        self.frame_canvas.grid(row=0, column=0, sticky=N + W + E + S)
        self.canvas_facerecog = Canvas(self.frame_canvas, bg='black')
        self.canvas_facerecog.pack(fill=BOTH, expand=YES)
        self.frame_notify = LabelFrame(self.bottom_frame_facerecog,
                                       text='Notify',
                                       bg='green')
        self.frame_notify.grid(row=0, column=1, sticky=N + W + E + S)
        self.scroll_notify = Scrollbar(self.frame_notify, orient=VERTICAL)
        self.scroll_notify.pack(side=RIGHT, fill=Y)
        self.txt_notify = Text(self.frame_notify,
                               bg='black',
                               fg='white',
                               width=20,
                               yscroll=self.scroll_notify.set)
        self.txt_notify.pack(fill=Y, expand=YES)
        self.scroll_notify.config(command=self.txt_notify.yview)

        self.bottom_frame_facerecog.rowconfigure(0, weight=1)
        self.bottom_frame_facerecog.columnconfigure(0, weight=1)

        # self.btnFaceRecog = Button(self.tabFaceRecog, text='Start', command=self.btn_FaceRecog_pressed)
        # self.btnFaceRecog.pack_forget()
        self.btnRefreshFR = Button(self.top_frame_facerecog,
                                   text='Refresh',
                                   command=self.btnRefreshFRPressed)
        self.btnRefreshFR.grid(row=0, column=1, sticky=W, padx=10)
        self.lblMinFace = Label(self.top_frame_facerecog,
                                text='Minimum face',
                                font=15)
        self.lblMinFace.grid(row=0, column=2, sticky=E)
        self.entrMinFace = Entry(self.top_frame_facerecog, width=10)
        self.entrMinFace.grid(row=0, column=3, sticky=E)
        self.entrMinFace.insert(END, str(self.fr.min_face))
        self.lblMinFace = Label(self.top_frame_facerecog,
                                text='Accuracy threshold',
                                font=15)
        self.lblMinFace.grid(row=0, column=4, sticky=E)
        self.entrAccuracyTH = Entry(self.top_frame_facerecog, width=10)
        self.entrAccuracyTH.grid(row=0, column=5, sticky=E)
        self.entrAccuracyTH.insert(END, str(self.fr.accuracy_th))

        self.top_frame_facerecog.rowconfigure(0, weight=1)
        self.top_frame_facerecog.columnconfigure(1, weight=1)

        self.tabFaceRecog.rowconfigure(1, weight=1)
        self.tabFaceRecog.columnconfigure(0, weight=1)

        # Tab AddFace
        self.left_frame_addface = LabelFrame(self.tabAddFace, text='Cam')
        self.right_frame_addface = LabelFrame(self.tabAddFace,
                                              text='Button',
                                              width=150)
        self.bottom_frame_addface = LabelFrame(self.tabAddFace, text='Console')
        self.left_frame_addface.grid(row=0, column=0, sticky=N + W + E + S)
        self.right_frame_addface.grid(row=0, column=1, sticky=N + W + E + S)
        self.bottom_frame_addface.grid(row=1,
                                       column=0,
                                       columnspan=2,
                                       sticky=W + E)
        self.tabAddFace.rowconfigure(0, weight=1)
        self.tabAddFace.columnconfigure(0, weight=1)
        self.canvas_addface = Canvas(self.left_frame_addface,
                                     width=300,
                                     height=300,
                                     bg='black')
        self.canvas_addface.pack(fill=BOTH, expand=YES)

        self.lblRecord = Label(self.right_frame_addface, text='Num of image')
        self.lblRecord.grid(row=0, column=0, sticky=E)
        self.entrRecord = Entry(self.right_frame_addface, width=4)
        self.entrRecord.insert(END, str(self.record_total))
        self.entrRecord.grid(row=0, column=1, sticky=N, padx=10, pady=10)
        self.btnRecord = Button(self.right_frame_addface,
                                text='Record',
                                command=self.btnRecordPressed)
        self.btnRecord.grid(row=1,
                            column=0,
                            sticky=N,
                            padx=10,
                            pady=10,
                            columnspan=2)
        self.btnTrain = Button(self.right_frame_addface,
                               text='Train',
                               command=self.btnTrainPressed)
        self.btnTrain.grid(row=2,
                           column=0,
                           sticky=S,
                           padx=10,
                           pady=10,
                           columnspan=2)
        self.btnReTrainAll = Button(self.right_frame_addface,
                                    text='Retrain all',
                                    command=self.btnReTrainAllPressed)
        self.btnReTrainAll.grid(row=3,
                                column=0,
                                sticky=S,
                                padx=10,
                                pady=10,
                                columnspan=2)

        self.scroll_console = Scrollbar(self.bottom_frame_addface,
                                        orient=VERTICAL)
        self.scroll_console.pack(side=RIGHT, fill=Y)
        self.txt_console = Text(self.bottom_frame_addface,
                                bg='black',
                                fg='white',
                                height=7,
                                yscroll=self.scroll_console.set)
        self.txt_console.pack(fill=BOTH, expand=YES)
        self.scroll_console.config(command=self.txt_console.yview)

        # Tab Attendance
        self.top_frame_attend = LabelFrame(self.tabAttendance)
        self.bottom_frame_attend = LabelFrame(self.tabAttendance, text='Table')
        self.top_frame_attend.grid(row=0, column=0, sticky=W + E)
        self.bottom_frame_attend.grid(row=1, column=0, sticky=N + W + E + S)
        self.btnShowAttend = Button(self.top_frame_attend,
                                    text='Show',
                                    command=self.btnShowAttendPressed)
        self.btnShowAttend.grid(row=0, column=0, sticky=W, padx=2)
        self.btnCleanAttend = Button(self.top_frame_attend,
                                     text='Clean',
                                     command=self.btnCleanAttendPressed)
        self.btnCleanAttend.grid(row=0, column=1, sticky=W)
        self.scroll_table = Scrollbar(self.bottom_frame_attend,
                                      orient=VERTICAL)
        self.scroll_table.pack(side=RIGHT, fill=Y)
        self.tblAttend = ttk.Treeview(self.bottom_frame_attend,
                                      yscroll=self.scroll_table.set)
        self.tblAttend['columns'] = ('ID', 'Name', 'Date', 'Time')
        self.tblAttend['show'] = 'headings'
        self.tblAttend.heading("#1", text="ID")
        self.tblAttend.heading("#2", text="Name")
        self.tblAttend.heading("#3", text="Date")
        self.tblAttend.heading("#4", text="Time")
        self.tblAttend.pack(fill=BOTH, expand=YES)
        self.scroll_table.config(command=self.tblAttend.yview)

        self.tabAttendance.rowconfigure(1, weight=1)
        self.tabAttendance.columnconfigure(0, weight=1)

    # Popup
    def popup_input_frame(self):
        input_name = Toplevel()
        input_name.wm_title("Input name")
        lab = Label(input_name, text="Enter name", borderwidth=2)
        lab.grid(row=0, column=0, padx=10, pady=10)
        txtBox = Text(input_name, height=1, width=50, borderwidth=2)
        txtBox.grid(row=0, column=1, padx=10, pady=10, columnspan=4)
        btnOkay = ttk.Button(
            input_name,
            text="Okay",
            command=lambda: [
                self.get_person_name(txtBox.get("1.0", 'end-1c')),
                input_name.destroy(),
                self.save_image()
            ])
        btnOkay.grid(row=1, column=3, padx=10, pady=10, sticky=E)
        btnCancel = ttk.Button(input_name,
                               text="Cancel",
                               command=input_name.destroy)
        btnCancel.grid(row=1, column=4, padx=10, pady=10, sticky=E)
        w = self.root.winfo_width()
        h = self.root.winfo_height()
        wf = input_name.winfo_width()
        hf = input_name.winfo_height()
        x = (w - wf) / 2
        y = (h - hf) / 2
        input_name.geometry('+%d+%d' % (x - 25, y))
        self.root.wait_window(input_name)

    # Control
    def btnCleanAttendPressed(self):
        clean_list = []
        with engine.connect() as con:
            rs = con.execute('''SELECT DISTINCT name, date From Attendance''')
            for row in rs:
                clean_list.append(row)
        for item in clean_list:
            self.clean_up_attendant(item[0],
                                    datetime.strptime(item[1], '%Y-%m-%d'))
        self.btnShowAttendPressed()

    def btnShowAttendPressed(self):
        self.tblAttend.delete(*self.tblAttend.get_children())
        with engine.connect() as con:
            rs = con.execute('''SELECT * FROM Attendance''')
            for row in rs:
                self.tblAttend.insert("", END, values=list(row))

    def btnRefreshFRPressed(self):
        self.stop_face_recog()
        # self.stop_update_frame_facerecog = True
        self.fr.load_model()
        self.name_cache = []
        for key, value in self.fr.class_names.items():
            self.name_cache.append(value)
        minFace = accuracyTH = None
        try:
            minFace = int(self.entrMinFace.get())
        except ValueError:
            self.entrMinFace.delete(0, 'end')
        try:
            accuracyTH = float(self.entrAccuracyTH.get())
        except ValueError:
            self.entrAccuracyTH.delete(0, 'end')
        self.fr.set_params(minFace, accuracyTH)
        time.sleep(0.5)
        # self.stop_update_frame_facerecog = False
        # self.update_frame_facerecog()
        self.start_face_recog()

    def btnTrainPressed(self):
        thread = threading.Thread(target=self.thread_train,
                                  args=(),
                                  daemon=True)
        thread.start()

    def btnReTrainAllPressed(self):
        thread = threading.Thread(target=self.thread_train,
                                  args=(),
                                  kwargs={'retrain': True},
                                  daemon=True)
        thread.start()

    def btnRecordPressed(self):
        try:
            self.record_total = int(self.entrRecord.get())
        except ValueError:
            self.entrMinFace.delete(0, 'end')
            return
        if not self.is_record:
            self.disable_buttons()
            self.image_buffer.clear()
            # self.printProgressBar(0, self.record_total, prefix='Progress:', suffix='Complete', length=50)
            # time.sleep(0.1)
            self.is_record = True

    def resize_frame(self, event):
        pass

    # Create thread to train
    def thread_train(self, retrain=False):
        self.disable_buttons()
        self.fc.train_SVM(self.data_folder,
                          outtext=self.write,
                          retrain_all=retrain,
                          retrain_folder=self.modified_folder)
        self.enable_buttons()
        self.modified_folder.clear()

    def disable_buttons(self):
        self.tabControl.tab(0, state='disable')
        self.tabControl.tab(2, state='disable')
        self.btnTrain['state'] = 'disable'
        self.btnRecord['state'] = 'disable'
        self.btnReTrainAll['state'] = 'disable'

    def enable_buttons(self):
        self.btnTrain['state'] = 'normal'
        self.btnRecord['state'] = 'normal'
        self.btnReTrainAll['state'] = 'normal'
        self.tabControl.tab(0, state='normal')
        self.tabControl.tab(2, state='normal')

    # Update frame face recognition
    def update_frame_facerecog(self):
        if self.stop_update_frame_facerecog:
            return
        with self.fr.lock_cap:
            isTrue, frame = self.vid.get_frame()
        if isTrue:
            with self.fr.lock_boxes:
                if self.fr.new_boxes:
                    is_new_boxes = True
                    self.last_boxes = self.fr.box_draw[0]
                    self.last_texts = self.fr.text_draw[0]
                    self.last_colors = [(255, 255, 0)] * len(self.last_boxes)
                    self.fr.new_boxes = False
                else:
                    is_new_boxes = False

            if is_new_boxes:
                list_index = []
                self.skip_list.clear()
                for idx, text in enumerate(self.last_texts):
                    name = text.split(':')[0]
                    if name != 'Unknown':
                        try:
                            index = self.name_cache.index(name)
                        except ValueError:
                            continue
                        list_index.append(index)
                        if self.name_time_pause[index] == datetime.min:
                            if self.name_counter[index] == 0:
                                self.name_counter[index] += 1
                            else:
                                if index not in self.name_last_frame:
                                    self.name_counter[index] = 0
                                self.name_counter[index] += 1
                            if self.name_counter[index] >= 6:
                                self.write(
                                    '\n' + name + '\n' +
                                    datetime.now().strftime("%Y-%m-%d, %H:%M")
                                    + '\n' + '_' * 20,
                                    out='notify')
                                self.update_attendant(name, datetime.now())
                                self.last_colors[idx] = (0, 255, 0)
                                self.name_time_pause[index] = datetime.now(
                                ) + timedelta(seconds=6)
                                self.name_counter[index] = 0
                        else:
                            if self.name_time_pause[index] > datetime.now():
                                self.green_box_counter[index] += 1
                                if self.green_box_counter[index] < 3:
                                    self.last_colors[idx] = (0, 255, 0)
                                else:
                                    self.skip_list.append(idx)
                                continue
                            else:
                                self.green_box_counter[index] = 0
                                self.name_time_pause[index] = datetime.min
                self.name_last_frame = list_index

            self.fr.draw_frame(frame,
                               self.last_boxes,
                               self.last_texts,
                               color=self.last_colors,
                               thick=2,
                               text_scale=1,
                               skip_list=self.skip_list)  # RGB
            w = self.canvas_facerecog.winfo_width()
            h = self.canvas_facerecog.winfo_height()
            # self.photo = ImageTk.PhotoImage(image=ImagePIL.fromarray(frame).resize((w, h)))
            self.photo = ImageTk.PhotoImage(
                image=self.resize_with_pad(ImagePIL.fromarray(frame), (w, h)))
            self.canvas_facerecog.create_image(0,
                                               0,
                                               image=self.photo,
                                               anchor='nw')
        self.root.after(20, self.update_frame_facerecog)

    # Update frame at Add face
    def update_frame_addface(self):
        if self.stop_update_frame_addface:
            return
        isTrue, frame = self.vid.get_frame()
        if isTrue:
            if self.is_record:
                self.image_buffer.append(frame.copy())
                self.record_count += 1
                text = 'record {}/{}'.format(self.record_count,
                                             self.record_total)
                self.write(text)
                cv2.putText(frame, text, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1,
                            (255, 0, 255), 2)
                time.sleep(0.5)
            w = self.canvas_addface.winfo_width()
            h = self.canvas_addface.winfo_height()
            # self.photo = ImageTk.PhotoImage(image=ImagePIL.fromarray(frame).resize((w, h)))
            self.photo = ImageTk.PhotoImage(
                image=self.resize_with_pad(ImagePIL.fromarray(frame), (w, h)))
            self.canvas_addface.create_image(0,
                                             0,
                                             image=self.photo,
                                             anchor='nw')
            if self.record_count >= self.record_total:
                self.is_record = False
                self.record_count = 0
                self.popup_input_frame()
                self.enable_buttons()
        self.root.after(20, self.update_frame_addface)

    # Event when changing tab
    def on_tab_selected(self, event):
        selected_tab = event.widget.select()
        tab_text = event.widget.tab(selected_tab, "text")
        if tab_text == 'FaceRecog':
            self.stop_update_frame_addface = True
            self.stop_update_frame_facerecog = False
            self.update_frame_facerecog()
            self.start_face_recog()
        if tab_text == 'AddFace':
            self.stop_face_recog()
            self.stop_update_frame_facerecog = True
            self.stop_update_frame_addface = False
            self.update_frame_addface()
        if tab_text == 'Attendance':
            self.stop_face_recog()
            self.stop_update_frame_facerecog = True
            self.stop_update_frame_addface = True

    # def btn_FaceRecog_pressed(self):
    #     if self.is_running_fr:
    #         self.stop_face_recog()
    #         self.btnFaceRecog.configure(text='Start')
    #     else:
    #         self.start_face_recog()
    #         self.btnFaceRecog.configure(text='Stop')

    # Print to console text box
    def write(self, *message, end="\n", sep=" ", out='console'):
        text = ""
        for item in message:
            text += "{}".format(item)
            text += sep
        text += end
        if out == 'console':
            self.txt_console.insert(INSERT, text)
            self.txt_console.see("end")
        if out == 'notify':
            self.txt_notify.insert(INSERT, text)
            self.txt_notify.see("end")

    @staticmethod
    def printProgressBar(iteration,
                         total,
                         prefix='',
                         suffix='',
                         decimals=1,
                         length=100,
                         fill='█',
                         printEnd="\r"):
        percent = ("{0:." + str(decimals) + "f}").format(
            100 * (iteration / float(total)))
        filledLength = int(length * iteration // total)
        bar = fill * filledLength + '-' * (length - filledLength)
        print(f'\r{prefix} |{bar}| {percent}% {suffix}', end=printEnd)
        # Print New Line on Complete
        if iteration == total:
            print()

    # Save image to folder
    def save_image(self):
        if self.person_name == '':
            self.write('Person name required')
            return
        image_folder = os.path.join(self.data_folder, self.person_name)
        self.write('Saving image to {}'.format(image_folder))
        self.modified_folder.append(self.person_name)
        if not os.path.exists(image_folder):
            os.mkdir(image_folder)

        for i, image in enumerate(self.image_buffer):
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            cv2.imwrite(
                image_folder + '/{}_{}_{}.jpg'.format(
                    self.person_name,
                    datetime.now().strftime("%Y-%m-%d-%H-%M-%S"), i), image)

        thread = threading.Thread(target=self.create_images_with_masks,
                                  args=(image_folder, ),
                                  daemon=True)
        thread.start()

    # Create image with masks
    def create_images_with_masks(self, image_folder):
        filenames = glob.glob(image_folder + '/*.jpg')
        images = [cv2.imread(img) for img in filenames]
        masks = [
            cv2.imread('images/masks/fm1.png'),
            cv2.imread('images/masks/fm2.png')
        ]
        self.write('Generating face with mask')
        for idx, img in enumerate(images):
            out_path = '{}/face_with_mask_{}.png'.format(image_folder, idx)
            if os.path.exists(out_path):
                continue
            out_img = add_face_mask(img, masks[idx % len(masks)])
            cv2.imwrite(out_path, out_img)
            self.write('\t{}/{}'.format(idx + 1, len(images)))
        self.write('Done')

    def get_person_name(self, name):
        self.person_name = name

    # Stop face recognition engine
    def stop_face_recog(self):
        if self.is_running_fr:
            self.fr.stop_thread_face_recog()
            self.is_running_fr = False

    # Start face recognition engine
    def start_face_recog(self):
        if not self.is_running_fr:
            self.fr.stop_flag[0] = False
            thread = threading.Thread(target=self.fr.thread_face_recog,
                                      args=(),
                                      daemon=True)
            thread.start()
            self.is_running_fr = True

    # Resize keep aspect ratio
    @staticmethod
    def resize_with_pad(image, desired_size):
        old_size = image.size
        ratiow = float(desired_size[0]) / old_size[0]
        ratioh = float(desired_size[1]) / old_size[1]
        ratio = min(ratiow, ratioh)
        new_size = tuple([int(max(x * ratio, 1)) for x in old_size])
        image = image.resize(new_size, ImagePIL.ANTIALIAS)
        new_im = ImagePIL.new("RGB", desired_size)
        new_im.paste(image, ((desired_size[0] - new_size[0]) // 2,
                             (desired_size[1] - new_size[1]) // 2))
        return new_im

    # Update attendant to database
    @staticmethod
    def update_attendant(name, dt):
        record = Attendance(name=name, date=dt.date(), time=dt.time())
        session.add(record)
        session.commit()

    # Delete records except first and last
    @staticmethod
    def clean_up_attendant(name, date):
        date_str = date.strftime('%Y-%m-%d')
        with engine.connect() as con:
            con.execute('''
            DELETE FROM Attendance
             WHERE name = '{0}'
             AND (time !=
                 (
                 SELECT MAX(time)
                 FROM Attendance WHERE name = '{0}'
                 AND DATE = '{1}'
                 )
                 AND
                 TIME != (
                 SELECT MIN(TIME)
                 FROM Attendance WHERE name = '{0}'
                 AND DATE = '{1}'
                 )
             )
             AND DATE = '{1}'      
            '''.format(name, date_str))

    def run(self):
        self.root.mainloop()

    def on_closing(self):
        if messagebox.askokcancel("Quit", "Do you want to quit?"):
            self.root.destroy()
Exemplo n.º 12
0
from DB import DB
import Result

# main
DB = DB()
up_id = DB.select_upid_by_processing()

up_vid_path = DB.select_upvidpath(up_id)
VS = Vs(up_vid_path, up_id)
VS.slicing()

face_input = DB.select_facecount(up_id)
FC = Fc("./slicing/" + str(up_id) + "/")
FC.clustering(face_input)

FR = Fr()
up_img_path = DB.select_upimgpath(up_id)
up_img = cv2.imread(up_img_path)
rep_img_paths, label_indexs = FC.rep_img(face_input)
i = 0
result = []
for rep_img_path in rep_img_paths:
    label_index = label_indexs[i]
    rep_img = cv2.imread(rep_img_path)

    if FR.compare_faces(rep_img, up_img):
        result = FC.save_result(label_index, up_id)
        break
    i += 1

DB.insert_result(up_id, result)
Exemplo n.º 13
0
 def test_object_creation(self):
     self.assertIsNotNone(FaceRecognition())
Exemplo n.º 14
0
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 30 13:46:47 2019

@author: dell
"""

from FaceRecognition import FaceRecognition

FaceRecognition.help()

FR = FaceRecognition(
    r"E:\!Kunal\ML\FaceRecognition\28-04-2019evenlargermodel.h5")

#FR.display()

identity = "Kunal"

FR.RegisterId(identity)

FR.capture()
while True:
    if (FR.isBlur() == True):
        print(
            "Image not captured/not captured properly. Press Spacebar when window is open to capture image."
        )
        FR.capture()
    else:
        break

import time
Exemplo n.º 15
0
    def face_recognition_test(self):
        confFile = self.app.pargs.conf
        dumpFolder = self.app.pargs.output

        if confFile is None:
            print("No --conf specified")
            exit(1)

        faceRecognition = FaceRecognition()
        faceRecognition.setTempFolder(dumpFolder)
        faceRecognition.setFaceCascadeFile(
            "haarcascade_frontalface_default.xml")
        faceRecognition.setConfigFile(open(confFile))
        faceRecognition.doRecognitionRoutine()

        if dumpFolder is not None:
            faceRecognition.dumpDebugInfo()
Exemplo n.º 16
0
import cv2
from FaceRecognition import FaceRecognition
from blink2 import Blink

video_capture = cv2.VideoCapture(0)

#IMPORTING MODEL AND TRAINING
#----------------------------
face_req = FaceRecognition()
print("training start")
face_req.train()
print("training done")
#----------------------------

blink = Blink(0.3, 3)
blink_count = 0

process_this_frame = True

while True:
    ret, frame = video_capture.read()
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
    rgb_small_frame = small_frame[:, :, ::-1]

    #processing every other frame
    if process_this_frame:
        face_locations, face_names = face_req.recognise(rgb_small_frame)

    process_this_frame = not process_this_frame
    did_blink = blink.detect_blink(frame)
    if did_blink:
Exemplo n.º 17
0
TOURE Talla - [email protected]
Exmaples of code using the FaceRecognition library

main.py
Description : This library containts the Class "FaceRecognition" whiich includes methods to perform face detection and face recongnition using 
Convolutionnal Neural Network. 

"""

from FaceRecognition import FaceRecognition



if __name__ == '__main__':

face_recognition= FaceRecognition.FaceRecognition()

raw_imgpath = '/Users/tallatoure/Documents/SEE5_FaceReco/Data/Raw_img/'



rawimgTalla= raw_imgpath+'Talla_Toure/'



#face_recognition.DEBUG_printimg(DEBUGimg)

face_recognition.user_init(rawimgTalla, "Talla_Toure")


Exemplo n.º 18
0
class Worker:
    def __init__(self, args):
        self.id_to_feat = OrderedDict()
        self.tr_pos=0
        self.tr_neg=0
        self.f_pos=0
        self.f_neg=0
        self.threshold = args.threshold
        self.wrong_list = []
        self.step_save_accuracy = args.step_accuracy
        self.accuracy = OrderedDict()
        self.features_model = FaceRecognition(args)
        self.wrong_matches = []
        self.wrong_not_matches = []
        self.save_one_in = args.save_one_in
        print('Threshold - {}'.format(self.threshold))
        
    def processing_dataset(self, name_all_images):
        idx_images = 1
        for name in name_all_images:
            start = time.time()
            label = name[1]
            img = cv2.imread(name[0])
            #img = cv2.resize(img,(600,600))
            ### get features from image
            
            features,aligned = self.features_model.predict(img,1)
            if np.any(features):
                self._compare_feat(features, label, name)
                if idx_images%self.step_save_accuracy==0:
                    self._calculate_accuracy(idx_images)
                idx_images+=1
                #cv2.imwrite('Trash/{}'.format(name[0].split('/')[-1]),aligned[::,::,::-1])
            #if idx_images == 100:
             #   break
            #print('Processing one frame - {}'.format(time.time() - start))
        self._calculate_accuracy(idx_images)
        #return self.accuracy, self.wrong_matches, self.wrong_not_matches

    def _compare_feat(self, features, label, name):
        id_to_min = OrderedDict({'id': None, 'min': 100000})
        for key, val in self.id_to_feat.items():
            dist = distance.euclidean(val[0], features)
            #dist = np.linalg.norm(features - val[0], axis=1) 
            #print(dist)
            if dist < id_to_min['min']:
                id_to_min['id'] = key
                id_to_min['min'] = dist
        for feat_wrong in self.wrong_list:
            dist = distance.euclidean(feat_wrong[0], features)
            if dist < id_to_min['min']:
                id_to_min['id'] = None
                id_to_min['min'] = dist
        if id_to_min['min'] <= self.threshold:
            if label==id_to_min['id']:
                self.tr_pos+=1
            else:
                self.f_pos+=1
                if id_to_min['id']==None:
                    self.wrong_matches.append([name,None,id_to_min['min']])
                else:
                    self.wrong_matches.append([name,self.id_to_feat[id_to_min['id']][1],id_to_min['min']])
        else:
            if label not in self.id_to_feat:
                self.tr_neg+=1
                self.id_to_feat[label] = [features,name]
            else:
                self.f_neg+=1
                self.wrong_not_matches.append([name,self.id_to_feat[label][1],id_to_min['min']])
                self.wrong_list.append([features,name,id_to_min['min']])
    def _calculate_accuracy(self, idx_images):
        accuracy = (self.tr_neg+self.tr_pos)/(self.tr_pos+self.tr_neg+self.f_neg+self.f_pos)
        print('iteration - {}, accuracy -{}, fp - {}, fn - {}'.format(idx_images, accuracy, self.f_pos, self.f_neg))
        self.accuracy[idx_images] = accuracy
        if idx_images%self.save_one_in==0:
            with open('tf_1d_result.json', 'w') as fp:
                json.dump(self.accuracy, fp)
            np.save('wrong_matches.npy',self.wrong_matches)
            np.save('wrong_not_matches.npy',self.wrong_not_matches)
Exemplo n.º 19
0
from PIL import Image
from FaceRecognition import FaceRecognition

face_recognition_object = FaceRecognition()

face_recognition_object.set_image_location('target_shoppers.png')
face_recognition_object.show_faces_in_image()
Exemplo n.º 20
0
 def test_image_location(self):
     image_location = 'tom.png'
     face_recognition_instance = FaceRecognition()
     face_recognition_instance.set_image_location(image_location)
     self.assertEqual(image_location,
                      face_recognition_instance.get_image_location())