PDF to Image converter code error in Python - python

Unable to run the code in VS code with python version 3.8.1:-
Coming with below error in terminal
pdftojpg.py
import os
import tempfile
#import cv2
from pdf2image import convert_from_path
filename = 'E:/djcodes/pyproject/ID.pdf'
with tempfile.TemporaryDirectory() as path:
images_from_path = convert_from_path(filename, output_folder=path, last_page=1, first_page =0)
base_filename = os.path.splitext(os.path.basename(filename))[0] + '.jpg'
save_dir = 'E:/djcodes/pyproject/image'
for page in images_from_path:
page.save(os.path.join(save_dir, base_filename), 'JPEG')
#img = cv2.imread(save_dir+os.path.splitext(os.path.basename(filename))[0]+'jpeg')
Terminal error:-
Traceback (most recent call last):
File "c:\Users\asus\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\ptvsd_launcher.py", line 48, in <module>
main(ptvsdArgs)
File "c:\Users\asus\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 432, in main
run()
File "c:\Users\asus\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 300, in run_file
setup_connection()
File "c:\Users\asus\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 288, in setup_connection
if not daemon.wait_for_launch(addr):
File "c:\Users\asus\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\old_ptvsd\ptvsd\runner.py", line 61, in wait_for_launch
_, start_session = self.start_client(addr)
File "c:\Users\asus\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\old_ptvsd\ptvsd\daemon.py", line 217, in start_client
connect(client, addr)
File "c:\Users\asus\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\old_ptvsd\ptvsd\socket.py", line 197, in connect
sock.connect(addr)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
Please help to resolve the issue

Related

FileNotFoundError: [Errno 2] No such file or directory: '/var/lib/pgadmin/azurecredentialcache'

I have been trying to install pgadmin4 on my linux os using the python package but when I try running the python script "python3 lib/python3.9/site-packages/pgadmin4/pgAdmin4.py" it brings the following error:
Traceback (most recent call last):
File "/home/gentle/pgAdmin4/pgAdmin4/bin/pgadmin4", line 5, in <module>
from pgadmin4.pgAdmin4 import main
File "/home/gentle/pgAdmin4/pgAdmin4/lib/python3.9/site-packages/pgadmin4/pgAdmin4.py", line 93, in <module>
app = create_app()
File "/home/gentle/pgAdmin4/pgAdmin4/lib/python3.9/site-packages/pgadmin4/pgadmin/__init__.py", line 270, in create_app
create_app_data_directory(config)
File "/home/gentle/pgAdmin4/pgAdmin4/lib/python3.9/site-packages/pgadmin4/pgadmin/setup/data_directory.py", line 117, in create_app_data_directory
_create_directory_if_not_exists(config.AZURE_CREDENTIAL_CACHE_DIR)
File "/home/gentle/pgAdmin4/pgAdmin4/lib/python3.9/site-packages/pgadmin4/pgadmin/setup/data_directory.py", line 20, in _create_directory_if_not_exists
os.mkdir(_path)
FileNotFoundError: [Errno 2] No such file or directory: '/var/lib/pgadmin/azurecredentialcache'
What should I do?
Normally what you find around in the internet is to create the lib/python2.7/site-packages/pgadmin4/config_local.py file with the following content:
import os
DATA_DIR = os.path.realpath(os.path.expanduser(u'~/.pgadmin/'))
LOG_FILE = os.path.join(DATA_DIR, 'pgadmin4.log')
SQLITE_PATH = os.path.join(DATA_DIR, 'pgadmin4.db')
SESSION_DB_PATH = os.path.join(DATA_DIR, 'sessions')
STORAGE_DIR = os.path.join(DATA_DIR, 'storage')
SERVER_MODE = False
Just add the following line to fix the problem:
AZURE_CREDENTIAL_CACHE_DIR = os.path.join(DATA_DIR, 'azurecredentialcache')

