Enoder commited on
Commit
f5238b4
·
verified ·
1 Parent(s): f769eb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -27
app.py CHANGED
@@ -3,48 +3,124 @@ import numpy as np
3
  from PIL import Image
4
  import requests
5
  from io import BytesIO
 
 
6
 
7
  # Initialisation des données
8
  if "images" not in st.session_state:
9
  st.session_state.images = []
 
 
 
 
 
 
 
 
10
 
11
- # Téléchargement d'images basiques selon le bouton cliqué
12
- def download_image(animal):
13
- url = {
14
- "Chat": "https://i.ibb.co/2FnDthw/IMG-6419.jpg", # Lien mis à jour pour le chat
15
- "Chien": "https://example.com/sample-dog-image.jpg",
16
- "Cheval": "https://example.com/sample-horse-image.jpg"
17
- }.get(animal, None)
18
- if url:
19
- response = requests.get(url)
20
- img = Image.open(BytesIO(response.content)).resize((300, 300))
21
- st.session_state.images.append(img)
22
- analyze_image(img)
 
 
 
 
 
 
 
 
 
23
 
24
- # Analyse basique de l'image
25
  def analyze_image(img):
26
- st.write("Analyse basique de l'image")
27
  arr = np.array(img)
28
  avg_color = arr.mean(axis=(0, 1))
29
- st.write(f"Couleur moyenne: {avg_color}")
 
 
 
 
 
 
 
 
 
 
30
 
31
  # Interface Streamlit
32
- st.title("Générateur d'Images Auto-Entrant (Démonstration)")
33
  st.write("Choisissez un animal pour générer une image et analyser ses pixels.")
34
 
 
35
  col1, col2, col3 = st.columns(3)
36
 
37
- if col1.button("Chat"):
38
- download_image("Chat")
39
- if col2.button("Chien"):
40
- download_image("Chien")
41
- if col3.button("Cheval"):
42
- download_image("Cheval")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
- # Affichage des images et analyses
45
  for idx, img in enumerate(st.session_state.images):
46
- st.image(img, caption=f"Image {idx+1}", width=300)
47
 
48
- # Bouton pour réinitialiser
49
- if st.button("Redémarrer", key="reset"):
50
- st.session_state.images = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  from PIL import Image
4
  import requests
5
  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://i.ibb.co/2FnDthw/IMG-6419.jpg",
27
+ "https://placekitten.com/300/300" # Exemple d'URL fictive
28
+ ],
29
+ "chien": [
30
+ "https://placedog.net/300/300", # Exemple d'URL fictive
31
+ "https://dog.ceo/api/breeds/image/random"
32
+ ],
33
+ "cheval": [
34
+ "https://placehorse.com/300/300" # Exemple d'URL fictive
35
+ ]
36
+ }
37
+
38
+ url = random.choice(animal_images[animal])
39
+ response = requests.get(url)
40
+ img = Image.open(BytesIO(response.content)).resize((300, 300))
41
+ return img
42
 
43
+ # Fonction pour analyser l'image (simulée)
44
  def analyze_image(img):
 
45
  arr = np.array(img)
46
  avg_color = arr.mean(axis=(0, 1))
47
+ return avg_color
48
+
49
+ # Fonction pour redémarrer le processus
50
+ def reset_process():
51
+ st.session_state.images = []
52
+ st.session_state.validated_data = []
53
+ st.session_state.animal_data = {
54
+ "chat": [],
55
+ "chien": [],
56
+ "cheval": []
57
+ }
58
 
59
  # Interface Streamlit
60
+ st.title("Générateur d'Images Auto-Entrant")
61
  st.write("Choisissez un animal pour générer une image et analyser ses pixels.")
62
 
63
+ # Boutons pour les animaux
64
  col1, col2, col3 = st.columns(3)
65
 
66
+ with col1:
67
+ if st.button("Chat"):
68
+ img = download_random_image("chat")
69
+ st.session_state.images.append(img)
70
+ avg_color = analyze_image(img)
71
+ st.write(f"Couleur moyenne : {avg_color}")
72
+
73
+ with col2:
74
+ if st.button("Chien"):
75
+ img = download_random_image("chien")
76
+ st.session_state.images.append(img)
77
+ avg_color = analyze_image(img)
78
+ st.write(f"Couleur moyenne : {avg_color}")
79
+
80
+ with col3:
81
+ if st.button("Cheval"):
82
+ img = download_random_image("cheval")
83
+ st.session_state.images.append(img)
84
+ avg_color = analyze_image(img)
85
+ st.write(f"Couleur moyenne : {avg_color}")
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
+ # Bouton pour redémarrer le processus
117
+ if st.button("Redémarrer"):
118
+ reset_process()
119
+
120
+ # Démarrer automatiquement après 3 secondes si le champ "Don" n'a pas été rempli
121
+ if "don_field_visible" not in st.session_state:
122
+ st.session_state.don_field_visible = False
123
+
124
+ if not st.session_state.don_field_visible:
125
+ time.sleep(3)
126
+ # Vous pouvez ajouter ici le code pour relancer le processus ou une action de votre choix