asigalov61
commited on
Update README.md
Browse files
README.md
CHANGED
@@ -48,6 +48,131 @@ snapshot_download(repo_id, repo_type=repo_type, local_dir=local_dir)
|
|
48 |
```
|
49 |
|
50 |
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
```
|
52 |
|
53 |
***
|
|
|
48 |
```
|
49 |
|
50 |
```python
|
51 |
+
import cv2
|
52 |
+
import os
|
53 |
+
from tqdm import tqdm
|
54 |
+
|
55 |
+
#===============================================================================================
|
56 |
+
|
57 |
+
def scan_videos(directory, videos_extensions=['.mkv', '.mp4', '.avi']):
|
58 |
+
|
59 |
+
video_files = [os.path.join(directory, f) for f in os.listdir(directory) if os.path.splitext(f)[1].lower() in videos_extensions]
|
60 |
+
|
61 |
+
return video_files
|
62 |
+
|
63 |
+
def extract_frames(video_path,
|
64 |
+
output_folder,
|
65 |
+
interval=0.1,
|
66 |
+
square_size=480,
|
67 |
+
scale_size=128,
|
68 |
+
images_ext='.jpg'
|
69 |
+
):
|
70 |
+
|
71 |
+
if not os.path.exists(output_folder):
|
72 |
+
os.makedirs(output_folder)
|
73 |
+
|
74 |
+
cap = cv2.VideoCapture(video_path)
|
75 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
76 |
+
frame_interval = int(fps * interval)
|
77 |
+
frame_count = 0
|
78 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
79 |
+
|
80 |
+
print('Video file:', os.path.basename(video_path))
|
81 |
+
|
82 |
+
with tqdm(total=total_frames, desc='Extracting frames') as pbar:
|
83 |
+
|
84 |
+
while True:
|
85 |
+
|
86 |
+
ret, frame = cap.read()
|
87 |
+
|
88 |
+
if not ret:
|
89 |
+
break
|
90 |
+
|
91 |
+
if frame_count % frame_interval == 0:
|
92 |
+
|
93 |
+
# Calculate the coordinates for cropping the center square
|
94 |
+
height, width = frame.shape[:2]
|
95 |
+
center_y, center_x = height // 2, width // 2
|
96 |
+
half_size = square_size // 2
|
97 |
+
top_left_x = max(center_x - half_size, 0)
|
98 |
+
top_left_y = max(center_y - half_size, 0)
|
99 |
+
bottom_right_x = min(center_x + half_size, width)
|
100 |
+
bottom_right_y = min(center_y + half_size, height)
|
101 |
+
|
102 |
+
square_frame = frame[top_left_y:bottom_right_y, top_left_x:bottom_right_x]
|
103 |
+
|
104 |
+
# Normalize brightness and contrast
|
105 |
+
normalized_frame = cv2.normalize(square_frame, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
|
106 |
+
|
107 |
+
# Resize
|
108 |
+
resized_frame = cv2.resize(normalized_frame, (scale_size, scale_size))
|
109 |
+
|
110 |
+
frame_name = os.path.join(output_folder, f"frame_{frame_count}{images_ext}")
|
111 |
+
cv2.imwrite(frame_name, resized_frame)
|
112 |
+
|
113 |
+
frame_count += 1
|
114 |
+
|
115 |
+
pbar.update(1)
|
116 |
+
|
117 |
+
cap.release()
|
118 |
+
|
119 |
+
print(f"Frames extracted to {output_folder}")
|
120 |
+
|
121 |
+
#===============================================================================================
|
122 |
+
|
123 |
+
videos_dir = 'Videos'
|
124 |
+
videos_extensions = ['.mkv', '.mp4', '.avi']
|
125 |
+
|
126 |
+
frames_output_dir = 'Output'
|
127 |
+
frames_extraction_interval = 0.1 # FPS * frames_extraction_interval
|
128 |
+
original_frame_size = 480
|
129 |
+
final_frame_size = 128
|
130 |
+
output_frames_extension = '.jpg'
|
131 |
+
|
132 |
+
#===============================================================================================
|
133 |
+
|
134 |
+
print('=' * 70)
|
135 |
+
print('Scanning videos dir...')
|
136 |
+
video_files = scan_videos(videos_dir)
|
137 |
+
print('Done!')
|
138 |
+
|
139 |
+
print('=' * 70)
|
140 |
+
print('Found', len(video_files), 'video files')
|
141 |
+
print('=' * 70)
|
142 |
+
|
143 |
+
print('Starting extraction...')
|
144 |
+
print('=' * 70)
|
145 |
+
|
146 |
+
for video in video_files:
|
147 |
+
|
148 |
+
extract_frames(video,
|
149 |
+
os.path.join(frames_output_dir, os.path.splitext(os.path.basename(video))[0]),
|
150 |
+
frames_extraction_interval,
|
151 |
+
original_frame_size,
|
152 |
+
final_frame_size,
|
153 |
+
output_frames_extension
|
154 |
+
)
|
155 |
+
|
156 |
+
print('=' * 70)
|
157 |
+
|
158 |
+
print('Extraction finished!')
|
159 |
+
print('=' * 70)
|
160 |
+
|
161 |
+
print('Scanning for extracted frames...')
|
162 |
+
|
163 |
+
frames_list = list()
|
164 |
+
|
165 |
+
for (dirpath, dirnames, filenames) in os.walk(frames_output_dir):
|
166 |
+
frames_list += [os.path.join(dirpath, file) for file in filenames if file.endswith(output_frames_extension)]
|
167 |
+
|
168 |
+
print('Done!')
|
169 |
+
print('=' * 70)
|
170 |
+
|
171 |
+
print('Found', len(frames_list), 'video frames')
|
172 |
+
print('=' * 70)
|
173 |
+
|
174 |
+
print('Done!')
|
175 |
+
print('=' * 70)
|
176 |
```
|
177 |
|
178 |
***
|