Code Monkey home page Code Monkey logo

image-organization-project-using-google-cloud-vision-api's Introduction

The automation process in this project involves using a Python script that performs the following steps automatically:

  1. Authenticate and Initialize Clients: The script uses service account credentials to authenticate with the Google Cloud Vision API and Google Drive API.

  2. List Images: It retrieves a list of image files from a specified Google Drive folder.

  3. Download Images: The script downloads each image locally for analysis.

  4. Analyze Images: It uses the Google Cloud Vision API to analyze the content of each image and retrieve labels/categories.

  5. Categorize and Move Images: Based on the labels retrieved, the script determines the appropriate folder for each image and moves it to the corresponding folder in Google Drive.

Here is a more detailed breakdown of the automated steps:

Script Details (organize_images.py)

import os
import io
from google.cloud import vision
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload, MediaFileUpload
from config import KEY_PATH, SOURCE_FOLDER_ID, CATEGORIES_TO_FOLDERS

# Authenticate and initialize clients
credentials = service_account.Credentials.from_service_account_file(KEY_PATH)
vision_client = vision.ImageAnnotatorClient(credentials=credentials)
drive_service = build('drive', 'v3', credentials=credentials)

def list_images(folder_id):
    query = f"'{folder_id}' in parents and mimeType contains 'image/'"
    results = drive_service.files().list(q=query, fields="files(id, name)").execute()
    items = results.get('files', [])
    return items

def download_image(file_id, file_name):
    request = drive_service.files().get_media(fileId=file_id)
    fh = io.FileIO(file_name, 'wb')
    downloader = MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
    return file_name

def upload_image(file_name, folder_id):
    file_metadata = {
        'name': file_name,
        'parents': [folder_id]
    }
    media = MediaFileUpload(file_name, resumable=True)
    file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
    return file.get('id')

def analyze_image(file_name):
    with open(file_name, 'rb') as image_file:
        content = image_file.read()
    image = vision.Image(content=content)
    response = vision_client.label_detection(image=image)
    labels = response.label_annotations
    categories = [label.description for label in labels]
    return categories

def categorize_and_move_images(folder_id, categories_to_folders):
    images = list_images(folder_id)
    for image in images:
        file_id = image['id']
        file_name = image['name']
        local_path = download_image(file_id, file_name)
        categories = analyze_image(local_path)
        
        # Determine the target folder based on categories
        target_folder_id = None
        for category in categories:
            if category in categories_to_folders:
                target_folder_id = categories_to_folders[category]
                break
        
        if target_folder_id:
            upload_image(local_path, target_folder_id)
            drive_service.files().delete(fileId=file_id).execute()
        os.remove(local_path)

if __name__ == '__main__':
    categorize_and_move_images(SOURCE_FOLDER_ID, CATEGORIES_TO_FOLDERS)

Configuration (config.py)

# config.py
KEY_PATH = 'path/to/your/service-account-file.json'
SOURCE_FOLDER_ID = 'your_source_folder_id'
CATEGORIES_TO_FOLDERS = {
    'Cat': 'folder_id_for_cats',
    'Dog': 'folder_id_for_dogs',
    # Add more categories and corresponding folder IDs
}

How the Automation Works:

  1. Authentication: The script uses the service account credentials to authenticate with Google Cloud services.
  2. Image Listing: It lists all images in the specified Google Drive folder.
  3. Downloading: Each image is downloaded locally for processing.
  4. Analysis: The Vision API is used to analyze the content of the image and obtain labels.
  5. Categorization: The script matches the obtained labels with the predefined categories.
  6. Moving Images: Based on the matched category, the image is uploaded to the corresponding folder in Google Drive, and the local copy is deleted.

To run this script, you simply execute:

python organize_images.py

This command triggers the entire workflow, handling the images in your Google Drive folder automatically according to the specified categories and folders.

image-organization-project-using-google-cloud-vision-api's People

Contributors

adesoji1 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.