Spaces:
Running
Running
Vinh Vu commited on
Commit ·
4e15caf
1
Parent(s): 0bccca4
Update code
Browse files- 01-crop_faces_with_mtcnn.py +2 -1
- 02-prepare_fake_real_dataset.py +37 -22
01-crop_faces_with_mtcnn.py
CHANGED
|
@@ -14,6 +14,8 @@ if physical_devices:
|
|
| 14 |
|
| 15 |
base_path = '.\\train_sample_videos\\FaceForensics++_C23\\'
|
| 16 |
|
|
|
|
|
|
|
| 17 |
def get_filename_only(file_path):
|
| 18 |
file_basename = os.path.basename(file_path)
|
| 19 |
filename_only = file_basename.split('.')[0]
|
|
@@ -60,7 +62,6 @@ for folder_name in sorted(os.listdir(base_path)):
|
|
| 60 |
for frame in frame_images:
|
| 61 |
print('Processing ', frame)
|
| 62 |
try:
|
| 63 |
-
detector = MTCNN()
|
| 64 |
image = cv2.cvtColor(cv2.imread(os.path.join(tmp_path, frame)), cv2.COLOR_BGR2RGB)
|
| 65 |
results = detector.detect_faces(image)
|
| 66 |
except Exception as e:
|
|
|
|
| 14 |
|
| 15 |
base_path = '.\\train_sample_videos\\FaceForensics++_C23\\'
|
| 16 |
|
| 17 |
+
detector = MTCNN()
|
| 18 |
+
|
| 19 |
def get_filename_only(file_path):
|
| 20 |
file_basename = os.path.basename(file_path)
|
| 21 |
filename_only = file_basename.split('.')[0]
|
|
|
|
| 62 |
for frame in frame_images:
|
| 63 |
print('Processing ', frame)
|
| 64 |
try:
|
|
|
|
| 65 |
image = cv2.cvtColor(cv2.imread(os.path.join(tmp_path, frame)), cv2.COLOR_BGR2RGB)
|
| 66 |
results = detector.detect_faces(image)
|
| 67 |
except Exception as e:
|
02-prepare_fake_real_dataset.py
CHANGED
|
@@ -7,7 +7,7 @@ from PIL import Image
|
|
| 7 |
|
| 8 |
MIN_IMAGE_SIZE = 90 # minimum width and height in pixels
|
| 9 |
|
| 10 |
-
base_path = '.\\train_sample_videos\\
|
| 11 |
dataset_path = '.\\prepared_dataset\\'
|
| 12 |
print('Creating Directory: ' + dataset_path)
|
| 13 |
os.makedirs(dataset_path, exist_ok=True)
|
|
@@ -43,13 +43,6 @@ def copy_large_faces(src_dir, dst_dir):
|
|
| 43 |
skipped += 1
|
| 44 |
except Exception:
|
| 45 |
skipped += 1
|
| 46 |
-
|
| 47 |
-
with open(os.path.join('.\\train_sample_videos\\csv\\', 'Deepfakes.csv'), newline='', encoding='utf-8') as csvfile:
|
| 48 |
-
reader = csv.DictReader(csvfile)
|
| 49 |
-
metadata = {}
|
| 50 |
-
for row in reader:
|
| 51 |
-
metadata[row['File Path']] = row['Label'].strip().upper()
|
| 52 |
-
print(len(metadata))
|
| 53 |
|
| 54 |
real_path = os.path.join(dataset_path, 'real')
|
| 55 |
print('Creating Directory: ' + real_path)
|
|
@@ -63,20 +56,42 @@ if os.path.exists(fake_path):
|
|
| 63 |
shutil.rmtree(fake_path)
|
| 64 |
os.makedirs(fake_path)
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
all_real_faces = [f for f in os.listdir(real_path) if os.path.isfile(os.path.join(real_path, f))]
|
| 82 |
print('Total Number of Real faces: ', len(all_real_faces))
|
|
|
|
| 7 |
|
| 8 |
MIN_IMAGE_SIZE = 90 # minimum width and height in pixels
|
| 9 |
|
| 10 |
+
base_path = '.\\train_sample_videos\\FaceForensics++_C23\\'
|
| 11 |
dataset_path = '.\\prepared_dataset\\'
|
| 12 |
print('Creating Directory: ' + dataset_path)
|
| 13 |
os.makedirs(dataset_path, exist_ok=True)
|
|
|
|
| 43 |
skipped += 1
|
| 44 |
except Exception:
|
| 45 |
skipped += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
real_path = os.path.join(dataset_path, 'real')
|
| 48 |
print('Creating Directory: ' + real_path)
|
|
|
|
| 56 |
shutil.rmtree(fake_path)
|
| 57 |
os.makedirs(fake_path)
|
| 58 |
|
| 59 |
+
# Iterate over all subfolders in FaceForensics++_C23 (excluding 'csv')
|
| 60 |
+
for folder_name in sorted(os.listdir(base_path)):
|
| 61 |
+
folder_path = os.path.join(base_path, folder_name)
|
| 62 |
+
if not os.path.isdir(folder_path) or folder_name == 'csv':
|
| 63 |
+
continue
|
| 64 |
+
|
| 65 |
+
csv_file = os.path.join(base_path, 'csv', folder_name + '.csv')
|
| 66 |
+
if not os.path.isfile(csv_file):
|
| 67 |
+
print(f'CSV not found for {folder_name}, skipping: {csv_file}')
|
| 68 |
+
continue
|
| 69 |
+
|
| 70 |
+
print(f'\n{"="*60}')
|
| 71 |
+
print(f'Processing folder: {folder_name}')
|
| 72 |
+
print(f'{"="*60}')
|
| 73 |
+
|
| 74 |
+
with open(csv_file, newline='', encoding='utf-8') as csvfile:
|
| 75 |
+
reader = csv.DictReader(csvfile)
|
| 76 |
+
metadata = {}
|
| 77 |
+
for row in reader:
|
| 78 |
+
metadata[row['File Path']] = row['Label'].strip().upper()
|
| 79 |
+
print(f'{folder_name}: {len(metadata)} entries')
|
| 80 |
+
|
| 81 |
+
for filename, label in metadata.items():
|
| 82 |
+
print(filename)
|
| 83 |
+
print(label)
|
| 84 |
+
tmp_path = os.path.join(os.path.join(folder_path, get_filename_only(filename)), 'faces')
|
| 85 |
+
print(tmp_path)
|
| 86 |
+
if os.path.exists(tmp_path):
|
| 87 |
+
if label == 'REAL':
|
| 88 |
+
print('Copying to :' + real_path)
|
| 89 |
+
copy_large_faces(tmp_path, real_path)
|
| 90 |
+
elif label == 'FAKE':
|
| 91 |
+
print('Copying to :' + tmp_fake_path)
|
| 92 |
+
copy_large_faces(tmp_path, tmp_fake_path)
|
| 93 |
+
else:
|
| 94 |
+
print('Ignored..')
|
| 95 |
|
| 96 |
all_real_faces = [f for f in os.listdir(real_path) if os.path.isfile(os.path.join(real_path, f))]
|
| 97 |
print('Total Number of Real faces: ', len(all_real_faces))
|