def predict(self, *grays): res = [] for gray in grays: faces = face_cascade.detectMultiScale(gray) for x, y, w, h in faces: if self.lbph_rec is not None: try: res.append(self.lbph_rec.predict(gray[y: y + h, x: x + w])[0]) except: res.append(None) for recognizer in (self.eigenface_rec, self.fisherface_rec): faces = face_cascade.detectMultiScale(gray) for x, y, w, h in faces: img = gray[y: y + h, x: x + w].copy() img = cv2.resize(img, (self.max_width, self.max_height)) if recognizer is not None: try: res.append(recognizer.predict(img)[0]) except: res.append(None) if not res: return None, None if len(res) < len(grays) * 3: for i in range(len(grays) * 3 - len(res)): res.append(None) top, occur = Counter(res).most_common(1)[0] percent = int((occur / len(res)) * 100) return top, percent
def predict(self, *grays): with open(r'static/trained.yaml') as file: documents = yaml.load(file, Loader=yaml.Loader) self.known_faces=np.array(documents) print(self.known_faces) face_locations = [] face_encodings = [] match=[] for gray in grays: faces = face_cascade.detectMultiScale(gray) for (x, y, w, h) in faces: img = gray.copy() img = Image.fromarray(img).save('static/temp/User.png') img1 = face_recognition.load_image_file("static/temp/User.png") #load image that need to be processed face_locations = face_recognition.face_locations(img1, model='hog') face_encodings = face_recognition.face_encodings(img1, face_locations) for face_encoding in face_encodings: #face recognition start match = face_recognition.compare_faces(self.known_faces, face_encoding, tolerance=0.30) print(match) id=0 for i in range(len(match)): if match[i]==True: id = i+1 else: pass print(id) percent=100 return int(id), int(percent)
def get_images_and_labels(self, same_size=False): image_paths = [ os.path.join(self.photos, f) for f in os.listdir(self.photos) ] images, labels = [], [] if same_size: width, height = self.get_photo_size() for image_path in image_paths: image_pil = cv2.imread(image_path) try: nbr = int(os.path.split(image_path)[1].split("_")[0]) except: continue gray = cv2.cvtColor(image_pil, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) image = np.array(gray, 'uint8') for x, y, w, h in faces: if same_size: resized = cv2.resize(image[y:y + h, x:x + w], (width, height)) images.append(resized) else: images.append(image[y:y + h, x:x + w]) labels.append(nbr) return images, labels
def crop_photos(paths): for image in paths: img = cv2.imread(image) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = detector.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: cv2.imwrite(image, gray[y:y + h, x:x + w])
def predict(self, *grays): res = [] #print("Start Predict") for gray in grays: #print("Stage 1") faces = face_cascade.detectMultiScale(gray) #print("Face Detected") #print(faces) for (x, y, w, h) in faces: if self.lbph_rec is not None: try: #print("OMG") output = self.lbph_rec.predict(gray[y:y + h, x:x + w]) #print(output) res.append(output) #print("haha") #print(res) except: #print("No") res.append(None) for recognizer in (self.eigenface_rec, self.fisherface_rec): #print("stage2") faces = face_cascade.detectMultiScale(gray) for (x, y, w, h) in faces: img = gray[y:y + h, x:x + w].copy() img = cv2.resize(img, (self.max_width, self.max_height)) if recognizer is not None: try: #print("Stage3") out = recognizer.predict(img) res.append(out) #print("hahaha") #print(res) except: #print("NONO") res.append(None) if not res: return None, None if len(res) < len(grays) * 3: for i in range(len(grays) * 3 - len(res)): res.append(None) top, occur = Counter(res).most_common(1)[0] #print(occur) percent = int((top[1])) #print(percent) #print(top) return int(top[0]), int(percent)
def get_photo_size(self): return 100,100 if len(os.listdir(self.photos)) == 0: return None, None image_paths = [os.path.join(self.photos, f) for f in os.listdir(self.photos)] image_pil = cv2.imread(image_paths[0]) gray = cv2.cvtColor(image_pil, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) width, height = faces[0][2:4] for image_path in image_paths: image_pil = cv2.imread(image_path) gray = cv2.cvtColor(image_pil, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray,scaleFactor = 1.3,minNeighbors = 4) try: x, y, w, h = faces[0] if w < width: width = w if h < height: height = h except: continue return width, height
def save_and_get_label(self): for i in range(5): self.read_image() grays, paths = [], [] for i in range(5): grays.append(self.read_image()[1]) for i, gray in enumerate(grays): faces = face_cascade.detectMultiScale(gray,scaleFactor = 1.3,minNeighbors = 4) for (x, y, w, h) in faces: paths.append('img' + str(i) + '.jpg') cv2.imwrite(paths[-1], gray[y:y + h, x:x + w]) return self.get_image_label(*paths)