Currently, I have a python program that is able to run without error. However, it is only able to run through a subfolder of images and extract the x, y, z coordinates of the 468 facial landmarks in each image. I want to edit it as such that the program will loop through the many subfolders and read the many images inside each subfolder. What needs to be stated in the "path" function and what needs to be edited in my code stated below? The folder is named as "nopain" and the subfolders are named as "1, 2, 3, etc..."
import os
import cv2
import mediapipe as mp
import time
from os import listdir
import matplotlib.pyplot as plt
from pathlib import Path
import glob
import numpy
path = glob.glob("C:/Users/Downloads/Mac master DB_no overlap/nopain/1/*.png")
fh = open('out.txt', 'w')
for file in path:
img = cv2.imread(file)
mpDraw = mp.solutions.drawing_utils
mpFaceMesh = mp.solutions.face_mesh
facemesh = mpFaceMesh.FaceMesh(max_num_faces=1)
drawSpec = mpDraw.DrawingSpec(thickness=1, circle_radius=2)
rgb_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
result = facemesh.process(rgb_image)
if result.multi_face_landmarks:
for faceLms in result.multi_face_landmarks:
mpDraw.draw_landmarks(img, faceLms, mpFaceMesh.FACEMESH_CONTOURS,
drawSpec, drawSpec)
for lm in faceLms.landmark:
print(lm, file, file = fh)
cv2.imshow("image", img)
cv2.destroyAllWindows()
fh.close()