Select Page

MHN Group – Object Presence 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)

# === Step 2: Clean duplicate columns like dog.1, dog.2 ===
meta_cols = ["image", "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"""

### Classification Framework: Maslow’s Hierarchy of Needs (6 Levels)

You will assign Maslow levels to a list of visual labels, based on whether the label refers to a **concrete, clearly visible object or action** that supports one or more core human needs.

Use the six levels of Maslow’s hierarchy as defined below. Many labels will belong to **no level** — that’s expected. Only assign a level if the connection is **direct, visible, and grounded in lived human experience**. Do **not** speculate or infer.

---

🔒 **Strict Classification Criteria**

- Only classify if there is **clear visual evidence** of the need being met.
- Do **not** classify based on metaphor, emotion, implication, or symbolic association.
- Do **not** include abstract or simulated representations (e.g., icons, emojis, screen-based depictions).
- Do **not** assign levels to general objects (e.g., “technology,” “phone”) unless they show concrete, real-world fulfilment of a need.
- If uncertain or weak, **do not classify**. It is better to leave a label blank than to misassign.

---

### Maslow’s Six Levels (Only use if clearly supported):

**Level 1: Physiological Needs**  
→ Survival and physical health: food, sleep, shelter, the human body  
→ e.g., food, bed, house, sleep, water, clothing, body

**Level 2: Safety Needs**  
→ Physical safety and protection from harm  
→ e.g., helmet, fence, medicine, police, fire extinguisher, seatbelt

**Level 3: Love and Belonging**  
→ Genuine visible social connection: family, intimacy, teams, touch  
→ e.g., hug, couple, group, wedding, friends, family, people holding hands

**Level 4: Esteem Needs**  
→ Achievement, competence, recognition, performance  
→ e.g., graduation, award, desk, book, office, formal clothing, certificate

**Level 5: Self-Actualization**  
→ Creativity, learning, solitude, nature, exploration  
→ e.g., mountain, forest, hiking, painting, music, sunset, reading, travel

**Level 6: Self-Transcendence**  
→ Connection to something larger than the self: awe, spirituality, service, ecological unity, the sacred  
→ e.g., stars, temple, ritual, meditation, animals in nature, hands lifted, volunteering, sky, cosmos

---

### Output Format

Return only clearly supported classifications. Many labels will have **no match**, and should be left unclassified.

Use this format:

label -> 1, 2, 3     ← Use numbers only if clearly supported  
label ->             ← Leave blank if not confidently classifiable


---

Now 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)

mappings = defaultdict(list)
for line in response.text.strip().splitlines():
    if "label" in line.lower():  # Skip example lines like 'label -> 1, 2, 3'
        continue
    match = re.match(r"(.+?)\s*->\s*(.*)", line.strip())
    if match:
        label, levels = match.groups()
        for level in levels.split(","):
            level = level.strip()
            if level.isdigit() and 1 <= int(level) <= 6:
                clean = f"level_{level}"
                mappings[clean_label(label)].append(clean)

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

# === Step 6: Create base table with empty level_1 to level_6 ===
base_df = df_filtered[["image", "category", "source_file"]].drop_duplicates().copy()
for i in range(1, 7):
    base_df[f"level_{i}"] = ""

base_df = (
    df_filtered[["image", "category", "source_file"]]
    .drop_duplicates(subset=["image", "category", "source_file"])
    .copy()
)

dupes = base_df.duplicated(subset=["image", "source_file"], keep=False)
if dupes.any():
    print("⚠️ Still duplicates in base_df on image + source_file!")
    print(base_df[dupes])

# === Step 7: Fill in each level column ===
# === Step 7: Fill in each level column ===
col_lookup = {clean_label(col): col for col in object_cols}

for label_raw, levels in mappings.items():
    label_lc = clean_label(label_raw)
    if label_lc in col_lookup:
        col = col_lookup[label_lc]
        matches = df_filtered[df_filtered[col] > 0]
        for _, row in matches.iterrows():
            key = (
                row["image"],
                row["category"],
                row["source_file"]
            )
            for level in levels:
                # Match all rows in base_df with the same image + source_file
                mask = (
                    (base_df["image"] == key[0]) &
                    (base_df["source_file"] == key[2])
                )
                matching_indices = base_df.index[mask]

                if len(matching_indices) == 0:
                    print(f"⚠️ No match for image={key[0]}, category={key[1]}, file={key[2]} on label={label_lc}")
                else:
                    for idx in matching_indices:
                        current = base_df.at[idx, level]
                        object_list = set(current.split(", ")) if current else set()
                        object_list.add(col)
                        base_df.at[idx, level] = ", ".join(sorted(object_list))

# === Step 8: Save output ===
output_file = "high_confidence_objects_by_search_categoryOb_image_maslow_table.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 "maslow table complete"')



←→