0

I was running a program where, the code sends a Folder ID, a folder name, an Image name and an Image to a function. The folder ID is the ID of Parent folder. The Folder name is the name of Child folder. The save_image`` code will check if there exist a child folder in the parent by that name and if it does it will save the image with the given name. However I am getting attribute error. I was using Google Collab while doing this.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-180-6db51ad10c39> in <module>
----> 1 process_images()

1 frames
<ipython-input-179-3abcafca36b1> in save_image(img_name, subfolder_name, img_data, dataset2_id)
     12 
     13             # Send a request to the Drive API to upload the image
---> 14             file = drive.files().create(body=file_metadata, media_body=media, fields='id').execute()
     15 
     16             # Print a message to confirm that the file was saved

AttributeError: 'GoogleDrive' object has no attribute 'files'

One of the solutions present was using path, however, that is out of the question for this code. The aim of this code is to read a dataset, modify it and paste the modified dataset in a separate folder using the same structure and name. Hence the folder where dataset is and where the dataset can be stored is known. However, for the child folder it will change depending on image. For image A01.png in Folder A inside Dataset, it's modified image is sent as img_data, while its name and name of folder A is sent to this save_image function. It will be stored in A folder inside Dataset2 folder as A01.png. That was my plan. The rest of the code is working fine. Its creating all the child folders, accessing all the images, modifying the images as needed. However, saving them is where I am stuck at.

def save_image(img_name, subfolder_name, img_data, dataset2_id):
    drive = GoogleDrive(gauth)
    dataset2_file_list = drive.ListFile({'q': f"'{dataset2_id}' in parents and trashed = false"}).GetList()
    # Iterate through the subfolders in the Dataset2 folder
    for subfolder in dataset2_file_list:
        # Check if the subfolder name matches the name of the subfolder in the Dataset folder
        if subfolder['title'] == subfolder_name:
            # Get the ID of the subfolder
            subfolder_id = subfolder.metadata['id']
            # Create the file metadata
            file_metadata = {'name': img_name, 'parents': [subfolder_id]}
            
            # Send a request to the Drive API to upload the image
            file = drive.files().create(body=file_metadata, media_body=media, fields='id').execute()
            
            # Print a message to confirm that the file was saved
            print(f'File ID: {file.get("id")}')

The issue is only in this region. It is getting proper string inputs. Other then the image which is numpy array all the other parameters are string variable in the function.

One solution suggested to add the following line. As you can see in this code, I already implemented that. It didn't work

Here is all my imports

# Install the PyDrive wrapper & import libraries.
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
import io
from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, toolsd

import cv2
from matplotlib.image import imread
import matplotlib.pyplot as plt
import numpy as np
import requests
from imhist import imhist, imcdf
from scipy.fftpack import fft2, ifft2
from skimage import filters
from mpl_toolkits.mplot3d import Axes3D

0 Answers0