Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,50 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify,render_template, send_file
|
| 2 |
from flask_cors import CORS
|
| 3 |
-
from PIL import Image
|
| 4 |
import io
|
| 5 |
import json
|
| 6 |
import os
|
| 7 |
import uuid
|
| 8 |
import google.generativeai as genai
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
generation_config = {
|
| 12 |
-
|
| 13 |
-
|
| 14 |
}
|
| 15 |
|
| 16 |
safety_settings = [
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
]
|
| 34 |
|
| 35 |
GOOGLE_API_KEY = os.environ.get("TOKEN")
|
| 36 |
|
| 37 |
-
|
| 38 |
# Configuration de l'API Gemini
|
| 39 |
-
genai.configure(api_key=GOOGLE_API_KEY)
|
| 40 |
|
| 41 |
app = Flask(__name__)
|
| 42 |
CORS(app)
|
| 43 |
|
| 44 |
-
|
| 45 |
@app.route('/', methods=['GET'])
|
| 46 |
def svt():
|
| 47 |
"""Renders the SVT page."""
|
| 48 |
return render_template("svt.html")
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
# Prompt pour la détection d'objets
|
| 54 |
DETECTION_PROMPT = "Detect items, with no more than 20 items. Output a json list where each entry contains the 2D bounding box in \"box_2d\" and a text label in \"label\"."
|
|
@@ -84,48 +80,56 @@ def analyze_image():
|
|
| 84 |
return jsonify({'error': 'No selected file'}), 400
|
| 85 |
|
| 86 |
if file:
|
| 87 |
-
# Générer un nom de fichier unique pour éviter les conflits
|
| 88 |
unique_filename = str(uuid.uuid4()) + os.path.splitext(file.filename)[1]
|
| 89 |
filename = os.path.join(app.config['UPLOAD_FOLDER'], unique_filename)
|
| 90 |
file.save(filename)
|
| 91 |
|
| 92 |
-
#
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
| 94 |
image_part = {
|
| 95 |
-
|
| 96 |
-
|
| 97 |
}
|
| 98 |
-
response = model.generate_content([DETECTION_PROMPT, image_part])
|
| 99 |
|
|
|
|
|
|
|
| 100 |
try:
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
output_filename = os.path.join(app.config['UPLOAD_FOLDER'], 'output_' + unique_filename)
|
| 119 |
-
|
| 120 |
|
| 121 |
-
#
|
| 122 |
-
|
| 123 |
response = model.generate_content([DESCRIPTION_PROMPT, image_part])
|
| 124 |
description = response.text
|
| 125 |
|
| 126 |
-
# 4. Renvoyer les résultats
|
| 127 |
return jsonify({
|
| 128 |
-
'image_path': '/uploads/' + 'output_' + unique_filename,
|
| 129 |
'description': description,
|
| 130 |
'detected_objects': detection_results
|
| 131 |
})
|
|
@@ -134,7 +138,6 @@ def analyze_image():
|
|
| 134 |
print(f"Une erreur s'est produite : {e}")
|
| 135 |
return jsonify({'error': f'Erreur lors du traitement de l\'image : {e}'}), 500
|
| 136 |
|
| 137 |
-
# Servir les fichiers statiques depuis le dossier 'uploads'
|
| 138 |
@app.route('/uploads/<filename>')
|
| 139 |
def uploaded_file(filename):
|
| 140 |
return send_file(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, render_template, send_file
|
| 2 |
from flask_cors import CORS
|
| 3 |
+
from PIL import Image
|
| 4 |
import io
|
| 5 |
import json
|
| 6 |
import os
|
| 7 |
import uuid
|
| 8 |
import google.generativeai as genai
|
| 9 |
+
import cv2
|
| 10 |
+
import numpy as np
|
| 11 |
|
| 12 |
generation_config = {
|
| 13 |
+
"temperature": 1,
|
| 14 |
+
"max_output_tokens": 8192,
|
| 15 |
}
|
| 16 |
|
| 17 |
safety_settings = [
|
| 18 |
+
{
|
| 19 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
| 20 |
+
"threshold": "BLOCK_NONE"
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
| 24 |
+
"threshold": "BLOCK_NONE"
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
| 28 |
+
"threshold": "BLOCK_NONE"
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
| 32 |
+
"threshold": "BLOCK_NONE"
|
| 33 |
+
},
|
| 34 |
]
|
| 35 |
|
| 36 |
GOOGLE_API_KEY = os.environ.get("TOKEN")
|
| 37 |
|
|
|
|
| 38 |
# Configuration de l'API Gemini
|
| 39 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 40 |
|
| 41 |
app = Flask(__name__)
|
| 42 |
CORS(app)
|
| 43 |
|
|
|
|
| 44 |
@app.route('/', methods=['GET'])
|
| 45 |
def svt():
|
| 46 |
"""Renders the SVT page."""
|
| 47 |
return render_template("svt.html")
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
# Prompt pour la détection d'objets
|
| 50 |
DETECTION_PROMPT = "Detect items, with no more than 20 items. Output a json list where each entry contains the 2D bounding box in \"box_2d\" and a text label in \"label\"."
|
|
|
|
| 80 |
return jsonify({'error': 'No selected file'}), 400
|
| 81 |
|
| 82 |
if file:
|
|
|
|
| 83 |
unique_filename = str(uuid.uuid4()) + os.path.splitext(file.filename)[1]
|
| 84 |
filename = os.path.join(app.config['UPLOAD_FOLDER'], unique_filename)
|
| 85 |
file.save(filename)
|
| 86 |
|
| 87 |
+
# Charger l'image avec OpenCV
|
| 88 |
+
image = cv2.imread(filename)
|
| 89 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convertir en RGB pour l'API Gemini
|
| 90 |
+
|
| 91 |
+
# Préparer l'image pour l'API Gemini
|
| 92 |
image_part = {
|
| 93 |
+
"mime_type": "image/jpeg",
|
| 94 |
+
"data": cv2.imencode('.jpg', image_rgb)[1].tobytes()
|
| 95 |
}
|
|
|
|
| 96 |
|
| 97 |
+
# Détection d'objets et dessin des boîtes (avec gestion d'erreur)
|
| 98 |
+
detection_results = [] # Initialiser une liste vide pour les résultats de détection
|
| 99 |
try:
|
| 100 |
+
model = genai.GenerativeModel("gemini-1.5-flash-exp", safety_settings=safety_settings, generation_config=generation_config)
|
| 101 |
+
response = model.generate_content([DETECTION_PROMPT, image_part])
|
| 102 |
+
cleaned_response_text = response.text.replace('\n', '')
|
| 103 |
+
|
| 104 |
+
if cleaned_response_text.startswith("```json"):
|
| 105 |
+
cleaned_response_text = cleaned_response_text[7:]
|
| 106 |
+
if cleaned_response_text.endswith("```"):
|
| 107 |
+
cleaned_response_text = cleaned_response_text[:-3]
|
| 108 |
+
|
| 109 |
+
detection_results = json.loads(cleaned_response_text)
|
| 110 |
+
|
| 111 |
+
# Dessiner les boîtes englobantes avec OpenCV
|
| 112 |
+
for item in detection_results:
|
| 113 |
+
box = item['box_2d']
|
| 114 |
+
label = str(item['label'])
|
| 115 |
+
x0, y0, x1, y1 = box
|
| 116 |
+
cv2.rectangle(image, (x0, y0), (x1, y1), (255, 0, 0), 2)
|
| 117 |
+
cv2.putText(image, label, (x0, y0 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
|
| 118 |
+
|
| 119 |
+
except Exception as e:
|
| 120 |
+
print(f"Erreur lors de la détection d'objets ou du dessin des boîtes : {e}")
|
| 121 |
+
|
| 122 |
+
# Enregistrer l'image traitée
|
| 123 |
output_filename = os.path.join(app.config['UPLOAD_FOLDER'], 'output_' + unique_filename)
|
| 124 |
+
cv2.imwrite(output_filename, image)
|
| 125 |
|
| 126 |
+
# Générer la description
|
| 127 |
+
model = genai.GenerativeModel("gemini-1.5-flash-exp", safety_settings=safety_settings, generation_config=generation_config)
|
| 128 |
response = model.generate_content([DESCRIPTION_PROMPT, image_part])
|
| 129 |
description = response.text
|
| 130 |
|
|
|
|
| 131 |
return jsonify({
|
| 132 |
+
'image_path': '/uploads/' + 'output_' + unique_filename,
|
| 133 |
'description': description,
|
| 134 |
'detected_objects': detection_results
|
| 135 |
})
|
|
|
|
| 138 |
print(f"Une erreur s'est produite : {e}")
|
| 139 |
return jsonify({'error': f'Erreur lors du traitement de l\'image : {e}'}), 500
|
| 140 |
|
|
|
|
| 141 |
@app.route('/uploads/<filename>')
|
| 142 |
def uploaded_file(filename):
|
| 143 |
return send_file(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|