Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,40 +1,94 @@
|
|
| 1 |
import matplotlib.pyplot as plt
|
| 2 |
import numpy as np
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def create_percentage_circle(percentage, title):
|
| 5 |
"""
|
| 6 |
Create a circular progress bar for percentage visualization.
|
| 7 |
-
Args:
|
| 8 |
-
percentage: Value between 0 and 100.
|
| 9 |
-
title: Title for the plot.
|
| 10 |
-
Returns:
|
| 11 |
-
Path to saved image.
|
| 12 |
"""
|
| 13 |
fig, ax = plt.subplots(figsize=(4, 4))
|
| 14 |
ax.set_aspect("equal")
|
| 15 |
|
| 16 |
-
# Draw background circle
|
| 17 |
circle = plt.Circle((0.5, 0.5), 0.4, color="lightgray")
|
| 18 |
ax.add_patch(circle)
|
| 19 |
|
| 20 |
-
# Draw progress arc
|
| 21 |
theta = np.linspace(0, 360 * (percentage / 100), 100)
|
| 22 |
x = 0.5 + 0.4 * np.cos(np.radians(theta))
|
| 23 |
y = 0.5 + 0.4 * np.sin(np.radians(theta))
|
| 24 |
ax.plot(x, y, color="blue", linewidth=20)
|
| 25 |
|
| 26 |
-
# Add percentage text
|
| 27 |
ax.text(0.5, 0.5, f"{percentage:.1f}%", ha="center", va="center", fontsize=20)
|
| 28 |
-
|
| 29 |
-
# Add title
|
| 30 |
ax.set_title(title, fontsize=12)
|
| 31 |
-
|
| 32 |
-
# Remove axes
|
| 33 |
ax.axis("off")
|
| 34 |
|
| 35 |
-
# Save image
|
| 36 |
output_path = f"screenshots/circle_{int(percentage)}_{int(np.random.randint(1000))}.png"
|
| 37 |
plt.savefig(output_path, bbox_inches="tight")
|
| 38 |
plt.close()
|
| 39 |
|
| 40 |
-
return output_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import matplotlib.pyplot as plt
|
| 2 |
import numpy as np
|
| 3 |
+
from latex import build_pdf
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
def create_percentage_circle(percentage, title):
|
| 7 |
"""
|
| 8 |
Create a circular progress bar for percentage visualization.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"""
|
| 10 |
fig, ax = plt.subplots(figsize=(4, 4))
|
| 11 |
ax.set_aspect("equal")
|
| 12 |
|
|
|
|
| 13 |
circle = plt.Circle((0.5, 0.5), 0.4, color="lightgray")
|
| 14 |
ax.add_patch(circle)
|
| 15 |
|
|
|
|
| 16 |
theta = np.linspace(0, 360 * (percentage / 100), 100)
|
| 17 |
x = 0.5 + 0.4 * np.cos(np.radians(theta))
|
| 18 |
y = 0.5 + 0.4 * np.sin(np.radians(theta))
|
| 19 |
ax.plot(x, y, color="blue", linewidth=20)
|
| 20 |
|
|
|
|
| 21 |
ax.text(0.5, 0.5, f"{percentage:.1f}%", ha="center", va="center", fontsize=20)
|
|
|
|
|
|
|
| 22 |
ax.set_title(title, fontsize=12)
|
|
|
|
|
|
|
| 23 |
ax.axis("off")
|
| 24 |
|
|
|
|
| 25 |
output_path = f"screenshots/circle_{int(percentage)}_{int(np.random.randint(1000))}.png"
|
| 26 |
plt.savefig(output_path, bbox_inches="tight")
|
| 27 |
plt.close()
|
| 28 |
|
| 29 |
+
return output_path
|
| 30 |
+
|
| 31 |
+
def generate_pdf_report(summary, screenshots, output_path):
|
| 32 |
+
"""
|
| 33 |
+
Generate a PDF report using LaTeX with summary and embedded screenshots.
|
| 34 |
+
"""
|
| 35 |
+
# Create LaTeX content
|
| 36 |
+
latex_content = r"""
|
| 37 |
+
\documentclass[a4paper,12pt]{article}
|
| 38 |
+
\usepackage[utf8]{inputenc}
|
| 39 |
+
\usepackage{graphicx}
|
| 40 |
+
\usepackage{geometry}
|
| 41 |
+
\geometry{margin=1in}
|
| 42 |
+
\usepackage{hyperref}
|
| 43 |
+
\usepackage{caption}
|
| 44 |
+
\usepackage{subcaption}
|
| 45 |
+
|
| 46 |
+
% Preamble for document setup
|
| 47 |
+
\title{Road Safety Analysis Report}
|
| 48 |
+
\author{Generated by Road Safety AI}
|
| 49 |
+
\date{\today}
|
| 50 |
+
|
| 51 |
+
\begin{document}
|
| 52 |
+
|
| 53 |
+
\maketitle
|
| 54 |
+
|
| 55 |
+
\section*{Analysis Summary}
|
| 56 |
+
\begin{itemize}
|
| 57 |
+
% Insert summary lines dynamically
|
| 58 |
+
"""
|
| 59 |
+
for line in summary.split('\n'):
|
| 60 |
+
if line.strip():
|
| 61 |
+
latex_content += f"\item {line.strip()}\n"
|
| 62 |
+
|
| 63 |
+
latex_content += r"""
|
| 64 |
+
\end{itemize}
|
| 65 |
+
|
| 66 |
+
\section*{Incident Screenshots}
|
| 67 |
+
\begin{figure}[h]
|
| 68 |
+
\centering
|
| 69 |
+
"""
|
| 70 |
+
|
| 71 |
+
for i, screenshot in enumerate(screenshots[:5]): # Limit to 5 images
|
| 72 |
+
latex_content += f"""
|
| 73 |
+
\begin{subfigure}[t]{0.45\textwidth}
|
| 74 |
+
\includegraphics[width=\textwidth]{{{screenshot}}}
|
| 75 |
+
\caption{Incident Frame {i+1}}
|
| 76 |
+
\end{subfigure}
|
| 77 |
+
"""
|
| 78 |
+
if (i + 1) % 2 == 0 and i < len(screenshots) - 1:
|
| 79 |
+
latex_content += r" \hfill "
|
| 80 |
+
|
| 81 |
+
latex_content += r"""
|
| 82 |
+
\caption*{Captured incidents during video analysis}
|
| 83 |
+
\end{figure}
|
| 84 |
+
|
| 85 |
+
\end{document}
|
| 86 |
+
"""
|
| 87 |
+
|
| 88 |
+
# Write LaTeX to a file
|
| 89 |
+
tex_file = "templates/report.tex"
|
| 90 |
+
with open(tex_file, "w") as f:
|
| 91 |
+
f.write(latex_content)
|
| 92 |
+
|
| 93 |
+
# Build PDF
|
| 94 |
+
build_pdf(pdf_file=output_path, tex_file=tex_file, clean_tex=True)
|