-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFaceApp.py
More file actions
62 lines (49 loc) · 2.49 KB
/
FaceApp.py
File metadata and controls
62 lines (49 loc) · 2.49 KB
1
2
3
4
5
6
7
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
import streamlit as st
import cv2
import numpy as np
# Charger le modèle cascade pour la détection de visages
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# Fonction pour détecter les visages
def detect_faces(image, scaleFactor, minNeighbors):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=scaleFactor, minNeighbors=minNeighbors)
return faces
# Fonction pour dessiner et enregistrer l'image avec les visages détectés
def save_image_with_faces(image, faces, output_path):
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 4) # Dessiner un rectangle bleu autour du visage
cv2.imwrite(output_path, image)
# Fonction pour convertir la couleur hexadécimale en BGR
def hex_to_bgr(hex_color):
hex_color = hex_color.lstrip('#')
return tuple(int(hex_color[i:i+2], 16) for i in (4, 2, 0))
# Interface utilisateur avec Streamlit
def main():
st.title('Détection de visages avec OpenCV et Streamlit')
uploaded_file = st.file_uploader("Uploader une image", type=['jpg', 'png', 'jpeg'])
if uploaded_file is not None:
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
image = cv2.imdecode(file_bytes, 1)
st.image(image, channels="BGR", caption='Image originale')
# Paramètres ajustables
scaleFactor = st.slider('scaleFactor', 1.1, 2.0, 1.2, step=0.1)
minNeighbors = st.slider('minNeighbors', 1, 10, 3)
color = st.color_picker('Choisir la couleur du rectangle', '#ff0000') # Par défaut, rouge
bgr_color = hex_to_bgr(color)
if st.button('Détecter les visages'):
faces = detect_faces(image, scaleFactor, minNeighbors)
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), bgr_color, 4)
st.image(image, channels="BGR", caption='Image avec visages détectés')
# Save the image with detected faces
output_path = 'image_with_faces.jpg' # Nom du fichier de sortie
save_image_with_faces(image, faces, output_path)
with open(output_path, 'rb') as file:
btn = st.download_button(
label="Télécharger image avec visages détectés",
data=file,
file_name="image_with_faces.jpg",
mime="image/jpeg"
)
if __name__ == '__main__':
main()