class RunFaceID(object): def __init__(self): super(RunFaceID, self).__init__() self.face_detection = FaceDetection() self.face_recognition = FaceRecognition() def predict(self, array_embeddings, embeddings_source): return "unknown" def processing(self, images, embeddings_source): frame = copy.deepcopy(images) faces = self.face_detection.detect_faces(frame) if faces is None or len(faces) < 1: return None data = {} array_img = [] labels = [] for x, y, w, h in faces: if w > 0 and h > 0: img_crop = frame[y:y + h, x:x + w, :] array_img.append(img_crop) # labels.append("unknown") array_img = np.array(array_img) # data["labels"] = labels if count >= NUMBER_FRAME: array_embeddings = self.face_recognition.embedding_image(array_img) data["labels"] = self.predict_labels(array_embeddings, embeddings_source) data["bounding_boxs"] = faces return data
class FaceFrameMorpher: def __init__(self): self.animated_scale = 0.0 self.animated_direction = 1 self.face_detection = FaceDetection() self.MAX_ANIMATION_SCALE = 50.0 def morph_frame_faces(self, frames): for frame in frames: faces = self.face_detection.detect_faces(frame) if len(faces) == 0: self.reset_animation() for (x, y, w, h) in faces: self.morph_pixels_in_area_animated(frame, x, y, w, h) return frames def animated_scale_tick(self): self.animated_scale += self.animated_direction if self.animated_scale == -self.MAX_ANIMATION_SCALE or self.animated_scale == self.MAX_ANIMATION_SCALE: self.animated_direction = -1 * self.animated_direction def reset_animation(self): self.animated_scale = 0.0 def morph_pixels_in_area_animated(self, img, x_pos, y_pos, width, height): section = img[y_pos:y_pos + height, x_pos:x_pos + width] self.animated_scale_tick() section = FaceFrameMorpher.vertical_wave(section, self.animated_scale) img[y_pos:y_pos + height, x_pos:x_pos + len(section)] = section @staticmethod def vertical_wave(img, wave_factor=20.0): img_output = np.zeros(img.shape, dtype=img.dtype) rows, cols = img.shape for i in range(rows): for j in range(cols): offset_x = int(wave_factor * math.sin(2 * 3.14 * i / 180)) offset_y = int(wave_factor * math.sin(2 * 3.14 * j / 180)) # print(offset_x, offset_y, i, rows) if j + offset_x < rows: img_output[i, j] = img[abs((i + offset_y) % cols), (j + offset_x) % cols] else: img_output[i, j] = img[i, j] return img_output
class RunFaceID(object): def __init__(self): super(RunFaceID, self).__init__() self.face_detection = FaceDetection() self.face_recognition = FaceRecognition() self.arr_embeddings = pickle.load(open("data_embeddings", "rb")) self.labels = pickle.load(open("labels", "rb")) def predict_labels(self, embeddings): dis_cs = cs(embeddings, self.arr_embeddings) index_list = np.argmax(dis_cs, axis=-1) label_pred = [] for i, index in enumerate(index_list): if dis_cs[i][index] > 0.6: label_pred.append(self.labels[index]) else: label_pred.append("unknown") return label_pred def processing(self, images, count): frame = copy.deepcopy(images) faces = self.face_detection.detect_faces(frame) if faces is None or len(faces) < 1: return None data = {} array_img = [] labels = [] for x, y, w, h in faces: if w > 0 and h > 0: img_crop = img_crop = frame[y:y + h, x:x + w, :] array_img.append(img_crop) labels.append("unknown") array_img = np.array(array_img) # data["labels"] = labels if count >= 5: array_embeddings = self.face_recognition.embedding_image(array_img) data["labels"] = self.predict_labels(array_embeddings) data["bounding_boxs"] = faces return data
# labels.append("unknown") array_img = np.array(array_img) # data["labels"] = labels if count >= NUMBER_FRAME: array_embeddings = self.face_recognition.embedding_image(array_img) data["labels"] = self.predict_labels(array_embeddings, embeddings_source) data["bounding_boxs"] = faces return data # runfaceid = RunFaceID() cap = cv2.VideoCapture(0) face_detect = FaceDetection() i = 0 while (True): # Capture frame-by-frame ret, frame = cap.read() faces = face_detect.detect_faces(frame) if len(faces) > 0: cv2.imwrite(str(i) + ".png", frame) i += 1 if i == 5: break # Display the resulting frame cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break
class RunFaceID(object): def __init__(self): super(RunFaceID, self).__init__() self.face_detection = FaceDetection() self.face_recognition = FaceRecognition() # self.arr_embeddings = pickle.load(open("data_embeddings", "rb")) # self.labels = pickle.load(open("labels", "rb")) def predict_labels(self, embeddings, embeddings_source, labels_index, labels_name): dis_cs = cs(embeddings, embeddings_source) index_list = np.argmax(dis_cs, axis=-1) label_pred = [] for i, index in enumerate(index_list): if dis_cs[i][index] > 0.6: label_index = labels_index[index] for i, (index_tmp, name_tmp) in enumerate(labels_name): if label_index == index_tmp: label_pred.append(labels_name[i]) else: label_pred.append([-1, "unknown"]) return label_pred def processing(self, images, count, embeddings_source, labels_index, labels_name, message_status): frame = copy.deepcopy(images) faces = self.face_detection.detect_faces(frame) if faces is None or len(faces) < 1: return None data = {} array_img = [] labels = [] for x, y, w, h in faces: if w > 0 and h > 0: img_crop = frame[y:y + h, x:x + w, :] array_img.append(img_crop) # labels.append("unknown") array_img = np.array(array_img) # data["labels"] = labels if count >= NUMBER_FRAME: array_embeddings = self.face_recognition.embedding_image(array_img) data["labels"] = self.predict_labels(array_embeddings, embeddings_source, labels_index, labels_name) data["bounding_boxs"] = faces return data def get_faces(self, images): faces = self.face_detection.detect_faces(images) return list(faces) def get_bb_embeddings(self, images): faces = self.get_faces(images) if len(faces) != 1: return None array_img = [] data = {} for x, y, w, h in faces: if w > 0 and h > 0: img_crop = images[y:y + h, x:x + w, :] array_img.append(img_crop) embeddings = self.face_recognition.embedding_image(array_img)[0] data["bounding_box"] = json.dumps(faces[0].tolist()) data['embeddings'] = json.dumps(embeddings.tolist()) return data