File size: 2,195 Bytes
f968273
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import cv2
import mediapipe as mp
import json
import os

def extractLandmarksFromVideo(videoPath, letter, outputDir='landmarks'):
    mpHands = mp.solutions.hands
    hands = mpHands.Hands(static_image_mode=False, max_num_hands=1, min_detection_confidence=0.5)
    cap = cv2.VideoCapture(videoPath)

    landmarksData = []

    while cap.isOpened():
        success, frame = cap.read()
        if not success:
            break

        frame = cv2.flip(frame, 1)
        imageRGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        results = hands.process(imageRGB)

        frameLandmarks = []
        if results.multi_hand_landmarks:
            handLandmarks = results.multi_hand_landmarks[0]
            for lm in handLandmarks.landmark:
                frameLandmarks.append({
                    "x": lm.x,
                    "y": lm.y,
                    "z": lm.z,
                })

        if frameLandmarks:
            landmarksData.append(frameLandmarks)

    cap.release()
    hands.close()

    os.makedirs(outputDir, exist_ok=True)
    outputFilename = f"{letter.upper()}.json"
    outputPath = os.path.join(outputDir, outputFilename)
    with open(outputPath, "w") as f:
        json.dump({"frames": landmarksData}, f, indent=2)

    print(f"Saved landmarks to {outputPath}")

def processAllVideosInDirectory(videoDirectory, outputDirectory='landmarks'):
    if not os.path.isdir(videoDirectory):
        print(f"Error: Video directory '{videoDirectory}' not found.")
        return

    os.makedirs(outputDirectory, exist_ok=True)

    for filename in os.listdir(videoDirectory):
        if filename.lower().endswith(('.mp4')): 
            videoPath = os.path.join(videoDirectory, filename)
            letter = os.path.splitext(filename)[0]
            print(f"Processing video: {filename} for letter: {letter}")
            extractLandmarksFromVideo(videoPath, letter, outputDirectory)
        else:
            print(f"Skipping non-video file: {filename}")

if __name__ == "__main__":
    videoInputDirectory = "../video_data" 
    landmarkOutputDirectory = "../processed_data/landmarks" 
    
    processAllVideosInDirectory(videoInputDirectory, landmarkOutputDirectory)