Spaces:
Runtime error
Runtime error
Docs updated
Browse files- app.py +61 -0
- requirements.txt +9 -0
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
|
6 |
+
# Define constants
|
7 |
+
SEQUENCE_LENGTH = 20 # Number of frames to extract
|
8 |
+
IMAGE_HEIGHT = 64 # Height of each frame
|
9 |
+
IMAGE_WIDTH = 64 # Width of each frame
|
10 |
+
CLASSES_LIST = ["Archery", "BabyCrawling", "Balance_Beam", "EyeMakeup", "LipStick"]
|
11 |
+
|
12 |
+
# Load the model
|
13 |
+
loaded_model = load_model(r"LRCN_model.h5")
|
14 |
+
|
15 |
+
def frames_extraction(video_reader):
|
16 |
+
frames_list = []
|
17 |
+
video_frames_count = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT))
|
18 |
+
skip_frames_window = max(int(video_frames_count / SEQUENCE_LENGTH), 1) # default skip is 1
|
19 |
+
for frame_counter in range(SEQUENCE_LENGTH):
|
20 |
+
video_reader.set(cv2.CAP_PROP_POS_FRAMES, frame_counter * skip_frames_window)
|
21 |
+
success, frame = video_reader.read()
|
22 |
+
if not success:
|
23 |
+
break
|
24 |
+
|
25 |
+
resized_frame = cv2.resize(frame, (IMAGE_HEIGHT, IMAGE_WIDTH))
|
26 |
+
normalized_frame = resized_frame / 255 # color 0-255
|
27 |
+
frames_list.append(normalized_frame)
|
28 |
+
video_reader.release()
|
29 |
+
|
30 |
+
return frames_list
|
31 |
+
|
32 |
+
# Function to classify the video
|
33 |
+
def classify_video(frames):
|
34 |
+
predicted_labels = np.argmax(loaded_model.predict(frames), axis=1)
|
35 |
+
predicted_class_label = CLASSES_LIST[predicted_labels[0]] # Ensure we get the label for the first prediction
|
36 |
+
return predicted_class_label
|
37 |
+
|
38 |
+
# Define the prediction function
|
39 |
+
def predict_video(video_file):
|
40 |
+
video_capture = cv2.VideoCapture(video_file)
|
41 |
+
features = []
|
42 |
+
video_reader = video_capture
|
43 |
+
frames = frames_extraction(video_reader)
|
44 |
+
if len(frames) == SEQUENCE_LENGTH:
|
45 |
+
features.append(frames)
|
46 |
+
features = np.asarray(features)
|
47 |
+
predicted_class = classify_video(features)
|
48 |
+
video_capture.release()
|
49 |
+
return predicted_class
|
50 |
+
|
51 |
+
# Gradio interface definition
|
52 |
+
iface = gr.Interface(
|
53 |
+
fn=predict_video,
|
54 |
+
inputs=gr.Video(),
|
55 |
+
outputs="text",
|
56 |
+
title="Action Recognition with LSTM",
|
57 |
+
description="Upload a video and get the predicted action class."
|
58 |
+
)
|
59 |
+
|
60 |
+
# Launch the Gradio interface
|
61 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
os-sys
|
2 |
+
opencv-python-headless
|
3 |
+
pafy
|
4 |
+
numpy
|
5 |
+
datetime
|
6 |
+
tensorflow
|
7 |
+
moviepy
|
8 |
+
matplotlib
|
9 |
+
scikit-learn
|