admin commited on
Commit
580abb7
·
1 Parent(s): b9a5e1d

fixing jpgs

Browse files
Files changed (2) hide show
  1. app.py +24 -8
  2. utils.py +2 -2
app.py CHANGED
@@ -8,8 +8,9 @@ import numpy as np
8
  import gradio as gr
9
  import librosa.display
10
  import matplotlib.pyplot as plt
11
- from utils import get_modelist, find_wav_files, embed_img
12
  from model import EvalNet
 
13
 
14
 
15
  TRANSLATE = {
@@ -39,7 +40,7 @@ def wav2mel(audio_path: str, width=0.496145124716553):
39
  librosa.display.specshow(log_mel_spec[:, i : i + step])
40
  plt.axis("off")
41
  plt.savefig(
42
- f"{TEMP_DIR}/output.jpg",
43
  bbox_inches="tight",
44
  pad_inches=0.0,
45
  )
@@ -65,7 +66,7 @@ def wav2cqt(audio_path: str, width=0.496145124716553):
65
  librosa.display.specshow(log_cqt_spec[:, i : i + step])
66
  plt.axis("off")
67
  plt.savefig(
68
- f"{TEMP_DIR}/output.jpg",
69
  bbox_inches="tight",
70
  pad_inches=0.0,
71
  )
@@ -91,7 +92,7 @@ def wav2chroma(audio_path: str, width=0.496145124716553):
91
  librosa.display.specshow(log_chroma_spec[:, i : i + step])
92
  plt.axis("off")
93
  plt.savefig(
94
- f"{TEMP_DIR}/output.jpg",
95
  bbox_inches="tight",
96
  pad_inches=0.0,
97
  )
@@ -101,6 +102,16 @@ def wav2chroma(audio_path: str, width=0.496145124716553):
101
  print(f"Error converting {audio_path} : {e}")
102
 
103
 
 
 
 
 
 
 
 
 
 
 
104
  def infer(wav_path: str, log_name: str, folder_path=TEMP_DIR):
105
  if os.path.exists(folder_path):
106
  shutil.rmtree(folder_path)
@@ -115,9 +126,14 @@ def infer(wav_path: str, log_name: str, folder_path=TEMP_DIR):
115
 
116
  spec = log_name.split("_")[-3]
117
  eval("wav2%s" % spec)(wav_path)
118
- input = embed_img(f"{folder_path}/output.jpg")
119
- output: torch.Tensor = model(input)
120
- pred_id = torch.max(output.data, 1)[1]
 
 
 
 
 
121
  return os.path.basename(wav_path), TRANSLATE[CLASSES[pred_id]]
122
 
123
 
@@ -125,7 +141,7 @@ if __name__ == "__main__":
125
  warnings.filterwarnings("ignore")
126
  models = get_modelist()
127
  examples = []
128
- example_wavs = find_wav_files()
129
  model_num = len(models)
130
  for wav in example_wavs:
131
  examples.append([wav, models[random.randint(0, model_num - 1)]])
 
8
  import gradio as gr
9
  import librosa.display
10
  import matplotlib.pyplot as plt
11
+ from collections import Counter
12
  from model import EvalNet
13
+ from utils import get_modelist, find_files, embed_img
14
 
15
 
16
  TRANSLATE = {
 
40
  librosa.display.specshow(log_mel_spec[:, i : i + step])
41
  plt.axis("off")
42
  plt.savefig(
43
+ f"{TEMP_DIR}/{i}.jpg",
44
  bbox_inches="tight",
45
  pad_inches=0.0,
46
  )
 
66
  librosa.display.specshow(log_cqt_spec[:, i : i + step])
67
  plt.axis("off")
68
  plt.savefig(
69
+ f"{TEMP_DIR}/{i}.jpg",
70
  bbox_inches="tight",
71
  pad_inches=0.0,
72
  )
 
92
  librosa.display.specshow(log_chroma_spec[:, i : i + step])
93
  plt.axis("off")
94
  plt.savefig(
95
+ f"{TEMP_DIR}/{i}.jpg",
96
  bbox_inches="tight",
97
  pad_inches=0.0,
98
  )
 
102
  print(f"Error converting {audio_path} : {e}")
103
 
104
 
105
+ def most_frequent_value(lst: list):
106
+ counter = Counter(lst)
107
+ max_count = max(counter.values())
108
+ for element, count in counter.items():
109
+ if count == max_count:
110
+ return element
111
+
112
+ return None
113
+
114
+
115
  def infer(wav_path: str, log_name: str, folder_path=TEMP_DIR):
116
  if os.path.exists(folder_path):
117
  shutil.rmtree(folder_path)
 
126
 
127
  spec = log_name.split("_")[-3]
128
  eval("wav2%s" % spec)(wav_path)
129
+ jpgs = find_files(folder_path, ".jpg")
130
+ preds = []
131
+ for jpg in jpgs:
132
+ input = embed_img(jpg)
133
+ output: torch.Tensor = model(input)
134
+ preds.append(torch.max(output.data, 1)[1])
135
+
136
+ pred_id = most_frequent_value(preds)
137
  return os.path.basename(wav_path), TRANSLATE[CLASSES[pred_id]]
138
 
139
 
 
141
  warnings.filterwarnings("ignore")
142
  models = get_modelist()
143
  examples = []
144
+ example_wavs = find_files()
145
  model_num = len(models)
146
  for wav in example_wavs:
147
  examples.append([wav, models[random.randint(0, model_num - 1)]])
utils.py CHANGED
@@ -18,11 +18,11 @@ def toCUDA(x):
18
  return x
19
 
20
 
21
- def find_wav_files(folder_path=f"{MODEL_DIR}/examples"):
22
  wav_files = []
23
  for root, _, files in os.walk(folder_path):
24
  for file in files:
25
- if file.endswith(".wav"):
26
  file_path = os.path.join(root, file)
27
  wav_files.append(file_path)
28
 
 
18
  return x
19
 
20
 
21
+ def find_files(folder_path=f"{MODEL_DIR}/examples", ext=".wav"):
22
  wav_files = []
23
  for root, _, files in os.walk(folder_path):
24
  for file in files:
25
+ if file.endswith(ext):
26
  file_path = os.path.join(root, file)
27
  wav_files.append(file_path)
28