gpt4 book ai didi

javascript - 使用Python和Flask实时更新心电图

转载 作者:行者123 更新时间:2023-12-01 01:18:11 33 4
gpt4 key购买 nike

我制作了一个简单的 python Flask 应用程序,用于与用于培训紧急服务的培训模拟器进行交互。在此应用程序中,我想显示心电图波形和生命体征。我能够使用 Chartjs 实现这一点,但是它非常慢,因此不适合我的需求。我创建了一个线程类,它使用flask-socketio定期将波形数据点提供给我的javascript。在 socketio 事件中,调用 javascript 函数来更新图表。我对所有这些技术都很陌生,我想知道我是否在线程方面做错了什么,或者滥用了 socketio,或者可能的 Chartjs 或 Flask 不是我应该用来实现我的目标的。请告诉我如何提高图表更新的性能,以便我可以在我的浏览器应用程序上显示典型的心电图波形,就像您在医院的心电图机上看到的那样。

app9.py(主应用程序):

from flask_socketio import SocketIO, emit
from flask import Flask, render_template, url_for,
copy_current_request_context, Markup
from random import random
from time import sleep
from threading import Thread, Event
import pylab
import scipy.signal as signal
import numpy as np
from itertools import cycle
from flask_bootstrap import Bootstrap

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['DEBUG'] = True

#turn the flask app into a socketio app
socketio = SocketIO(app)

#random number Generator Thread
thread = Thread()
thread_stop_event = Event()


class ECGThread(Thread):
def __init__(self):
self.delay = 1
super(ECGThread, self).__init__()

def waveformGenerator(self):
#infinite loop of pqrst
print("Generating waveform")
i=0
pqrst = signal.wavelets.daub(10)
pqrst_list = pqrst.tolist()
while not thread_stop_event.isSet():
for i in cycle(pqrst_list):
data=i
socketio.emit('ecg-update', {'data': data})
#print('emitted: ' + str(data))
sleep(self.delay)

def run(self):
self.waveformGenerator()


@app.route('/')
def index():
#only by sending this page first will the client be connected to the
socketio instance
return render_template('index9.html')

@socketio.on('connect')
def test_connect():
# need visibility of the global thread object
global thread
print('Client connected')

#Start the waveform generator thread only if the thread has not been
started before.
if not thread.isAlive():
print("Starting Thread")
thread = ECGThread()
thread.start()

@socketio.on('disconnect')
def test_disconnect():
print('Client disconnected')


if __name__ == '__main__':
socketio.run(app,debug=True, host='0.0.0.0')

main9.js(用于处理心电图的 JavaScript):

// function to add data to a chart
function addData(chart, label, data){
chart.data.labels.push(label);
chart.data.datasets.forEach((dataset) => {
dataset.data.push(data);
});
chart.update();
}

// function to remove data to a chart
function removeData(chart){
chart.data.labels.pop();
chart.data.datasets.forEach((dataset) => {
dataset.data.pop();
});
chart.update()
}


// ECG Chart
var ecgChart = document.getElementById("ecgChart");

var ecgChart = new Chart(ecgChart, {
type: 'line',
data: {
labels:["1","2","3","4","5","6","7","8","9","10"],
datasets: [
{
label: "My First Dataset",
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(75,192,192,0.4)",
borderColor: "rgba(75,192,192,1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(75,192,192,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(75,192,192,1)",
pointHoverBorderColor: "rgba(220,220,220,1)",
pointBorderWidth: 2,
pointRadius: 1,
pointHitRadius: 10,
data: [0.18817680007769047, 0.527201188931723,
0.6884590394536007, 0.2811723436605783,
-0.24984642432731163, -0.19594627437737416,
0.1273693403357969, 0.09305736460357118,
-0.0713941471663973,-0.02945753682187802]
}]
}
});

// connect to the socket server.
var socket = io.connect('http://' + document.domain + ':' +
location.port);

// receive details from server
socket.on('ecg-update', function(msg) {
number = msg.data
console.log(msg.data);
addData(ecgChart,'new data', msg.data);
removeData(ecgChart);
});

index9.html(HTML):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>{{ title }}</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-
scale=1">
<script src="http://code.jquery.com/jquery-2.1.1.min.js">
</script>
<link rel="stylesheet" href="/static/normalize.css">
<link rel="stylesheet" href="/static/main.css">

<script type="text/javascript"
src="//cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.js">
</script>
<script type="text/javascript"
src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js">
</script>
<script type="text/javascript" src="{{ url_for('static',
filename='modernizr-3.6.0.min.js') }}"></script>

</head>
<body>
<canvas id="ecgChart" width="600" height="400"></canvas>
<script type="text/javascript" src="{{ url_for('static', filename='main9.js') }}"></script>
</body>
</html>

最佳答案

我无法使用 Chart.js 获得所需的结果,但是我找到了两种创建实时流式心电图图的方法。

1.) smoothiecharts - http://smoothiecharts.org/ - 制作实时流图表非常好且简单,但是没有实现我想要的结果所需的可定制性。

2.) http://theblogofpeterchen.blogspot.com/2015/02/html5-high-performance-real-time.html - 我发现一个博客,其中有人在不使用任何图表库的情况下制作了实时心电图,而是编写了自己的构造函数,用于使用 requestAnimationFrame() 制作实时图表以使其工作。这就是我用的。现在我只需要学习如何改变心率哈哈。

关于javascript - 使用Python和Flask实时更新心电图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54522957/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com