Spaces:
Build error
Build error
File size: 1,416 Bytes
9b2dc59 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import cv2
# import imageio
import numpy as np
import tensorflow as tf
from huggingface_hub import from_pretrained_keras
from tensorflow.keras.optimizers import Adam
from .constants import LEARNING_RATE
def predict_label(path):
frames = load_video(path)
model = get_model()
prediction = model.predict(tf.expand_dims(example, axis=0))[0]
label = np.argmax(pred, axis=0)
return label
def load_video(path):
"""
Load video from path and return a list of frames.
The video is converted to grayscale because it is the format expected by the model.
"""
cap = cv2.VideoCapture(path)
frames = []
try:
while True:
ret, frame = cap.read()
if not ret:
break
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frames.append(frame)
finally:
cap.release()
return np.array(frames)
def get_model():
"""
Download the model from the Hugging Face Hub and compile it.
"""
model = from_pretrained_keras("pablorodriper/video-vision-transformer")
model.compile(
optimizer=Adam(learning_rate=LEARNING_RATE),
loss="sparse_categorical_crossentropy",
# metrics=[
# keras.metrics.SparseCategoricalAccuracy(name="accuracy"),
# keras.metrics.SparseTopKCategoricalAccuracy(5, name="top-5-accuracy"),
# ],
)
return model
|