-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto_json.py
66 lines (55 loc) · 2.05 KB
/
to_json.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
import json
import subprocess
import keras
import pandas as pd
import numpy as np
from flask import Flask, jsonify
import tensorflow as tf
from keras.models import load_model
import librosa
import librosa.display
import matplotlib.pyplot as plt
import soundfile as sd
from pydub import AudioSegment
import soundfile as sf
import moviepy.editor as moviepy
def convert_webm_to_wav(file):
command = ['ffmpeg', '-i', file, '-acodec', 'pcm_s16le', '-ac', '1', '-ar', '16000', './voice.wav']
subprocess.run(command,stdout=subprocess.PIPE,stdin=subprocess.PIPE)
def voice():
class_names = ['POP', 'R&B_Soul', '랩_힙합', '록_메탈', '발라드', '일렉트로니카']
# src = "./voice.wav"
# dst = "./test.wav"
# audSeg = AudioSegment.from_mp3("./voice.wav")
# audSeg.export(dst, format="wav")
# sr= 22050
# sf.write('test.wav', 'voice.webm', sr, format='WAV', endian='LITTLE', subtype='PCM_16') # 깨지지 않음
#채널 : 1채널
#clip = moviepy.VideoFileClip('voice.webm')
#clip.audio.write_audiofile("./voice.wav")
convert_webm_to_wav('voice.webm')
y, sr = librosa.load('voice.wav')
# y, sr = sd.read("voice.wav")
S = librosa.feature.melspectrogram(y, sr=sr)
S_DB = librosa.amplitude_to_db(S, ref=np.max)
librosa.display.specshow(S_DB, sr=sr, hop_length=512)
plt.savefig("t.png")
img = tf.keras.utils.load_img("t.png", target_size=(653, 979))
img_array = tf.keras.utils.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)
VGGmodel2 = load_model('Music_scketch_final_model.hdf5')
predictions = VGGmodel2.predict(img_array)
plt.imshow(img)
score = tf.nn.softmax(predictions[0])
genre = class_names[np.argmax(score)]
return genre
def csv_genre(genre):
print(genre)
data = pd.read_csv('music_list.csv')
df = pd.DataFrame(data)
genre_df = df.loc[(df['genre'] == genre)]
genre_df30 = genre_df[:30]
# genre_df30 = genre_df.sample(n=30)
s_id = genre_df30[['song_id']]
json = s_id.to_json(orient='records', force_ascii=False)
return json