how to convert mp3 to wav and calculate psnr in python - python

i want to convert my mp3 to wav file so i can input it on psnr. but when i tried to convert it, i cannot do that. can someone check my code? i was trying this code to convert and the psnr code from here
import scipy.io.wavfile as wavfile
import os.path
from subprocess import call
if (not os.path.isfile(file)):
wavConvertCommand = \
[file,
"-i", fileMp3, "-acodec", "pcm_u8", "-ar", "22050", fileWav]
call(wavConvertCommand)
def snr(file):
if (os.path.isfile(file)):
data = wavfile.read(file)[1]
singleChannel = data
try:
singleChannel = numpy.sum(data, axis=1)
except:
# was mono after all
pass
norm = singleChannel / (max(numpy.amax(singleChannel), -1 * numpy.amin(singleChannel)))
return stats.signaltonoise(norm)
my file location
F:\KULIAH\SEMESTER8\SKRIPSI\MusicLockApp\media\mp3\ytmp3free.cc_bebe-rexha-meant-to-be-lyrics-ft-florida-georgia-line-youtubemp3free.org_1_2uxNfcC.mp3

Related

Batch processing of Tiff files using skimage (Python)

I am looking to; open, process and save multiple TIFFs in Python.
I have the following code to open, process and save 1 (one) TIFF, but I have trouble with multiple files:
import skimage.io
import skimage.viewer
import skimage
import skimage.io
# Read 1 image.TIF:
image = skimage.io.imread(fname=path)
image[2,1]= 1.0
# Process the file (make binary)
gray_image = skimage.color.rgb2gray(image)
# Blur the image to denoise (larger sigma = more noise removed)
blurred_image = skimage.filters.gaussian(gray_image, sigma=5)
# Adding threshold, t:
t = 0.8
binary_mask = blurred_image < t
# Save the file to another location:
skimage.io.imsave(fname=path, arr = binary_mask)
Any help is appreciated!
Here's a multiprocessing approach that may help:
import skimage
from concurrent.futures import ProcessPoolExecutor
from glob import glob
import os.path
source_dir = '<your source directory>'
target_dir = '<your target directory>'
filetype = '*.tif'
def process(path):
image = skimage.io.imread(fname=path)
image[2,1] = 1.0
gray_image = skimage.color.rgb2gray(image)
blurred_image = skimage.filters.gaussian(gray_image, sigma=5)
outpath = os.path.join(target_dir, os.path.basename(path))
arr = blurred_image < 0.8
skimage.io.imsave(fname=outpath, arr=arr)
def main():
with ProcessPoolExecutor() as executor:
filelist = glob(os.path.join(source_dir, filetype))
executor.map(process, filelist)
if __name__ == '__main__':
main()
Use glob to identify all the files matching the *.tif pattern then utilise the ProcessPoolExecutor's map function to process each file in its own process. As the processing is mainly CPU intensive, multiprocessing is likely to be the best fit for this
Is it necessary that this be parallelized? It's not a huge bit of processing that you are performing. If you don't need parallel processing you can just run a for loop on your images
import skimage.io
import skimage.viewer
import skimage
import skimage.io
import os
import glob
# set up an in and out directory
in_dir = 'directory\with\images'
out_ir = 'directory\for\procecessed\images'
# make a list of all of the raw image files
os.chdir(in_dir)
filelist = glob.glob('*.png') # change to whatever file pattern you need here
for file_iter in filelist:
os.chdir(in_dir)
image = skimage.io.imread(fname=file_iter)
image[2,1]= 1.0
# Process the file (make binary)
gray_image = skimage.color.rgb2gray(image)
# Blur the image to denoise (larger sigma = more noise removed)
blurred_image = skimage.filters.gaussian(gray_image, sigma=5)
# Adding threshold, t:
t = 0.8
binary_mask = blurred_image < t
# Save the file to another location:
out_filename = file_iter[:-4] + 'processed.png' # make new filename based on old filename
os.chdir(out_dir)
skimage.io.imsave(fname=out_filename, arr = binary_mask)

I want to process bulk of wav files at a time and convert them all into text like hindi english tamil etc by using python programming

import speech_recognition as sr
import os
from pydub import AudioSegment
from pydub.silence import split_on_silence
r = sr.Recognizer()
def get_large_audio_transcription(path,):
"""
Splitting the large audio file into chunks
and apply speech recognition on each of these chunks
"""
sound = AudioSegment.from_wav(path)
chunks = split_on_silence(sound,
min_silence_len = 500,
silence_thresh = sound.dBFS-16,
keep_silence=500,
)
folder_name = "audio-chunks"
if not os.path.isdir(folder_name):
os.mkdir(folder_name)
whole_text = ""
for i, audio_chunk in enumerate(chunks, start=1):
chunk_filename = os.path.join(folder_name, f"chunk{i}.wav")
audio_chunk.export(chunk_filename, format="wav")
with sr.AudioFile(chunk_filename) as source:
audio_listened = r.record(source)
try:
text = r.recognize_google(audio_listened,language="hi-IN")
except sr.UnknownValueError as e:
print("Error:", str(e))
else:
text = f"{text.capitalize()}. "
print(chunk_filename, ":", text)
whole_text += text
return whole_text
path = "G:\ezimex\audiowav\wav1.wav"
print("\nFull text:",get_large_audio_transcription(path))
Using this code I pass one audio at a time but it takes to much time to process all audio one by one into text.
I want to process all wav files process at once and covert all into text. please help me to solve it using python. the all audio files stores in a folder audiowav.
How to process these bulked files at once using python and convert into text?

Convert .mat file to image in google colab

