I want to import an image on Google Colab from PIL. By doing so:
from PIL import Image
img = Image.open('smallhouse.jpg')
img
However, I get this error.
FileNotFoundError Traceback (most recent call last)
<ipython-input-20-8ab03a99a9db> in <module>()
1
2 from PIL import Image
----> 3 img = Image.open('smallhouse.jpg')
4 img
/usr/local/lib/python3.7/dist-packages/PIL/Image.py in open(fp, mode)
2807
2808 if filename:
-> 2809 fp = builtins.open(filename, "rb")
2810 exclusive_fp = True
2811
FileNotFoundError: [Errno 2] No such file or directory: 'smallhouse.jpg'
Not sure why. Thanks!
1. You must upload the smallhouse.jpg image to your Google Drive.
2. In your .ipynb file, you need to include this code at the beginning of your application: drive.mount('/content/gdrive').
Go to the URL provided. Then, you will be prompted to give permission to access your Google Drive. Copy the code provided, access your application and paste the code there.
3. Finally, you can pass your image's path, which now is: /content/gdrive/My Drive/smallhouse.jpg.
Your code will look something like this:
# Imports
from google.colab import drive
from PIL import Image
# Mount Google Drive for fast, responsible access to files
drive.mount('/content/gdrive')
# Open image
img = Image.open('/content/gdrive/My Drive/smallhouse.jpg')
img
Related
I want to tried using OCR and pytesseract. I have code like this
!sudo apt install tesseract-ocr
!pip install pytesseract
import pytesseract
import shutil
import os
import random
try:
from PIL import Image
except ImportError:
import Image
from google.colab import files
uploaded = files.upload()
extractedInformation = pytesseract.image_to_string(Image.open(1.jpg))
or you can check it out here : Google Colab
But when I try to upload some image and try to extract the file,it show error like this
UnidentifiedImageError Traceback (most recent call last)
<ipython-input-37-44411f3825fa> in <module>
----> 1 extractedInformation = pytesseract.image_to_string(Image.open('1.jpg'))
/usr/local/lib/python3.7/dist-packages/PIL/Image.py in open(fp, mode)
2894
2895 Note that this function decodes pixel data only, not entire images.
-> 2896 If you have an entire image file in a string, wrap it in a
2897 :py:class:`~io.BytesIO` object, and use :py:func:`~PIL.Image.open` to load it.
2898
UnidentifiedImageError: cannot identify image file '1.jpg'
I have asked my friend to try the code but why that he has no error like me? The code is run smoothly with no error. is that something missing in my computer? or something that I should Install first?
Long shot but I am stuck. Running this test code in Amazon Sagemaker with a Python3 kernel and the function doesn't recognize the web location of the image (media.ci.org). I can't find anything in the documentation from facenet_pytorch about this, anyone know if this is possible? I can do this in R, but much slower.
pip install facenet-pytorch
from facenet_pytorch import MTCNN, InceptionResnetV1
# If required, create a face detection pipeline using MTCNN:
mtcnn = MTCNN()
# Create an inception resnet (in eval mode):
resnet = InceptionResnetV1(pretrained='vggface2').eval()
from PIL import Image
img = Image.open("https://media.ci.org/w_150/v1609425653/ChildPhotos/Published/08704975_wctwrt.jpg")
FileNotFoundError Traceback (most recent call last)
in
----> 1 img = Image.open("https://media.ci.org/w_150/v1609425653/ChildPhotos/Published/08704975_wctwrt.jpg")
/opt/conda/lib/python3.7/site-packages/PIL/Image.py in open(fp, mode, formats)
3090
3091 if filename:
-> 3092 fp = builtins.open(filename, "rb")
3093 exclusive_fp = True
3094
FileNotFoundError: [Errno 2] No such file or directory: 'https://media.ci.org/w_150/v1609425653/ChildPhotos/Published/08704975_wctwrt.jpg'
You should ideally use requests to download the image locally and then using PIL.Image to open something like this.
from PIL import Image
import requests
from io import BytesIO
response = requests.get(url)
img = Image.open(BytesIO(response.content))
Good day, friends.
I'm trying to load my own file to Google Colab from my own disc, and I use the code with image.load_img. But programm thinks that there is no such a file. I see this file and not agree with Google. )
Could you please make an advice how can I make code to work correctly. Please tell me if I made any mistake. And what is the right way to type path when file is on PC and file is in Colab folder
Thank you very much.
Code is:
from tensorflow.keras import utils
from tensorflow.keras.preprocessing import image
import numpy as np
import pandas as pd
import pylab
from google.colab import files
from PIL import Image
path = 'C:\XYZ\pic7.jpg'
x = image.load_img(path, target_size = (800, 600), color_mode = 'grayscale')
Colab says:
...
FileNotFoundError Traceback (most recent call last)
<ipython-input-43-a3755b9d97b5> in <module>()
9 path = 'C:\XYZ\pic7.jpg'
10
---> 11 x = image.load_img(path, target_size = (800, 600), color_mode = 'grayscale')
1 frames
/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/utils.py in load_img(path, grayscale, color_mode, target_size, interpolation)
111 raise ImportError('Could not import PIL.Image. '
112 'The use of `load_img` requires PIL.')
--> 113 with open(path, 'rb') as f:
114 img = pil_image.open(io.BytesIO(f.read()))
115 if color_mode == 'grayscale':
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\XYZ\\pic7.jpg'
Google Colab runs on a remote server, not your local machine, so it has no access to "C:\" or any of your local drives.
See the examples for how to work with external data in Colab, including mounting Google Drive - so you'll need to put your images there first.
For those who are interested in decision.
I mounted my Google drive, as dicribed at https://colab.research.google.com/notebooks/io.ipynb#scrollTo=u22w3BFiOveA (Aneroid adviced this link),
Changed path to '/content/drive/MyDrive/myfolder/pic7.jpg'.
Anf after that programm stoped to critisize my path. And error changed. It's Success for me. )
Best wishes to Aneroid.
So to use files from google drive in google colab I used this code:
from google.colab import drive
drive.mount('/content/gdrive')
!ln -s /content/gdrive/My\ Drive/ /mydrive
!ls /mydrive
Drive already mounted at /content/gdrive; to attempt to forcibly remount, call drive.mount("/content/gdrive", force_remount=True).
app 'Colab Notebooks' lixo 'My Drive' pixellib yolov3
Inside of pixellib I have the folder meat and the file pretraining.h5.
I installed this too:
!pip3 install pixellib
Until here, ok, but when I run this code:
import pixellib
from pixellib.custom_train import instance_custom_training
train_maskrcnn = instance_custom_training()
train_maskrcnn.modelConfig(network_backbone = "resnet101", num_classes= 2, batch_size = 4)
train_maskrcnn.load_pretrained_model("/mydrive/pixellib/pretraining.h5")
train_maskrcnn.load_dataset("/mydrive/pixellib/meat")
train_maskrcnn.train_model(num_epochs = 20, augmentation=True, path_trained_models = "/mydrive/pixellib/mask_rcnn_models")
the following error message appears:
Using resnet101 as network backbone For Mask R-CNN model
---------------------------------------------------------------------------
IsADirectoryError Traceback (most recent call last)
<ipython-input-44-8a3b66f50c89> in <module>()
7 train_maskrcnn.modelConfig(network_backbone = "resnet101", num_classes= 2, batch_size = 4)
8 train_maskrcnn.load_pretrained_model("/mydrive/pixellib/pretraining.h5")
----> 9 train_maskrcnn.load_dataset("/mydrive/pixellib/meat")
10 train_maskrcnn.train_model(num_epochs = 20, augmentation=True, path_trained_models = "/mydrive/pixellib/mask_rcnn_models")
7 frames
/usr/local/lib/python3.6/dist-packages/PIL/Image.py in open(fp, mode)
2807
2808 if filename:
-> 2809 fp = builtins.open(filename, "rb")
2810 exclusive_fp = True
2811
IsADirectoryError: [Errno 21] Is a directory: '/mydrive/pixellib/meat/train/78-3.jpg'
What kind of error is that?
Something is wrong with my code?
If you have a json for every image in this directory (he said this in a comment), it seems likely to me that one of your json files has a .jpg ending. Either download both json and jpg and confirm that they are the correct file type, or you try to plot the image instead to see if it is an image.
In the following code I am trying to use pythons pillow/PIL library. This is the first time using this and I am just trying to open my image to make sure it is working. I have my code I used below. I just googled an image and saved it using the name smallhouse.jpg. However I am running into the following errors below.
import numpy as np
from PIL import Image
img = Image.open('smallhouse.jpg')
img.show()
This is the error I am getting. Do I need to download a PIL package? I am using Jupiter notebook.
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-10-e26e3b39215f> in <module>()
1 import numpy as np
2 from PIL import Image
----> 3 img = Image.open('smallhouse.jpg')
4 img.show()
~\Anaconda3\lib\site-packages\PIL\Image.py in open(fp, mode)
2546
2547 if filename:
-> 2548 fp = builtins.open(filename, "rb")
2549 exclusive_fp = True
2550
FileNotFoundError: [Errno 2] No such file or directory: 'smallhouse.jpg'
The problem described here:
FileNotFoundError: [Errno 2] No such file or directory: 'smallhouse.jpg'
Please make sure you put the image file smallhouse.jpg in the same directory with your notebook.