Google colab error to use files from google drive - python

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.

Related

Web Hosted Picture Reading in Sagemaker via facenet_pytorch?

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))

Google Colab: No such file or directory on local PC

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.

"FileNotFoundError" when importing Image from PIL on Google Colab

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

Download a Kaggle Dataset

I would like to download a Kaggle Dataset. I generated the Kaggle.json file, but unfortunately I don't have a drive (I can't use it). Is there any option to generate the username and token in directly in the code?
For example I tried this
x = '{"username":"<USERNAME>","key":"<TOKEN>"}'
y = json.loads(x)
api = KaggleApi(y)
api.authenticate()
files = api.competition_download_files("two-sigma-financial-news")
The error is
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-6-237de0539a08> in <module>()
1 api = KaggleApi(y)
----> 2 api.authenticate()
3 files = api.competition_download_files("two-sigma-financial-news")
/usr/local/lib/python3.6/dist-packages/kaggle/api/kaggle_api_extended.py in authenticate(self)
164 raise IOError('Could not find {}. Make sure it\'s located in'
165 ' {}. Or use the environment method.'.format(
--> 166 self.config_file, self.config_dir))
167
168 # Step 3: load into configuration!
OSError: Could not find kaggle.json. Make sure it's located in /root/.kaggle. Or use the environment method.
But it isn't right. May someone could help me plase? I'm using Colab, but I don't want to store the JSON file in my Google Drive. Is there any option to generate the JSON file directly?
Thanks in advance.
Maybe this post helps: https://www.kaggle.com/general/51898
it links to this script:
# Info on how to get your api key (kaggle.json) here: https://github.com/Kaggle/kaggle-api#api-credentials
!pip install kaggle
api_token = {"username":"USERNAME","key":"API_KEY"}
import json
import zipfile
import os
with open('/content/.kaggle/kaggle.json', 'w') as file:
json.dump(api_token, file)
!chmod 600 /content/.kaggle/kaggle.json
!kaggle config path -p /content
!kaggle competitions download -c jigsaw-toxic-comment-classification-challenge
os.chdir('/content/competitions/jigsaw-toxic-comment-classification-challenge')
for file in os.listdir():
zip_ref = zipfile.ZipFile(file, 'r')
zip_ref.extractall()
zip_ref.close()
from: https://gist.github.com/jayspeidell/d10b84b8d3da52df723beacc5b15cb27

Don't understand this FileNotFoundError when trying to use pillow/PIL library

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.

Categories