CoolFace
Apppublic

salman508/Action_Based_Video_Classification

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
app.py109 linesDownload Raw Back to root
1import gradio as gr2import os3import numpy as np4import tensorflow as tf5from tensorflow import keras6import pandas as pd7import cv28 9# Define constants10IMG_SIZE = 22411MAX_SEQ_LENGTH = 3012NUM_FEATURES = 204813 14# Load the trained model15model_filepath = "lstm_model.h5"  # Replace with the actual path16loaded_model = keras.models.load_model(model_filepath)17train_df = pd.DataFrame({18    'tag': ['BabyCrawling', 'CricketShot']19})20label_processor = keras.layers.StringLookup(num_oov_indices=0, vocabulary=np.unique(train_df["tag"]))21def crop_center_square(frame):22    y, x = frame.shape[0:2]23    min_dim = min(y, x)24    start_x = (x // 2) - (min_dim // 2)25    start_y = (y // 2) - (min_dim // 2)26    return frame[start_y : start_y + min_dim, start_x : start_x + min_dim]27    28def load_video(path, max_frames=0, resize=(IMG_SIZE, IMG_SIZE)):29    cap = cv2.VideoCapture(path)30    frames = []31    try:32        while True:33            ret, frame = cap.read()34            if not ret:35                break36            frame = crop_center_square(frame)37            frame = cv2.resize(frame, resize)38            frame = frame[:, :, [2, 1, 0]]39            frames.append(frame)40 41            if len(frames) == max_frames:42                break43    finally:44        cap.release()45    return np.array(frames)46# Load the feature extractor47def build_feature_extractor():48    feature_extractor = keras.applications.InceptionV3(49        weights="imagenet",50        include_top=False,51        pooling="avg",52        input_shape=(IMG_SIZE, IMG_SIZE, 3),53    )54    preprocess_input = keras.applications.inception_v3.preprocess_input55 56    inputs = keras.Input((IMG_SIZE, IMG_SIZE, 3))57    preprocessed = preprocess_input(inputs)58 59    outputs = feature_extractor(preprocessed)60    return keras.Model(inputs, outputs, name="feature_extractor")61 62feature_extractor = build_feature_extractor()63 64# Function for preparing a single video for prediction65def prepare_single_video(frames):66    frames = frames[None, ...]67    frame_mask = np.zeros(shape=(1, MAX_SEQ_LENGTH,), dtype="bool")68    frame_features = np.zeros(shape=(1, MAX_SEQ_LENGTH, NUM_FEATURES), dtype="float32")69 70    for i, batch in enumerate(frames):71        video_length = batch.shape[0]72        length = min(MAX_SEQ_LENGTH, video_length)73        for j in range(length):74            frame_features[i, j, :] = feature_extractor.predict(batch[None, j, :])75        frame_mask[i, :length] = 1  # 1 = not masked, 0 = masked76 77    return frame_features, frame_mask78 79# Function for making predictions80def sequence_prediction(video_file):81    class_vocab = label_processor.get_vocabulary()82 83    # Load the video frames84    frames = load_video(video_file)85 86    # Prepare the frames for prediction87    frame_features, frame_mask = prepare_single_video(frames)88 89    # Make predictions using the loaded model90    probabilities = loaded_model.predict([frame_features, frame_mask])[0]91 92    # Get the predicted label93    predicted_label = class_vocab[np.argmax(probabilities)]94 95    return predicted_label96example_list=[97        ["video-1.mp4"],98        ["video-2.mp4"],99    ]100# Gradio interface101iface = gr.Interface(102    fn=sequence_prediction,103    inputs=gr.Video(label="Upload a video file"),104    outputs="text",105    examples=example_list,106)107 108# Launch the Gradio app109iface.launch()