novas bibliotecas

This commit is contained in:
kl3z00
2026-10-27 09:14:16 +00:00
parent 8b91a01e60
commit 06b8812a2b
3 changed files with 248 additions and 129 deletions
+15 -2
View File
@@ -1,8 +1,21 @@
from .dataset import extract_features
from .dataset import (
extract_features,
rename_generated_images,
rename_real_images,
)
from .plotting import line_plot
from .plotting_bars import bar_plot
from .plotting_scatter import scatter_plot
from .plotting_correlation import correlation_plot
from .plotting_box import box_plot
__all__ = ["extract_features", "line_plot", "bar_plot", "scatter_plot", "correlation_plot", "box_plot"]
__all__ = [
"extract_features",
"rename_real_images",
"rename_generated_images",
"line_plot",
"bar_plot",
"scatter_plot",
"correlation_plot",
"box_plot",
]
+47 -1
View File
@@ -1,10 +1,56 @@
from pathlib import Path
from uuid import uuid4
import pandas as pd
from .features import extract_features as extract_image_features
from tqdm import tqdm
support_ext = {".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff", ".webp"}
def _rename_images(folder_path, prefix):
folder = Path(folder_path)
if not folder.exists():
raise FileNotFoundError(f"Folder not found: {folder}")
if not folder.is_dir():
raise NotADirectoryError(f"Expected a folder: {folder}")
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(),
)
# Stage every file first so existing names such as real1.jpg cannot collide.
staged_images = []
for image in images:
temporary_path = folder / f".spectralforensics-{uuid4().hex}{image.suffix}"
image.rename(temporary_path)
staged_images.append((temporary_path, image.suffix.lower()))
renamed_images = []
for index, (temporary_path, suffix) in enumerate(staged_images, start=1):
new_path = folder / f"{prefix}{index}{suffix}"
temporary_path.rename(new_path)
renamed_images.append(new_path)
print(f"{len(renamed_images)} images renamed with the '{prefix}' prefix.")
return renamed_images
def rename_real_images(folder_path):
"""Rename supported images sequentially as real1, real2, and so on."""
return _rename_images(folder_path, "real")
def rename_generated_images(folder_path):
"""Rename supported images sequentially as gen1, gen2, and so on."""
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):
path = Path(path)
if not path.exists():
@@ -25,4 +71,4 @@ def extract_features(path, output_csv=None, image_size=(255, 255), high_freq_thr
df = pd.DataFrame(results)
if output_csv:
df.to_csv(output_csv, index=False, encoding="utf-8-sig")
return df
return df