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")
Related
I have a h5 file that contains the "catvnoncat" dataset. when I try to run the following code, I get an error which I will include at the bottom. I have tried getting the dataset from three different sources to exclude to possibility of a corrupted file.
What I would like to know is what is causing the problem?
# My Code
import h5py
train_dataset = h5py.File('train_catvnoncat.h5', "r")
# The Error
Traceback (most recent call last):
File "f:\Downloads\Compressed\coursera-deep-learning-specialization-master_2\coursera-deep-learning-specialization-master\C1 - Neural Networks and Deep Learning\Week 2\Logistic Regression as a Neural Network\lr_utils.py", line 4, in <module>
train_dataset = h5py.File('train_catvnoncat.h5', "r")
File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\h5py\_hl\files.py", line 507, in __init__
fid = make_fid(name, mode, userblock_size, fapl, fcpl, swmr=swmr)
File "C:\Users\Mohsen\AppData\Local\Programs\Python\Python39\lib\site-packages\h5py\_hl\files.py", line 220, 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 106, in h5py.h5f.open
FileNotFoundError: [Errno 2] Unable to open file (unable to open file: name = 'train_catvnoncat.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)
Your code is looking in the current directory which is not where the file is.
Based on the error message, it looks like you are on windows. Is the file 'train_catvnoncat.h5' in your Downloads folder? Find that file on your system and copy the full path. You can then update this:
train_dataset = h5py.File('train_catvnoncat.h5', "r")
to something that contains the full path. Similar to this:
train_dataset = h5py.File('C:/Users/Moshen/Downloads/train_catvnoncat.h5', "r")
Pay attention to the slashes and make sure you update the file path to the actual value.
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
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')
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
I have a script (seen below) that opens a collection of images. It took me a while to figure this out, but I realized that the script can't seem to open any of the image files with an underscore in the name. An example of my image directory is as follows:
Images\img1.jpg
Images\img2.jpg
Images\img3_c.jpg
The script will behave as expected when dealing with "normal" image file names. The moment the script has to open a file with an underscore in the name, it returns an error as follows:
OSError: cannot identify image file <_io.BufferedReader name='TrainingSets\\CottonWoolSpots\\0006_c.jpg'>
I would like to avoid changing my file names if possible. Any help is appreciated.
Script:
import numpy as np
from skimage import io
import glob
import os
def importAllImgs():
imagePath = glob.glob("TrainingSets\CottonWoolSpots\*.jpg")
im_coll = io.ImageCollection(imagePath)
im_array = []
for i in range(len(im_coll)):
image = im_coll[i]
im_array.append(image)
return im_array
if __name__ == "__main__":
testArray = importAllImgs()
EDIT FULL STACKTRACE + ERROR:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 734, in debugfile
debugger.run("runfile(%r, args=%r, wdir=%r)" % (filename, args, wdir))
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\bdb.py", line 431, in run
exec(cmd, globals, locals)
File "<string>", line 1, in <module>
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
execfile(filename, namespace)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "//mspm1bnas50s/home58/biegad1/python scripts/batchimportimgs_0_1.py", line 54, in <module>
testArray = importAllImgs()
File "//mspm1bnas50s/home58/biegad1/python scripts/batchimportimgs_0_1.py", line 36, in importAllImgs
image = im_coll[i]
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\skimage\io\collection.py", line 264, in __getitem__
self.data[idx] = self.load_func(self.files[n], **kwargs)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\skimage\io\_io.py", line 61, in imread
img = call_plugin('imread', fname, plugin=plugin, **plugin_args)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\skimage\io\manage_plugins.py", line 211, in call_plugin
return func(*args, **kwargs)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\skimage\io\_plugins\pil_plugin.py", line 36, in imread
im = Image.open(f)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\PIL\Image.py", line 2309, in open
% (filename if filename else fp))
OSError: cannot identify image file <_io.BufferedReader name='TrainingSets\\CottonWoolSpots\\0006_c.jpg'>
OKAY! So. It appears that I've been working with corrupted image files the entire time, hence the errors.