cesar commited on
Commit
46c38d5
1 Parent(s): 025ff1d

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +113 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """🎬 Keras Video Classification CNN-RNN model
3
+
4
+ Spaces for showing the model usage.
5
+
6
+ Author:
7
+ - Thomas Chaigneau @ChainYo
8
+ """
9
+ import os
10
+ import cv2
11
+ import gradio as gr
12
+ import numpy as np
13
+
14
+ from tensorflow import keras
15
+ from tensorflow_docs.vis import embed
16
+
17
+ from huggingface_hub import from_pretrained_keras
18
+
19
+
20
+ IMG_SIZE = 224
21
+ NUM_FEATURES = 2048
22
+
23
+ model = from_pretrained_keras("keras-io/video-classification-cnn-rnn")
24
+ samples = []
25
+ for file in os.listdir("samples"):
26
+ tag = file.split("_")[1]
27
+ samples.append([f"samples/{file}"])
28
+
29
+
30
+ def crop_center_square(frame):
31
+ y, x = frame.shape[0:2]
32
+ min_dim = min(y, x)
33
+ start_x = (x // 2) - (min_dim // 2)
34
+ start_y = (y // 2) - (min_dim // 2)
35
+ return frame[start_y : start_y + min_dim, start_x : start_x + min_dim]
36
+
37
+
38
+ def load_video(path, max_frames=0, resize=(IMG_SIZE, IMG_SIZE)):
39
+ cap = cv2.VideoCapture(path)
40
+ frames = []
41
+ try:
42
+ while True:
43
+ ret, frame = cap.read()
44
+ if not ret:
45
+ break
46
+ frame = crop_center_square(frame)
47
+ frame = cv2.resize(frame, resize)
48
+ frame = frame[:, :, [2, 1, 0]]
49
+ frames.append(frame)
50
+
51
+ if len(frames) == max_frames:
52
+ break
53
+ finally:
54
+ cap.release()
55
+ return np.array(frames)
56
+
57
+
58
+ def build_feature_extractor():
59
+ feature_extractor = keras.applications.InceptionV3(
60
+ weights="imagenet",
61
+ include_top=False,
62
+ pooling="avg",
63
+ input_shape=(IMG_SIZE, IMG_SIZE, 3),
64
+ )
65
+ preprocess_input = keras.applications.inception_v3.preprocess_input
66
+
67
+ inputs = keras.Input((IMG_SIZE, IMG_SIZE, 3))
68
+ preprocessed = preprocess_input(inputs)
69
+
70
+ outputs = feature_extractor(preprocessed)
71
+ return keras.Model(inputs, outputs, name="feature_extractor")
72
+
73
+
74
+ feature_extractor = build_feature_extractor()
75
+
76
+ def prepare_video(frames, max_seq_length: int = 20):
77
+ frames = frames[None, ...]
78
+ frame_mask = np.zeros(shape=(1, max_seq_length,), dtype="bool")
79
+ frame_features = np.zeros(shape=(1, max_seq_length, NUM_FEATURES), dtype="float32")
80
+
81
+ for i, batch in enumerate(frames):
82
+ video_length = batch.shape[0]
83
+ length = min(max_seq_length, video_length)
84
+ for j in range(length):
85
+ frame_features[i, j, :] = feature_extractor.predict(batch[None, j, :])
86
+ frame_mask[i, :length] = 1 # 1 = not masked, 0 = masked
87
+
88
+ return frame_features, frame_mask
89
+
90
+
91
+ def sequence_prediction(path):
92
+ class_vocab = ["CricketShot", "PlayingCello", "Punch", "ShavingBeard", "TennisSwing"]
93
+
94
+ frames = load_video(path)
95
+ frame_features, frame_mask = prepare_video(frames)
96
+ probabilities = model.predict([frame_features, frame_mask])[0]
97
+
98
+ preds = {}
99
+ for i in np.argsort(probabilities)[::-1]:
100
+ preds[class_vocab[i]] = float(probabilities[i])
101
+ return preds
102
+
103
+
104
+ article = article = "<div style='text-align: center;'><a href='https://github.com/ChainYo' target='_blank'>Space by Thomas Chaigneau</a><br><a href='https://keras.io/examples/vision/video_classification/' target='_blank'>Keras example by Sayak Paul</a></div>"
105
+ app = gr.Interface(
106
+ sequence_prediction,
107
+ inputs=[gr.inputs.Video(label="Video", type="mp4")],
108
+ outputs=[gr.outputs.Label(label="Prediction", type="confidences")],
109
+ title="Keras Video Classification with CNN-RNN",
110
+ description="Video classification demo using CNN-RNN based model.",
111
+ article=article,
112
+ examples=samples
113
+ ).launch(enable_queue=True, cache_examples=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ opencv-python-headless
2
+ tensorflow
3
+ git+https://github.com/tensorflow/docs