Chachado1234 commited on
Commit
e431aaa
·
verified ·
1 Parent(s): b0f6b1e

Ajout du fichier app.py pour une application Gradio permettant l’édition de photos avec l’IA.
Fonctionnalités incluses :
✅ Correction automatique des couleurs (contraste, luminosité, saturation)
✅ Suppression d’objets/arrière-plan avec IA
✅ Maquillage virtuel (rouge à lèvres, blush)

Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ import mediapipe as mp
5
+ from rembg import remove
6
+ from PIL import Image, ImageEnhance
7
+
8
+ # Initialisation de Mediapipe pour la détection de visage
9
+ mp_face_mesh = mp.solutions.face_mesh
10
+ face_mesh = mp_face_mesh.FaceMesh()
11
+
12
+ # Fonction pour la suppression d’arrière-plan
13
+ def remove_bg(image):
14
+ return remove(image)
15
+
16
+ # Fonction pour la retouche beauté
17
+ def beauty_filter(image, smooth_factor=50):
18
+ img = np.array(image)
19
+ img_rgb = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
20
+
21
+ results = face_mesh.process(img_rgb)
22
+ if results.multi_face_landmarks:
23
+ mask = np.zeros_like(img_rgb)
24
+ for face_landmarks in results.multi_face_landmarks:
25
+ points = [(int(p.x * img.shape[1]), int(p.y * img.shape[0])) for p in face_landmarks.landmark]
26
+ convex_hull = cv2.convexHull(np.array(points))
27
+ cv2.fillConvexPoly(mask, convex_hull, (255, 255, 255))
28
+
29
+ blur = cv2.GaussianBlur(img_rgb, (smooth_factor, smooth_factor), 0)
30
+ img_rgb = np.where(mask == (255, 255, 255), blur, img_rgb)
31
+
32
+ return Image.fromarray(cv2.cvtColor(img_rgb, cv2.COLOR_BGR2RGB))
33
+
34
+ # Fonction pour la correction automatique des couleurs
35
+ def auto_color_correction(image, brightness, contrast, saturation):
36
+ img = ImageEnhance.Brightness(image).enhance(brightness)
37
+ img = ImageEnhance.Contrast(img).enhance(contrast)
38
+ img = ImageEnhance.Color(img).enhance(saturation)
39
+ return img
40
+
41
+ # Fonction pour le maquillage virtuel
42
+ def virtual_makeup(image, lipstick_intensity, blush_intensity):
43
+ img = np.array(image)
44
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
45
+
46
+ results = face_mesh.process(img)
47
+ if results.multi_face_landmarks:
48
+ for face_landmarks in results.multi_face_landmarks:
49
+ lips = [(int(p.x * img.shape[1]), int(p.y * img.shape[0])) for p in face_landmarks.landmark[61:68]]
50
+ cv2.fillPoly(img, [np.array(lips)], (0, 0, int(255 * lipstick_intensity)))
51
+
52
+ cheek = [(int(p.x * img.shape[1]), int(p.y * img.shape[0])) for p in face_landmarks.landmark[234:236]]
53
+ cv2.fillPoly(img, [np.array(cheek)], (int(255 * blush_intensity), 0, 0))
54
+
55
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
56
+ return Image.fromarray(img)
57
+
58
+ # Interface Gradio
59
+ with gr.Blocks() as demo:
60
+ gr.Markdown("## Éditeur de Photos IA : Retouche Beauté, Maquillage, Amélioration des Images")
61
+
62
+ with gr.Tab("Suppression d'Arrière-Plan"):
63
+ img_input = gr.Image(type="pil", label="Image Originale")
64
+ img_output = gr.Image(type="pil", label="Image sans Arrière-Plan")
65
+ btn_remove_bg = gr.Button("Supprimer l'Arrière-Plan")
66
+ btn_remove_bg.click(remove_bg, inputs=img_input, outputs=img_output)
67
+
68
+ with gr.Tab("Retouche Beauté"):
69
+ img_input2 = gr.Image(type="pil", label="Image Originale")
70
+ smooth_slider = gr.Slider(5, 100, value=50, label="Lissage de la peau")
71
+ img_output2 = gr.Image(type="pil", label="Image Retouchée")
72
+ btn_beauty = gr.Button("Appliquer Retouche Beauté")
73
+ btn_beauty.click(beauty_filter, inputs=[img_input2, smooth_slider], outputs=img_output2)
74
+
75
+ with gr.Tab("Correction des Couleurs"):
76
+ img_input3 = gr.Image(type="pil", label="Image Originale")
77
+ brightness = gr.Slider(0.5, 2.0, value=1.0, label="Luminosité")
78
+ contrast = gr.Slider(0.5, 2.0, value=1.0, label="Contraste")
79
+ saturation = gr.Slider(0.5, 2.0, value=1.0, label="Saturation")
80
+ img_output3 = gr.Image(type="pil", label="Image Corrigée")
81
+ btn_correct_colors = gr.Button("Corriger les Couleurs")
82
+ btn_correct_colors.click(auto_color_correction, inputs=[img_input3, brightness, contrast, saturation], outputs=img_output3)
83
+
84
+ with gr.Tab("Maquillage Virtuel"):
85
+ img_input4 = gr.Image(type="pil", label="Image Originale")
86
+ lipstick_slider = gr.Slider(0.0, 1.0, value=0.5, label="Intensité Rouge à Lèvres")
87
+ blush_slider = gr.Slider(0.0, 1.0, value=0.5, label="Intensité Blush")
88
+ img_output4 = gr.Image(type="pil", label="Image avec Maquillage")
89
+ btn_makeup = gr.Button("Appliquer Maquillage Virtuel")
90
+ btn_makeup.click(virtual_makeup, inputs=[img_input4, lipstick_slider, blush_slider], outputs=img_output4)
91
+
92
+ demo.launch()