atualização final
This commit is contained in:
@@ -1,13 +1,10 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from .features import extract_features as extract_image_features
|
from .features import extract_features as extract_image_features
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
|
|
||||||
support_ext = {".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff", ".webp"}
|
support_ext = {".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff", ".webp"}
|
||||||
|
|
||||||
|
|
||||||
def _rename_images(folder_path, prefix):
|
def _rename_images(folder_path, prefix):
|
||||||
folder = Path(folder_path)
|
folder = Path(folder_path)
|
||||||
if not folder.exists():
|
if not folder.exists():
|
||||||
@@ -15,42 +12,27 @@ def _rename_images(folder_path, prefix):
|
|||||||
if not folder.is_dir():
|
if not folder.is_dir():
|
||||||
raise NotADirectoryError(f"Expected a folder: {folder}")
|
raise NotADirectoryError(f"Expected a folder: {folder}")
|
||||||
|
|
||||||
images = sorted(
|
images = sorted((file for file in folder.iterdir()if file.is_file() and file.suffix.lower() in support_ext),
|
||||||
(
|
key=lambda file: file.name.lower(),)
|
||||||
file
|
|
||||||
for file in folder.iterdir()
|
|
||||||
if file.is_file() and file.suffix.lower() in support_ext
|
|
||||||
),
|
|
||||||
key=lambda file: file.name.lower(),
|
|
||||||
)
|
|
||||||
|
|
||||||
# Stage every file first so existing names such as real1.jpg cannot collide.
|
|
||||||
staged_images = []
|
staged_images = []
|
||||||
for image in images:
|
for image in images:
|
||||||
temporary_path = folder / f".spectralforensics-{uuid4().hex}{image.suffix}"
|
temporary_path = folder / f".spectralforensics-{uuid4().hex}{image.suffix}"
|
||||||
image.rename(temporary_path)
|
image.rename(temporary_path)
|
||||||
staged_images.append((temporary_path, image.suffix.lower()))
|
staged_images.append((temporary_path, image.suffix.lower()))
|
||||||
|
|
||||||
renamed_images = []
|
renamed_images = []
|
||||||
for index, (temporary_path, suffix) in enumerate(staged_images, start=1):
|
for index, (temporary_path, suffix) in enumerate(staged_images, start=1):
|
||||||
new_path = folder / f"{prefix}{index}{suffix}"
|
new_path = folder / f"{prefix}{index}{suffix}"
|
||||||
temporary_path.rename(new_path)
|
temporary_path.rename(new_path)
|
||||||
renamed_images.append(new_path)
|
renamed_images.append(new_path)
|
||||||
|
|
||||||
print(f"{len(renamed_images)} images renamed with the '{prefix}' prefix.")
|
print(f"{len(renamed_images)} images renamed with the '{prefix}' prefix.")
|
||||||
return renamed_images
|
return renamed_images
|
||||||
|
|
||||||
|
|
||||||
def rename_real_images(folder_path):
|
def rename_real_images(folder_path):
|
||||||
"""Rename supported images sequentially as real1, real2, and so on."""
|
|
||||||
return _rename_images(folder_path, "real")
|
return _rename_images(folder_path, "real")
|
||||||
|
|
||||||
|
|
||||||
def rename_generated_images(folder_path):
|
def rename_generated_images(folder_path):
|
||||||
"""Rename supported images sequentially as gen1, gen2, and so on."""
|
|
||||||
return _rename_images(folder_path, "gen")
|
return _rename_images(folder_path, "gen")
|
||||||
|
|
||||||
|
|
||||||
def extract_features(path, output_csv=None, image_size=(255, 255), high_freq_threshold=0.65, output="all", show_progress=True):
|
def extract_features(path, output_csv=None, image_size=(255, 255), high_freq_threshold=0.65, output="all", show_progress=True):
|
||||||
path = Path(path)
|
path = Path(path)
|
||||||
if not path.exists():
|
if not path.exists():
|
||||||
|
|||||||
Reference in New Issue
Block a user