-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformat_dataset.py
More file actions
61 lines (46 loc) · 1.52 KB
/
format_dataset.py
File metadata and controls
61 lines (46 loc) · 1.52 KB
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
import librosa
import librosa.display
import matplotlib.pyplot as plt
import os
import numpy as np
from PIL import Image
import torch
import torch.nn as nn
from torchvision import transforms
def resize_img(path):
# Open the image
image = Image.open(path)
# Resize the image
resized_image = image.resize((200, 200))
# Save the resized image
resized_image.save("imgs/resized_image.png")
# Call the function with the path to your image
def preprocess():
filename = os.path.join("datasets","4.wav")
print(os.path.isfile(filename))
# Load the audio file
audio, sr = librosa.load(filename)
# Compute the spectrogram
spectrogram = librosa.feature.melspectrogram(y=audio, sr=sr)
# Convert the spectrogram to dB scale
spectrogram_db = librosa.power_to_db(spectrogram, ref=np.max)
# Display the spectrogram
plt.figure(figsize=(10, 4))
librosa.display.specshow(spectrogram_db, sr=sr)
plt.savefig("imgs/test.png", bbox_inches='tight', pad_inches=0)
resize_img("imgs/test.png")
def convert_to_tensor():
resized_image = Image.open("imgs/resized_image.png")
resized_image = resized_image.convert("RGB")
# Apply the ToTensor transformation
transform = transforms.ToTensor()
image_tensor = transform(resized_image)
# Convert the integer to an integer tensor
int_tensor = torch.tensor(1)
return (image_tensor, int_tensor)
# Print the PyTorch matrix
def format():
preprocess()
tup = convert_to_tensor()
print(tup[1])
return tup