Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -18,80 +18,79 @@ loaded_model = loaded_model.to(device)
|
|
18 |
loaded_model.eval()
|
19 |
|
20 |
label_names = [
|
21 |
-
|
22 |
-
|
23 |
]
|
24 |
|
25 |
def load_video(video_path):
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
|
36 |
def preprocess_video(video_frames):
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
|
56 |
def predict_video(video):
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
|
77 |
iface = gr.Interface(
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
)
|
90 |
|
91 |
if __name__ == "__main__":
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
)
|
|
|
18 |
loaded_model.eval()
|
19 |
|
20 |
label_names = [
|
21 |
+
'Archery', 'BalanceBeam', 'BenchPress', 'ApplyEyeMakeup', 'BasketballDunk',
|
22 |
+
'BandMarching', 'BabyCrawling', 'ApplyLipstick', 'BaseballPitch', 'Basketball'
|
23 |
]
|
24 |
|
25 |
def load_video(video_path):
|
26 |
+
try:
|
27 |
+
if not os.path.exists(video_path):
|
28 |
+
raise ValueError(f"Video file not found: {video_path}")
|
29 |
+
|
30 |
+
video = EncodedVideo.from_path(video_path)
|
31 |
+
video_data = video.get_clip(start_sec=0, end_sec=video.duration)
|
32 |
+
return video_data['video']
|
33 |
+
except Exception as e:
|
34 |
+
raise ValueError(f"Error loading video: {str(e)}")
|
35 |
|
36 |
def preprocess_video(video_frames):
|
37 |
+
try:
|
38 |
+
transform_temporal = UniformTemporalSubsample(16)
|
39 |
+
video_frames = transform_temporal(video_frames)
|
40 |
+
video_frames = video_frames.float() / 255.0
|
41 |
+
if video_frames.shape[0] == 3:
|
42 |
+
video_frames = video_frames.permute(1, 0, 2, 3)
|
43 |
+
mean = torch.tensor([0.485, 0.456, 0.406])
|
44 |
+
std = torch.tensor([0.229, 0.224, 0.225])
|
45 |
+
for t in range(video_frames.shape[0]):
|
46 |
+
video_frames[t] = F_t.normalize(video_frames[t], mean, std)
|
47 |
+
video_frames = torch.stack([
|
48 |
+
F_t.resize(frame, [224, 224], antialias=True)
|
49 |
+
for frame in video_frames
|
50 |
+
])
|
51 |
+
video_frames = video_frames.unsqueeze(0)
|
52 |
+
return video_frames
|
53 |
+
except Exception as e:
|
54 |
+
raise ValueError(f"Error preprocessing video: {str(e)}")
|
55 |
|
56 |
def predict_video(video):
|
57 |
+
if video is None:
|
58 |
+
return "Please upload a video file."
|
59 |
+
|
60 |
+
try:
|
61 |
+
video_data = load_video(video)
|
62 |
+
processed_video = preprocess_video(video_data)
|
63 |
+
processed_video = processed_video.to(device)
|
64 |
+
with torch.no_grad():
|
65 |
+
outputs = loaded_model(processed_video)
|
66 |
+
logits = outputs.logits
|
67 |
+
probabilities = F.softmax(logits, dim=-1)[0]
|
68 |
+
top_3 = torch.topk(probabilities, 3)
|
69 |
+
results = [
|
70 |
+
f"{label_names[idx.item()]}: {prob.item():.2%}"
|
71 |
+
for idx, prob in zip(top_3.indices, top_3.values)
|
72 |
+
]
|
73 |
+
return "\n".join(results)
|
74 |
+
except Exception as e:
|
75 |
+
return f"Error processing video: {str(e)}"
|
76 |
|
77 |
iface = gr.Interface(
|
78 |
+
fn=predict_video,
|
79 |
+
inputs=gr.Video(label="Upload Video"),
|
80 |
+
outputs=gr.Textbox(label="Top 3 Predictions"),
|
81 |
+
title="Video Action Recognition",
|
82 |
+
description="Upload a video to classify the action being performed. The model will return the top 3 predictions.",
|
83 |
+
examples=[
|
84 |
+
["test_video_1.avi"],
|
85 |
+
["test_video_2.avi"],
|
86 |
+
["test_video_3.avi"]
|
87 |
+
],
|
88 |
+
cache_examples=True
|
89 |
)
|
90 |
|
91 |
if __name__ == "__main__":
|
92 |
+
iface.launch(
|
93 |
+
debug=False,
|
94 |
+
server_name="0.0.0.0",
|
95 |
+
server_port=7860
|
96 |
+
)
|
|