Spaces:
Runtime error
Runtime error
import gradio as gr | |
import torch | |
from PIL import Image | |
import io | |
import pandas as pd | |
from model import RadarDetectionModel | |
from feature_extraction import (calculate_amplitude, classify_amplitude, | |
calculate_distribution_range, classify_distribution_range, | |
calculate_attenuation_rate, classify_attenuation_rate, | |
count_reflections, classify_reflections) | |
from report_generation import generate_report | |
from utils import plot_detection | |
from database import save_report, get_report_history | |
from report_generation import render_report | |
model = RadarDetectionModel() | |
def process_image(image): | |
detection_result = model.detect(image) | |
np_image = np.array(image) | |
amplitude = calculate_amplitude(np_image) | |
amplitude_class = classify_amplitude(amplitude) | |
box = detection_result['boxes'][0].tolist() | |
distribution_range = calculate_distribution_range(box) | |
distribution_class = classify_distribution_range(distribution_range) | |
attenuation_rate = calculate_attenuation_rate(np_image) | |
attenuation_class = classify_attenuation_rate(attenuation_rate) | |
reflection_count = count_reflections(np_image) | |
reflection_class = classify_reflections(reflection_count) | |
features = { | |
"振幅": amplitude_class, | |
"分布范围": distribution_class, | |
"衰减速度": attenuation_class, | |
"反射次数": reflection_class | |
} | |
report = generate_report(detection_result, image, features) | |
detection_image = plot_detection(image, detection_result) | |
save_report(report) | |
return detection_image, report | |
def analyze_radar_image(image): | |
detection_image, report = process_image(image) | |
report_html = render_report(report) | |
return detection_image, report_html | |
def display_history(): | |
reports = get_report_history() | |
history_html = "<div class='history-container'><h3>历史记录</h3>" | |
for report in reports: | |
history_html += f""" | |
<div class='history-item'> | |
<p><strong>报告ID:</strong> {report.report_id}</p> | |
<p><strong>缺陷类型:</strong> {report.defect_type}</p> | |
<p><strong>描述:</strong> {report.description}</p> | |
<p><strong>创建时间:</strong> {report.created_at}</p> | |
</div> | |
""" | |
history_html += "</div>" | |
return history_html | |
with gr.Blocks(css="static/style.css") as iface: | |
gr.Markdown("# 雷达图谱分析系统") | |
with gr.Row(): | |
with gr.Column(scale=1): | |
input_image = gr.Image(type="pil", label="上传雷达图谱") | |
analyze_button = gr.Button("分析") | |
with gr.Column(scale=2): | |
output_image = gr.Image(type="pil", label="检测结果") | |
output_report = gr.HTML(label="分析报告") | |
history_button = gr.Button("查看历史记录") | |
history_output = gr.HTML() | |
analyze_button.click(analyze_radar_image, inputs=[ | |
input_image], outputs=[output_image, output_report]) | |
history_button.click(display_history, inputs=[], outputs=[history_output]) | |
iface.launch() | |