Spaces:
Sleeping
Sleeping
Add technical report generation feature
Browse files
app.py
CHANGED
|
@@ -4,16 +4,50 @@ from PIL import Image
|
|
| 4 |
import numpy as np
|
| 5 |
import os
|
| 6 |
from pathlib import Path
|
|
|
|
|
|
|
| 7 |
|
| 8 |
from model import RadarDetectionModel
|
| 9 |
from feature_extraction import (calculate_amplitude, classify_amplitude,
|
| 10 |
calculate_distribution_range, classify_distribution_range,
|
| 11 |
calculate_attenuation_rate, classify_attenuation_rate,
|
| 12 |
-
count_reflections, classify_reflections
|
|
|
|
| 13 |
from report_generation import generate_report, render_report
|
| 14 |
from utils import plot_detection
|
| 15 |
from database import save_report, get_report_history
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# Initialize model with HF token from environment
|
| 18 |
model = None
|
| 19 |
try:
|
|
@@ -31,15 +65,15 @@ def initialize_model():
|
|
| 31 |
return None, f"Error initializing model: {str(e)}"
|
| 32 |
return model, None
|
| 33 |
|
| 34 |
-
def process_image(image):
|
| 35 |
if image is None:
|
| 36 |
-
return None, "Please upload an image."
|
| 37 |
|
| 38 |
# Initialize model if needed
|
| 39 |
global model
|
| 40 |
model, error = initialize_model()
|
| 41 |
if error:
|
| 42 |
-
return None, error
|
| 43 |
|
| 44 |
try:
|
| 45 |
# Convert to PIL Image if needed
|
|
@@ -78,18 +112,29 @@ def process_image(image):
|
|
| 78 |
report = generate_report(detection_result, image, features)
|
| 79 |
detection_image = plot_detection(image, detection_result)
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
# Save report if database is configured
|
| 82 |
try:
|
| 83 |
save_report(report)
|
| 84 |
except Exception as e:
|
| 85 |
print(f"Warning: Could not save report: {str(e)}")
|
| 86 |
|
| 87 |
-
return detection_image, render_report(report)
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
error_msg = f"Error processing image: {str(e)}"
|
| 91 |
print(error_msg)
|
| 92 |
-
return None, error_msg
|
| 93 |
|
| 94 |
def display_history():
|
| 95 |
try:
|
|
@@ -127,11 +172,13 @@ with gr.Blocks(css=css) as iface:
|
|
| 127 |
with gr.Row():
|
| 128 |
with gr.Column(scale=1):
|
| 129 |
input_image = gr.Image(type="pil", label="Upload Radar Image")
|
|
|
|
| 130 |
analyze_button = gr.Button("Analyze", variant="primary")
|
| 131 |
|
| 132 |
with gr.Column(scale=2):
|
| 133 |
output_image = gr.Image(type="pil", label="Detection Result")
|
| 134 |
output_report = gr.HTML(label="Analysis Report")
|
|
|
|
| 135 |
|
| 136 |
with gr.Row():
|
| 137 |
history_button = gr.Button("View History")
|
|
@@ -140,8 +187,8 @@ with gr.Blocks(css=css) as iface:
|
|
| 140 |
# Set up event handlers
|
| 141 |
analyze_button.click(
|
| 142 |
fn=process_image,
|
| 143 |
-
inputs=[input_image],
|
| 144 |
-
outputs=[output_image, output_report]
|
| 145 |
)
|
| 146 |
|
| 147 |
history_button.click(
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import os
|
| 6 |
from pathlib import Path
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
import tempfile
|
| 9 |
|
| 10 |
from model import RadarDetectionModel
|
| 11 |
from feature_extraction import (calculate_amplitude, classify_amplitude,
|
| 12 |
calculate_distribution_range, classify_distribution_range,
|
| 13 |
calculate_attenuation_rate, classify_attenuation_rate,
|
| 14 |
+
count_reflections, classify_reflections,
|
| 15 |
+
extract_features)
|
| 16 |
from report_generation import generate_report, render_report
|
| 17 |
from utils import plot_detection
|
| 18 |
from database import save_report, get_report_history
|
| 19 |
|
| 20 |
+
class TechnicalReportGenerator:
|
| 21 |
+
def __init__(self):
|
| 22 |
+
self.timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 23 |
+
|
| 24 |
+
def _generate_technical_section(self, detection_result, features):
|
| 25 |
+
"""Generate technical analysis section of the report."""
|
| 26 |
+
tech_doc = "## Technical Analysis\n\n"
|
| 27 |
+
|
| 28 |
+
# Detection Results
|
| 29 |
+
tech_doc += "### Detection Results\n\n```python\n"
|
| 30 |
+
tech_doc += f"Confidence Scores: {detection_result['scores'].tolist()}\n"
|
| 31 |
+
tech_doc += f"Bounding Boxes: {detection_result['boxes'].tolist()}\n"
|
| 32 |
+
tech_doc += f"Labels: {detection_result['labels'].tolist()}\n"
|
| 33 |
+
tech_doc += "```\n\n"
|
| 34 |
+
|
| 35 |
+
# Feature Analysis
|
| 36 |
+
tech_doc += "### Feature Analysis\n\n"
|
| 37 |
+
for feature_name, value in features.items():
|
| 38 |
+
tech_doc += f"- **{feature_name}**: {value}\n"
|
| 39 |
+
|
| 40 |
+
# Signal Processing Details
|
| 41 |
+
tech_doc += "\n### Signal Processing Metrics\n\n"
|
| 42 |
+
tech_doc += "| Metric | Value | Classification |\n"
|
| 43 |
+
tech_doc += "|--------|--------|----------------|\n"
|
| 44 |
+
tech_doc += f"|Amplitude|{features['Amplitude']}|{classify_amplitude(features['Amplitude'])}|\n"
|
| 45 |
+
tech_doc += f"|Distribution Range|{features['Distribution Range']}|{features['Distribution Range']}|\n"
|
| 46 |
+
tech_doc += f"|Attenuation Rate|{features['Attenuation Rate']}|{features['Attenuation Rate']}|\n"
|
| 47 |
+
tech_doc += f"|Reflection Count|{features['Reflection Count']}|{features['Reflection Count']}|\n"
|
| 48 |
+
|
| 49 |
+
return tech_doc
|
| 50 |
+
|
| 51 |
# Initialize model with HF token from environment
|
| 52 |
model = None
|
| 53 |
try:
|
|
|
|
| 65 |
return None, f"Error initializing model: {str(e)}"
|
| 66 |
return model, None
|
| 67 |
|
| 68 |
+
def process_image(image, generate_tech_report=False):
|
| 69 |
if image is None:
|
| 70 |
+
return None, "Please upload an image.", None
|
| 71 |
|
| 72 |
# Initialize model if needed
|
| 73 |
global model
|
| 74 |
model, error = initialize_model()
|
| 75 |
if error:
|
| 76 |
+
return None, error, None
|
| 77 |
|
| 78 |
try:
|
| 79 |
# Convert to PIL Image if needed
|
|
|
|
| 112 |
report = generate_report(detection_result, image, features)
|
| 113 |
detection_image = plot_detection(image, detection_result)
|
| 114 |
|
| 115 |
+
# Generate technical report if requested
|
| 116 |
+
tech_report = None
|
| 117 |
+
if generate_tech_report:
|
| 118 |
+
report_gen = TechnicalReportGenerator()
|
| 119 |
+
tech_report = report_gen._generate_technical_section(detection_result, features)
|
| 120 |
+
|
| 121 |
+
# Save technical report to a temporary file
|
| 122 |
+
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.md') as f:
|
| 123 |
+
f.write(tech_report)
|
| 124 |
+
tech_report = f.name
|
| 125 |
+
|
| 126 |
# Save report if database is configured
|
| 127 |
try:
|
| 128 |
save_report(report)
|
| 129 |
except Exception as e:
|
| 130 |
print(f"Warning: Could not save report: {str(e)}")
|
| 131 |
|
| 132 |
+
return detection_image, render_report(report), tech_report
|
| 133 |
|
| 134 |
except Exception as e:
|
| 135 |
error_msg = f"Error processing image: {str(e)}"
|
| 136 |
print(error_msg)
|
| 137 |
+
return None, error_msg, None
|
| 138 |
|
| 139 |
def display_history():
|
| 140 |
try:
|
|
|
|
| 172 |
with gr.Row():
|
| 173 |
with gr.Column(scale=1):
|
| 174 |
input_image = gr.Image(type="pil", label="Upload Radar Image")
|
| 175 |
+
tech_report_checkbox = gr.Checkbox(label="Generate Technical Report", value=False)
|
| 176 |
analyze_button = gr.Button("Analyze", variant="primary")
|
| 177 |
|
| 178 |
with gr.Column(scale=2):
|
| 179 |
output_image = gr.Image(type="pil", label="Detection Result")
|
| 180 |
output_report = gr.HTML(label="Analysis Report")
|
| 181 |
+
tech_report_output = gr.File(label="Technical Report")
|
| 182 |
|
| 183 |
with gr.Row():
|
| 184 |
history_button = gr.Button("View History")
|
|
|
|
| 187 |
# Set up event handlers
|
| 188 |
analyze_button.click(
|
| 189 |
fn=process_image,
|
| 190 |
+
inputs=[input_image, tech_report_checkbox],
|
| 191 |
+
outputs=[output_image, output_report, tech_report_output]
|
| 192 |
)
|
| 193 |
|
| 194 |
history_button.click(
|