My Jupyter Notebook has the following code to upload an image to Colab:
from google.colab import files
uploaded = files.upload()
I get prompted
from deepface import DeepFace
import cv2
import matplotlib.pyplot as plt
from google.colab import files
from io import BytesIO
from PIL import Image
uploaded = files.upload()
next line
# im = Image.open(BytesIO(uploaded['img.PNG']))
img = cv2.imread("theCorona.PNG")
plt.imshow(img[:,:, ::-1])
plt.show()
Am assuming you might not have written the file from memory?
try the below code after the upload:
with open("wash care labels", 'w') as f:
f.write(uploaded[uploaded.keys()[0]])
replace "wash care labels.xx" with your file name. This writes the file from memory. then try calling the file.
Hope this works for you.
Hack to upload image file in colab!
https://colab.research.google.com/
Following code loads image (file(s)) from local drive to colab.
from google.colab import files
from io import BytesIO
from PIL import Image
uploaded = files.upload()
im = Image.open(BytesIO(uploaded['Image_file_name.jpg']))
View the image in google colab notebook using following command:
import matplotlib.pyplot as plt
plt.imshow(im)
plt.show()
You can upload files manually to you google colab working directory by clicking on the folder drawing button on the left. They are then accessible just as they would be on your computer.
after you uploaded it to your notebook, do this
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
img = cv2.imread('./your image file.jpg')
cv2_imshow(img)
cv2.waitKey()
Colab google: uploading images in multiple subdirectories: If you would like to upload images (or files) in multiples subdirectories by using Colab google, please follow the following steps: - I'll suppose that your images(files) are split into 3 subdirectories (train, validate, test) in the main directory called (dataDir): 1- Zip the folder (dataDir) to (dataDir.zip) 2- Write this code in a Colab cell:
from google.colab import files
uploaded = files.upload()
3- Press on 'Choose Files' and upload (dataDir.zip) from your PC to the Colab Now the (dataDir.zip) is uploaded to your google drive! 4- Let us unzip the folder(dataDir.zip) to a folder called (data) by writing this simple code:
import zipfile
import io
data = zipfile.ZipFile(io.BytesIO(uploaded['dataDir.zip']), 'r')
data.extractall()
5- Now everything is ready, let us check that by printing content of (data) folder:
data.printdir()
6- Then to read the images, count them, split them and play around them, please write the following code:
train_data_dir = 'data/training'
validation_data_dir = 'data/validation'
test_data_dir = 'data/test'
target_names = [item for item in os.listdir(train_data_dir) if os.path.isdir(os.path.join(train_data_dir, item))]
nb_train_samples = sum([len(files) for _, _, files in os.walk(train_data_dir)])
nb_validation_samples = sum([len(files) for _, _, files in os.walk(validation_data_dir)])
nb_test_samples = sum([len(files) for _, _, files in os.walk(test_data_dir)])
total_nb_samples = nb_train_samples + nb_validation_samples + nb_test_samples
nb_classes = len(target_names) # number of output classes
print('Training a CNN Multi-Classifier Model ......')
print('\n - names of classes: ', target_names, '\n - # of classes: ', nb_classes)
print(' - # of trained samples: ', nb_train_samples, '\n - # of validation samples: ', nb_validation_samples,
'\n - # of test samples: ', nb_test_samples,
'\n - total # of samples: ', total_nb_samples, '\n - train ratio:', round(nb_train_samples/total_nb_samples*100, 2),
'\n - validation ratio:', round(nb_validation_samples/total_nb_samples*100, 2),
'\n - test ratio:', round(nb_test_samples/total_nb_samples*100, 2),
' %', '\n - # of epochs: ', epochs, '\n - batch size: ', batch_size)
7- That is it! Enjoy!