Access denied when using pydub - python

I want to use this script to convert a mp3 file to a wav file.
import pydub
from pydub import AudioSegment
pydub.AudioSegment.converter=r"C:\Users\sunha\ffmpeg-4.1-win64-static\bin"
sound = AudioSegment.from_mp3("筷子兄弟 - 小苹果.mp3")
sound.export("筷子兄弟 - 小苹果.wav", format="wav")
But the problem is my access is denied.
Traceback (most recent call last):
File "<ipython-input-1-5faa7bcb6b97>", line 1, in <module>
runfile('C:/Users/sunha/project-001/untitled1.py', wdir='C:/Users/sunha/project-001')
File "E:\program\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
execfile(filename, namespace)
File "E:\program\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/sunha/project-001/untitled1.py", line 4, in <module>
sound = AudioSegment.from_mp3("筷子兄弟 - 小苹果.mp3")
File "E:\program\lib\site-packages\pydub\audio_segment.py", line 716, in from_mp3
return cls.from_file(file, 'mp3', parameters=parameters)
File "E:\program\lib\site-packages\pydub\audio_segment.py", line 697, in from_file
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
File "E:\program\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 143, in __init__
super(SubprocessPopen, self).__init__(*args, **kwargs)
File "E:\program\lib\subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "E:\program\lib\subprocess.py", line 1178, in _execute_child
startupinfo)
PermissionError: [WinError 5] 拒绝访问。
How can I possibly fix the problem?

I tried out multiple solutions, which helped me, as follows:
Rebooting the computer and/or IDE,
Updating Python, IDE, computer,
pip install pydirectory (Command/Anaconda prompt, run as administrator)
Applying read permissions to the file,
Specifying the path to the file, and adding 'r' before the path, to indicate you want to 'read' the audio file, example at the bottom.
I hope this helps you or at least someone else running into this error.
Best of luck.
Source
Example, run in the same folder as the audio file:
from pydub import AudioSegment
import os
os.chmod('audio2.mp3', 777) #You might not need this
sound = AudioSegment.from_mp3(r"D:/audio.mp3")
#Put the 'r' in front of the filepath, to 'read' the audio file.
sound.export("audio.wav", format="wav")

Related

Python tempfile.TemporaryDirectory() cleanup crashes with PermissionError and NotADirectoryError

Premise
I'm trying to convert some PDF to images via pdf2image and poppler, to then run some computervision tasks on.
The conversion itsself works fine.
However, the conversion creates some artifacts for each page in the pdf as it is being converted, which I would like to be deleted at the end of the function. To facilitate this, I am using tempfile.TemporaryDirectory(). The function looks as follow:
with tempfile.TemporaryDirectory() as path:
images_from_path: [Image] = convert_from_path(
os.path.join(path_superfolder, "calibration_target.pdf"),
size=(2480, 3508),
output_folder=path, poppler_path=r'E:\poppler-22.04.0\Library\bin')
if len(images_from_path) >= page:
images_from_path[page - 1].save(os.path.join(path_superfolder, "result.jpg"))
Problem
The trouble is, that the program always crashes with the following errors, after transforming the PDF and writing the required image to a file.
Traceback (most recent call last):
File "C:\Python310\lib\shutil.py", line 617, in _rmtree_unsafe
os.unlink(fullname)
PermissionError: [WinError 32] The process cannot access the file, because it is being used by another process: 'C:\\Users\\tobia\\AppData\\Local\\Temp\\tmp24c4bmzv\\bd76d834-672e-49fc-ac30-7751b7b660d0-01.ppm'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python310\lib\tempfile.py", line 843, in onerror
_os.unlink(path)
PermissionError: [WinError 32] The process cannot access the file, because it is being used by another process: 'C:\\Users\\tobia\\AppData\\Local\\Temp\\tmp24c4bmzv\\bd76d834-672e-49fc-ac30-7751b7b660d0-01.ppm'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python310\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "E:\PyCharm 2022.2.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "E:\PyCharm 2022.2.3\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:\Dokumente\Uni\Informatik\BA_Thesis\tumexam-scheduling-codebase\generate_data.py", line 393, in <module>
extract_calibration_page_as_image_from_pdf()
File "D:\Dokumente\Uni\Informatik\BA_Thesis\tumexam-scheduling-codebase\generate_data.py", line 190, in extract_calibration_page_as_image_from_pdf
tmp_dir.cleanup()
File "C:\Python310\lib\tempfile.py", line 873, in cleanup
self._rmtree(self.name, ignore_errors=self._ignore_cleanup_errors)
File "C:\Python310\lib\tempfile.py", line 855, in _rmtree
_shutil.rmtree(name, onerror=onerror)
File "C:\Python310\lib\shutil.py", line 749, in rmtree
return _rmtree_unsafe(path, onerror)
File "C:\Python310\lib\shutil.py", line 619, in _rmtree_unsafe
onerror(os.unlink, fullname, sys.exc_info())
File "C:\Python310\lib\tempfile.py", line 846, in onerror
cls._rmtree(path, ignore_errors=ignore_errors)
File "C:\Python310\lib\tempfile.py", line 855, in _rmtree
_shutil.rmtree(name, onerror=onerror)
File "C:\Python310\lib\shutil.py", line 749, in rmtree
return _rmtree_unsafe(path, onerror)
File "C:\Python310\lib\shutil.py", line 600, in _rmtree_unsafe
onerror(os.scandir, path, sys.exc_info())
File "C:\Python310\lib\shutil.py", line 597, in _rmtree_unsafe
with os.scandir(path) as scandir_it:
NotADirectoryError: [WinError 267] Directory name invalid: 'C:\\Users\\tobia\\AppData\\Local\\Temp\\tmp24c4bmzv\\bd76d834-672e-49fc-ac30-7751b7b660d0-01.ppm'
When stepping through the cleanup routine, everything seems fine, the path is correct and it starts deleting files, until at some point the internal path variable gets jumbled up and the routine crashes, because obviously a file is not a directory. To me it seems like a race condition is causing problems here.
What I have already tried
Rewriting the function to not use with and instead explicitly call the routine with tmp_dir.cleanup()
Just creating the directory without populating it with the conversion artifacts. The cleanup works in this case.
The documentation for tempfile mentions Permission errors occuring when files are open. The files are however only used in this function and if this is what is causing the error, I am unsure where the files are still opened or which function is causing this. My suspicion of course would be the conversion function.
While experimenting some more and writing this question, I found a working solution:
with tempfile.TemporaryDirectory() as path:
images_from_path: [Image] = convert_from_path(
os.path.join(path_superfolder, f"calibration_target_{exam_type}.pdf"),
size=(2480, 3508),
output_folder=path, poppler_path=r'E:\poppler-22.04.0\Library\bin')
if len(images_from_path) >= page:
images_from_path[page - 1].save(os.path.join(path_superfolder, "result.jpg"))
images_from_path = []
It seems that somehow, the routine had trouble cleaning up, because the converted images, are actually the artifacts created by pdf2image and were still being held by my data structure. Resetting the data structure, before implicitly initiating the cleanup fixed the issue.
If there is a better way of tackling this issue, please do not hesitate to inform me.

