Spaces:
Runtime error
Runtime error
File size: 1,468 Bytes
788a6f6 b30d7de 788a6f6 7042d52 788a6f6 b30d7de 788a6f6 b30d7de 788a6f6 7042d52 b30d7de 788a6f6 7042d52 788a6f6 7042d52 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
"""
Code here was custom written, but based on ChatGPT-4.
Example prompt: write a python function that uses an api that given an image returns a text summary of it
"""
import json
import os
from concurrent.futures import ThreadPoolExecutor
from typing import Dict, List, Optional
from dotenv import load_dotenv
from google.cloud import vision
from google.oauth2 import service_account
from code_tasks.image_dimensions import get_image_dimensions
load_dotenv()
VISION_CREDENTIALS = service_account.Credentials.from_service_account_info(
json.loads(os.environ["GOOGLE_APPLICATION_KEY"])
)
def analyze_images(images) -> List[Dict]:
max = 20
def get_image_info(image_url: str) -> Optional[Dict]:
client = vision.ImageAnnotatorClient(credentials=VISION_CREDENTIALS)
image = vision.Image()
image.source.image_uri = image_url # type: ignore
try:
response = client.label_detection(image=image) # type: ignore
labels = [label.description for label in response.label_annotations]
dimensions = get_image_dimensions(image_url)
return {
"url": image_url,
"labels": ", ".join(labels),
"dimensions": f"{dimensions[0]}x{dimensions[1]}",
}
except:
return None
with ThreadPoolExecutor() as executor:
images = executor.map(get_image_info, images[:max])
return [i for i in images if i]
|