-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
103 lines (98 loc) · 3.08 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#https://medium.com/swlh/decoding-noaa-satellite-images-using-50-lines-of-code-3c5d1d0a08da
import scipy.io.wavfile as wav
import scipy.signal as signal
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import datetime
from flask import Flask, render_template,redirect,request
import os
SAVESFOLDER="static/wav/"
IMGFOLDER="static/img/"
app=Flask(__name__)
def noaaResample(name:str):
fs, data = wav.read(SAVESFOLDER+name)
data_crop = data[20*fs:21*fs]
analytical_signal = signal.hilbert(data)
data_am = np.abs(analytical_signal)
frame_width = int(0.5*fs)
w, h = frame_width, data_am.shape[0]//frame_width
image = Image.new('RGB', (w, h))
px, py = 0, 0
for p in range(data_am.shape[0]):
lum = int(data_am[p]//32 - 32)
if lum < 0: lum = 0
if lum > 255: lum = 255
image.putpixel((px, py), (0, lum, 0))
px += 1
if px >= w:
px = 0
py += 1
if py >= h:
break
image = image.resize((w, 4*h))
plt.imshow(image)
filename=name.replace(".wav",".png")
plt.savefig(IMGFOLDER+filename)
return filename
def noaa(name:str):
fs, data = wav.read(SAVESFOLDER+name)
data_crop = data[20*fs:21*fs]
resample = 4
data = data[::resample]
fs = fs//resample
analytical_signal = signal.hilbert(data)
data_am = np.abs(analytical_signal)
frame_width = int(0.5*fs)
w, h = frame_width, data_am.shape[0]//frame_width
image = Image.new('RGB', (w, h))
px, py = 0, 0
for p in range(data_am.shape[0]):
lum = int(data_am[p]//32 - 32)
if lum < 0: lum = 0
if lum > 255: lum = 255
image.putpixel((px, py), (0, lum, 0))
px += 1
if px >= w:
px = 0
py += 1
if py >= h:
break
image = image.resize((w, 4*h))
plt.imshow(image)
filename=name.replace(".wav",".png")
plt.savefig(IMGFOLDER+filename)
return filename
@app.route("/",methods=['GET','POST'])
def main():
if not os.path.exists(SAVESFOLDER):
print("exist")
os.mkdir(SAVESFOLDER)
if request.method == 'POST':
name="wavnoaa"+str(datetime.datetime.now()).replace(" ","").replace("-","").replace(":","").replace(".","")+".wav"
file = request.files["file"]
file.save(SAVESFOLDER+name)
try:
resample=request.form["resample"]
print(resample,type(resample))
except :
resample=None
if resample:
return redirect("outr/"+name)
else:
return redirect("out/"+name)
return render_template("index.html")
@app.route("/outr/<string:name>")
def outr(name):
filename=noaaResample(name)
return render_template("out.html",name="img/"+filename)
@app.route("/out/<string:name>")
def out(name):
filename=noaa(name)
return render_template("out.html",name="img/"+filename)
try:
if __name__ == '__main__':
app.run(debug=True,host="0.0.0.0",port=9600)
except :
if __name__ == '__main__':
app.run(debug=True,host="0.0.0.0",port=9600)