|
a |
|
b/src/app.py |
|
|
1 |
# coding=utf-8 |
|
|
2 |
from __future__ import division, print_function |
|
|
3 |
import os |
|
|
4 |
import numpy as np |
|
|
5 |
|
|
|
6 |
from flask import Flask, redirect, url_for, request, render_template |
|
|
7 |
from werkzeug.utils import secure_filename |
|
|
8 |
from gevent.pywsgi import WSGIServer |
|
|
9 |
from predict import * |
|
|
10 |
from utils import * |
|
|
11 |
from config import get_config |
|
|
12 |
app = Flask(__name__) |
|
|
13 |
app.config['MAX_CONTENT_LENGTH'] = 5 * 1024 * 1024 |
|
|
14 |
|
|
|
15 |
global classesM |
|
|
16 |
classesM = ['N','V','L','R','Paced','A','F']#,'f','j','E','a','J','Q','e','S'] |
|
|
17 |
|
|
|
18 |
print('Check http://127.0.0.1:5002/') |
|
|
19 |
|
|
|
20 |
|
|
|
21 |
def model_predict(img_path): |
|
|
22 |
data = uploadedData(img_path, csvbool = True) |
|
|
23 |
sr = data[0] |
|
|
24 |
|
|
|
25 |
data = data[1:] |
|
|
26 |
size = len(data) |
|
|
27 |
if size > 9001: |
|
|
28 |
size = 9001 |
|
|
29 |
data = data[:size] |
|
|
30 |
div = size // 1000 |
|
|
31 |
data, peaks = preprocess(data, config) |
|
|
32 |
return predictByPart(data, peaks) |
|
|
33 |
|
|
|
34 |
@app.route('/', methods=['GET']) |
|
|
35 |
def index(): |
|
|
36 |
# Main page |
|
|
37 |
return render_template('index.html') |
|
|
38 |
|
|
|
39 |
@app.route('/predict', methods=['GET', 'POST']) |
|
|
40 |
def upload(): |
|
|
41 |
|
|
|
42 |
if request.method == 'POST': |
|
|
43 |
# Get the file from post request |
|
|
44 |
f = request.files['file'] |
|
|
45 |
if not f: |
|
|
46 |
return "No file!" |
|
|
47 |
basepath = os.path.dirname(__file__) |
|
|
48 |
|
|
|
49 |
file_path = os.path.join(basepath, 'uploads', secure_filename(f.filename)) |
|
|
50 |
try: |
|
|
51 |
print("You already analyzed the data file. We delete it and re-analyze it!") |
|
|
52 |
os.remove(file_path) |
|
|
53 |
except: |
|
|
54 |
print("The file is new!") |
|
|
55 |
f.save(file_path) |
|
|
56 |
predicted, result = model_predict(file_path) |
|
|
57 |
length = len(predicted) |
|
|
58 |
|
|
|
59 |
result = str(length) +" parts of the divided data were estimated as the followings with paired probabilities. \n"+result |
|
|
60 |
|
|
|
61 |
return format_result(result) |
|
|
62 |
return None |
|
|
63 |
|
|
|
64 |
def format_result(text): |
|
|
65 |
# Extract the summary part at the end |
|
|
66 |
summary_start = text.rfind(")") + 1 |
|
|
67 |
summary = text[summary_start:].strip() |
|
|
68 |
data = text[:summary_start].strip() |
|
|
69 |
|
|
|
70 |
# Format the summary sentence |
|
|
71 |
summary_parts = summary.split(", ") |
|
|
72 |
formatted_summary = "The results show: " |
|
|
73 |
formatted_summary += ", ".join(f"{part.split('-')[1]} beats labeled as {part.split('-')[0]}" for part in summary_parts) |
|
|
74 |
|
|
|
75 |
# Combine the formatted parts |
|
|
76 |
formatted_text = f"{data}, {formatted_summary}." |
|
|
77 |
return formatted_text |
|
|
78 |
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
|
82 |
if __name__ == '__main__': |
|
|
83 |
config = get_config() |
|
|
84 |
#app.run(port=5002, debug=True) |
|
|
85 |
|
|
|
86 |
# Serve the app with gevent |
|
|
87 |
http_server = WSGIServer(('', 5002), app) |
|
|
88 |
http_server.serve_forever() |