Select Page

DA Grouping AI Object Analysis Code

import os
import pandas as pd
import os
import glob
import re
from collections import defaultdict
from vertexai.preview.generative_models import GenerativeModel
from tqdm import tqdm
import html  

# === Utility: label cleaning for consistent matching ===
def clean_label(label):
    return re.sub(r"[^\w\s-]", "", label).strip().lower()


# === Step 0: Load all Clarifai CSVs from both folders ===
folder_paths = [
  
]

csv_files = []
for path in folder_paths:
    csv_files.extend(glob.glob(os.path.join(path, "*.csv")))

# === Step 1: Merge and tag source files ===
all_rows = []
for file in tqdm(csv_files, desc="📂 Merging CSVs"):
    df = pd.read_csv(file)
    df["source_file"] = os.path.basename(file)
    all_rows.append(df)

df = pd.concat(all_rows, ignore_index=True)

# Rename category to avoid name collision with Gemini classifications
df.rename(columns={"category": "clarifai_category"}, inplace=True)

# === Step 2: Clean duplicate columns like dog.1, dog.2 ===
meta_cols = ["image", "clarifai_category", "source_file"]
data_cols = [col for col in df.columns if col not in meta_cols]


grouped_duplicates = defaultdict(list)
for col in data_cols:
    base = re.sub(r"\.\d+$", "", col)
    grouped_duplicates[base].append(col)

cleaned_data = {}
for group, cols in grouped_duplicates.items():
    cleaned_data[group] = df[cols].max(axis=1)

object_df = pd.DataFrame(cleaned_data).apply(pd.to_numeric, errors="coerce")
df_cleaned = pd.concat([df[meta_cols], object_df], axis=1)

# === Step 3: Filter objects with confidence ≥ 0.87 ===
object_cols = [col for col in df_cleaned.columns if col not in meta_cols]
df_filtered = df_cleaned.copy()
for col in object_cols:
    df_filtered[col] = df_filtered[col].where(df_filtered[col] >= 0.87, 0)

# === Step 4: Ask Gemini to classify labels into Maslow levels ===

# Clean labels (optional but recommended)
# === Step 4: Ask Gemini to classify labels into Maslow levels ===
data_cols = list(cleaned_data.keys())  

# Clean labels (optional but recommended)
cleaned_labels = [re.sub(r"[^\w\s-]", "", label).strip() for label in data_cols]

prompt = f"""
I have a list of object detection labels extracted from Clarifai outputs across multiple image datasets.

Your task is to classify only those labels that refer to **physically visible, concrete objects or entities** that could clearly be seen in a photo — such as a person, bicycle, tree, spoon, mountain, or keyboard.

🛑 EXCLUDE any labels that:
- Refer to **moods, emotions, feelings** (e.g. joy, safety, loneliness, confidence)
- Refer to **concepts, ideas, genres or themes** (e.g. creativity, freedom, punk, indie, minimalism)
- Are **descriptive words or adjectives** (e.g. simple, beautiful, elegant, symmetrical)
- Refer to **styles, aesthetics or subcultures** (e.g. vaporwave, grunge, brutalist, steampunk)
- Refer to **verbs, actions or events** (e.g. running, singing, setting up, wedding)
- Refer to **identities or symbolic categories** (e.g. feminist, soldier, teacher, scientist, influencer)
- Refer to **clothing, fashion items or accessories** (e.g. fedora, hoodie, boots, glasses) — clothing does not count as an object; if a person is present, assume they are clothed
- Refer to **brands, media, art styles, or cultural tags** (e.g. Coca-Cola, Disney, Bauhaus, Catalan)
- Are **scene descriptions or compositional terms** (e.g. background, close-up, studio, landscape)
- Refer to **materials or textures** (e.g. plastic, wood, shiny, reflective)
- Refer to **symbols or scientific concepts** (e.g. electricity, pattern, logo, geometry)

✅ Only include labels that are clearly and literally **objects**, **entities**, or **anatomical parts** that can be visually confirmed in an image. If there is doubt, **leave it out**.

You must only use the following 10 valid physical categories:

1.  **Humans** – visible people or human body parts (e.g. face, hand, torso)
2.  **Animals & Non-Human Organisms** – mammals, birds, insects, microbes, etc.
3.  **Plants & Botanical Elements** – trees, flowers, leaves, roots, etc.
4.  **Natural Landscapes & Phenomena** – sky, sunset, sand, mountain, wave
5.  **Built Structures & Indoor/Urban Spaces** – buildings, roads, windows, walls
6.  **Vehicles & Transportation** – cars, planes, bicycles, boats, etc.
7.  **Food, Beverages & Tableware** – fruit, pizza, drinks, plate, cup
8.  **Manual Tools & Functional Objects** – scissors, broom, chair, hammer
9.  **Technology & Digital Media** – phones, laptops, screens, electronics
10. **Art, Sports & Recreational Objects** – books, instruments, toys, gym equipment

❌ Do NOT make up new categories.
❌ Do NOT try to "guess" the meaning of symbolic or abstract labels.
❌ If the label is ambiguous, skip it entirely.

Return your output in this strict format (one per line):

[label] -> [category]

Classify the following labels:
{chr(10).join(cleaned_labels)}
"""

