Salimshakeel commited on
Commit
7cd255e
·
1 Parent(s): 1ff63b5
Files changed (2) hide show
  1. Dockerfile +6 -4
  2. services/summarizer.py +28 -14
Dockerfile CHANGED
@@ -1,6 +1,3 @@
1
- # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
- # You will also find guides on how best to write your Dockerfile
3
-
4
  FROM python:3.12-slim
5
 
6
  ENV TORCH_HOME=/tmp/torch_cache
@@ -9,7 +6,12 @@ WORKDIR /code
9
 
10
  COPY ./requirements.txt /code/requirements.txt
11
 
12
- RUN apt-get update && apt-get install -y libgl1 libglib2.0-0
 
 
 
 
 
13
 
14
  RUN pip install --no-cache-dir --upgrade pip \
15
  && pip install --no-cache-dir -r /code/requirements.txt
 
 
 
 
1
  FROM python:3.12-slim
2
 
3
  ENV TORCH_HOME=/tmp/torch_cache
 
6
 
7
  COPY ./requirements.txt /code/requirements.txt
8
 
9
+ # Install ffmpeg along with other dependencies
10
+ RUN apt-get update && apt-get install -y \
11
+ libgl1 \
12
+ libglib2.0-0 \
13
+ ffmpeg \
14
+ && apt-get clean && rm -rf /var/lib/apt/lists/*
15
 
16
  RUN pip install --no-cache-dir --upgrade pip \
17
  && pip install --no-cache-dir -r /code/requirements.txt
services/summarizer.py CHANGED
@@ -2,6 +2,9 @@ import cv2
2
  import torch
3
  from config import SCORE_THRESHOLD
4
  from services.model_loader import load_model
 
 
 
5
 
6
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
7
  model = load_model("Model/epoch-199.pkl")
@@ -20,13 +23,12 @@ def get_scores(features):
20
  return scores
21
 
22
  def get_selected_indices(scores, picks, threshold=SCORE_THRESHOLD):
 
23
  print("Threshold for selection:", threshold)
24
- print("Scores:", scores.shape, scores)
25
- print("Picks:", picks.shape, picks)
26
- return [picks[i] for i, score in enumerate(scores) if score >= threshold]
27
-
28
- import subprocess
29
- import os
30
 
31
  def save_summary_video(video_path, selected_indices, output_path, fps=15):
32
  import cv2
@@ -34,24 +36,36 @@ def save_summary_video(video_path, selected_indices, output_path, fps=15):
34
  cap = cv2.VideoCapture(video_path)
35
  selected = set(selected_indices)
36
  frame_id = 0
37
- frames = {}
38
 
39
  while cap.isOpened():
40
  ret, frame = cap.read()
41
  if not ret:
42
  break
43
  if frame_id in selected:
44
- frames[frame_id] = frame
45
  frame_id += 1
46
  cap.release()
47
 
48
- if not frames:
49
  print("No frames selected.")
50
  return
51
 
52
- h, w, _ = list(frames.values())[0].shape
 
 
 
 
 
 
 
 
 
53
 
54
- writer = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
55
- for fid in sorted(frames.keys()):
56
- writer.write(frames[fid])
57
- writer.release()
 
 
 
 
2
  import torch
3
  from config import SCORE_THRESHOLD
4
  from services.model_loader import load_model
5
+ import subprocess
6
+ import os
7
+ import numpy as np
8
 
9
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10
  model = load_model("Model/epoch-199.pkl")
 
23
  return scores
24
 
25
  def get_selected_indices(scores, picks, threshold=SCORE_THRESHOLD):
26
+ selected = [picks[i] for i, score in enumerate(scores) if score >= threshold]
27
  print("Threshold for selection:", threshold)
28
+ print("Scores:", len(scores), scores)
29
+ print("Picks:", len(picks), picks)
30
+ print("Selected indices:", len(selected), selected)
31
+ return selected
 
 
32
 
33
  def save_summary_video(video_path, selected_indices, output_path, fps=15):
34
  import cv2
 
36
  cap = cv2.VideoCapture(video_path)
37
  selected = set(selected_indices)
38
  frame_id = 0
39
+ frames = []
40
 
41
  while cap.isOpened():
42
  ret, frame = cap.read()
43
  if not ret:
44
  break
45
  if frame_id in selected:
46
+ frames.append(frame)
47
  frame_id += 1
48
  cap.release()
49
 
50
+ if len(frames) == 0:
51
  print("No frames selected.")
52
  return
53
 
54
+ h, w, _ = frames[0].shape
55
+ print("Video dimensions:", w, h)
56
+
57
+ out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (h, w))
58
+ for frame in frames:
59
+ out.write(frame)
60
+ out.release()
61
+
62
+ print("Fixing the video with ffmpeg")
63
+ # fix_video_with_ffmpeg(output_path)
64
 
65
+ def fix_video_with_ffmpeg(path):
66
+ temp_path = path + ".fixed.mp4"
67
+ subprocess.run([
68
+ "ffmpeg", "-y", "-i", path,
69
+ "-vcodec", "libx264", "-pix_fmt", "yuv420p", temp_path
70
+ ])
71
+ os.replace(temp_path, path)