Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,76 +3,78 @@ import numpy as np
|
|
| 3 |
from PIL import Image
|
| 4 |
import random
|
| 5 |
|
| 6 |
-
#
|
| 7 |
oe = []
|
| 8 |
nn = []
|
| 9 |
|
| 10 |
-
# Fonction pour
|
| 11 |
-
def
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
generated_image = np.zeros((height, width, 3), dtype=np.uint8)
|
| 14 |
-
|
| 15 |
-
for i in range(1, height - 1):
|
| 16 |
-
for j in range(1, width - 1):
|
| 17 |
-
# Obtenir les valeurs des pixels environnants (haut, bas, gauche, droite)
|
| 18 |
-
neighbors = [
|
| 19 |
-
base_pixels[i-1, j], # haut
|
| 20 |
-
base_pixels[i+1, j], # bas
|
| 21 |
-
base_pixels[i, j-1], # gauche
|
| 22 |
-
base_pixels[i, j+1], # droite
|
| 23 |
-
]
|
| 24 |
-
|
| 25 |
-
# Moyenne des pixels environnants pour la variation
|
| 26 |
-
mean_pixel = np.mean(neighbors, axis=0)
|
| 27 |
-
|
| 28 |
-
# Ajustement aléatoire pour éviter la répétition
|
| 29 |
-
variation = np.array([random.randint(-15, 15) for _ in range(3)])
|
| 30 |
-
new_pixel = np.clip(mean_pixel + variation, 0, 255)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
return Image.fromarray(generated_image)
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
image = Image.open(uploaded_file).resize((300, 300))
|
| 44 |
-
# Convertir l'image en tableau de pixels pour analyse
|
| 45 |
-
pixel_data = np.array(image)
|
| 46 |
-
return pixel_data
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
st.title("Générateur d'images basées sur pixels environnants")
|
| 50 |
uploaded_file = st.file_uploader("Téléchargez une image de référence (JPG ou PNG)", type=["jpg", "jpeg", "png"])
|
| 51 |
|
| 52 |
-
# Génération
|
| 53 |
if uploaded_file:
|
| 54 |
-
# Traiter l'image téléchargée
|
| 55 |
base_pixels = process_uploaded_image(uploaded_file)
|
| 56 |
-
st.image(Image.fromarray(base_pixels), caption="Image de référence", use_column_width=True)
|
| 57 |
|
| 58 |
-
#
|
| 59 |
if st.button("Générer une image"):
|
| 60 |
-
generated_image =
|
| 61 |
st.image(generated_image, caption="Image générée", use_column_width=True)
|
| 62 |
|
| 63 |
-
#
|
| 64 |
if st.button("Oui"):
|
| 65 |
-
# Enregistrer les
|
| 66 |
-
oe.
|
| 67 |
-
oe
|
| 68 |
-
st.success("Image validée
|
|
|
|
| 69 |
elif st.button("Non"):
|
| 70 |
-
#
|
| 71 |
-
nn.
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
st.
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
st.
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import random
|
| 5 |
|
| 6 |
+
# Listes pour stocker les pixels validés et non validés
|
| 7 |
oe = []
|
| 8 |
nn = []
|
| 9 |
|
| 10 |
+
# Fonction pour traiter et redimensionner l'image importée
|
| 11 |
+
def process_uploaded_image(uploaded_file):
|
| 12 |
+
image = Image.open(uploaded_file).resize((300, 300))
|
| 13 |
+
pixel_data = np.array(image)
|
| 14 |
+
return pixel_data
|
| 15 |
+
|
| 16 |
+
# Fonction pour générer une image en fonction des pixels voisins
|
| 17 |
+
def generate_image_from_neighbors(base_pixels):
|
| 18 |
+
height, width, _ = base_pixels.shape
|
| 19 |
generated_image = np.zeros((height, width, 3), dtype=np.uint8)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Itération sur chaque pixel de l'image
|
| 22 |
+
for i in range(height):
|
| 23 |
+
for j in range(width):
|
| 24 |
+
# Collecte des couleurs des pixels environnants
|
| 25 |
+
surrounding_pixels = []
|
| 26 |
+
for di in range(-1, 2): # -1, 0, 1
|
| 27 |
+
for dj in range(-1, 2): # -1, 0, 1
|
| 28 |
+
if 0 <= i + di < height and 0 <= j + dj < width:
|
| 29 |
+
surrounding_pixels.append(base_pixels[i + di, j + dj])
|
| 30 |
+
|
| 31 |
+
# Calculer la moyenne des pixels environnants
|
| 32 |
+
surrounding_pixels = np.array(surrounding_pixels)
|
| 33 |
+
new_pixel_color = np.mean(surrounding_pixels, axis=0)
|
| 34 |
|
| 35 |
+
# Ajouter une petite variation aléatoire pour générer de l'unicité
|
| 36 |
+
variation = np.array([random.randint(-20, 20) for _ in range(3)])
|
| 37 |
+
new_pixel_color = np.clip(new_pixel_color + variation, 0, 255).astype(np.uint8)
|
| 38 |
+
generated_image[i, j] = new_pixel_color
|
| 39 |
|
| 40 |
return Image.fromarray(generated_image)
|
| 41 |
|
| 42 |
+
# Interface utilisateur pour télécharger et afficher l'image importée
|
| 43 |
+
st.title("Générateur d'images d'animaux basé sur l'image importée")
|
| 44 |
+
st.write("Choisissez un animal pour générer une image ou téléchargez une image comme référence.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Téléchargement de l'image importée
|
|
|
|
| 47 |
uploaded_file = st.file_uploader("Téléchargez une image de référence (JPG ou PNG)", type=["jpg", "jpeg", "png"])
|
| 48 |
|
| 49 |
+
# Génération d'image selon la référence téléchargée
|
| 50 |
if uploaded_file:
|
|
|
|
| 51 |
base_pixels = process_uploaded_image(uploaded_file)
|
| 52 |
+
st.image(Image.fromarray(base_pixels), caption="Image importée de référence", use_column_width=True)
|
| 53 |
|
| 54 |
+
# Choisir l'animal et générer une nouvelle image basée sur cette référence
|
| 55 |
if st.button("Générer une image"):
|
| 56 |
+
generated_image = generate_image_from_neighbors(base_pixels)
|
| 57 |
st.image(generated_image, caption="Image générée", use_column_width=True)
|
| 58 |
|
| 59 |
+
# Options de validation de l'image générée
|
| 60 |
if st.button("Oui"):
|
| 61 |
+
# Enregistrer les pixels de l'image générée dans la liste "oe"
|
| 62 |
+
oe.extend(generated_image.getdata())
|
| 63 |
+
oe = list(set(oe)) # Enlever les doublons
|
| 64 |
+
st.success("Image validée!")
|
| 65 |
+
|
| 66 |
elif st.button("Non"):
|
| 67 |
+
# Enregistrer les pixels de l'image générée dans la liste "nn"
|
| 68 |
+
nn.extend(generated_image.getdata())
|
| 69 |
+
nn = list(set(nn)) # Enlever les doublons
|
| 70 |
+
st.error("Image rejetée!")
|
| 71 |
+
|
| 72 |
+
# Affichage des listes de pixels validés et non validés
|
| 73 |
+
st.write("Pixels validés (oe):", oe)
|
| 74 |
+
st.write("Pixels non validés (nn):", nn)
|
| 75 |
|
| 76 |
+
# Option pour réinitialiser les listes
|
| 77 |
+
if st.button("Réinitialiser les listes"):
|
| 78 |
+
oe.clear()
|
| 79 |
+
nn.clear()
|
| 80 |
+
st.success("Les listes de pixels ont été réinitialisées.")
|