model = GenerativeModel("gemini-2.5-pro")
response = model.generate_content(prompt)
print("🔎 Gemini raw response:\n", response.text)

categories = [
    "Humans", "Animals & Non-Human Organisms", "Plants & Botanical Elements",
    "Natural Landscapes & Phenomena", "Built Structures & Indoor/Urban Spaces",
    "Vehicles & Transportation", "Food, Beverages & Tableware", "Manual Tools & Functional Objects",
    "Technology & Digital Media", "Art, Sports & Recreational Objects"
]
mappings = {}
for line in response.text.strip().splitlines():
    if "->" in line:
        match = re.match(r"(.+?)\s*->\s*(.+)", line.strip())
        if match:
            label, category = match.groups()
            label = clean_label(label)
            category = category.strip()
            if category in categories:  # ✅ safety filter
                mappings[label] = category


# === Step 5: Parse Gemini response ===
print("🔍 Number of labels classified:", len(mappings))
print("🧪 Sample:", list(mappings.items())[:5])


# Drop duplicates in columns (not rows!)
df_filtered = df_filtered.loc[:, ~df_filtered.columns.duplicated()].copy()

# Create base_df with image, category, and source_file
base_df = df_filtered[["image", "clarifai_category", "source_file"]].drop_duplicates().copy()
base_df = base_df.rename(columns={"clarifai_category": "category"})

# Reset index
base_df = base_df.reset_index(drop=True)
base_df.index.name = None

# Check column names for safety
assert base_df.columns.is_unique, "❌ Duplicate column names in base_df!"
assert base_df.index.is_unique, "❌ Duplicate index in base_df!"


base_df = base_df.reset_index(drop=True)

# Wipe index name just in case
base_df.index.name = None
assert base_df.index.is_unique, "❌ Index is STILL not unique"

# Add empty category columns
for cat in categories:
    base_df[cat] = ""

# Optional: Debug index duplication
dupes = base_df.index.duplicated()
if dupes.any():
    print("⚠️ Duplicate index values found:")
    print(base_df[dupes])
else:
    print("✅ Index is clean.")

print("🧪 base_df index is_unique:", base_df.index.is_unique)
print("🧪 base_df shape:", base_df.shape)
print("🧪 base_df.columns (check for dups):", list(base_df.columns))


# Fill in categories
base_df = base_df.reset_index(drop=True)
assert not base_df.index.has_duplicates, "❌ base_df index still has duplicates before matching!"

col_lookup = {clean_label(col): col for col in object_cols}
if not all(col in df_filtered.columns for col in col_lookup.values()):
    missing = [col for col in col_lookup.values() if col not in df_filtered.columns]
    raise ValueError(f"❌ Some expected object columns missing: {missing}")

for label_lc, category in mappings.items():
    if label_lc in col_lookup:
        col = col_lookup[label_lc]
        matches = df_filtered[df_filtered[col] > 0]
        for _, row in matches.iterrows():
            image_val = row["image"]
            source_val = row["source_file"]
            
            mask = (
                (base_df["image"] == image_val) &
                (base_df["source_file"] == source_val)
            )


            matching_indices = base_df.index[mask]
            for idx in matching_indices:
                current = base_df.loc[idx, category]
                objects = set()

                if pd.notna(current) and current:
                    objects = set(map(str.strip, current.split(",")))

                objects.add(col)
                base_df.loc[idx, category] = ", ".join(sorted(objects))


# === Step 8: Save output ===
output_file = "All_object_DA_categorie.csv"
base_df.to_csv(output_file, index=False)

print(f"✅ Done! Saved pivoted Maslow table to:\n{output_file}")
os.system('afplay /System/Library/Sounds/Glass.aiff && say "DA cat completed"')



←→