thinh-huynh-re commited on
Commit
ebc167e
·
1 Parent(s): 53628ab
Files changed (2) hide show
  1. app.py +28 -2
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,13 +1,13 @@
1
  import os
2
  from typing import List, Tuple
3
  import multiprocessing
 
4
 
5
  import numpy as np
6
  import pandas as pd
7
  import streamlit as st
8
  import torch
9
  from torch import Tensor
10
- from decord import VideoReader, cpu
11
  from transformers import AutoFeatureExtractor, TimesformerForVideoClassification
12
 
13
  np.random.seed(0)
@@ -49,7 +49,27 @@ def load_model(model_name: str):
49
  return feature_extractor, model
50
 
51
 
52
- def inference(file_path: str):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  videoreader = VideoReader(VIDEO_TMP_PATH, num_threads=1, ctx=cpu(0))
54
 
55
  # sample 8 frames
@@ -58,6 +78,12 @@ def inference(file_path: str):
58
  clip_len=8, frame_sample_rate=4, seg_len=len(videoreader)
59
  )
60
  video = videoreader.get_batch(indices).asnumpy()
 
 
 
 
 
 
61
 
62
  inputs = feature_extractor(list(video), return_tensors="pt")
63
 
 
1
  import os
2
  from typing import List, Tuple
3
  import multiprocessing
4
+ import cv2
5
 
6
  import numpy as np
7
  import pandas as pd
8
  import streamlit as st
9
  import torch
10
  from torch import Tensor
 
11
  from transformers import AutoFeatureExtractor, TimesformerForVideoClassification
12
 
13
  np.random.seed(0)
 
49
  return feature_extractor, model
50
 
51
 
52
+ def read_video(file_path: str) -> np.ndarray:
53
+ cap = cv2.VideoCapture(file_path)
54
+ length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) # 1000 frames
55
+ print("Number of frames", length)
56
+
57
+ indices = sample_frame_indices(clip_len=8, frame_sample_rate=4, seg_len=length)
58
+
59
+ frames: List[np.array] = []
60
+ for i in indices:
61
+ cap.set(1, i)
62
+ ret, frame = cap.read()
63
+ if not ret:
64
+ continue
65
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
66
+ frames.append(frame)
67
+ return np.array(frames)
68
+
69
+
70
+ def read_video_decord(file_path: str) -> np.ndarray:
71
+ from decord import VideoReader, cpu
72
+
73
  videoreader = VideoReader(VIDEO_TMP_PATH, num_threads=1, ctx=cpu(0))
74
 
75
  # sample 8 frames
 
78
  clip_len=8, frame_sample_rate=4, seg_len=len(videoreader)
79
  )
80
  video = videoreader.get_batch(indices).asnumpy()
81
+ # print(video.shape) # (8, 720, 1280, 3)
82
+ return video
83
+
84
+
85
+ def inference(file_path: str):
86
+ video = read_video(file_path)
87
 
88
  inputs = feature_extractor(list(video), return_tensors="pt")
89
 
requirements.txt CHANGED
@@ -1,5 +1,6 @@
1
  streamlit
2
  transformers
3
  torch
4
- decord
5
  black
 
 
1
  streamlit
2
  transformers
3
  torch
4
+ # decord
5
  black
6
+ opencv-python