Spaces:
Running
Running
Vinh Vu commited on
Commit ·
04a8c82
1
Parent(s): 4a7e2c6
Update to loop all folder - convert video to img
Browse files- 00-convert_video_to_image.py +64 -50
00-convert_video_to_image.py
CHANGED
|
@@ -3,60 +3,74 @@ import os
|
|
| 3 |
import cv2
|
| 4 |
import math
|
| 5 |
|
| 6 |
-
base_path = '.\\train_sample_videos\\'
|
| 7 |
-
videos_path = os.path.join(base_path, 'Deepfakes')
|
| 8 |
|
| 9 |
def get_filename_only(file_path):
|
| 10 |
file_basename = os.path.basename(file_path)
|
| 11 |
filename_only = file_basename.split('.')[0]
|
| 12 |
return filename_only
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
metadata[row['File Path']] = row['Label'].strip().upper()
|
| 19 |
-
print(len(metadata))
|
| 20 |
-
|
| 21 |
-
for filename in metadata.keys():
|
| 22 |
-
video_basename = os.path.basename(filename)
|
| 23 |
-
print(video_basename)
|
| 24 |
-
if (video_basename.endswith(".mp4")):
|
| 25 |
-
tmp_path = os.path.join(videos_path, get_filename_only(video_basename))
|
| 26 |
-
print('Creating Directory: ' + tmp_path)
|
| 27 |
-
os.makedirs(tmp_path, exist_ok=True)
|
| 28 |
-
print('Converting Video to Images...')
|
| 29 |
-
count = 0
|
| 30 |
-
video_file = os.path.join(videos_path, video_basename)
|
| 31 |
-
cap = cv2.VideoCapture(video_file)
|
| 32 |
-
frame_rate = cap.get(5) #frame rate
|
| 33 |
-
while(cap.isOpened()):
|
| 34 |
-
frame_id = cap.get(1) #current frame number
|
| 35 |
-
ret, frame = cap.read()
|
| 36 |
-
if (ret != True):
|
| 37 |
-
break
|
| 38 |
-
if (frame_id % math.floor(frame_rate) == 0):
|
| 39 |
-
print('Original Dimensions: ', frame.shape)
|
| 40 |
-
if frame.shape[1] < 300:
|
| 41 |
-
scale_ratio = 2
|
| 42 |
-
elif frame.shape[1] > 1900:
|
| 43 |
-
scale_ratio = 0.33
|
| 44 |
-
elif frame.shape[1] > 1000 and frame.shape[1] <= 1900 :
|
| 45 |
-
scale_ratio = 0.5
|
| 46 |
-
else:
|
| 47 |
-
scale_ratio = 1
|
| 48 |
-
print('Scale Ratio: ', scale_ratio)
|
| 49 |
-
|
| 50 |
-
width = int(frame.shape[1] * scale_ratio)
|
| 51 |
-
height = int(frame.shape[0] * scale_ratio)
|
| 52 |
-
dim = (width, height)
|
| 53 |
-
new_frame = cv2.resize(frame, dim, interpolation = cv2.INTER_AREA)
|
| 54 |
-
print('Resized Dimensions: ', new_frame.shape)
|
| 55 |
-
|
| 56 |
-
new_filename = '{}-{:03d}.png'.format(os.path.join(tmp_path, get_filename_only(filename)), count)
|
| 57 |
-
count = count + 1
|
| 58 |
-
cv2.imwrite(new_filename, new_frame)
|
| 59 |
-
cap.release()
|
| 60 |
-
print("Done!")
|
| 61 |
-
else:
|
| 62 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import cv2
|
| 4 |
import math
|
| 5 |
|
| 6 |
+
base_path = '.\\train_sample_videos\\FaceForensics++_C23\\'
|
|
|
|
| 7 |
|
| 8 |
def get_filename_only(file_path):
|
| 9 |
file_basename = os.path.basename(file_path)
|
| 10 |
filename_only = file_basename.split('.')[0]
|
| 11 |
return filename_only
|
| 12 |
|
| 13 |
+
# Iterate over all subfolders in base_path (excluding 'csv')
|
| 14 |
+
for folder_name in sorted(os.listdir(base_path)):
|
| 15 |
+
folder_path = os.path.join(base_path, folder_name)
|
| 16 |
+
if not os.path.isdir(folder_path) or folder_name == 'csv':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
continue
|
| 18 |
+
|
| 19 |
+
csv_file = os.path.join(base_path, 'csv', folder_name + '.csv')
|
| 20 |
+
if not os.path.isfile(csv_file):
|
| 21 |
+
print(f'CSV not found for {folder_name}, skipping: {csv_file}')
|
| 22 |
+
continue
|
| 23 |
+
|
| 24 |
+
print(f'\n{"="*60}')
|
| 25 |
+
print(f'Processing folder: {folder_name}')
|
| 26 |
+
print(f'{"="*60}')
|
| 27 |
+
|
| 28 |
+
with open(csv_file, newline='', encoding='utf-8') as csvfile:
|
| 29 |
+
reader = csv.DictReader(csvfile)
|
| 30 |
+
metadata = {}
|
| 31 |
+
for row in reader:
|
| 32 |
+
metadata[row['File Path']] = row['Label'].strip().upper()
|
| 33 |
+
print(f'{folder_name}: {len(metadata)} entries')
|
| 34 |
+
|
| 35 |
+
for filename in metadata.keys():
|
| 36 |
+
video_basename = os.path.basename(filename)
|
| 37 |
+
print(video_basename)
|
| 38 |
+
if (video_basename.endswith(".mp4")):
|
| 39 |
+
tmp_path = os.path.join(folder_path, get_filename_only(video_basename))
|
| 40 |
+
print('Creating Directory: ' + tmp_path)
|
| 41 |
+
os.makedirs(tmp_path, exist_ok=True)
|
| 42 |
+
print('Converting Video to Images...')
|
| 43 |
+
count = 0
|
| 44 |
+
video_file = os.path.join(folder_path, video_basename)
|
| 45 |
+
cap = cv2.VideoCapture(video_file)
|
| 46 |
+
frame_rate = cap.get(5) #frame rate
|
| 47 |
+
while(cap.isOpened()):
|
| 48 |
+
frame_id = cap.get(1) #current frame number
|
| 49 |
+
ret, frame = cap.read()
|
| 50 |
+
if (ret != True):
|
| 51 |
+
break
|
| 52 |
+
if (frame_id % math.floor(frame_rate) == 0):
|
| 53 |
+
print('Original Dimensions: ', frame.shape)
|
| 54 |
+
if frame.shape[1] < 300:
|
| 55 |
+
scale_ratio = 2
|
| 56 |
+
elif frame.shape[1] > 1900:
|
| 57 |
+
scale_ratio = 0.33
|
| 58 |
+
elif frame.shape[1] > 1000 and frame.shape[1] <= 1900 :
|
| 59 |
+
scale_ratio = 0.5
|
| 60 |
+
else:
|
| 61 |
+
scale_ratio = 1
|
| 62 |
+
print('Scale Ratio: ', scale_ratio)
|
| 63 |
+
|
| 64 |
+
width = int(frame.shape[1] * scale_ratio)
|
| 65 |
+
height = int(frame.shape[0] * scale_ratio)
|
| 66 |
+
dim = (width, height)
|
| 67 |
+
new_frame = cv2.resize(frame, dim, interpolation = cv2.INTER_AREA)
|
| 68 |
+
print('Resized Dimensions: ', new_frame.shape)
|
| 69 |
+
|
| 70 |
+
new_filename = '{}-{:03d}.png'.format(os.path.join(tmp_path, get_filename_only(filename)), count)
|
| 71 |
+
count = count + 1
|
| 72 |
+
cv2.imwrite(new_filename, new_frame)
|
| 73 |
+
cap.release()
|
| 74 |
+
print("Done!")
|
| 75 |
+
else:
|
| 76 |
+
continue
|