def live(): fd = face() cap = cv2.VideoCapture(0) ret = True while ret: ret, frame = cap.read() detected, x, y, w, h = fd.detectFace(frame) if (detected is not None): f = detected detected = cv2.cvtColor(detected, cv2.COLOR_BGR2GRAY) detected = cv2.resize(detected, (48, 48)) detected = detected[:, :, np.newaxis] detected = detected.astype('float') / 255.0 detected = np.expand_dims(detected, axis=0) prediction = model.predict(detected)[0] index = int(np.argmax(prediction)) print("Result: ", result[index]) #cv2.putText(frame, (x,y),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),2) cv2.rectangle(frame, (x, y), (x + w, y + h), (252, 160, 39), 3) cv2.imshow('onlyFace', f) cv2.imshow('frame', frame) if (cv2.waitKey(1) & 0XFF == ord('q')): break cap.release() cv2.destroyAllWindows()
def photo_analyse(img_list): img_path_list = [] filtered_img_list = [] for img in img_list: detect = face(img) if detect is not '[]': img_path_list.append(str(img)) filtered_img_list.append(img) analyzed_imgs = [] text = [] for i in range(len(filtered_img_list)): filtered_img_list[i] = handwriting(filtered_img_list[i]) lines = json.loads(filtered_img_list[i])['recognitionResult']['lines'] text.append('') for line in lines: text[i] += line['text'] + '\n' for i in range(len(text)): if text[i] not in text[i + 1:]: analyzed_imgs.append(img_path_list[i]) print(analyzed_imgs) return analyzed_imgs
import cv2 from face_detection import face from keras.models import load_model import numpy as np from embedding import emb from retreive_pymongo_data import database label = None a = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0} people = {0: "satinder", 1: "sagat", 2: "mandeep", 3: "sobhu", 4: "ravi"} abhi = None data = database() e = emb() fd = face() print('attendance till now is ') data.view() model = load_model('face_reco1.MODEL') def test(): test_run = cv2.imread('1.jpg', 1) test_run = cv2.resize(test_run, (160, 160)) test_run = test_run.astype('float') / 255.0 test_run = np.expand_dims(test_run, axis=0) test_run = e.calculate(test_run) test_run = np.expand_dims(test_run, axis=0) test_run = model.predict(test_run)[0]
def recognize(self, get_attendance = False, FRAMES = 30): with keras.backend.get_session().graph.as_default(): e = emb() fd = face() model = load_model('face_reco2.MODEL') n_frames = 0 attendance = dict() ret = True cap = cv2.VideoCapture(CAM_CONSTANT, cv2.CAP_DSHOW) WindowName = 'StudentDetector' cv2.namedWindow(WindowName, cv2.WINDOW_NORMAL) cv2.setWindowProperty(WindowName, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) cv2.setWindowProperty(WindowName, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_NORMAL) for person in self.people.values(): attendance[person] = 0 print(attendance.items()) for i in range(FRAMES): ret, frame = cap.read() frame = cv2.flip(frame, 1) try: det, coor = fd.detectFace(frame) except: det, coor = None, None if (det is not None): for i in range(len(det)): detected = det[i] k = coor[i] f = detected detected = cv2.resize(detected, (160, 160)) # detected=np.rollaxis(detected,2,0) detected = detected.astype('float') / 255.0 detected = np.expand_dims(detected, axis=0) feed = e.calculate(detected) feed = np.expand_dims(feed, axis=0) prediction = model.predict(feed)[0] result = int(np.argmax(prediction)) if get_attendance: if (np.max(prediction) > .70): for i in self.people: if (result == i): self.label = self.people[i] attendance[self.label] += 1 if (self.a[i] == 0): print("a") self.a[i] = 1 abhi = i else: self.label = 'unknown' cv2.putText(frame, self.label, (k[0], k[1]), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) cv2.rectangle(frame, (k[0], k[1]), (k[0] + k[2], k[1] + k[3]), (252, 160, 39), 3) n_frames += 1 cv2.imshow(WindowName, frame) if (cv2.waitKey(1) & 0XFF == ord('q')): break for person in attendance.keys(): attendance[person] /= n_frames cap.release() cv2.destroyAllWindows() return attendance
def detect(num): subno = num data = database() a = {} people = {} def getdata(): name = [] records = data.db.pa.find() j = 0 for i in records: j += 1 name.append(i["name"]) k = len(name) for i in range(0, k): people[i] = name[i] a[i] = 0 getdata() print(a) print(people) label = None abhi = None x = a e = emb() fd = face() model = load_model('face_reco2.MODEL') def test(): test_run = cv2.imread('1.jpg', 1) test_run = cv2.resize(test_run, (160, 160)) #test_run=np.rollaxis(test_run,2,0) test_run = test_run.astype('float') / 255.0 test_run = np.expand_dims(test_run, axis=0) test_run = e.calculate(test_run) test_run = np.expand_dims(test_run, axis=0) test_run = model.predict(test_run)[0] cap = cv2.VideoCapture(-1) ret = True test() while ret: ret, frame = cap.read() frame = cv2.flip(frame, 1) det, coor = fd.detectFace(frame) if (det is not None): for i in range(len(det)): detected = det[i] k = coor[i] f = detected detected = cv2.resize(detected, (160, 160)) #detected=np.rollaxis(detected,2,0) detected = detected.astype('float') / 255.0 detected = np.expand_dims(detected, axis=0) feed = e.calculate(detected) feed = np.expand_dims(feed, axis=0) prediction = model.predict(feed)[0] result = int(np.argmax(prediction)) if (np.max(prediction) > .70): for i in people: if (result == i): label = people[i] if (a[i] == 0): print("a") data.update(label, subno) a[i] = 1 abhi = i else: label = 'unknown' cv2.putText(frame, label, (k[0], k[1]), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) x = k[0] y = k[1] if (abhi is not None): if (a[abhi] == 1): cv2.putText(frame, "your attendance is complete", (x, y - 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) cv2.rectangle(frame, (k[0], k[1]), (k[0] + k[2], k[1] + k[3]), (252, 160, 39), 3) cv2.imshow('onlyFace', f) cv2.imshow('frame', frame) if (cv2.waitKey(1) & 0XFF == ord('q')): break cap.release() cv2.destroyAllWindows() data.export_csv()