def predict(self, im, boxes, landmarks): Olandmark = [] for idx in range(len(boxes)): animoji_class = Animoji() box = boxes[idx] landmark = landmarks[idx] animoji = animojis[idx] animoji_class.face = self.face_net.predict(image.copy(), box, landmark) animoji_class.left_eye, animoji_class.right_eye = self.eyes_net.predict( image.copy(), box, landmark) animoji_class.mouse = self.mouse_net.predict( image.copy(), box, landmark) animoji_class.nose = self.nose_net.predict(image.copy(), box, landmark) Olandmark.append(animoji_class.get_animoji()) return np.asarray(Olandmark, dtype=np.float32)
assert (len(boxes_pred.shape) == 2), str(boxes_pred.shape) assert (len(landmarks_pred.shape) == 3), str(landmarks_pred.shape) assert (len(animojis_pred.shape) == 3), str(animojis_pred.shape) iou, _ = IoU(bbox, boxes_pred) idx = np.argmax(iou) if iou[idx] < 0.4: error_detect_face.append(filename) continue box_pred = boxes_pred[idx][:-1] landmark_pred = landmarks_pred[idx] animoji_pred = animojis_pred[idx] animoji = animoji.reshape(70, 2) animoji = Animoji(animoji) animoji_pred = Animoji(animoji_pred) left_eye_point = animoji.left_eye right_eye_point = animoji.right_eye nose_point = animoji.nose mouse_point = animoji.mouse face_point = animoji.face left_eye_point_pred = animoji_pred.left_eye right_eye_point_pred = animoji_pred.right_eye nose_point_pred = animoji_pred.nose mouse_point_pred = animoji_pred.mouse face_point_pred = animoji_pred.face landmark_left_eye = landmark_pred[0]
assert (len(animojis_pred.shape) == 3), str(animojis_pred.shape) iou, _ = IoU(bbox, boxes_pred) idx = np.argmax(iou) if iou[idx] < 0.4: max_iou.append(iou[idx]) error_detect_face.append(filepath) continue ####### result = ratate(boxes_pred[idx], landmarks_pred[idx][0:2], points=[animoji.reshape(70, 2)]) img_rotated, (animoji_rotated, ), _ = result animoji_rotated = Animoji(animoji_rotated) face_point = animoji_rotated.face p1 = np.min(face_point, axis=0) p2 = np.max(face_point, axis=0) p3 = boxes_pred[idx][0:2] p4 = boxes_pred[idx][2:4] dx1, dy1 = p3 - p1 dx2, dy2 = p2 - p4 Lx, Ly = p4 - p3 + 1 face_rate.append( (dx1 / Lx, dy1 / Ly, dx2 / Lx, dy2 / Ly, (p1, p2, p3, p4)))
# print(filepath) image = cv2.imread(filepath) h, w, _ = image.shape line_w = int(max(h, w) / 500 + 1) _, _, boxes, landmarks, animojis, _ = facenet.predict(image.copy()) if boxes is None or landmark is None or animojis is None: test_miss += 1 continue if image_idx >= 100: continue animojis_landmarks = landmarknet.predict(image.copy(), boxes, landmarks) for box, animoji, animoji_landmark in zip(boxes, animojis, animojis_landmarks): Animoji_landmark = Animoji(animoji_landmark) Animoji_pred = Animoji(animoji) Animoji_landmark.face = Animoji_pred.face animoji_landmark = Animoji_landmark.get_animoji() cv2.putText(image, str(np.round(box[4], 2)), (int(box[0]), int(box[1])), cv2.FONT_HERSHEY_TRIPLEX, 1, color=(255, 0, 255)) cv2.rectangle(image, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (0, 0, 255)) for x, y in animoji_landmark: cv2.circle(image, (int(x + 0.5), int(y + 0.5)), line_w, (0, 0, 255))
if iou[idx] < 0.4: # max_iou.append(iou[idx]) # error_detect_face.append(filepath) continue img = cv2.imread(filepath) ratate_vector = landmarks_pred[idx][0:2] out_points = [ landmarks_pred[idx], animoji.reshape(70, 2), animojis_pred[idx] ] result = ratate(boxes_pred[idx], ratate_vector, points=out_points, img=img) img_rotated, (landmark_pred_rotated, animoji_rotated, animoji_pred_rotated), _ = result Animoji_rotated = Animoji(animoji_rotated) Animoji_pred_rotated = Animoji(animoji_pred_rotated) output_image_path = os.path.join(output_image, filename) if not os.path.exists(os.path.dirname(output_image_path)): os.makedirs(os.path.dirname(output_image_path)) cv2.imwrite(output_image_path, img_rotated) imgh, imgw, imgc = img_rotated.shape face_flipped_by_x = cv2.flip(img_rotated, 1) name, ext = os.path.splitext(filename) flipped_name = name + '_filpped' + ext output_flipped_image_path = os.path.join(output_image, flipped_name) cv2.imwrite(output_flipped_image_path, face_flipped_by_x) left_eye_point = Animoji_rotated.left_eye