Initial commit: 25 scripts Python sauvegardés
Date: 2026-08-28 11:43:34 EDT
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Analyse une image avec l'API Gemini et écrit le résultat dans un fichier.
|
||||
Usage: python3 gemini_analyze_frame.py <image_path> <output_path>
|
||||
|
||||
La clé API Google doit être dans /tmp/gkey.txt (écrite par l'utilisateur
|
||||
car Hermes masque les clés API dans tous les outils).
|
||||
"""
|
||||
import subprocess, base64, json, sys, os
|
||||
|
||||
IMAGE_PATH = sys.argv[1]
|
||||
OUTPUT_PATH = sys.argv[2]
|
||||
|
||||
# Lire la clé depuis le fichier (contourne le masking Hermes)
|
||||
with open("/tmp/gkey.txt") as f:
|
||||
API_KEY = f.read().strip()
|
||||
|
||||
# Encoder l'image
|
||||
with open(IMAGE_PATH, "rb") as f:
|
||||
img_b64 = base64.b64encode(f.read()).decode()
|
||||
|
||||
# Construire la requête Gemini
|
||||
body = {
|
||||
"contents": [{
|
||||
"parts": [
|
||||
{"text": "Décris précisément cette image. Interface, texte, boutons, cases cochées, rôles, menus — tout ce qui est visible. Réponds en français."},
|
||||
{"inline_data": {"mime_type": "image/png", "data": img_b64}}
|
||||
]
|
||||
}]
|
||||
}
|
||||
|
||||
# Appeler Gemini (curl pour éviter les timeouts Python HTTP)
|
||||
result = subprocess.run([
|
||||
"curl", "-s",
|
||||
"--max-time", "30",
|
||||
"-H", "Content-Type: application/json",
|
||||
"-d", json.dumps(body),
|
||||
f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={API_KEY}"
|
||||
], capture_output=True, text=True, timeout=35)
|
||||
|
||||
data = json.loads(result.stdout)
|
||||
|
||||
if "error" in data:
|
||||
print(f"API ERROR: {data['error']}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
text = data["candidates"][0]["content"]["parts"][0]["text"]
|
||||
|
||||
# Écrire dans le fichier de sortie (contourne CCR)
|
||||
with open(OUTPUT_PATH, "w") as f:
|
||||
f.write(text)
|
||||
|
||||
print(f"✅ {len(text)} chars → {OUTPUT_PATH}")
|
||||
print(text[:300])
|
||||
@@ -0,0 +1,64 @@
|
||||
#!/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")
|
||||
Reference in New Issue
Block a user