OsError : Unable to open file in pixellib

I'm trying to change background of image using pixellib library but am encountering problem. I'm getting this error:
Traceback (most recent call last): File
"C:/Users/FEd/PycharmProjects/pythonProject/PIXEL.py", line 16, in
change_bg.load_pascalvoc_model("deeplabv3_xception_tf_dim_ordering_tf_kernels.h5")
File
"C:\Users\FEd\PycharmProjects\pythonProject\venv\lib\site-packages\pixellib\tune_bg.py",
line 43, in load_pascalvoc_model
self.model.load_weights(model_path) File "C:\Users\FEd\PycharmProjects\pythonProject\venv\lib\site-packages\tensorflow\python\keras\engine\training.py",
line 2319, in load_weights
with h5py.File(filepath, 'r') as f: File "C:\Users\FEd\PycharmProjects\pythonProject\venv\lib\site-packages\h5py_hl\files.py",
line 427, in init
swmr=swmr) File "C:\Users\FEd\PycharmProjects\pythonProject\venv\lib\site-packages\h5py_hl\files.py",
line 190, in make_fid
fid = h5f.open(name, flags, fapl=fapl) File "h5py_objects.pyx", line 54, in h5py._objects.with_phil.wrapper File
"h5py_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py\h5f.pyx", line 96, in h5py.h5f.open OSError: Unable to open
file (unable to open file: name =
'deeplabv3_xception_tf_dim_ordering_tf_kernels.h5', errno = 2, error
message = 'No such file or directory', flags = 0, o_flags = 0)
Have used os.getcwd to get the current working directory but still am getting same error. Have found this link states i should use os.getcwd to get current working directory
Code
import pixellib
from pixellib.tune_bg import alter_bg
import os
my_dir = os.getcwd()
print(my_dir)
m1 = my_dir+"\sample.jpg"
m2 = my_dir+"\change_background.jpg"
print(m1)
print(m2)
change_bg = alter_bg()
change_bg.load_pascalvoc_model("deeplabv3_xception_tf_dim_ordering_tf_kernels.h5")
change_bg.change_bg_img(f_image_path =m1, b_image_path = m2, output_image_name="new_img.jpg")
print("done")

Traceback (most recent call last): convert_model.py

