0

I recently took the code from MediaPipe to generate a face mesh landmark from emotional face images (RAVDESS data). However, I seem to struggle in exporting generated face mesh landmark data in the form of PNG. I'm also doing this from Google Colaboratory.

I have created function def save() to automatically resized and save generated images using cv2.imwrite(). I tried this but nothing shows in the folder saved_images. I suspect that the for I did is wrong and I may misunderstood the concept of looping in programming (I'm still new to programming). Below is the code.

from google.colab import files
uploaded = files.upload()

import cv2
from google.colab.patches import cv2_imshow
import math
import numpy as np
import mediapipe as mp

DESIRED_HEIGHT = 480
DESIRED_WIDTH = 480

def save(image):
  h, w = image.shape[:2]
  if h < w:
    resized = cv2.resize(image, (DESIRED_WIDTH, math.floor(h/(w/DESIRED_WIDTH))))
  else:
    resized = cv2.resize(image, (math.floor(w/(h/DESIRED_HEIGHT)), DESIRED_HEIGHT))
  
  for i in resized:
    i=0
    filename = "/saved_images/save_%i.png"%i
    cv2.imwrite(filename, resized)
    i+=1

mp_face_mesh = mp.solutions.face_mesh
mp_drawing_styles = mp.solutions.drawing_styles
with mp_face_mesh.FaceMesh(
    static_image_mode=True,
    refine_landmarks=True,
    max_num_faces=2,
    min_detection_confidence=0.5) as face_mesh:
  for name, image in images.items():
    results = face_mesh.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    print(f'Face landmarks of {name}:')
    if not results.multi_face_landmarks:
      continue

    (h, w, c) = image.shape[:3]
    annotated_image = np.zeros((h,w,c), np.uint8)
    for face_landmarks in results.multi_face_landmarks:
      mp_drawing.draw_landmarks(
          image=annotated_image,
          landmark_list=face_landmarks,
          connections=mp_face_mesh.FACEMESH_TESSELATION,
          landmark_drawing_spec=None,
          connection_drawing_spec=mp_drawing_styles
          .get_default_face_mesh_tesselation_style())
      mp_drawing.draw_landmarks(
          image=annotated_image,
          landmark_list=face_landmarks,
          connections=mp_face_mesh.FACEMESH_CONTOURS,
          landmark_drawing_spec=None,
          connection_drawing_spec=mp_drawing_styles
          .get_default_face_mesh_contours_style())
      mp_drawing.draw_landmarks(
          image=annotated_image,
          landmark_list=face_landmarks,
          connections=mp_face_mesh.FACEMESH_IRISES,
          landmark_drawing_spec=None,
          connection_drawing_spec=mp_drawing_styles
          .get_default_face_mesh_iris_connections_style())
      save(annotated_image)  

However, in the def save(), when I tried to use cv2.imshow(resized) instead of for, it shows the generated face landmarks file successfully. It even iterated correctly (because of for face_landmarks in results.multi_face_landmarks:, I guess?)

So the entire code works but the for in def save(). What should I do to fix this? Thanks in advance.

0 Answers0