Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- utils/mediapipe_utils.py +86 -0
- utils/video_processor.py +92 -0
utils/mediapipe_utils.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import mediapipe as mp
|
| 3 |
+
import numpy as np
|
| 4 |
+
from tqdm import tqdm
|
| 5 |
+
|
| 6 |
+
class MediaPipeProcessor:
|
| 7 |
+
def __init__(self, config):
|
| 8 |
+
self.config = config.get('mediapipe_config', {})
|
| 9 |
+
self.setup_mediapipe()
|
| 10 |
+
|
| 11 |
+
def setup_mediapipe(self):
|
| 12 |
+
"""Configura os modelos do MediaPipe"""
|
| 13 |
+
self.mp_holistic = mp.solutions.holistic
|
| 14 |
+
self.mp_drawing = mp.solutions.drawing_utils
|
| 15 |
+
self.mp_drawing_styles = mp.solutions.drawing_styles
|
| 16 |
+
|
| 17 |
+
self.holistic = self.mp_holistic.Holistic(
|
| 18 |
+
static_image_mode=self.config.get('static_image_mode', False),
|
| 19 |
+
model_complexity=self.config.get('model_complexity', 1),
|
| 20 |
+
smooth_landmarks=self.config.get('smooth_landmarks', True),
|
| 21 |
+
min_detection_confidence=self.config.get('min_detection_confidence', 0.5),
|
| 22 |
+
min_tracking_confidence=self.config.get('min_tracking_confidence', 0.5)
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
def process_video(self, video_path):
|
| 26 |
+
"""Processa o vídeo e extrai keypoints"""
|
| 27 |
+
cap = cv2.VideoCapture(video_path)
|
| 28 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 29 |
+
keypoints_data = []
|
| 30 |
+
|
| 31 |
+
print(f"Extraindo keypoints de {total_frames} frames...")
|
| 32 |
+
|
| 33 |
+
for frame_idx in tqdm(range(total_frames), desc="Processando frames"):
|
| 34 |
+
ret, frame = cap.read()
|
| 35 |
+
if not ret:
|
| 36 |
+
break
|
| 37 |
+
|
| 38 |
+
# Converter BGR para RGB
|
| 39 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 40 |
+
results = self.holistic.process(frame_rgb)
|
| 41 |
+
|
| 42 |
+
frame_keypoints = self.extract_frame_keypoints(results)
|
| 43 |
+
frame_keypoints['frame_index'] = frame_idx
|
| 44 |
+
frame_keypoints['timestamp'] = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000.0
|
| 45 |
+
|
| 46 |
+
keypoints_data.append(frame_keypoints)
|
| 47 |
+
|
| 48 |
+
cap.release()
|
| 49 |
+
return keypoints_data
|
| 50 |
+
|
| 51 |
+
def extract_frame_keypoints(self, results):
|
| 52 |
+
"""Extrai keypoints de um frame"""
|
| 53 |
+
keypoints = {}
|
| 54 |
+
|
| 55 |
+
# Face landmarks
|
| 56 |
+
if results.face_landmarks:
|
| 57 |
+
keypoints['face_landmarks'] = self.landmarks_to_array(
|
| 58 |
+
results.face_landmarks.landmark, 468
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Pose landmarks
|
| 62 |
+
if results.pose_landmarks:
|
| 63 |
+
keypoints['pose_landmarks'] = self.landmarks_to_array(
|
| 64 |
+
results.pose_landmarks.landmark, 33
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# Left hand landmarks
|
| 68 |
+
if results.left_hand_landmarks:
|
| 69 |
+
keypoints['left_hand_landmarks'] = self.landmarks_to_array(
|
| 70 |
+
results.left_hand_landmarks.landmark, 21
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Right hand landmarks
|
| 74 |
+
if results.right_hand_landmarks:
|
| 75 |
+
keypoints['right_hand_landmarks'] = self.landmarks_to_array(
|
| 76 |
+
results.right_hand_landmarks.landmark, 21
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
return keypoints
|
| 80 |
+
|
| 81 |
+
def landmarks_to_array(self, landmarks, expected_count):
|
| 82 |
+
"""Converte landmarks do MediaPipe para array numpy"""
|
| 83 |
+
if not landmarks or len(landmarks) != expected_count:
|
| 84 |
+
return np.zeros((expected_count, 3))
|
| 85 |
+
|
| 86 |
+
return np.array([[lm.x, lm.y, lm.z] for lm in landmarks])
|
utils/video_processor.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
from tqdm import tqdm
|
| 4 |
+
import subprocess
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
class VideoProcessor:
|
| 8 |
+
def __init__(self, config):
|
| 9 |
+
self.config = config.get('video_normalization', {})
|
| 10 |
+
|
| 11 |
+
def normalize_video(self, input_path, output_path):
|
| 12 |
+
"""Normaliza o vídeo usando OpenCV"""
|
| 13 |
+
cap = cv2.VideoCapture(input_path)
|
| 14 |
+
|
| 15 |
+
# Obter propriedades do vídeo original
|
| 16 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 17 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 18 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 19 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 20 |
+
|
| 21 |
+
# Configurações de saída
|
| 22 |
+
target_resolution = self.config.get('target_resolution', (width, height))
|
| 23 |
+
target_fps = self.config.get('target_fps', fps)
|
| 24 |
+
|
| 25 |
+
# Definir codec e writer
|
| 26 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 27 |
+
out = cv2.VideoWriter(
|
| 28 |
+
output_path, fourcc, target_fps, target_resolution
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
print(f"Processando {total_frames} frames...")
|
| 32 |
+
|
| 33 |
+
for _ in tqdm(range(total_frames), desc="Normalizando vídeo"):
|
| 34 |
+
ret, frame = cap.read()
|
| 35 |
+
if not ret:
|
| 36 |
+
break
|
| 37 |
+
|
| 38 |
+
# Redimensionar
|
| 39 |
+
frame = cv2.resize(frame, target_resolution)
|
| 40 |
+
|
| 41 |
+
# Normalização de cor e brilho
|
| 42 |
+
if self.config.get('normalize_brightness', True):
|
| 43 |
+
frame = self.normalize_brightness(frame)
|
| 44 |
+
|
| 45 |
+
if self.config.get('enhance_contrast', True):
|
| 46 |
+
frame = self.enhance_contrast(frame)
|
| 47 |
+
|
| 48 |
+
out.write(frame)
|
| 49 |
+
|
| 50 |
+
cap.release()
|
| 51 |
+
out.release()
|
| 52 |
+
|
| 53 |
+
# Usar FFmpeg para melhor compressão (opcional)
|
| 54 |
+
self.optimize_with_ffmpeg(output_path)
|
| 55 |
+
|
| 56 |
+
return output_path
|
| 57 |
+
|
| 58 |
+
def normalize_brightness(self, frame):
|
| 59 |
+
"""Normaliza o brilho do frame"""
|
| 60 |
+
# Converter para YUV e normalizar canal Y (luminância)
|
| 61 |
+
yuv = cv2.cvtColor(frame, cv2.COLOR_BGR2YUV)
|
| 62 |
+
yuv[:,:,0] = cv2.equalizeHist(yuv[:,:,0])
|
| 63 |
+
return cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR)
|
| 64 |
+
|
| 65 |
+
def enhance_contrast(self, frame):
|
| 66 |
+
"""Melhora o contraste usando CLAHE"""
|
| 67 |
+
lab = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)
|
| 68 |
+
l, a, b = cv2.split(lab)
|
| 69 |
+
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
|
| 70 |
+
l = clahe.apply(l)
|
| 71 |
+
lab = cv2.merge((l, a, b))
|
| 72 |
+
return cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
|
| 73 |
+
|
| 74 |
+
def optimize_with_ffmpeg(self, input_path):
|
| 75 |
+
"""Otimiza o vídeo com FFmpeg"""
|
| 76 |
+
temp_path = input_path.replace('.mp4', '_temp.mp4')
|
| 77 |
+
|
| 78 |
+
cmd = [
|
| 79 |
+
'ffmpeg', '-i', input_path,
|
| 80 |
+
'-c:v', 'libx264', '-preset', 'medium', '-crf', '23',
|
| 81 |
+
'-c:a', 'aac', '-b:a', '128k',
|
| 82 |
+
'-movflags', '+faststart',
|
| 83 |
+
'-y', temp_path
|
| 84 |
+
]
|
| 85 |
+
|
| 86 |
+
try:
|
| 87 |
+
subprocess.run(cmd, check=True, capture_output=True)
|
| 88 |
+
os.replace(temp_path, input_path)
|
| 89 |
+
except (subprocess.CalledProcessError, FileNotFoundError):
|
| 90 |
+
# Fallback se FFmpeg não estiver disponível
|
| 91 |
+
if os.path.exists(temp_path):
|
| 92 |
+
os.remove(temp_path)
|