Added initial analysis output.
Browse files
app.py
CHANGED
|
@@ -36,10 +36,12 @@
|
|
| 36 |
import datetime
|
| 37 |
import deepfakeecg
|
| 38 |
import ecg_plot
|
|
|
|
| 39 |
import gradio
|
| 40 |
import io
|
| 41 |
import matplotlib.pyplot as plt
|
| 42 |
import matplotlib.ticker
|
|
|
|
| 43 |
import numpy
|
| 44 |
import pathlib
|
| 45 |
import random
|
|
@@ -242,8 +244,34 @@ def analyze(request: gradio.Request):
|
|
| 242 |
log(f'Session "{request.session_hash}": Analyze ECG #{Sessions[request.session_hash].Selected + 1}!')
|
| 243 |
|
| 244 |
data = Sessions[request.session_hash].Results[Sessions[request.session_hash].Selected]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
print(data)
|
| 246 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
|
| 249 |
# ###### Main program #######################################################
|
|
@@ -312,6 +340,36 @@ img.logo-image {
|
|
| 312 |
"""
|
| 313 |
|
| 314 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
# ====== Create GUI =========================================================
|
| 316 |
with gradio.Blocks(css = css, theme = gradio.themes.Glass(secondary_hue=gradio.themes.colors.blue)) as gui:
|
| 317 |
|
|
@@ -356,6 +414,8 @@ with gradio.Blocks(css = css, theme = gradio.themes.Glass(secondary_hue=gradio.t
|
|
| 356 |
preview = True)
|
| 357 |
outputGallery.select(select)
|
| 358 |
gradio.Markdown('## Analysis')
|
|
|
|
|
|
|
| 359 |
|
| 360 |
# ====== Add click event handling for "Generate" button ==================
|
| 361 |
buttonGenerate.click(predict,
|
|
@@ -367,19 +427,22 @@ with gradio.Blocks(css = css, theme = gradio.themes.Glass(secondary_hue=gradio.t
|
|
| 367 |
)
|
| 368 |
|
| 369 |
# ====== Add click event handling for "Analyze" button ===================
|
| 370 |
-
buttonAnalyze.click(analyze
|
|
|
|
|
|
|
|
|
|
| 371 |
|
| 372 |
# ====== Add click event handling for download buttons ===================
|
| 373 |
# Using hidden button and JavaScript, to generate download file on-the-fly:
|
| 374 |
# https://github.com/gradio-app/gradio/issues/9230#issuecomment-2323771634
|
| 375 |
buttonCSV.click(downloadCSV)
|
| 376 |
buttonCSV.click(fn = downloadCSV, inputs = None, outputs = [ buttonCSV_hidden ]).then(
|
| 377 |
-
|
| 378 |
-
|
| 379 |
buttonPDF.click(downloadPDF)
|
| 380 |
buttonPDF.click(fn = downloadPDF, inputs = None, outputs = [ buttonPDF_hidden ]).then(
|
| 381 |
-
|
| 382 |
-
|
| 383 |
|
| 384 |
# ====== Run on startup ==================================================
|
| 385 |
gui.load(predict,
|
|
|
|
| 36 |
import datetime
|
| 37 |
import deepfakeecg
|
| 38 |
import ecg_plot
|
| 39 |
+
import getopt
|
| 40 |
import gradio
|
| 41 |
import io
|
| 42 |
import matplotlib.pyplot as plt
|
| 43 |
import matplotlib.ticker
|
| 44 |
+
import neurokit2
|
| 45 |
import numpy
|
| 46 |
import pathlib
|
| 47 |
import random
|
|
|
|
| 244 |
log(f'Session "{request.session_hash}": Analyze ECG #{Sessions[request.session_hash].Selected + 1}!')
|
| 245 |
|
| 246 |
data = Sessions[request.session_hash].Results[Sessions[request.session_hash].Selected]
|
| 247 |
+
|
| 248 |
+
data = data.t().detach().cpu().numpy()[1:] / 1000
|
| 249 |
+
|
| 250 |
+
print(data[0])
|
| 251 |
+
print(len(data[0]))
|
| 252 |
+
|
| 253 |
+
leadI = data[0]
|
| 254 |
+
|
| 255 |
+
signals, info = neurokit2.ecg_process(leadI, sampling_rate = deepfakeecg.ECG_SAMPLING_RATE)
|
| 256 |
+
neurokit2.ecg_plot(signals, info)
|
| 257 |
+
|
| 258 |
+
# DIN A4 landscape: w=11.7, h=8.27
|
| 259 |
+
# w = 508/25.4 # mm to inch
|
| 260 |
+
# h = 122/25.4 # mm to inch
|
| 261 |
+
# matplotlib.pyplot.gcf().set_size_inches(w, h, forward=True)
|
| 262 |
+
# pdf.savefig(matplotlib.pyplot.gcf())
|
| 263 |
+
# matplotlib.pyplot.close()
|
| 264 |
+
|
| 265 |
print(data)
|
| 266 |
|
| 267 |
+
return matplotlib.pyplot.gcf()
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
# ###### Print usage and exit ###############################################
|
| 271 |
+
def usage(exitCode = 0):
|
| 272 |
+
sys.stdout.write('Usage: ' + sys.argv[0] + ' [-d|--device cpu|cuda] [-v|--version]\n')
|
| 273 |
+
sys.exit(exitCode)
|
| 274 |
+
|
| 275 |
|
| 276 |
|
| 277 |
# ###### Main program #######################################################
|
|
|
|
| 340 |
"""
|
| 341 |
|
| 342 |
|
| 343 |
+
# ====== Check arguments ====================================================
|
| 344 |
+
try:
|
| 345 |
+
options, args = getopt.getopt(
|
| 346 |
+
sys.argv[1:],
|
| 347 |
+
'd:v',
|
| 348 |
+
[
|
| 349 |
+
'device=',
|
| 350 |
+
'version'
|
| 351 |
+
])
|
| 352 |
+
for option, optarg in options:
|
| 353 |
+
print(option)
|
| 354 |
+
if option in ( '-d', '--device' ):
|
| 355 |
+
runOnDevice = optarg
|
| 356 |
+
elif option in ( '-v', '--version' ):
|
| 357 |
+
sys.stdout.write('PyTorch version: ' + torch.__version__ + '\n')
|
| 358 |
+
sys.stdout.write('CUDA version: ' + torch.version.cuda + '\n')
|
| 359 |
+
sys.stdout.write('CUDA available: ' + ('yes' if torch.cuda.is_available() else 'no') + '\n')
|
| 360 |
+
sys.stdout.write('Device: ' + runOnDevice + '\n')
|
| 361 |
+
sys.exit(1)
|
| 362 |
+
else:
|
| 363 |
+
sys.stderr.write('ERROR: Invalid option ' + option + '!\n')
|
| 364 |
+
sys.exit(1)
|
| 365 |
+
|
| 366 |
+
except getopt.GetoptError as error:
|
| 367 |
+
sys.stderr.write('ERROR: ' + str(error) + '\n')
|
| 368 |
+
usage(1)
|
| 369 |
+
if len(args) > 0:
|
| 370 |
+
usage(1)
|
| 371 |
+
|
| 372 |
+
|
| 373 |
# ====== Create GUI =========================================================
|
| 374 |
with gradio.Blocks(css = css, theme = gradio.themes.Glass(secondary_hue=gradio.themes.colors.blue)) as gui:
|
| 375 |
|
|
|
|
| 414 |
preview = True)
|
| 415 |
outputGallery.select(select)
|
| 416 |
gradio.Markdown('## Analysis')
|
| 417 |
+
with gradio.Row():
|
| 418 |
+
analysisOutput = gradio.Plot()
|
| 419 |
|
| 420 |
# ====== Add click event handling for "Generate" button ==================
|
| 421 |
buttonGenerate.click(predict,
|
|
|
|
| 427 |
)
|
| 428 |
|
| 429 |
# ====== Add click event handling for "Analyze" button ===================
|
| 430 |
+
buttonAnalyze.click(analyze,
|
| 431 |
+
inputs = [],
|
| 432 |
+
outputs = [ analysisOutput ]
|
| 433 |
+
)
|
| 434 |
|
| 435 |
# ====== Add click event handling for download buttons ===================
|
| 436 |
# Using hidden button and JavaScript, to generate download file on-the-fly:
|
| 437 |
# https://github.com/gradio-app/gradio/issues/9230#issuecomment-2323771634
|
| 438 |
buttonCSV.click(downloadCSV)
|
| 439 |
buttonCSV.click(fn = downloadCSV, inputs = None, outputs = [ buttonCSV_hidden ]).then(
|
| 440 |
+
fn = None, inputs = None, outputs = None,
|
| 441 |
+
js = "() => document.querySelector('#download_csv_hidden').click()")
|
| 442 |
buttonPDF.click(downloadPDF)
|
| 443 |
buttonPDF.click(fn = downloadPDF, inputs = None, outputs = [ buttonPDF_hidden ]).then(
|
| 444 |
+
fn = None, inputs = None, outputs = None,
|
| 445 |
+
js = "() => document.querySelector('#download_pdf_hidden').click()")
|
| 446 |
|
| 447 |
# ====== Run on startup ==================================================
|
| 448 |
gui.load(predict,
|