Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,59 +6,90 @@ from io import BytesIO
|
|
| 6 |
import random
|
| 7 |
import time
|
| 8 |
|
| 9 |
-
# Initialisation des données
|
| 10 |
-
if "images" not in st.session_state:
|
| 11 |
-
st.session_state.images = []
|
| 12 |
-
if "validated_data" not in st.session_state:
|
| 13 |
-
st.session_state.validated_data = []
|
| 14 |
-
if "animal_data" not in st.session_state:
|
| 15 |
-
st.session_state.animal_data = {
|
| 16 |
-
"chat": [],
|
| 17 |
-
"chien": [],
|
| 18 |
-
"cheval": []
|
| 19 |
-
}
|
| 20 |
-
|
| 21 |
# Fonction pour télécharger une image aléatoire
|
| 22 |
def download_random_image(animal):
|
| 23 |
-
# Liste d'URLs d'images fictives à remplacer par de vraies images
|
| 24 |
animal_images = {
|
| 25 |
"chat": [
|
| 26 |
-
"https://
|
| 27 |
-
"https://placekitten.com/
|
|
|
|
| 28 |
],
|
| 29 |
"chien": [
|
| 30 |
-
"https://
|
| 31 |
"https://dog.ceo/api/breeds/image/random"
|
| 32 |
],
|
| 33 |
"cheval": [
|
| 34 |
-
"https://
|
|
|
|
| 35 |
]
|
| 36 |
}
|
| 37 |
-
|
| 38 |
url = random.choice(animal_images[animal])
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# Interface Streamlit
|
| 60 |
-
st.title("
|
| 61 |
-
st.write("Choisissez un animal pour générer
|
| 62 |
|
| 63 |
# Boutons pour les animaux
|
| 64 |
col1, col2, col3 = st.columns(3)
|
|
@@ -66,61 +97,32 @@ col1, col2, col3 = st.columns(3)
|
|
| 66 |
with col1:
|
| 67 |
if st.button("Chat"):
|
| 68 |
img = download_random_image("chat")
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
|
| 73 |
with col2:
|
| 74 |
if st.button("Chien"):
|
| 75 |
img = download_random_image("chien")
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
|
| 80 |
with col3:
|
| 81 |
if st.button("Cheval"):
|
| 82 |
img = download_random_image("cheval")
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
# Affichage des images générées
|
| 88 |
-
for idx, img in enumerate(st.session_state.images):
|
| 89 |
-
st.image(img, caption=f"Image {idx + 1}", width=300)
|
| 90 |
-
|
| 91 |
-
# Boutons de validation
|
| 92 |
-
if st.button("Oui"):
|
| 93 |
-
if st.session_state.images:
|
| 94 |
-
avg_color = analyze_image(st.session_state.images[-1])
|
| 95 |
-
st.session_state.validated_data.append(avg_color)
|
| 96 |
-
st.session_state.animal_data["chat" if "chat" in st.session_state.images[-1].filename else "chien" if "chien" in st.session_state.images[-1].filename else "cheval"].append(avg_color)
|
| 97 |
-
|
| 98 |
-
if st.button("Non"):
|
| 99 |
-
if st.session_state.images:
|
| 100 |
-
st.session_state.images.pop() # Ignore l'image
|
| 101 |
-
# Modifier les données précédentes pour éviter la même erreur
|
| 102 |
-
# Implémentez votre logique ici pour ajuster les données
|
| 103 |
-
|
| 104 |
-
# Champ de données manuelles
|
| 105 |
-
if st.button("Don"):
|
| 106 |
-
st.session_state.don_field_visible = True
|
| 107 |
-
|
| 108 |
-
if "don_field_visible" in st.session_state and st.session_state.don_field_visible:
|
| 109 |
-
don_text = st.text_area("Entrez vos données manuelles :")
|
| 110 |
-
if st.button("Envoyer"):
|
| 111 |
-
if don_text:
|
| 112 |
-
# Traitement des données du champ "Don"
|
| 113 |
-
st.session_state.animal_data["chat"].append(don_text) # Exemple d'utilisation
|
| 114 |
-
st.session_state.don_field_visible = False
|
| 115 |
|
| 116 |
-
#
|
| 117 |
-
|
| 118 |
-
reset_process()
|
| 119 |
|
| 120 |
-
#
|
| 121 |
-
if "
|
| 122 |
-
st.session_state.
|
|
|
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
| 6 |
import random
|
| 7 |
import time
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# Fonction pour télécharger une image aléatoire
|
| 10 |
def download_random_image(animal):
|
|
|
|
| 11 |
animal_images = {
|
| 12 |
"chat": [
|
| 13 |
+
"https://placekitten.com/300/300",
|
| 14 |
+
"https://placekitten.com/301/301",
|
| 15 |
+
"https://placekitten.com/302/302"
|
| 16 |
],
|
| 17 |
"chien": [
|
| 18 |
+
"https://images.dog.ceo/breeds/hound-afghan/n02096585_1034.jpg",
|
| 19 |
"https://dog.ceo/api/breeds/image/random"
|
| 20 |
],
|
| 21 |
"cheval": [
|
| 22 |
+
"https://cdn.pixabay.com/photo/2015/09/20/10/07/horse-557133_1280.jpg",
|
| 23 |
+
"https://cdn.pixabay.com/photo/2018/05/07/17/24/horse-3380533_1280.jpg"
|
| 24 |
]
|
| 25 |
}
|
| 26 |
+
|
| 27 |
url = random.choice(animal_images[animal])
|
| 28 |
+
try:
|
| 29 |
+
response = requests.get(url)
|
| 30 |
+
response.raise_for_status() # Vérifie si la requête a réussi
|
| 31 |
+
img = Image.open(BytesIO(response.content)).resize((300, 300))
|
| 32 |
+
return img
|
| 33 |
+
except (requests.exceptions.RequestException, UnidentifiedImageError) as e:
|
| 34 |
+
st.error(f"Erreur lors du téléchargement ou de l'ouverture de l'image : {e}")
|
| 35 |
+
return None
|
| 36 |
+
|
| 37 |
+
# Fonction pour transformer les pixels
|
| 38 |
+
def transform_image(original_image, method):
|
| 39 |
+
width, height = original_image.size
|
| 40 |
+
img_array = np.array(original_image)
|
| 41 |
+
|
| 42 |
+
# Crée une nouvelle image vide
|
| 43 |
+
new_img_array = np.zeros_like(img_array)
|
| 44 |
+
|
| 45 |
+
for x in range(width):
|
| 46 |
+
for y in range(height):
|
| 47 |
+
if method == "moyenne":
|
| 48 |
+
# Moyenne des voisins
|
| 49 |
+
neighbors = []
|
| 50 |
+
for dx in [-1, 0, 1]:
|
| 51 |
+
for dy in [-1, 0, 1]:
|
| 52 |
+
if dx == 0 and dy == 0:
|
| 53 |
+
continue
|
| 54 |
+
nx, ny = x + dx, y + dy
|
| 55 |
+
if 0 <= nx < width and 0 <= ny < height:
|
| 56 |
+
neighbors.append(img_array[ny, nx])
|
| 57 |
+
if neighbors:
|
| 58 |
+
avg_color = np.mean(neighbors, axis=0)
|
| 59 |
+
new_img_array[y, x] = avg_color
|
| 60 |
+
|
| 61 |
+
elif method == "inversion":
|
| 62 |
+
# Inversion des couleurs
|
| 63 |
+
new_img_array[y, x] = 255 - img_array[y, x]
|
| 64 |
+
|
| 65 |
+
elif method == "flou":
|
| 66 |
+
# Filtre de flou simple
|
| 67 |
+
neighbors = []
|
| 68 |
+
for dx in [-1, 0, 1]:
|
| 69 |
+
for dy in [-1, 0, 1]:
|
| 70 |
+
nx, ny = x + dx, y + dy
|
| 71 |
+
if 0 <= nx < width and 0 <= ny < height:
|
| 72 |
+
neighbors.append(img_array[ny, nx])
|
| 73 |
+
new_img_array[y, x] = np.mean(neighbors, axis=0)
|
| 74 |
+
|
| 75 |
+
elif method == "luminosite":
|
| 76 |
+
# Augmentation de la luminosité
|
| 77 |
+
new_img_array[y, x] = np.clip(img_array[y, x] + 50, 0, 255)
|
| 78 |
+
|
| 79 |
+
elif method == "detec_bords":
|
| 80 |
+
# Détection des bords simple
|
| 81 |
+
if x > 0 and y > 0 and x < width - 1 and y < height - 1:
|
| 82 |
+
gx = (img_array[y-1, x+1] + 2 * img_array[y, x+1] + img_array[y+1, x+1] -
|
| 83 |
+
img_array[y-1, x-1] - 2 * img_array[y, x-1] - img_array[y+1, x-1])
|
| 84 |
+
gy = (img_array[y+1, x-1] + 2 * img_array[y+1, x] + img_array[y+1, x+1] -
|
| 85 |
+
img_array[y-1, x-1] - 2 * img_array[y-1, x] - img_array[y-1, x+1])
|
| 86 |
+
new_img_array[y, x] = np.clip(np.sqrt(gx**2 + gy**2), 0, 255)
|
| 87 |
+
|
| 88 |
+
return Image.fromarray(new_img_array.astype('uint8'))
|
| 89 |
|
| 90 |
# Interface Streamlit
|
| 91 |
+
st.title("Transformateur d'Images")
|
| 92 |
+
st.write("Choisissez un animal pour générer et transformer une image.")
|
| 93 |
|
| 94 |
# Boutons pour les animaux
|
| 95 |
col1, col2, col3 = st.columns(3)
|
|
|
|
| 97 |
with col1:
|
| 98 |
if st.button("Chat"):
|
| 99 |
img = download_random_image("chat")
|
| 100 |
+
if img:
|
| 101 |
+
st.session_state.image = img
|
| 102 |
+
st.image(img, caption="Image d'origine", use_column_width=True)
|
| 103 |
|
| 104 |
with col2:
|
| 105 |
if st.button("Chien"):
|
| 106 |
img = download_random_image("chien")
|
| 107 |
+
if img:
|
| 108 |
+
st.session_state.image = img
|
| 109 |
+
st.image(img, caption="Image d'origine", use_column_width=True)
|
| 110 |
|
| 111 |
with col3:
|
| 112 |
if st.button("Cheval"):
|
| 113 |
img = download_random_image("cheval")
|
| 114 |
+
if img:
|
| 115 |
+
st.session_state.image = img
|
| 116 |
+
st.image(img, caption="Image d'origine", use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
# Sélection de la méthode de transformation
|
| 119 |
+
method = st.selectbox("Choisissez une méthode de transformation", ["moyenne", "inversion", "flou", "luminosite", "detec_bords"])
|
|
|
|
| 120 |
|
| 121 |
+
# Transformation de l'image
|
| 122 |
+
if "image" in st.session_state:
|
| 123 |
+
transformed_image = transform_image(st.session_state.image, method)
|
| 124 |
+
st.image(transformed_image, caption="Image Transformée", use_column_width=True)
|
| 125 |
|
| 126 |
+
# Bouton pour redémarrer le processus
|
| 127 |
+
if st.button("Redémarrer"):
|
| 128 |
+
st.session_state.image = None
|