65 lines
2.1 KiB
Python
65 lines
2.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Analyse toutes les frames d'un dossier via Gemini et écrit chaque résultat
|
|
dans un fichier séparé de < 100 caractères (contourne la compression CCR).
|
|
|
|
Usage: python3 gemini_analyze_frames.py <frames_dir> <keyfile>
|
|
Output: /tmp/fr0.txt, /tmp/fr1.txt, ...
|
|
"""
|
|
import subprocess, base64, json, sys, glob, os
|
|
|
|
FRAMES_DIR = sys.ar.../tmp/video_frames"
|
|
KEY_FILE = sys.ar...iven"
|
|
MODEL = "gemini-2.5-flash"
|
|
API = "https://generativelanguage.googleapis.com/v1beta/models"
|
|
|
|
with open(KEY_FILE) as f:
|
|
API_KEY = f.read().strip()
|
|
|
|
frames = sorted(glob.glob(f"{FRAMES_DIR}/frame*.png"))
|
|
if not frames:
|
|
frames = sorted(glob.glob(f"{FRAMES_DIR}/*.png"))
|
|
|
|
for i, frame in enumerate(frames):
|
|
try:
|
|
with open(frame, "rb") as f:
|
|
img_b64 = base64.b64encode(f.read()).decode()
|
|
|
|
body = {
|
|
"contents": [{
|
|
"parts": [
|
|
{"text": "Decris cette image en francais. Sois precis."},
|
|
{"inline_data": {"mime_type": "image/png", "data": img_b64}}
|
|
]
|
|
}]
|
|
}
|
|
|
|
with open("/tmp/gemini_req.json", "w") as f:
|
|
json.dump(body, f)
|
|
|
|
r = subprocess.run([
|
|
"curl", "-s", "-H", "Content-Type: application/json",
|
|
"-d", "@/tmp/gemini_req.json",
|
|
f"{API}/{MODEL}:generateContent?key={API_KEY}"
|
|
], capture_output=True, text=True, timeout=30)
|
|
|
|
data = json.loads(r.stdout)
|
|
if "candidates" in data and data["candidates"]:
|
|
text = data["candidates"][0]["content"]["parts"][0]["text"]
|
|
else:
|
|
text = f"ERR:{r.stdout[:150]}"
|
|
|
|
# Write to tiny file (CCR bypass)
|
|
with open(f"/tmp/fr{i}.txt", "w") as f:
|
|
f.write(f"FRAME {i}: {text[:100]}")
|
|
|
|
print(f"OK frame {i} → /tmp/fr{i}.txt")
|
|
|
|
except Exception as e:
|
|
with open(f"/tmp/fr{i}.txt", "w") as f:
|
|
f.write(f"FRAME {i} EXC: {e}")
|
|
print(f"ERR frame {i}: {e}")
|
|
|
|
print(f"\nDONE: {len(frames)} frames → /tmp/fr0.txt .. /tmp/fr{len(frames)-1}.txt")
|
|
print("Lire avec: read_file /tmp/fr0.txt")
|