Files
obfuscation_platform/project/visualization/visualizer5.py
T

115 lines
3.5 KiB
Python

import os
import json
import re
import matplotlib.pyplot as plt
from collections import defaultdict
from matplotlib.ticker import PercentFormatter
# -------------------
# CONFIG
# -------------------
input_dir = "C:/Users/bijan/Desktop/obfuscation_platform/project/evaluation_results/evaluation_results_50_face_object_analysis/"
output_dir = "C:/Users/bijan/Desktop/obfuscation_platform/project/visual_results/in_paper/face_object_analysis/"
os.makedirs(output_dir, exist_ok=True)
# Font sizes
FONT_SIZE_LABELS = 12
FONT_SIZE_TICKS = 10
FONT_SIZE_TITLE = 14
FONT_SIZE_LEGEND = 10
# Patterns for bars
HATCH_PATTERNS = ["///", "\\\\", "xxx", "...", "\\\\"]
# -------------------
# Regex & data struct
# -------------------
filename_pattern = re.compile(r"results_(.+?)_(.+?)\.json")
data = defaultdict(dict)
# -------------------
# Read all JSON files
# -------------------
for filename in os.listdir(input_dir):
if filename.endswith(".json"):
match = filename_pattern.match(filename)
if not match:
continue
dataset, technik = match.groups()
filepath = os.path.join(input_dir, filename)
with open(filepath, "r", encoding="utf-8") as f:
content = json.load(f)
face_obj = content.get("face-object-insights", {})
with_overlap = face_obj.get(r"\mathrm{Acc}_{\mathrm{det}}^{\mathrm{with\,overlap}}")
without_overlap = face_obj.get(r"\mathrm{Acc}_{\mathrm{det}}^{\mathrm{without\,overlap}}")
data[dataset][technik] = (with_overlap, without_overlap)
# -------------------
# Plot per dataset
# -------------------
for dataset, technik_data in data.items():
print(dataset)
techniken = list(technik_data.keys())
desired_order = ["Black", "Random", "Blur", "Pixel", "Style", "Swap", "Synthetic"]
techniken = [t for t in desired_order if t in technik_data]
with_vals = [technik_data[t][0] for t in techniken]
without_vals = [technik_data[t][1] for t in techniken]
x = range(len(techniken))
bar_width = 0.35
fig, ax = plt.subplots(figsize=(6, 3))
bars1 = ax.bar(
[i - bar_width/2 for i in x],
[v * 100 for v in with_vals], # convert to %
width=bar_width,
label="With overlap",
hatch=HATCH_PATTERNS[0]
)
bars2 = ax.bar(
[i + bar_width/2 for i in x],
[v * 100 for v in without_vals], # convert to %
width=bar_width,
label="Without overlap",
hatch=HATCH_PATTERNS[1]
)
# X-axis
ax.set_xticks(x)
ax.set_xticklabels(techniken, fontsize=FONT_SIZE_TICKS)
# Y-axis
ax.set_ylabel(r"$\mathrm{Acc}_{\mathrm{det}}$", fontsize=FONT_SIZE_LABELS)
ax.tick_params(axis="y", labelsize=FONT_SIZE_TICKS)
ax.yaxis.set_major_formatter(PercentFormatter(xmax=100))
ax.set_ylim(0, 105)
# Title & legend
ax.legend(loc="lower left", fontsize=FONT_SIZE_LEGEND, framealpha=0.9)
# Grid
ax.grid(axis='y', linestyle='--', alpha=0.7)
# Value labels on bars
for bars in [bars1, bars2]:
for bar in bars:
height = bar.get_height()
ax.annotate(f"{height:.0f}",
xy=(bar.get_x() + bar.get_width() / 2, height),
xytext=(0, 3), # offset
textcoords="offset points",
ha='center', va='bottom',
fontsize=FONT_SIZE_TICKS)
plt.tight_layout()
plt.savefig(os.path.join(output_dir, f"{dataset}.pdf"))
plt.close()
print(f"Fertig! Plot gespeichert als '{dataset}.pdf'")