I'm currently trying to converting the images from a .mat file to .jpg file downloaded from this site- BrainTumorDataset. All the files contained in the directory are .mat files, now I want to convert all the files in .jpg format via python for making a project: Brain Tumor Classification via CNN. I searched in StackOverflow and got a answer. But I cannot do using their code in the answer part. Here I used the code-
from os import path
import os
from matplotlib import pyplot as plt
import numpy as np
import h5py
from PIL import Image
import re
import sys
from glob import glob
dir_path = path.dirname(path.abspath('/content/drive/MyDrive/Colab Notebooks/dataset/brain tumor/New folder'))
path_to_mat_files = path.join(dir_path, "*.mat")
found_files = glob(path_to_mat_files, recursive=True)
total_files = 0
def convert_to_png(file: str, number: int):
global total_files
if path.exists(file):
print(file, "already exist\nSkipping...")
else:
h5_file = h5py.File(file, 'r')
png = file[:-3] + "png"
cjdata = h5_file['cjdata']
image = np.array(cjdata.get('image')).astype(np.float64)
label = cjdata.get('label')[0,0]
PID = cjdata.get('PID')
PID = ''.join(chr(c) for c in PID)
tumorBorder = np.array(cjdata.get('tumorBorder'))[0]
tumorMask = np.array(cjdata.get('tumorMask'))
h5_file.close()
hi = np.max(image)
lo = np.min(image)
image = (((image - lo)/(hi-lo))*255).astype(np.uint8)
im = Image.fromarray(image)
im.save(png)
os.system(f"mv {png} {dir_path}\\png_images")#make sure folder png_images exist
total_files += 1
print("saving", png, "File No: ", number)
for file in found_files:
if "cvind.mat" in file:
continue
convert_to_png(file, total_files)
print("Finished converting all files: ", total_files)
and I got the msg :
/content/drive/MyDrive/Colab Notebooks/dataset/brain tumor/cvind (1).mat already exist
Skipping...
Finished converting all files: 0
Here is my colab notebook link. and here is the dataset link in my google drive. I want to convert .mat images in .jpg format. How can I do this? What should I do to convert .mat images in .jpg format?

OpenCV read video files with multiple streams/tracks

I have a video file that contains multiple streams as shown below using VLC media player:
Video Information
When I try to read it using Python + OpenCV using the following code:
vidObj = cv2.VideoCapture("video.avi")
ret, frame = vidObj.read()
I can only read the first track of the video. How can I read all the video tracks at the same time?
As far as I could tell, OpenCV does not allow choosing video stream, so this is not possible. However, you can do it rather easily with ffmpeg command line utilities:
import numpy as np
import json
import subprocess
def videoInfo(filename):
proc = subprocess.run([
*"ffprobe -v quiet -print_format json -show_format -show_streams".split(),
filename
], capture_output=True)
proc.check_returncode()
return json.loads(proc.stdout)
def readVideo(filename):
cmd = ["ffmpeg", "-i", filename]
streams = 0
for stream in videoInfo(filename)["streams"]:
index = stream["index"]
if stream["codec_type"] == "video":
width = stream["width"]
height = stream["height"]
cmd += "-map", f"0:{index}"
streams = streams + 1
cmd += "-f", "rawvideo", "-pix_fmt", "rgb24", "-"
shape = np.array([streams, height, width, 3])
with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc:
while True:
data = proc.stdout.read(shape.prod()) # One byte per each element
if not data:
return
yield np.frombuffer(data, dtype=np.uint8).reshape(shape)
Note that the code reads all video streams and assumes that each has the same resolution. It lacks proper error handling but got the job done in my scientific project.
For example, reading stereoscopic stream:
import matplotlib.pyplot as plt
for left, right in readVideo("testvideo.mkv"):
plt.imshow(left)
plt.show()
plt.imshow(right)
plt.show()

Pull random image from directory, encode to base64 and then print

I'm having difficult time trying to work these two together. It's frustrating me a little, so I hope to find ideas/solutions.
The complete works (what I'm planning on) should grab a random image from an online directory, encode it to base64 then print the base64. I've had total curl madness going all day and now I'm turning to python. Onwards!
These are kinda just notes at the minute but should explain the process.
import random, os
import base64
def search(): #get file
path = r"/Users/Impshum/Pictures" #should be able to http
random_filename = random.choice([
x for x in os.listdir(path)
if os.path.isfile(os.path.join(path, x))
])
print(random_filename) #not printing full location
def encode(): #encode to base64
image = open('heaven.jpg', 'rb')
image_read = image.read()
image_64_encode = base64.encodestring(image_read)
print image_64_encode
search() #notes
encode() #notes
Many thanks in advance.
You have most of the code you need
import random, os
import base64
def search(path): #get file
random_filename = random.choice([
x for x in os.listdir(path)
if os.path.isfile(os.path.join(path, x))
])
return os.path.join(path, random_filename)
def encode(path):
image = open(path, 'rb')
image_read = image.read()
image.close()
image_64_encode = base64.encodestring(image_read)
return image_64_encode
print(encode(search(r"/Users/Impshum/Pictures")))
There are things you can do to make this "nicer", but this should get you started.
For instance, you might want to use glob instead of os.listdir / os.path.join, etc. And using a context manager
import glob
import base64
import random
def search(path): #get file
random_filename = random.choice(glob.glob(path))
return random_filename
def encode(path):
with open(path, 'rb') as image:
image_read = image.read()
image_64_encode = base64.encodestring(image_read)
return image_64_encode
print(encode(search(r"/Users/Impshum/Pictures/*")))
Error handling is left as an exercise to the OP

Categories