Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import pandas as pd
|
4 |
+
from ecg_image_kit import digitize_ecg
|
5 |
+
import neurokit2 as nk
|
6 |
+
|
7 |
+
def process_ecg_image(image):
|
8 |
+
# Save uploaded image temporarily
|
9 |
+
image_path = "temp_ecg_image.png"
|
10 |
+
image.save(image_path)
|
11 |
+
|
12 |
+
# Digitize image
|
13 |
+
time_series = load_and_digitize_ecg(image_path)
|
14 |
+
|
15 |
+
# Analyze ECG
|
16 |
+
results = analyze_ecg(time_series, sampling_rate=250)
|
17 |
+
|
18 |
+
# Generate plots and tables
|
19 |
+
plots = []
|
20 |
+
tables = []
|
21 |
+
for lead, data in results.items():
|
22 |
+
# Plot ECG signal with R-peaks
|
23 |
+
fig, ax = plt.subplots()
|
24 |
+
signals = data["signals"]
|
25 |
+
info = data["info"]
|
26 |
+
ax.plot(signals["ECG_Clean"], label="Clean ECG")
|
27 |
+
ax.plot(info["ECG_R_Peaks"], signals["ECG_Clean"][info["ECG_R_Peaks"]], "ro", label="R-peaks")
|
28 |
+
ax.set_title(f"{lead} ECG")
|
29 |
+
ax.legend()
|
30 |
+
plots.append(fig)
|
31 |
+
|
32 |
+
# Create table of features
|
33 |
+
analysis = data["analysis"]
|
34 |
+
table = pd.DataFrame({
|
35 |
+
"Feature": ["Heart Rate (Mean)", "ECG Quality"],
|
36 |
+
"Value": [analysis.get("ECG_Rate_Mean", "N/A"), analysis.get("ECG_Quality_Mean", "N/A")]
|
37 |
+
})
|
38 |
+
tables.append(table)
|
39 |
+
|
40 |
+
return plots, tables
|
41 |
+
|
42 |
+
# Define Gradio interface
|
43 |
+
iface = gr.Interface(
|
44 |
+
fn=process_ecg_image,
|
45 |
+
inputs=gr.Image(type="pil", label="Upload ECG Image"),
|
46 |
+
outputs=[
|
47 |
+
gr.Gallery(label="ECG Plots"),
|
48 |
+
gr.Dataframe(label="Analysis Results")
|
49 |
+
],
|
50 |
+
title="ECG Image Analysis",
|
51 |
+
description="Upload a 12-lead ECG image to digitize and analyze it."
|
52 |
+
)
|
53 |
+
|
54 |
+
# Launch interface (for local testing)
|
55 |
+
if __name__ == "__main__":
|
56 |
+
iface.launch()
|