Showing
1 changed file
with
70 additions
and
0 deletions
소스코드/Demo.py
0 → 100644
1 | +import cv2 | ||
2 | +import numpy as np | ||
3 | +import pyaudio | ||
4 | +import librosa | ||
5 | +import librosa.display | ||
6 | +import matplotlib.pyplot as plt | ||
7 | +import time | ||
8 | +import tensorflow as tf | ||
9 | +import tensorflow.keras as keras | ||
10 | + | ||
11 | +CATEGORES = ["angry", "happy", "lonely", "sad"] | ||
12 | + | ||
13 | +model = keras.load_model("yout model path") | ||
14 | + | ||
15 | +rate = 16000 | ||
16 | +chunk_size = rate // 4 | ||
17 | + | ||
18 | +""" | ||
19 | + 생성된 데이터를 모델에 맞게 변경함 | ||
20 | + @param filepath(str) : path where your image file is | ||
21 | + @return array that has reshaped for predict | ||
22 | +""" | ||
23 | +def prepare(filepath): | ||
24 | + img_array = cv2.imread(filepath, cv2.IMREAD_COLOR) | ||
25 | + new_array = cv2.resize(img_array, (62, 78)) | ||
26 | + return new_array.reshape(-1, 62, 78, 1) | ||
27 | + | ||
28 | +p = pyaudio.PyAudio() | ||
29 | +stream = p.open(format=pyaudio.paFloat32, | ||
30 | + channels=1, | ||
31 | + rate=rate, | ||
32 | + input=True, | ||
33 | + input_device_index=1, | ||
34 | + frames_per_buffer=chunk_size) | ||
35 | + | ||
36 | + | ||
37 | +frames = [] | ||
38 | + | ||
39 | +plt.figure(figsize=(10, 4)) | ||
40 | +do_melspec = librosa.feature.melspectrogram | ||
41 | +pwr_to_db = librosa.core.power_to_db | ||
42 | + | ||
43 | +while True: | ||
44 | + | ||
45 | + start = time.time() | ||
46 | + | ||
47 | + data = stream.read(chunk_size) | ||
48 | + data = np.fromstring(data, dtype=np.float32) | ||
49 | + | ||
50 | + melspec = do_melspec(y=data, sr=rate, n_mels=128, fmax=4000) | ||
51 | + norm_melspec = pwr_to_db(melspec, ref=np.max) | ||
52 | + | ||
53 | + frames.append(norm_melspec) | ||
54 | + | ||
55 | + if len(frames) == 20: | ||
56 | + | ||
57 | + | ||
58 | + stack = np.hstack(frames) | ||
59 | + librosa.display.specshow(stack, fmax=4000) | ||
60 | + plt.savefig('your save path' + '.jpg', dpi=300) | ||
61 | + prediction = model.predict([prepare('path where your image is')]) | ||
62 | + print(CATEGORES[int(prediction[0][0])]) | ||
63 | + plt.draw() | ||
64 | + plt.pause(0.0001) | ||
65 | + plt.clf() | ||
66 | + #break | ||
67 | + frames.pop(0) | ||
68 | + | ||
69 | + | ||
70 | + t = time.time() - start |
-
Please register or login to post a comment