-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.py
More file actions
78 lines (53 loc) · 2.65 KB
/
Program.py
File metadata and controls
78 lines (53 loc) · 2.65 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import json
import os
import random
import tempfile
import time
import requests
from Time_Stamp_Extractor import TimeStampExtractor
from Frame_Extractor import FrameExtractor
from Layout_Model import LayoutModel
script_dir = os.path.dirname(os.path.abspath(__file__))
# create directories for data
lectures_data_path = os.path.join(script_dir, "lectures.json")
with open(lectures_data_path, "r", encoding="utf-8") as f:
lectures_data = json.load(f)
videos_with_frames = []
for index, lecture in enumerate(lectures_data):
video = random.sample(lecture["videos"], 1)[0]
lecture_name = lecture["lecture"]
lecture_video_name = video["video_name"]
headers = {
"Cookie": "JSESSIONID=node01ddxo7huujd5sufziwqk9ck2q8109666.node0",
}
response = requests.get(video["path"], headers=headers, stream=True)
if response.status_code != 200:
raise Exception(f"Failed to download video from {video['path']}, status code: {response.status_code}")
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=True) as temp_file:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
temp_file.write(chunk)
temp_file.flush()
temp_path = temp_file.name
print("temp file")
# extract the time stamps (frame indices) at which the slides are changing
timeExtractor = TimeStampExtractor(lecture_name=lecture_name, video_id=video["id"], video_path=temp_file.name, sample_rate = 0.2)
slideChanges = timeExtractor.extract_timestamps()
print("time extractor")
# extract the frames at the previoiusly defined indices
frameExtractor = FrameExtractor(lecture_name=lecture_name, video_id=video["id"], video_path=temp_file.name)
frames = frameExtractor.get_frames(slideChanges)
time.sleep(2)
print("frame extractor")
random_frames = random.sample(frames, 4)
# Add frames into the video dict (without mutating original lecture data)
video_with_frames = dict(video) # make a shallow copy
video_with_frames["frames"] = random_frames
videos_with_frames.append(video_with_frames)
# run layout detection model and store png + json results
layoutDetector = LayoutModel(frames_list=random_frames, lecture_name=lecture_name, video_id = video["id"])
layoutDetector.run_all_frames()
# save results into a combined JSON file
output_path = os.path.join(script_dir, "selected_frames.json")
with open(output_path, "w", encoding="utf-8") as f:
json.dump(videos_with_frames, f, indent=4, ensure_ascii=False)