Skip to content

Instantly share code, notes, and snippets.

@tfriedel
Last active May 19, 2024 21:23
Show Gist options
  • Save tfriedel/9eb875c55250a7354668101a9ab39553 to your computer and use it in GitHub Desktop.
Save tfriedel/9eb875c55250a7354668101a9ab39553 to your computer and use it in GitHub Desktop.
ChatGPT explains how to process images with ChatGPT

Vision API Exporter

You:

I need to analyse bulk images (50-100) and export results as csv. I want to use the Chatgpt Vision api ( https://platform.openai.com/docs/guides/vision ).

Modify this code to export results:

import base64
import requests

# OpenAI API Key
api_key = "YOUR_OPENAI_API_KEY"

# Function to encode the image
def encode_image(image_path):
  with open(image_path, "rb") as image_file:
    return base64.b64encode(image_file.read()).decode('utf-8')

# Path to your image
image_path = "path_to_your_image.jpg"

# Getting the base64 string
base64_image = encode_image(image_path)

headers = {
  "Content-Type": "application/json",
  "Authorization": f"Bearer {api_key}"
}

payload = {
  "model": "gpt-4o",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "What’s in this image?"
        },
        {
          "type": "image_url",
          "image_url": {
            "url": f"data:image/jpeg;base64,{base64_image}"
          }
        }
      ]
    }
  ],
  "max_tokens": 300
}

response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)

print(response.json())

ChatGPT:

To analyze bulk images and export results to a CSV file using the ChatGPT Vision API, you can modify your script to loop through multiple images, make API calls for each image, and then collect the results. Here is a modified version of your code that accomplishes this task:

import base64
import requests
import os
import csv

# OpenAI API Key
api_key = "YOUR_OPENAI_API_KEY"

# Function to encode the image
def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

# Function to get image analysis from the API
def analyze_image(base64_image):
    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {api_key}"
    }
    
    payload = {
        "model": "gpt-4o",
        "messages": [
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": "What’s in this image?"
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": f"data:image/jpeg;base64,{base64_image}"
                        }
                    }
                ]
            }
        ],
        "max_tokens": 300
    }
    
    response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
    return response.json()

# Directory containing images
image_directory = "path_to_your_image_directory"

# List to hold analysis results
results = []

# Process each image in the directory
for image_filename in os.listdir(image_directory):
    if image_filename.lower().endswith(('.png', '.jpg', '.jpeg')):
        image_path = os.path.join(image_directory, image_filename)
        base64_image = encode_image(image_path)
        analysis_result = analyze_image(base64_image)
        results.append({
            "image": image_filename,
            "analysis": analysis_result
        })

# Export results to CSV
csv_file_path = "analysis_results.csv"
with open(csv_file_path, mode='w', newline='') as csv_file:
    fieldnames = ["image", "analysis"]
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    
    writer.writeheader()
    for result in results:
        writer.writerow(result)

print(f"Analysis results have been exported to {csv_file_path}")

Notes:

  1. Image Directory: Set the image_directory variable to the path where your images are stored.
  2. CSV Export: The results will be saved in analysis_results.csv in the same directory as your script. You can change the path if needed.
  3. Error Handling: You may want to add error handling for API requests and file I/O operations.
  4. API Key: Replace "YOUR_OPENAI_API_KEY" with your actual OpenAI API key.

This script will iterate through all images in the specified directory, analyze each image using the ChatGPT Vision API, and export the results to a CSV file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment