thienphuc12339 commited on
Commit
8a07d8a
·
verified ·
1 Parent(s): 0479ad8

Update preprocessing.py

Browse files
Files changed (1) hide show
  1. preprocessing.py +17 -52
preprocessing.py CHANGED
@@ -1,6 +1,7 @@
 
 
1
  import cv2
2
  import mediapipe as mp
3
- import numpy as np
4
  import tensorflow as tf
5
 
6
  class VideoPreprocessor:
@@ -11,29 +12,10 @@ class VideoPreprocessor:
11
  self.LOWER_LIP_INDICES = [146, 91, 181, 84, 17, 314, 405, 321, 375, 291]
12
  self.LIP_INDICES = self.UPPER_LIP_INDICES + self.LOWER_LIP_INDICES
13
 
14
- def preprocess_video(self, video_path, frame_interval=2, max_frames=100):
15
  cap = cv2.VideoCapture(video_path)
16
  frames = []
17
- frame_counter = 0
18
- processed_frames = 0
19
-
20
- # Check if video opened successfully
21
- if not cap.isOpened():
22
- print(f"Cannot open video file: {video_path}")
23
- return None
24
-
25
- # Video properties
26
- fps = cap.get(cv2.CAP_PROP_FPS)
27
- frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
28
- duration = frame_count / fps if fps > 0 else 0
29
- width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
30
- height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
31
- print(f"Video properties: FPS={fps}, Frame count={frame_count}, Duration={duration}s, Width={width}, Height={height}")
32
-
33
- # Desired frame size
34
- desired_width = 640
35
- desired_height = 480
36
-
37
  # Utilize mediapipe's GPU acceleration if available
38
  with self.mp_face_mesh.FaceMesh(
39
  static_image_mode=False,
@@ -47,15 +29,6 @@ class VideoPreprocessor:
47
  if not ret:
48
  break
49
 
50
- if frame_counter % frame_interval != 0:
51
- frame_counter += 1
52
- continue
53
-
54
- frame_counter += 1
55
-
56
- # Resize frame to desired dimensions
57
- frame = cv2.resize(frame, (desired_width, desired_height))
58
-
59
  # Convert the BGR image to RGB
60
  rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
61
 
@@ -85,36 +58,28 @@ class VideoPreprocessor:
85
  # Resize to 85x85 pixels
86
  lip_frame_resized = cv2.resize(lip_frame, (85, 85))
87
 
88
- # Convert to grayscale using OpenCV
89
- lip_frame_gray = cv2.cvtColor(lip_frame_resized, cv2.COLOR_BGR2GRAY)
90
 
91
  frames.append(lip_frame_gray)
92
- processed_frames += 1
93
-
94
- if processed_frames >= max_frames:
95
- break
96
  except Exception as e:
97
  print(f"Error processing frame: {e}")
98
  continue # Skip this frame
99
  else:
100
  print("No face landmarks detected in frame.")
101
 
102
- cap.release()
103
-
104
- if not frames:
105
- print("No frames extracted during preprocessing.")
106
- return None # Return None to indicate failure
107
 
108
- # Convert frames to NumPy array
109
- frames = np.array(frames)
 
110
 
111
- # Normalize the frames
112
- mean = np.mean(frames)
113
- std = np.std(frames)
114
- normalized_frames = (frames - mean) / std
115
 
116
- # Add channel dimension and convert to TensorFlow tensor
117
- normalized_frames = np.expand_dims(normalized_frames, axis=-1)
118
- normalized_frames = tf.convert_to_tensor(normalized_frames, dtype=tf.float32)
 
119
 
120
- return normalized_frames
 
1
+ # Updated preprocessing.py
2
+
3
  import cv2
4
  import mediapipe as mp
 
5
  import tensorflow as tf
6
 
7
  class VideoPreprocessor:
 
12
  self.LOWER_LIP_INDICES = [146, 91, 181, 84, 17, 314, 405, 321, 375, 291]
13
  self.LIP_INDICES = self.UPPER_LIP_INDICES + self.LOWER_LIP_INDICES
14
 
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,
 
29
  if not ret:
30
  break
31
 
 
 
 
 
 
 
 
 
 
32
  # Convert the BGR image to RGB
33
  rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
34
 
 
58
  # Resize to 85x85 pixels
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
+ # Normalize the frames
81
+ mean = tf.math.reduce_mean(frames)
82
+ std = tf.math.reduce_std(tf.cast(frames, tf.float32))
83
+ normalized_frames = tf.cast((frames - mean), tf.float32) / std
84
 
85
+ return normalized_frames