Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,72 +2,77 @@ import streamlit as st
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
import random
|
| 5 |
-
import io
|
| 6 |
|
| 7 |
-
# Initialisation des
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
# Fonction pour
|
| 11 |
-
def
|
| 12 |
-
# Ouvrir et redimensionner l'image importée
|
| 13 |
-
image = Image.open(uploaded_file).resize((300, 300))
|
| 14 |
-
# Convertir l'image en tableau de pixels pour analyse
|
| 15 |
-
pixel_data = np.array(image)
|
| 16 |
-
return pixel_data
|
| 17 |
-
|
| 18 |
-
# Fonction pour générer une image avec variations basées sur les pixels environnants
|
| 19 |
-
def generate_image_with_variation(base_pixels, label, avoid_data=None):
|
| 20 |
width, height = 300, 300
|
| 21 |
generated_image = np.zeros((height, width, 3), dtype=np.uint8)
|
| 22 |
-
|
| 23 |
-
for i in range(height):
|
| 24 |
-
for j in range(width):
|
| 25 |
-
#
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
new_pixel = np.clip(base_pixel + variation, 0, 255)
|
| 31 |
-
|
| 32 |
-
# Éviter les combinaisons déjà marquées comme erreur
|
| 33 |
-
if avoid_data and (i, j) in avoid_data:
|
| 34 |
-
new_pixel = (new_pixel + np.array([random.randint(0, 50) for _ in range(3)])) % 255
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
generated_image[i, j] = new_pixel
|
| 37 |
|
| 38 |
return Image.fromarray(generated_image)
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
#
|
|
|
|
| 45 |
uploaded_file = st.file_uploader("Téléchargez une image de référence (JPG ou PNG)", type=["jpg", "jpeg", "png"])
|
| 46 |
|
| 47 |
-
# Génération
|
| 48 |
if uploaded_file:
|
| 49 |
-
# Traiter l'image téléchargée
|
| 50 |
base_pixels = process_uploaded_image(uploaded_file)
|
| 51 |
-
st.image(Image.fromarray(base_pixels), caption="Image
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
animal = st.selectbox("Sélectionnez un animal :", ["chat", "chien", "cheval"])
|
| 55 |
if st.button("Générer une image"):
|
| 56 |
-
generated_image =
|
| 57 |
-
st.image(generated_image, caption=
|
| 58 |
|
| 59 |
-
#
|
| 60 |
if st.button("Oui"):
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
elif st.button("Non"):
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
for j in range(300):
|
| 67 |
-
error_data[animal].add((i, j))
|
| 68 |
-
st.error("Image rejetée et erreurs enregistrées!")
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
import random
|
|
|
|
| 5 |
|
| 6 |
+
# Initialisation des listes de paramètres
|
| 7 |
+
oe = []
|
| 8 |
+
nn = []
|
| 9 |
|
| 10 |
+
# Fonction pour générer une image en fonction des pixels environnants
|
| 11 |
+
def generate_image_with_neighboring_pixels(base_pixels, avoid_data=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
width, height = 300, 300
|
| 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 |
+
# Éviter les données précédemment refusées
|
| 33 |
+
if avoid_data and tuple(new_pixel) in avoid_data:
|
| 34 |
+
new_pixel = (new_pixel + np.array([random.randint(20, 40) for _ in range(3)])) % 255
|
| 35 |
+
|
| 36 |
generated_image[i, j] = new_pixel
|
| 37 |
|
| 38 |
return Image.fromarray(generated_image)
|
| 39 |
|
| 40 |
+
# Fonction pour traiter l'image importée
|
| 41 |
+
def process_uploaded_image(uploaded_file):
|
| 42 |
+
# Ouvrir et redimensionner l'image importée
|
| 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 |
+
# Interface utilisateur
|
| 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 de l'image
|
| 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 |
+
# Bouton pour générer l'image
|
|
|
|
| 59 |
if st.button("Générer une image"):
|
| 60 |
+
generated_image = generate_image_with_neighboring_pixels(base_pixels, avoid_data=set(nn))
|
| 61 |
+
st.image(generated_image, caption="Image générée", use_column_width=True)
|
| 62 |
|
| 63 |
+
# Validation de l'image générée
|
| 64 |
if st.button("Oui"):
|
| 65 |
+
# Enregistrer les paramètres dans la liste 'oe'
|
| 66 |
+
oe.clear()
|
| 67 |
+
oe.append(base_pixels.tolist())
|
| 68 |
+
st.success("Image validée et paramètres enregistrés dans 'oe'!")
|
| 69 |
elif st.button("Non"):
|
| 70 |
+
# Ajouter les paramètres dans la liste 'nn'
|
| 71 |
+
nn.append(generated_image.getdata().tolist())
|
| 72 |
+
st.error("Image rejetée et paramètres enregistrés dans 'nn'!")
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# Afficher les listes 'oe' et 'nn'
|
| 75 |
+
st.write("### Liste des paramètres validés ('oe'):")
|
| 76 |
+
st.write(oe)
|
| 77 |
+
st.write("### Liste des paramètres rejetés ('nn'):")
|
| 78 |
+
st.write(nn)
|