Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -64,17 +64,64 @@ dict_frog_desc={'American_bull':'赤蛙科,是一種水生青蛙。是現存
|
|
64 |
|
65 |
loaded_model = joblib.load('./RFC_model')
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
def flog(A):
|
68 |
test_scaled = FeatureExtraction(A)
|
69 |
result = loaded_model.predict(test_scaled)
|
70 |
img=cv2.imread('./'+labels[result[0]]+'.jpg')
|
71 |
img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
#return dict_frog[labels[result[0]]],img
|
73 |
-
return dict_frog[labels[result[0]]],dict_frog_desc[labels[result[0]]],img
|
74 |
|
75 |
output_label = gr.Label()
|
76 |
output_desc = gr.Label()
|
77 |
output_image = gr.Image()
|
|
|
|
|
78 |
|
79 |
title="台灣青蛙聲音辨識"
|
80 |
|
@@ -104,6 +151,6 @@ gr.Interface(flog,
|
|
104 |
inputs=[
|
105 |
gr.inputs.Audio(source="microphone", type="filepath"),
|
106 |
],
|
107 |
-
outputs=[output_label,output_desc,output_image],title=title,description=description).launch()
|
108 |
#outputs=[output_label,output_image],title=title,description=description).launch()
|
109 |
|
|
|
64 |
|
65 |
loaded_model = joblib.load('./RFC_model')
|
66 |
|
67 |
+
def Audio(fname,SAMPLE_RATE):
|
68 |
+
y, sr = librosa.load(fname, sr=SAMPLE_RATE)
|
69 |
+
return y, sr
|
70 |
+
|
71 |
+
def AudioImg(y,sr):
|
72 |
+
img=librosa.display.waveplot(y, sr)
|
73 |
+
fig = plt.figure()
|
74 |
+
plot_img_np = get_img_from_fig(fig)
|
75 |
+
return plot_img_np
|
76 |
+
|
77 |
+
def MFCC_spectrogram(y,sr):
|
78 |
+
# 提取頻域音頻資料 MFCC spectrogram feature
|
79 |
+
melspec = librosa.feature.melspectrogram(y, sr, n_fft=1024, hop_length=512, n_mels=128)
|
80 |
+
# 轉成 log scale 已呈現頻譜資料
|
81 |
+
logmelspec = librosa.power_to_db(melspec)
|
82 |
+
# 繪圖
|
83 |
+
img=librosa.display.specshow(logmelspec, sr=sr, x_axis='time', y_axis='mel')
|
84 |
+
fig = plt.figure()
|
85 |
+
plot_img_np = get_img_from_fig(fig)
|
86 |
+
#plt.title('Features of unknow frog by MFCC (Mel Frquency Cepstral Coefficients)')
|
87 |
+
#plt.show()
|
88 |
+
return plot_img_np
|
89 |
+
|
90 |
+
def get_img_from_fig(fig, dpi=180):
|
91 |
+
buf = io.BytesIO()
|
92 |
+
fig.savefig(buf, format="png", dpi=dpi)
|
93 |
+
buf.seek(0)
|
94 |
+
img_arr = np.frombuffer(buf.getvalue(), dtype=np.uint8)
|
95 |
+
buf.close()
|
96 |
+
img = cv2.imdecode(img_arr, 1)
|
97 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
98 |
+
return img
|
99 |
+
|
100 |
+
|
101 |
def flog(A):
|
102 |
test_scaled = FeatureExtraction(A)
|
103 |
result = loaded_model.predict(test_scaled)
|
104 |
img=cv2.imread('./'+labels[result[0]]+'.jpg')
|
105 |
img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
106 |
+
|
107 |
+
y,sr=Audio(A,SAMPLE_RATE)
|
108 |
+
fig = plt.figure()
|
109 |
+
plt.title("waveplot",fontsize=20)
|
110 |
+
AudioImg(y,sr)
|
111 |
+
img2 = get_img_from_fig(fig)
|
112 |
+
|
113 |
+
fig = plt.figure()
|
114 |
+
plt.title("MFCC Feature",fontsize=20)
|
115 |
+
MFCC_spectrogram(y,sr)
|
116 |
+
img3 = get_img_from_fig(fig)
|
117 |
#return dict_frog[labels[result[0]]],img
|
118 |
+
return dict_frog[labels[result[0]]],dict_frog_desc[labels[result[0]]],img,img2,img3
|
119 |
|
120 |
output_label = gr.Label()
|
121 |
output_desc = gr.Label()
|
122 |
output_image = gr.Image()
|
123 |
+
output_image2 = gr.Image()
|
124 |
+
output_image3 = gr.Image()
|
125 |
|
126 |
title="台灣青蛙聲音辨識"
|
127 |
|
|
|
151 |
inputs=[
|
152 |
gr.inputs.Audio(source="microphone", type="filepath"),
|
153 |
],
|
154 |
+
outputs=[output_label,output_desc,output_image,output_image2,output_image3],title=title,description=description).launch()
|
155 |
#outputs=[output_label,output_image],title=title,description=description).launch()
|
156 |
|