Spaces:
Sleeping
Sleeping
change num_frames
Browse files
app.py
CHANGED
|
@@ -50,7 +50,7 @@ class EfficientNetLSTM(nn.Module):
|
|
| 50 |
|
| 51 |
# Load Model (Global)
|
| 52 |
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 53 |
-
MODEL_PATH = "
|
| 54 |
|
| 55 |
print("⏳ Đang tải models...")
|
| 56 |
try:
|
|
@@ -67,7 +67,7 @@ except Exception as e:
|
|
| 67 |
|
| 68 |
# Transform
|
| 69 |
transform = A.Compose([
|
| 70 |
-
A.Resize(height=
|
| 71 |
A.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 72 |
A.ToTensorV2(),
|
| 73 |
])
|
|
@@ -115,11 +115,11 @@ def predict_webcam_stream(image, buffer_state, history_log):
|
|
| 115 |
current_buffer = buffer_state if buffer_state is not None else []
|
| 116 |
current_buffer.append(frame_tensor)
|
| 117 |
|
| 118 |
-
if len(current_buffer) >
|
| 119 |
current_buffer.pop(0) # Xóa frame cũ nhất
|
| 120 |
|
| 121 |
# 3. Dự đoán Fall (Chỉ khi đủ 16 frames và có người)
|
| 122 |
-
if len(current_buffer) ==
|
| 123 |
# Gom buffer thành batch
|
| 124 |
video_tensor = torch.stack(current_buffer).unsqueeze(0).to(DEVICE)
|
| 125 |
|
|
@@ -161,8 +161,8 @@ def analyze_uploaded_video(video_path):
|
|
| 161 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 162 |
|
| 163 |
# Lấy 16 frames rải đều
|
| 164 |
-
if total_frames >=
|
| 165 |
-
indices = np.linspace(0, total_frames - 1,
|
| 166 |
else:
|
| 167 |
indices = np.arange(total_frames)
|
| 168 |
# Pad nếu thiếu (đơn giản hóa)
|
|
@@ -176,9 +176,9 @@ def analyze_uploaded_video(video_path):
|
|
| 176 |
|
| 177 |
cap.release()
|
| 178 |
|
| 179 |
-
if len(frames) <
|
| 180 |
# Pad frame cuối nếu thiếu
|
| 181 |
-
while len(frames) <
|
| 182 |
frames.append(frames[-1])
|
| 183 |
|
| 184 |
video_tensor = torch.stack(frames).unsqueeze(0).to(DEVICE)
|
|
|
|
| 50 |
|
| 51 |
# Load Model (Global)
|
| 52 |
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 53 |
+
MODEL_PATH = "best_model_efficientnet_lstm_v2.pth" # Đảm bảo file này nằm cùng thư mục
|
| 54 |
|
| 55 |
print("⏳ Đang tải models...")
|
| 56 |
try:
|
|
|
|
| 67 |
|
| 68 |
# Transform
|
| 69 |
transform = A.Compose([
|
| 70 |
+
A.Resize(height=288, width=288),
|
| 71 |
A.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 72 |
A.ToTensorV2(),
|
| 73 |
])
|
|
|
|
| 115 |
current_buffer = buffer_state if buffer_state is not None else []
|
| 116 |
current_buffer.append(frame_tensor)
|
| 117 |
|
| 118 |
+
if len(current_buffer) > 32:
|
| 119 |
current_buffer.pop(0) # Xóa frame cũ nhất
|
| 120 |
|
| 121 |
# 3. Dự đoán Fall (Chỉ khi đủ 16 frames và có người)
|
| 122 |
+
if len(current_buffer) == 32:
|
| 123 |
# Gom buffer thành batch
|
| 124 |
video_tensor = torch.stack(current_buffer).unsqueeze(0).to(DEVICE)
|
| 125 |
|
|
|
|
| 161 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 162 |
|
| 163 |
# Lấy 16 frames rải đều
|
| 164 |
+
if total_frames >= 32:
|
| 165 |
+
indices = np.linspace(0, total_frames - 1, 32, dtype=int)
|
| 166 |
else:
|
| 167 |
indices = np.arange(total_frames)
|
| 168 |
# Pad nếu thiếu (đơn giản hóa)
|
|
|
|
| 176 |
|
| 177 |
cap.release()
|
| 178 |
|
| 179 |
+
if len(frames) < 32:
|
| 180 |
# Pad frame cuối nếu thiếu
|
| 181 |
+
while len(frames) < 32:
|
| 182 |
frames.append(frames[-1])
|
| 183 |
|
| 184 |
video_tensor = torch.stack(frames).unsqueeze(0).to(DEVICE)
|