Jiqing commited on
Commit
da66119
1 Parent(s): 45d8577

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +34 -7
README.md CHANGED
@@ -33,7 +33,7 @@ import cv2
33
  import numpy as np
34
  import torch
35
  from huggingface_hub import hf_hub_download
36
- from transformers import AutoProcessor, AutoModel
37
 
38
 
39
  def pyav_decode(container, sampling_rate, num_frames, clip_idx, num_clips, target_fps):
@@ -85,7 +85,7 @@ def decode(container, sampling_rate, num_frames, clip_idx, num_clips, target_fps
85
  Returns:
86
  frames (tensor): decoded frames from the video.
87
  """
88
- assert clip_idx >= -2, "Not valied clip_idx {}".format(clip_idx)
89
  frames, fps = pyav_decode(container, sampling_rate, num_frames, clip_idx, num_clips, target_fps)
90
  clip_size = sampling_rate * num_frames / target_fps * fps
91
  index = torch.linspace(0, clip_size - 1, num_frames)
@@ -96,10 +96,33 @@ def decode(container, sampling_rate, num_frames, clip_idx, num_clips, target_fps
96
 
97
  return frames
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
- file = hf_hub_download(repo_id="Intel/tvp_demo", filename="0A8ZT.mp4", repo_type="dataset")
101
 
102
- model = AutoModel.from_pretrained("Intel/tvp-base")
103
 
104
  decoder_kwargs = dict(
105
  container=av.open(file, metadata_errors="ignore"),
@@ -112,11 +135,15 @@ decoder_kwargs = dict(
112
  raw_sampled_frms = decode(**decoder_kwargs)
113
  raw_sampled_frms = raw_sampled_frms.permute(0, 3, 1, 2)
114
 
 
115
  processor = AutoProcessor.from_pretrained("Intel/tvp-base")
 
116
  data = processor(
117
- text=["person turn a light on."], videos=list(raw_sampled_frms.numpy()), return_tensors="pt", max_text_length=100
118
  )
119
 
 
 
120
  output = model(**data)
121
 
122
  print(f"The model's output is {output}")
@@ -125,7 +152,7 @@ def get_video_duration(filename):
125
  cap = cv2.VideoCapture(filename)
126
  if cap.isOpened():
127
  rate = cap.get(5)
128
- frame_num =cap.get(7)
129
  duration = frame_num/rate
130
  return duration
131
  return -1
@@ -133,7 +160,7 @@ def get_video_duration(filename):
133
  duration = get_video_duration(file)
134
  timestamp = output['logits'].tolist()
135
  start, end = round(timestamp[0][0]*duration, 1), round(timestamp[0][1]*duration, 1)
136
- print(f"The time slot of the video corresponding to the text is from {start}s to {end}s")
137
  ```
138
 
139
  ### Limitations and bias
 
33
  import numpy as np
34
  import torch
35
  from huggingface_hub import hf_hub_download
36
+ from transformers import AutoProcessor, TvpForVideoGrounding
37
 
38
 
39
  def pyav_decode(container, sampling_rate, num_frames, clip_idx, num_clips, target_fps):
 
85
  Returns:
86
  frames (tensor): decoded frames from the video.
87
  """
88
+ assert clip_idx >= -2, "Not a valied clip_idx {}".format(clip_idx)
89
  frames, fps = pyav_decode(container, sampling_rate, num_frames, clip_idx, num_clips, target_fps)
90
  clip_size = sampling_rate * num_frames / target_fps * fps
91
  index = torch.linspace(0, clip_size - 1, num_frames)
 
96
 
97
  return frames
98
 
99
+ def get_resize_size(image, max_size):
100
+ """
101
+ Args:
102
+ image: np.ndarray
103
+ max_size: The max size of height and width
104
+
105
+ Returns:
106
+ (height, width)
107
+ Note the height/width order difference >>> pil_img = Image.open("raw_img_tensor.jpg") >>> pil_img.size (640,
108
+ 480) # (width, height) >>> np_img = np.array(pil_img) >>> np_img.shape (480, 640, 3) # (height, width, 3)
109
+ """
110
+ height, width = image.shape[-2:]
111
+
112
+ if height >= width:
113
+ ratio = width * 1.0 / height
114
+ new_height = max_size
115
+ new_width = new_height * ratio
116
+ else:
117
+ ratio = height * 1.0 / width
118
+ new_width = max_size
119
+ new_height = new_width * ratio
120
+ size = {"height": int(new_height), "width": int(new_width)}
121
+ return size
122
 
123
+ file = hf_hub_download(repo_id="Intel/tvp_demo", filename="3MSZA.mp4", repo_type="dataset")
124
 
125
+ model = TvpForVideoGrounding.from_pretrained("Intel/tvp-base")
126
 
127
  decoder_kwargs = dict(
128
  container=av.open(file, metadata_errors="ignore"),
 
135
  raw_sampled_frms = decode(**decoder_kwargs)
136
  raw_sampled_frms = raw_sampled_frms.permute(0, 3, 1, 2)
137
 
138
+ text = "person turn a light on."
139
  processor = AutoProcessor.from_pretrained("Intel/tvp-base")
140
+ size = get_resize_size(raw_sampled_frms, model.config.max_img_size)
141
  data = processor(
142
+ text=[text], videos=list(raw_sampled_frms.numpy()), return_tensors="pt", max_text_length=100, size=size
143
  )
144
 
145
+ data["pixel_values"] = data["pixel_values"].to(model.dtype)
146
+ data["labels"] = torch.tensor([30.96, 24.3, 30.4])
147
  output = model(**data)
148
 
149
  print(f"The model's output is {output}")
 
152
  cap = cv2.VideoCapture(filename)
153
  if cap.isOpened():
154
  rate = cap.get(5)
155
+ frame_num = cap.get(7)
156
  duration = frame_num/rate
157
  return duration
158
  return -1
 
160
  duration = get_video_duration(file)
161
  timestamp = output['logits'].tolist()
162
  start, end = round(timestamp[0][0]*duration, 1), round(timestamp[0][1]*duration, 1)
163
+ print(f"The time slot of the video corresponding to the text \"{text}\" is from {start}s to {end}s")
164
  ```
165
 
166
  ### Limitations and bias