DavidNgoue commited on
Commit
62ec819
·
verified ·
1 Parent(s): 9901fb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -12
app.py CHANGED
@@ -6,6 +6,7 @@ from io import BytesIO
6
  import barcode
7
  from barcode.writer import ImageWriter
8
  import qrcode
 
9
 
10
 
11
  def image_to_bytes(img):
@@ -164,24 +165,63 @@ def main():
164
  # Charger le modèle Haarcascade pour la détection des visages
165
  face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
166
 
167
- # Bouton pour démarrer la détection des visages
168
- if st.button("Lancer la détection des visages"):
 
 
 
169
 
170
- # Initialiser la webcam
171
- cap = cv2.VideoCapture(0) # 0 pour la webcam par défaut
 
 
172
 
173
- if not cap.isOpened():
174
- st.error("Impossible d'accéder à la webcam.")
175
- else:
176
- st.info("Appuyez sur Ctrl+C pour arrêter la détection.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
 
178
- # Flux vidéo en temps réel
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  frame_placeholder = st.empty()
180
 
181
- while True:
182
  ret, frame = cap.read()
183
  if not ret:
184
- st.error("Erreur lors de la capture vidéo.")
185
  break
186
 
187
  # Convertir en niveaux de gris
@@ -200,10 +240,10 @@ def main():
200
  # Afficher l'image dans l'interface Streamlit
201
  frame_placeholder.image(frame, channels="RGB", use_container_width=True)
202
 
203
- # Libérer la webcam une fois le flux terminé
204
  cap.release()
205
 
206
 
207
 
 
208
  if __name__ == "__main__":
209
  main()
 
6
  import barcode
7
  from barcode.writer import ImageWriter
8
  import qrcode
9
+ import tempfile
10
 
11
 
12
  def image_to_bytes(img):
 
165
  # Charger le modèle Haarcascade pour la détection des visages
166
  face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
167
 
168
+ # Choix de la source
169
+ detection_option = st.radio(
170
+ "Choisis la source pour la détection faciale",
171
+ options=["Webcam", "Vidéo téléversée"]
172
+ )
173
 
174
+ if detection_option == "Webcam":
175
+ # Option pour utiliser la webcam
176
+ if st.button("Lancer la détection via webcam"):
177
+ cap = cv2.VideoCapture(0) # 0 pour la webcam par défaut
178
 
179
+ if not cap.isOpened():
180
+ st.error("Impossible d'accéder à la webcam, Hugging Face et le navigateur bloquent l'accès.")
181
+ else:
182
+ st.info("Appuyez sur Ctrl+C pour arrêter la détection.")
183
+ frame_placeholder = st.empty()
184
+
185
+ while True:
186
+ ret, frame = cap.read()
187
+ if not ret:
188
+ st.error("Erreur lors de la capture vidéo.")
189
+ break
190
+
191
+ # Convertir en niveaux de gris
192
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
193
+
194
+ # Détecter les visages
195
+ faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
196
+
197
+ # Dessiner des rectangles autour des visages détectés
198
+ for (x, y, w, h) in faces:
199
+ cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
200
 
201
+ # Convertir l'image pour Streamlit
202
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
203
+
204
+ # Afficher l'image dans l'interface Streamlit
205
+ frame_placeholder.image(frame, channels="RGB", use_container_width=True)
206
+
207
+ # Libérer la webcam une fois le flux terminé
208
+ cap.release()
209
+
210
+ elif detection_option == "Vidéo téléversée":
211
+ # Option pour téléverser une vidéo
212
+ uploaded_video = st.file_uploader("Charge une vidéo", type=["mp4", "avi", "mov"])
213
+ if uploaded_video is not None:
214
+ video_bytes = uploaded_video.read()
215
+ tfile = tempfile.NamedTemporaryFile(delete=False)
216
+ tfile.write(video_bytes)
217
+ tfile.close()
218
+
219
+ cap = cv2.VideoCapture(tfile.name)
220
  frame_placeholder = st.empty()
221
 
222
+ while cap.isOpened():
223
  ret, frame = cap.read()
224
  if not ret:
 
225
  break
226
 
227
  # Convertir en niveaux de gris
 
240
  # Afficher l'image dans l'interface Streamlit
241
  frame_placeholder.image(frame, channels="RGB", use_container_width=True)
242
 
 
243
  cap.release()
244
 
245
 
246
 
247
+
248
  if __name__ == "__main__":
249
  main()