Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,127 +1,99 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
-
import requests
|
| 5 |
-
from io import BytesIO
|
| 6 |
import random
|
| 7 |
import time
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
st.
|
| 91 |
-
st.
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
with col2:
|
| 104 |
-
if st.button("Chien"):
|
| 105 |
-
img = download_random_image("chien")
|
| 106 |
-
if img:
|
| 107 |
-
st.session_state.image = img
|
| 108 |
-
st.image(img, caption="Image d'origine", use_column_width=True)
|
| 109 |
-
|
| 110 |
-
with col3:
|
| 111 |
-
if st.button("Cheval"):
|
| 112 |
-
img = download_random_image("cheval")
|
| 113 |
-
if img:
|
| 114 |
-
st.session_state.image = img
|
| 115 |
-
st.image(img, caption="Image d'origine", use_column_width=True)
|
| 116 |
-
|
| 117 |
-
# Sélection de la méthode de transformation
|
| 118 |
-
method = st.selectbox("Choisissez une méthode de transformation", ["moyenne", "inversion", "flou", "luminosite", "detec_bords"])
|
| 119 |
-
|
| 120 |
-
# Transformation de l'image
|
| 121 |
-
if "image" in st.session_state:
|
| 122 |
-
transformed_image = transform_image(st.session_state.image, method)
|
| 123 |
-
st.image(transformed_image, caption="Image Transformée", use_column_width=True)
|
| 124 |
-
|
| 125 |
-
# Bouton pour redémarrer le processus
|
| 126 |
-
if st.button("Redémarrer"):
|
| 127 |
-
st.session_state.image = None
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
| 4 |
import random
|
| 5 |
import time
|
| 6 |
+
import io
|
| 7 |
+
|
| 8 |
+
# Configuration de la page Streamlit
|
| 9 |
+
st.title("Générateur d'images d'animaux")
|
| 10 |
+
st.write("Choisissez un animal pour générer une image ou téléchargez une image à déchiffrer.")
|
| 11 |
+
|
| 12 |
+
# Initialisation des variables
|
| 13 |
+
image_data = []
|
| 14 |
+
validation_data = {}
|
| 15 |
+
auto_training_data = {}
|
| 16 |
+
|
| 17 |
+
# Fonction pour créer une image aléatoire
|
| 18 |
+
def create_image(label):
|
| 19 |
+
width, height = 300, 300
|
| 20 |
+
image = np.zeros((height, width, 3), dtype=np.uint8)
|
| 21 |
+
|
| 22 |
+
if label == "chat":
|
| 23 |
+
for i in range(height):
|
| 24 |
+
for j in range(width):
|
| 25 |
+
image[i, j] = [random.randint(150, 255), random.randint(150, 255), random.randint(150, 255)]
|
| 26 |
+
elif label == "chien":
|
| 27 |
+
for i in range(height):
|
| 28 |
+
for j in range(width):
|
| 29 |
+
image[i, j] = [random.randint(100, 200), random.randint(50, 150), random.randint(0, 100)]
|
| 30 |
+
elif label == "cheval":
|
| 31 |
+
for i in range(height):
|
| 32 |
+
for j in range(width):
|
| 33 |
+
image[i, j] = [random.randint(0, 50), random.randint(0, 50), random.randint(0, 50)]
|
| 34 |
+
|
| 35 |
+
return Image.fromarray(image)
|
| 36 |
+
|
| 37 |
+
# Fonction pour traiter l'image téléchargée
|
| 38 |
+
def process_uploaded_image(uploaded_file):
|
| 39 |
+
# Ouvrir l'image
|
| 40 |
+
image = Image.open(uploaded_file)
|
| 41 |
+
# Redimensionner l'image à 300x300
|
| 42 |
+
image = image.resize((300, 300))
|
| 43 |
+
return image
|
| 44 |
+
|
| 45 |
+
# Fonction pour gérer la validation des images
|
| 46 |
+
def validate_image(is_valid, label):
|
| 47 |
+
if is_valid:
|
| 48 |
+
validation_data[label] = validation_data.get(label, 0) + 1
|
| 49 |
+
else:
|
| 50 |
+
if label in validation_data:
|
| 51 |
+
del validation_data[label]
|
| 52 |
+
|
| 53 |
+
# Interface utilisateur
|
| 54 |
+
animal = st.radio("Sélectionnez un animal :", ("chat", "chien", "cheval"))
|
| 55 |
+
uploaded_file = st.file_uploader("Ou téléchargez une image (JPG, PNG)", type=["jpg", "jpeg", "png"])
|
| 56 |
+
|
| 57 |
+
if st.button("Générer une image"):
|
| 58 |
+
img = create_image(animal)
|
| 59 |
+
st.image(img, caption=f"Image générée pour un {animal}", use_column_width=True)
|
| 60 |
+
|
| 61 |
+
if st.button("Oui"):
|
| 62 |
+
validate_image(True, animal)
|
| 63 |
+
st.success("Image validée!")
|
| 64 |
+
elif st.button("Non"):
|
| 65 |
+
validate_image(False, animal)
|
| 66 |
+
st.error("Image rejetée!")
|
| 67 |
+
|
| 68 |
+
# Si une image est téléchargée, la traiter et l'afficher
|
| 69 |
+
if uploaded_file is not None:
|
| 70 |
+
uploaded_image = process_uploaded_image(uploaded_file)
|
| 71 |
+
st.image(uploaded_image, caption="Image téléchargée", use_column_width=True)
|
| 72 |
+
|
| 73 |
+
# Validation de l'image téléchargée
|
| 74 |
+
if st.button("Valider l'image téléchargée"):
|
| 75 |
+
validate_image(True, "image téléchargée")
|
| 76 |
+
st.success("Image téléchargée validée!")
|
| 77 |
+
if st.button("Rejeter l'image téléchargée"):
|
| 78 |
+
validate_image(False, "image téléchargée")
|
| 79 |
+
st.error("Image téléchargée rejetée!")
|
| 80 |
+
|
| 81 |
+
# Bouton pour réinitialiser le processus
|
| 82 |
+
if st.button("Réinitialiser"):
|
| 83 |
+
validation_data.clear()
|
| 84 |
+
st.success("Le processus a été réinitialisé.")
|
| 85 |
+
|
| 86 |
+
# Champ de texte pour données manuelles
|
| 87 |
+
if st.button("Don"):
|
| 88 |
+
don_text = st.text_area("Entrez vos données :", "")
|
| 89 |
+
if st.button("Envoyer"):
|
| 90 |
+
if don_text:
|
| 91 |
+
auto_training_data['data'] = don_text
|
| 92 |
+
st.success("Données envoyées!")
|
| 93 |
+
else:
|
| 94 |
+
st.error("Le champ de texte ne peut pas être vide.")
|
| 95 |
+
|
| 96 |
+
# Démarrer le processus automatiquement après 3 secondes si le champ 'Don' n'est pas rempli
|
| 97 |
+
if 'data' not in auto_training_data:
|
| 98 |
+
time.sleep(3)
|
| 99 |
+
st.success("Démarrage automatique du processus d'apprentissage...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|