JahDaGanj commited on
Commit
2a2a3f3
·
verified ·
1 Parent(s): 29afec0

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +76 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ import mediapipe as mp
5
+ from PIL import Image
6
+ import torch
7
+ from torchvision import transforms
8
+ from urllib.request import urlopen
9
+ from io import BytesIO
10
+
11
+ # ------------------ Haarsegmentierung: MODNet (einfaches Mock-Skript) ------------------
12
+ # In der echten Space-Version würdest du das Modell hier laden
13
+ def segment_hair(image):
14
+ # Hier sollte ein echtes Haarsegmentierungsmodell wie MODNet eingesetzt werden
15
+ # Zur Demonstration: Dummy-Funktion mit simplen Thresholds
16
+ img = np.array(image)
17
+ gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
18
+ mask = cv2.inRange(gray, 20, 255) # Pseudo-Haarmaske
19
+ result = cv2.bitwise_and(img, img, mask=mask)
20
+ return Image.fromarray(result)
21
+
22
+ # ------------------ Gesichts-Position erkennen ------------------
23
+ def get_head_bbox(image):
24
+ mp_face_detection = mp.solutions.face_detection
25
+ with mp_face_detection.FaceDetection(model_selection=1, min_detection_confidence=0.5) as face_detection:
26
+ results = face_detection.process(cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
27
+ if not results.detections:
28
+ return None
29
+ for detection in results.detections:
30
+ bbox = detection.location_data.relative_bounding_box
31
+ h, w, _ = image.shape
32
+ x = int(bbox.xmin * w)
33
+ y = int(bbox.ymin * h)
34
+ width = int(bbox.width * w)
35
+ height = int(bbox.height * h)
36
+ return (x, y, width, height)
37
+ return None
38
+
39
+ # ------------------ Frisur einsetzen ------------------
40
+ def transfer_hair(frisur_img, ziel_img):
41
+ frisur = segment_hair(frisur_img).convert("RGBA")
42
+ ziel = ziel_img.convert("RGBA")
43
+ ziel_np = np.array(ziel)
44
+ bbox = get_head_bbox(np.array(ziel))
45
+
46
+ if bbox is None:
47
+ return "Kein Gesicht erkannt", None
48
+
49
+ x, y, w, h = bbox
50
+
51
+ # Frisur skalieren
52
+ frisur = frisur.resize((w, int(h * 1.5)))
53
+
54
+ # Frisur leicht höher setzen
55
+ y_offset = y - int(h * 0.6)
56
+ x_offset = x
57
+
58
+ result = ziel.copy()
59
+ result.paste(frisur, (x_offset, max(0, y_offset)), frisur)
60
+
61
+ return result
62
+
63
+ # ------------------ Gradio Interface ------------------
64
+ with gr.Blocks() as demo:
65
+ gr.Markdown("## 🧠 KI-basierte Frisurübertragung\nLade ein Frisurbild und ein Zielbild hoch – die Frisur wird automatisch übertragen.")
66
+
67
+ with gr.Row():
68
+ frisur_input = gr.Image(label="Frisurbild (Portrait mit Frisur)", type="pil")
69
+ ziel_input = gr.Image(label="Zielbild (Ganzkörper, Gesicht sichtbar)", type="pil")
70
+
71
+ output = gr.Image(label="Ergebnisbild mit übertragener Frisur")
72
+
73
+ btn = gr.Button("Frisur übertragen")
74
+ btn.click(fn=transfer_hair, inputs=[frisur_input, ziel_input], outputs=output)
75
+
76
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio
2
+ opencv-python
3
+ numpy
4
+ mediapipe
5
+ torch
6
+ torchvision
7
+ Pillow