Spaces:
Runtime error
Runtime error
Commit
·
c6939df
1
Parent(s):
c10e033
logging added
Browse files
app.py
CHANGED
@@ -1,15 +1,29 @@
|
|
1 |
import gradio as gr
|
2 |
import utils
|
3 |
from config import KINETICS_600_LABELS, MODEL
|
|
|
|
|
4 |
|
5 |
def get_predictions(video_path):
|
6 |
-
|
|
|
|
|
|
|
7 |
model = MODEL
|
8 |
probs = model(video)
|
9 |
labels = utils.get_top_k(probs, label_map=KINETICS_600_LABELS)
|
|
|
|
|
|
|
10 |
return labels
|
11 |
|
|
|
12 |
label = gr.components.Label(num_top_classes=5)
|
13 |
vd = gr.components.Video()
|
|
|
|
|
|
|
14 |
iface = gr.Interface(fn=get_predictions, inputs=vd, outputs=label)
|
15 |
-
iface.launch(
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import utils
|
3 |
from config import KINETICS_600_LABELS, MODEL
|
4 |
+
from logger import logging
|
5 |
+
|
6 |
|
7 |
def get_predictions(video_path):
|
8 |
+
|
9 |
+
logging.info(f">>> Getting predictions for video file : {video_path}")
|
10 |
+
|
11 |
+
video, _ = utils.preprocess_video(video_path)
|
12 |
model = MODEL
|
13 |
probs = model(video)
|
14 |
labels = utils.get_top_k(probs, label_map=KINETICS_600_LABELS)
|
15 |
+
|
16 |
+
logging.info(f"Getting predictions successful : {labels}")
|
17 |
+
|
18 |
return labels
|
19 |
|
20 |
+
|
21 |
label = gr.components.Label(num_top_classes=5)
|
22 |
vd = gr.components.Video()
|
23 |
+
|
24 |
+
logging.info(">>> Launching the gradio app... ")
|
25 |
+
|
26 |
iface = gr.Interface(fn=get_predictions, inputs=vd, outputs=label)
|
27 |
+
iface.launch(share=True)
|
28 |
+
|
29 |
+
logging.info(">>> Launched successfully.")
|
config.py
CHANGED
@@ -1,20 +1,31 @@
|
|
1 |
import tensorflow as tf
|
2 |
import tensorflow_hub as hub
|
3 |
-
from keras.models import load_model
|
4 |
from pathlib import Path
|
5 |
import numpy as np
|
6 |
import config
|
7 |
import os
|
|
|
8 |
|
9 |
|
10 |
FRAME_HT = 224
|
11 |
FRAME_WD = 224
|
12 |
FRAME_NUM = 8
|
|
|
|
|
13 |
TENSORFLOW_HUB_URL_LABELS = "https://raw.githubusercontent.com/tensorflow/models/f8af2291cced43fc9f1d9b41ddbf772ae7b0d7d2/official/projects/movinet/files/kinetics_600_labels.txt"
|
14 |
TENSORFLOW_HUB_URL_MODEL = "https://tfhub.dev/tensorflow/movinet/a2/base/kinetics-600/classification/3"
|
|
|
15 |
MODEL_PATH = os.path.join(os.getcwd(), 'models', 'Activity_recognition.h5')
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
labels_path = tf.keras.utils.get_file(
|
20 |
fname=os.path.join(os.getcwd(), 'labels.txt'),
|
@@ -26,10 +37,20 @@ def get_labels():
|
|
26 |
lines = labels_path.read_text().splitlines()
|
27 |
KINETICS_600_LABELS = np.array([line.strip() for line in lines])
|
28 |
|
|
|
|
|
29 |
return KINETICS_600_LABELS
|
30 |
|
31 |
|
32 |
-
def get_model():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
encoder = hub.KerasLayer(TENSORFLOW_HUB_URL_MODEL, trainable=True)
|
34 |
|
35 |
inputs = tf.keras.layers.Input(
|
@@ -38,12 +59,14 @@ def get_model():
|
|
38 |
name='image'
|
39 |
)
|
40 |
|
41 |
-
# [batch_size, 600]
|
42 |
outputs = encoder(dict(image=inputs))
|
43 |
|
44 |
model = tf.keras.Model(inputs, outputs, name='movinet')
|
45 |
|
|
|
|
|
46 |
return model
|
47 |
|
48 |
-
|
49 |
MODEL = get_model()
|
|
|
|
1 |
import tensorflow as tf
|
2 |
import tensorflow_hub as hub
|
|
|
3 |
from pathlib import Path
|
4 |
import numpy as np
|
5 |
import config
|
6 |
import os
|
7 |
+
from logger import logging
|
8 |
|
9 |
|
10 |
FRAME_HT = 224
|
11 |
FRAME_WD = 224
|
12 |
FRAME_NUM = 8
|
13 |
+
|
14 |
+
# tensorflow urls to download the model and lables
|
15 |
TENSORFLOW_HUB_URL_LABELS = "https://raw.githubusercontent.com/tensorflow/models/f8af2291cced43fc9f1d9b41ddbf772ae7b0d7d2/official/projects/movinet/files/kinetics_600_labels.txt"
|
16 |
TENSORFLOW_HUB_URL_MODEL = "https://tfhub.dev/tensorflow/movinet/a2/base/kinetics-600/classification/3"
|
17 |
+
|
18 |
MODEL_PATH = os.path.join(os.getcwd(), 'models', 'Activity_recognition.h5')
|
19 |
|
20 |
+
|
21 |
+
def get_labels() :
|
22 |
+
"""
|
23 |
+
Downloads and saves the labels for tensorflow 'movienet' model.
|
24 |
+
|
25 |
+
Returns the path of the file 'labels.txt' where the labels are saved.
|
26 |
+
"""
|
27 |
+
|
28 |
+
logging.info(">>> Downloading the labels 'movienet' model... ")
|
29 |
|
30 |
labels_path = tf.keras.utils.get_file(
|
31 |
fname=os.path.join(os.getcwd(), 'labels.txt'),
|
|
|
37 |
lines = labels_path.read_text().splitlines()
|
38 |
KINETICS_600_LABELS = np.array([line.strip() for line in lines])
|
39 |
|
40 |
+
logging.info("Labels retrieved successfully.")
|
41 |
+
|
42 |
return KINETICS_600_LABELS
|
43 |
|
44 |
|
45 |
+
def get_model() -> tf.keras.models.Model :
|
46 |
+
"""
|
47 |
+
Downloads the tensorflow 'movienet' model.
|
48 |
+
|
49 |
+
Returns tensorflow.keras.models.Model object instance.
|
50 |
+
"""
|
51 |
+
|
52 |
+
logging.info(">>> Downloading the 'movienet' model from tensorflow...")
|
53 |
+
|
54 |
encoder = hub.KerasLayer(TENSORFLOW_HUB_URL_MODEL, trainable=True)
|
55 |
|
56 |
inputs = tf.keras.layers.Input(
|
|
|
59 |
name='image'
|
60 |
)
|
61 |
|
|
|
62 |
outputs = encoder(dict(image=inputs))
|
63 |
|
64 |
model = tf.keras.Model(inputs, outputs, name='movinet')
|
65 |
|
66 |
+
logging.info("Model downloaded successfully.")
|
67 |
+
|
68 |
return model
|
69 |
|
70 |
+
|
71 |
MODEL = get_model()
|
72 |
+
KINETICS_600_LABELS = get_labels()
|
logger.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import logging
|
2 |
+
|
3 |
+
logging.basicConfig(
|
4 |
+
format="[ %(asctime)s ] %(lineno)d %(name)s - %(levelname)s %(message)s",
|
5 |
+
level=logging.INFO
|
6 |
+
)
|
7 |
+
|
utils.py
CHANGED
@@ -1,50 +1,67 @@
|
|
1 |
import tensorflow as tf
|
2 |
import cv2
|
3 |
-
import os
|
4 |
import numpy as np
|
5 |
-
from pathlib import Path
|
6 |
import config
|
|
|
7 |
|
8 |
|
9 |
|
|
|
|
|
|
|
|
|
10 |
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
# load the video
|
14 |
-
video_capture = cv2.VideoCapture(video_path)
|
15 |
|
16 |
-
|
17 |
-
original_number_of_frames = video_capture.get(cv2.CAP_PROP_FRAME_COUNT)
|
18 |
|
19 |
-
|
20 |
-
|
|
|
21 |
|
22 |
-
|
23 |
-
for i in range(0, config.FRAME_NUM ):
|
24 |
-
video_capture.set(cv2.CAP_PROP_POS_FRAMES, i*frame_interval)
|
25 |
-
success, frame = video_capture.read()
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
|
42 |
-
input_tensor = tf.expand_dims(new_video_array, axis=0)
|
43 |
|
|
|
44 |
|
45 |
-
|
46 |
|
47 |
-
|
48 |
|
49 |
|
50 |
# Get top_k labels and probabilities
|
@@ -72,5 +89,4 @@ def get_top_k(probs, label_map,k=5 ):
|
|
72 |
output = dict()
|
73 |
for label, prob in zip(labels, top_probs):
|
74 |
output[label] = float(prob) / 100
|
75 |
-
print(output)
|
76 |
return output
|
|
|
1 |
import tensorflow as tf
|
2 |
import cv2
|
|
|
3 |
import numpy as np
|
|
|
4 |
import config
|
5 |
+
from logger import logging
|
6 |
|
7 |
|
8 |
|
9 |
+
def preprocess_video(video_path : str) -> tuple[tf.Tensor, list] :
|
10 |
+
"""
|
11 |
+
Preprocess the video by keeping the required number of frames,
|
12 |
+
resizing the frames and normalizing the frames.
|
13 |
|
14 |
+
params :
|
15 |
+
video_path : path of the video file
|
16 |
|
17 |
+
returns :
|
|
|
|
|
18 |
|
19 |
+
Returns tuple (input_tensor, frame_list)
|
|
|
20 |
|
21 |
+
input_tensor : video with required number of frames and size
|
22 |
+
frame_list : list of required number of frames
|
23 |
+
"""
|
24 |
|
25 |
+
logging.info(">>> Preprocessing the video....")
|
|
|
|
|
|
|
26 |
|
27 |
+
# load the video
|
28 |
+
video_capture = cv2.VideoCapture(video_path)
|
29 |
+
|
30 |
+
# the number of frames in the original video
|
31 |
+
original_number_of_frames = video_capture.get(cv2.CAP_PROP_FRAME_COUNT)
|
32 |
+
|
33 |
+
# gap between two consecutive frames to capture
|
34 |
+
frame_interval = int(original_number_of_frames / config.FRAME_NUM)
|
35 |
+
|
36 |
+
new_video , frame_list = [] , []
|
37 |
+
for i in range(0, config.FRAME_NUM ):
|
38 |
+
video_capture.set(cv2.CAP_PROP_POS_FRAMES, i*frame_interval)
|
39 |
+
success, frame = video_capture.read()
|
40 |
+
|
41 |
+
if not success :
|
42 |
+
logging.info("video loading failed")
|
43 |
+
break
|
44 |
+
|
45 |
+
frame_list.append(frame)
|
46 |
+
# Resize the Frame to fixed height and width.
|
47 |
+
resized_frame = cv2.resize(frame, (config.FRAME_HT, config.FRAME_WD))
|
48 |
+
|
49 |
+
# Normalize the resized frame by dividing it with 255 so that each pixel value then lies between 0 and 1
|
50 |
+
normalized_frame = resized_frame / 255
|
51 |
+
|
52 |
+
# Append the normalized frame into the frames list
|
53 |
+
new_video.append(normalized_frame)
|
54 |
+
|
55 |
+
new_video_array = np.asarray(new_video)
|
56 |
|
57 |
+
input_tensor = tf.expand_dims(new_video_array, axis=0)
|
58 |
|
|
|
59 |
|
60 |
+
video_capture.release()
|
61 |
|
62 |
+
logging.info("Video processing successful.")
|
63 |
|
64 |
+
return input_tensor, frame_list
|
65 |
|
66 |
|
67 |
# Get top_k labels and probabilities
|
|
|
89 |
output = dict()
|
90 |
for label, prob in zip(labels, top_probs):
|
91 |
output[label] = float(prob) / 100
|
|
|
92 |
return output
|