Vehicle-Counting / core /exporter.py
riezqidr's picture
Add initial project structure with Streamlit UI and utility functions
ab2f940
Raw
History Blame Contribute Delete
2.81 kB
"""
Export Utilities
Fungsi-fungsi untuk export hasil counting ke CSV
dan membuat DataFrame summary untuk ditampilkan di Streamlit.
"""
import pandas as pd
from pathlib import Path
from datetime import datetime
def export_counts_to_csv(counts_data, output_path="outputs/counting_results.csv"):
"""
Export data counting ke file CSV.
Args:
counts_data: dict dengan format:
{
"total": int,
"per_class": {
"Car": int,
"Motorcycle": int,
"Bus": int,
"Truck": int
}
}
output_path: path untuk menyimpan CSV
Returns:
str: path file yang dibuat
"""
output_path = Path(output_path)
output_path.parent.mkdir(parents=True, exist_ok=True)
rows = []
# baris per kelas (ambil dari data, tidak hardcode)
per_class = counts_data.get("per_class", {})
for cls_name, count in sorted(per_class.items()):
rows.append({
"Kelas": cls_name,
"Jumlah": count
})
# baris total
rows.append({
"Kelas": "TOTAL",
"Jumlah": counts_data.get("total", 0)
})
df = pd.DataFrame(rows)
# tambahkan metadata
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# save ke CSV
df.to_csv(output_path, index=False)
# append timestamp ke file
with open(output_path, "a") as f:
f.write(f"\n# Timestamp: {timestamp}\n")
return str(output_path)
def create_summary_dataframe(counts_data):
"""
Buat DataFrame summary untuk ditampilkan di UI Streamlit.
Args:
counts_data: dict hasil dari counter.get_counts()
Returns:
pandas DataFrame
"""
per_class = counts_data.get("per_class", {})
rows = []
for cls_name, count in sorted(per_class.items()):
rows.append({
"Kelas Kendaraan": cls_name,
"Jumlah Terdeteksi": count
})
df = pd.DataFrame(rows)
return df
def export_detailed_log(frame_logs, output_path="outputs/detailed_log.csv"):
"""
Export log detail per-frame ke CSV.
Berguna untuk analisis lebih lanjut.
Args:
frame_logs: list of dict, setiap dict berisi info per frame:
{
"frame_number": int,
"num_detections": int,
"num_tracked": int,
"cumulative_count": int,
"fps": float
}
output_path: path output
Returns:
str: path file
"""
output_path = Path(output_path)
output_path.parent.mkdir(parents=True, exist_ok=True)
df = pd.DataFrame(frame_logs)
df.to_csv(output_path, index=False)
return str(output_path)