Spaces:
Sleeping
Sleeping
Update preprocessing.py
Browse files- preprocessing.py +36 -16
preprocessing.py
CHANGED
|
@@ -15,7 +15,8 @@ class VideoPreprocessor:
|
|
| 15 |
def preprocess_video(self, video_path):
|
| 16 |
cap = cv2.VideoCapture(video_path)
|
| 17 |
frames = []
|
| 18 |
-
|
|
|
|
| 19 |
with self.mp_face_mesh.FaceMesh(
|
| 20 |
static_image_mode=False,
|
| 21 |
max_num_faces=1,
|
|
@@ -27,53 +28,72 @@ class VideoPreprocessor:
|
|
| 27 |
ret, frame = cap.read()
|
| 28 |
if not ret:
|
| 29 |
break
|
| 30 |
-
|
|
|
|
| 31 |
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
|
|
|
|
|
| 32 |
results = face_mesh.process(rgb_frame)
|
| 33 |
-
|
| 34 |
if results.multi_face_landmarks:
|
|
|
|
| 35 |
face_landmarks = results.multi_face_landmarks[0]
|
|
|
|
| 36 |
try:
|
|
|
|
| 37 |
lip_landmarks = [face_landmarks.landmark[i] for i in self.LIP_INDICES]
|
|
|
|
|
|
|
| 38 |
h, w, _ = frame.shape
|
| 39 |
x_coords = [int(landmark.x * w) for landmark in lip_landmarks]
|
| 40 |
y_coords = [int(landmark.y * h) for landmark in lip_landmarks]
|
| 41 |
-
|
| 42 |
x_min, x_max = max(0, min(x_coords)), min(w, max(x_coords))
|
| 43 |
y_min, y_max = max(0, min(y_coords)), min(h, max(y_coords))
|
| 44 |
-
|
| 45 |
if x_max > x_min and y_max > y_min:
|
|
|
|
| 46 |
lip_frame = frame[y_min:y_max, x_min:x_max]
|
|
|
|
|
|
|
| 47 |
lip_frame_resized = cv2.resize(lip_frame, (85, 85))
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
frames.append(lip_frame_gray)
|
| 50 |
except Exception as e:
|
| 51 |
print(f"Error processing frame: {e}")
|
| 52 |
-
continue
|
| 53 |
else:
|
| 54 |
print("No face landmarks detected in frame.")
|
| 55 |
-
|
| 56 |
cap.release()
|
| 57 |
-
|
| 58 |
if not frames:
|
| 59 |
print("No frames extracted during preprocessing.")
|
| 60 |
-
return None
|
| 61 |
-
|
|
|
|
| 62 |
frames = tf.stack(frames)
|
| 63 |
-
|
|
|
|
| 64 |
desired_num_frames = 75
|
| 65 |
num_frames = frames.shape[0]
|
| 66 |
if num_frames < desired_num_frames:
|
|
|
|
| 67 |
padding = tf.zeros((desired_num_frames - num_frames, 85, 85, 1), dtype=tf.float32)
|
| 68 |
frames = tf.concat([frames, padding], axis=0)
|
| 69 |
elif num_frames > desired_num_frames:
|
|
|
|
| 70 |
frames = frames[:desired_num_frames]
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
std = tf.math.reduce_std(tf.cast(frames, tf.float32))
|
| 74 |
normalized_frames = tf.cast((frames - mean), tf.float32) / std
|
| 75 |
-
|
| 76 |
-
return normalized_frames
|
| 77 |
print(f"Type of lip_frame_gray: {lip_frame_gray.dtype}")
|
| 78 |
print(f"Type of frames[0]: {frames[0].dtype}") # Trước khi stack
|
| 79 |
|
|
|
|
| 15 |
def preprocess_video(self, video_path):
|
| 16 |
cap = cv2.VideoCapture(video_path)
|
| 17 |
frames = []
|
| 18 |
+
|
| 19 |
+
# Utilize mediapipe's GPU acceleration if available
|
| 20 |
with self.mp_face_mesh.FaceMesh(
|
| 21 |
static_image_mode=False,
|
| 22 |
max_num_faces=1,
|
|
|
|
| 28 |
ret, frame = cap.read()
|
| 29 |
if not ret:
|
| 30 |
break
|
| 31 |
+
|
| 32 |
+
# Convert the BGR image to RGB
|
| 33 |
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 34 |
+
|
| 35 |
+
# Process the frame and get the facial landmarks
|
| 36 |
results = face_mesh.process(rgb_frame)
|
| 37 |
+
|
| 38 |
if results.multi_face_landmarks:
|
| 39 |
+
# Get the landmarks for the first face
|
| 40 |
face_landmarks = results.multi_face_landmarks[0]
|
| 41 |
+
|
| 42 |
try:
|
| 43 |
+
# Extract lip landmarks
|
| 44 |
lip_landmarks = [face_landmarks.landmark[i] for i in self.LIP_INDICES]
|
| 45 |
+
|
| 46 |
+
# Extract bounding box around the lips
|
| 47 |
h, w, _ = frame.shape
|
| 48 |
x_coords = [int(landmark.x * w) for landmark in lip_landmarks]
|
| 49 |
y_coords = [int(landmark.y * h) for landmark in lip_landmarks]
|
| 50 |
+
|
| 51 |
x_min, x_max = max(0, min(x_coords)), min(w, max(x_coords))
|
| 52 |
y_min, y_max = max(0, min(y_coords)), min(h, max(y_coords))
|
| 53 |
+
|
| 54 |
if x_max > x_min and y_max > y_min:
|
| 55 |
+
# Crop the lip region
|
| 56 |
lip_frame = frame[y_min:y_max, x_min:x_max]
|
| 57 |
+
|
| 58 |
+
# Resize to 160x160
|
| 59 |
lip_frame_resized = cv2.resize(lip_frame, (85, 85))
|
| 60 |
+
|
| 61 |
+
# Convert to grayscale using TensorFlow
|
| 62 |
+
lip_frame_gray = tf.image.rgb_to_grayscale(lip_frame_resized)
|
| 63 |
+
|
| 64 |
frames.append(lip_frame_gray)
|
| 65 |
except Exception as e:
|
| 66 |
print(f"Error processing frame: {e}")
|
| 67 |
+
continue # Skip this frame
|
| 68 |
else:
|
| 69 |
print("No face landmarks detected in frame.")
|
| 70 |
+
|
| 71 |
cap.release()
|
| 72 |
+
|
| 73 |
if not frames:
|
| 74 |
print("No frames extracted during preprocessing.")
|
| 75 |
+
return None # Return None to indicate failure
|
| 76 |
+
|
| 77 |
+
# Stack frames into a tensor
|
| 78 |
frames = tf.stack(frames)
|
| 79 |
+
|
| 80 |
+
# Adjust frames to match expected input length
|
| 81 |
desired_num_frames = 75
|
| 82 |
num_frames = frames.shape[0]
|
| 83 |
if num_frames < desired_num_frames:
|
| 84 |
+
# Pad frames with zeros
|
| 85 |
padding = tf.zeros((desired_num_frames - num_frames, 85, 85, 1), dtype=tf.float32)
|
| 86 |
frames = tf.concat([frames, padding], axis=0)
|
| 87 |
elif num_frames > desired_num_frames:
|
| 88 |
+
# Truncate frames to desired_num_frames
|
| 89 |
frames = frames[:desired_num_frames]
|
| 90 |
+
|
| 91 |
+
# Normalize the frames
|
| 92 |
+
mean = tf.math.reduce_mean(frames)
|
| 93 |
std = tf.math.reduce_std(tf.cast(frames, tf.float32))
|
| 94 |
normalized_frames = tf.cast((frames - mean), tf.float32) / std
|
| 95 |
+
|
| 96 |
+
return normalized_frames # Return TensorFlow tensor
|
| 97 |
print(f"Type of lip_frame_gray: {lip_frame_gray.dtype}")
|
| 98 |
print(f"Type of frames[0]: {frames[0].dtype}") # Trước khi stack
|
| 99 |
|