FileNotFoundError: [WinError 2] The system cannot find the file specified using whisper

I've been trying to use OpenAI's whisper to transcribe some text.
Whenever I run, I get a FileNotFounderror.
My code is as follows:
import whisper
import os
print(os.listdir())
# f = open('test_text.txt', 'r')
# content = f. read()
# print(content)
# f. close()
audio = 'Users/geoff/Downloads/micro-machines.wav'
model = whisper.load_model("tiny")
result = model.transcribe('micro-machines.wav', fp16=False)
print(result['text'])
The commented out part with the opening of the text file was done as a test and runs each time without issue.
When the code prints the directory, I get the following correct list of files, But then the error follows immediarely after:
['.idea', 'main.py', 'micro-machines.wav', 'test_text.txt', 'venv']
Traceback (most recent call last):
File "C:\Users\geoff\PycharmProjects\pythonProject3\main.py", line 16, in <module>
result = model.transcribe('micro-machines.wav', fp16=False)
File "C:\Users\geoff\PycharmProjects\pythonProject3\venv\lib\site-packages\whisper\transcribe.py", line 82, in transcribe
mel = log_mel_spectrogram(audio)
File "C:\Users\geoff\PycharmProjects\pythonProject3\venv\lib\site-packages\whisper\audio.py", line 111, in log_mel_spectrogram
audio = load_audio(audio)
File "C:\Users\geoff\PycharmProjects\pythonProject3\venv\lib\site-packages\whisper\audio.py", line 42, in load_audio
ffmpeg.input(file, threads=0)
File "C:\Users\geoff\PycharmProjects\pythonProject3\venv\lib\site-packages\ffmpeg\_run.py", line 313, in run
process = run_async(
File "C:\Users\geoff\PycharmProjects\pythonProject3\venv\lib\site-packages\ffmpeg\_run.py", line 284, in run_async
return subprocess.Popen(
File "C:\Users\geoff\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\geoff\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
There seems to be a mismatch in the file paths you've used for the commented part as observed from the output:
C:\Users\geoff\ PycharmProjects \ pythonProject3
and the following part.
audio = 'Users/geoff/Downloads/micro-machines.wav'
You may verify where the .wav file exists - in 'Downloads' or in 'pythonProject3' folder and try again.

Having issue with subprocess library in python during text extraction from image using OCR

I have been using python OCR code to extract text from the image but i am getting some error. I think error is with subprocess library, however it is built in library. So i couldn't figure out the error exactly. can anyone please help me in resolving this error. My code and error are as below.
OCR code
import os
import tempfile
import subprocess
def ocr(path):
temp = tempfile.NamedTemporaryFile(delete=False)
process = subprocess.Popen(['tesseract', path, temp.name], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
process.communicate()
with open(temp.name + '.txt', 'r') as handle:
contents = handle.read()
os.remove(temp.name + '.txt')
os.remove(temp.name)
return contents
str = ocr("C:\\Users\\hp\\Desktop\\MS Thesis\\opencv-text-detection\\opencv-text-detection\\images\\sign.jpg")
print(str)
by executing th above code I got following error
[7]: runfile('C:/Users/hp/Desktop/MS Thesis/python text detection.py', wdir='C:/Users/hp/Desktop/MS Thesis')
Traceback (most recent call last):
File "", line 1, in
runfile('C:/Users/hp/Desktop/MS Thesis/python text detection.py', wdir='C:/Users/hp/Desktop/MS Thesis')
File "C:\Users\hp\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
execfile(filename, namespace)
File "C:\Users\hp\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/hp/Desktop/MS Thesis/python text detection.py", line 26, in
str = ocr("C:\Users\hp\Desktop\MS Thesis\opencv-text-detection\opencv-text-detection\images\sign.jpg")
File "C:/Users/hp/Desktop/MS Thesis/python text detection.py", line 15, in ocr
process = subprocess.Popen(['tesseract', path, temp.name], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
File "C:\Users\hp\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 171, in init
super(SubprocessPopen, self).init(*args, **kwargs)
File "C:\Users\hp\Anaconda3\lib\subprocess.py", line 769, in init
restore_signals, start_new_session)
File "C:\Users\hp\Anaconda3\lib\subprocess.py", line 1172, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
I have been using Python 3.7.1 with anaconda on window 7
Look like there is an error because it cannot found the file
if the file is in the same directory I will recommend you use:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.path.join(BASE_DIR, 'file_dir')

File used by another process error: Python

I am trying to change the contents of a destination file with the help of some source file. When the work is done, I am making sure that I have closed all the files in code (ofcourse, I have made sure that it is not opened in any editor too.)
def write_to_file(self, _source_path, _destination_path):
f_source = open(_source_path, 'r')
f_destination = open(_destination_path, 'r')
f_temp = open(self.temp_path, 'w+')
while source_line or destination_line:...
f_temp.close()
f_destination.close()
f_source.close()
shutil.move(self.temp_path, _destination_path)
return
I am getting below error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Program Files\JetBrains\PyCharm\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:\Program Files\JetBrains\PyCharm\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/rohit.tayal/PycharmProjects/AutoNVMerge/MainActivity.py", line 151, in <module>
script.write_to_file(source_path, destination_path)
File "C:/Users/rohit.tayal/PycharmProjects/AutoNVMerge/MainActivity.py", line 62, in write_to_file
shutil.move(self.temp_path, _destination_path)
File "C:\Anaconda\envs\AutoNVMerge\lib\shutil.py", line 581, in move
os.unlink(src)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\auto_nv_merge.xml'
C:\auto_nv_merge.xml is my temp file.
When working with
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\auto_nv_merge.xml'
you have 2 options, be a hero and try to hunt down the process that is holding that file hostage or just restart your computer.
I usually go with the latter, restart

Can´t save matplotlib figure to .eps in Windows while using text.usetex : True

When I run the following code: http://dpaste.com/0210P09 everything works fine on my Linux distro.
However, in Windows7 (64bit) I cannot successfully save it in eps or svg.
This is the error that I get:
Traceback (most recent call last):
File "<ipython-input-1-b8da411d11b0>", line 1, in <module>
runfile('C:/Users/12151056/Documents/Python Scripts/MyPython/1Tplot_2.py', wdir='C:/Users/12151056/Documents/Python Scripts/MyPython')
File "C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "C:/Users/12151056/Documents/Python Scripts/MyPython/1Tplot_2.py", line 67, in <module>
plt.savefig('excel-6.eps')
File "C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 577, in savefig
res = fig.savefig(*args, **kwargs)
File "C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\figure.py", line 1476, in savefig
self.canvas.print_figure(*args, **kwargs)
File "C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\backend_bases.py", line 2211, in print_figure
**kwargs)
File "C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py", line 1009, in print_eps
return self._print_ps(outfile, 'eps', *args, **kwargs)
File "C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py", line 1033, in _print_ps
**kwargs)
File "C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py", line 1398, in _print_figure_tex
rotated=psfrag_rotated)
File "C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\backends\backend_ps.py", line 1556, in gs_distill
your image.\nHere is the full report generated by ghostscript:\n\n' + fh.read())
TypeError: Can't convert 'bytes' object to str implicitly
If i use text.usetex : False I have no problem at all.
I have livetex full and Ghostscript installed.
Any suggestions to try and solve it?
There are two problems here. The first and minor one is that in
C:\Users\12151056\AppData\Local\Continuum\Anaconda3\lib\site-package\matplotlib\backends\backend_ps.py", line 1556
you should change
fh.read()
to
fh.read().decode()
This is a Python 2/3 bug, that occurs while handling the fact that something with Ghostscript went wrong.
The main problem is that matplotlib cannot find Ghostscript because it is not in the path environmental variable. You can check that by opening a command prompt and trying the command gswin32c or gs. If these commands are not found you have to add the path of these Ghostscript executables to the path environmental variable. In my case it was:
C:\Program Files (x86)\gs\gs8.54\bin
but it depends on your installation path.

Categories

Resources