When implementing the code:
https://github.com/fizyr/keras-retinanet
Below is the code of convert_model.py script
# Allow relative imports when being executed as script.
if __name__ == "__main__" and __package__ is None:
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
import keras_retinanet.bin # noqa: F401
__package__ = "keras_retinanet.bin"
# Change these to absolute imports if you copy this script outside the keras_retinanet package.
from keras_retinanet.models import load_model
model_path = os.path.join('..', 'snapshots', 'resnet50_coco_best_v2.1.0.h5')
model = load_model(model_path, backbone_name='resnet50')
Executing the convert_model.py script, the below error occured:
Traceback (most recent call last):
File "convert_model.py", line 33, in <module>
model = load_model(model_path, backbone_name='resnet50')
File "..\..\keras_retinanet\models\__init__.py", line 83, in load_model
model = keras.models.load_model(filepath, custom_objects=backbone(backbone_name).custom_objects)
File "C:\Python\Python36\lib\site-packages\keras\engine\saving.py", line 249, in load_model
f = h5py.File(filepath, mode='r')
File "C:\Python\Python36\lib\site-packages\h5py\_hl\files.py", line 312, in __init__
fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
File "C:\Python\Python36\lib\site-packages\h5py\_hl\files.py", line 142, in make_fid
fid = h5f.open(name, flags, fapl=fapl)
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py\h5f.pyx", line 78, in h5py.h5f.open
OSError: Unable to open file (unable to open file: name = '..\snapshots\resnet50_coco_best_v2.1.0.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)
Could you please help me fix the issue?
Make sure that you have the file referenced downloaded and that it is in the same folder as your current python script. Then change the last line to:
model = load_model('filename', backbone_name='resnet50')

Not Found Error in Pytesser3

import pytesser3
import Image
a = Image.open("C:\\Users\\dell\\Desktop\\test.png")
b = pytesser3.image_to_string(a)
print(b)
I tried to make a simple OCR program but whenever I run it I get the following error. I have even uninstalled pytesser3 and reinstalled it but nothing has changed. What am I doing wrong?
Traceback (most recent call last):
File "C:/Users/dell/Desktop/crossword.py", line 4, in <module>
b = pytesser3.image_to_string(a)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\site- packages\pytesser3\__init__.py", line 30, in image_to_string
call_tesseract(scratch_image_name, scratch_text_name_root)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\site- packages\pytesser3\__init__.py", line 20, in call_tesseract
proc = subprocess.Popen(args)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 990, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified:
Here is the test.png:
https://i.stack.imgur.com/NBjbc.png
import pytesser3
import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python36-32\\Lib\\site-packages\\pytesseract'
a = Image.open("C:\\Users\\dell\\Desktop\\test.png")
b = pytesseract.image_to_string(a)
print(b)
I did what you said but now I've got a different error:
Traceback (most recent call last):
File "C:/Users/dell/Desktop/crossword.py", line 6, in <module>
b = pytesseract.image_to_string(a)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pytesseract\pytesseract.py", line 122, in image_to_string
config=config)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pytesseract\pytesseract.py", line 46, in run_tesseract
proc = subprocess.Popen(command, stderr=subprocess.PIPE)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\Users\dell\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 990, in _execute_child
startupinfo)
PermissionError: [WinError 5] Access denied.
You will have to declare the pytesseract path:
import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-
OCR/tesseract'
open "init.py" file in pytesser3 module. The path would be like "c:\....\Python\site-packages\pytesser3" or inside "c:\....\Python\Lib\site-packages\pytesser3".
And modify the variable value for "tesseract_exe_name = " to your pytesseract path which would be like "C:\....\Python\Lib\site-packages\pytesseract" or "c:\....\Python\site-packages\pytesseract"
It will resolve the issue.

OSError when reading image files using matplotlib

When trying to follow the example posted here. The code segment is the same as given in the example
import matplotlib.image as mpimg
filename = "/home/MarshOrchid.jpg"
image = mpimg.imread(filename)
# Print out its shape
print(image.shape)
Running the code gives the following error message. What could be the underlying cause?
Traceback (most recent call last):
File "test2.py", line 5, in <module>
image = mpimg.imread(filename)
File "/tfw/lib/python3.4/site-packages/matplotlib-1.5.1-py3.4-linux- x86_64.egg/matplotlib/image.py", line 1304, in imread
im = pilread(fname)
File "/tfw/lib/python3.4/site-packages/matplotlib-1.5.1-py3.4-linux-x86_64.egg/matplotlib/image.py", line 1283, in pilread
return pil_to_array(image)
File "/tfw/lib/python3.4/site-packages/matplotlib-1.5.1-py3.4-linux-x86_64.egg/matplotlib/image.py", line 1400, in pil_to_array
x = toarray(im)
File "/tfw/lib/python3.4/site-packages/matplotlib-1.5.1-py3.4-linux-x86_64.egg/matplotlib/image.py", line 1383, in toarray
x_str = im.tobytes('raw', im.mode)
File "/tfw/lib/python3.4/site-packages/Pillow-3.2.0-py3.4-linux-x86_64.egg/PIL/Image.py", line 678, in tobytes
File "/tfw/lib/python3.4/site-packages/Pillow-3.2.0-py3.4-linux-x86_64.egg/PIL/ImageFile.py", line 235, in load
File "/tfw/lib/python3.4/site-packages/Pillow-3.2.0-py3.4-linux-x86_64.egg/PIL/ImageFile.py", line 59, in raise_ioerror
OSError: broken data stream when reading image file

Categories