salman508 commited on
Commit
821cadc
·
verified ·
1 Parent(s): 28fbdd3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -0
app.py CHANGED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import numpy as np
4
+ import tensorflow as tf
5
+ from tensorflow import keras
6
+ import pandas as pd
7
+ import cv2
8
+
9
+ # Define constants
10
+ IMG_SIZE = 224
11
+ MAX_SEQ_LENGTH = 30
12
+ NUM_FEATURES = 2048
13
+
14
+ # Load the trained model
15
+ model_filepath = "lstm_model.h5" # Replace with the actual path
16
+ loaded_model = keras.models.load_model(model_filepath)
17
+ train_df = pd.DataFrame({
18
+ 'tag': ['BabyCrawling', 'CricketShot']
19
+ })
20
+ label_processor = keras.layers.StringLookup(num_oov_indices=0, vocabulary=np.unique(train_df["tag"]))
21
+ def 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
+
28
+ def 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
+ break
36
+ 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
+ break
43
+ finally:
44
+ cap.release()
45
+ return np.array(frames)
46
+ # Load the feature extractor
47
+ def 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_input
55
+
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
+
62
+ feature_extractor = build_feature_extractor()
63
+
64
+ # Function for preparing a single video for prediction
65
+ def 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 = masked
76
+
77
+ return frame_features, frame_mask
78
+
79
+ # Function for making predictions
80
+ def sequence_prediction(video_file):
81
+ class_vocab = label_processor.get_vocabulary()
82
+
83
+ # Load the video frames
84
+ frames = load_video(video_file)
85
+
86
+ # Prepare the frames for prediction
87
+ frame_features, frame_mask = prepare_single_video(frames)
88
+
89
+ # Make predictions using the loaded model
90
+ probabilities = loaded_model.predict([frame_features, frame_mask])[0]
91
+
92
+ # Get the predicted label
93
+ predicted_label = class_vocab[np.argmax(probabilities)]
94
+
95
+ return predicted_label
96
+ example_list=[
97
+ ["video1.mp4"],
98
+ ["video2.mp4"],
99
+ ]
100
+ # Gradio interface
101
+ iface = 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 app
109
+ iface.launch()