KarthikAI commited on
Commit
a042988
·
verified ·
1 Parent(s): 1e60bad

Create face_to_prompt.py

Browse files
Files changed (1) hide show
  1. face_to_prompt.py +50 -0
face_to_prompt.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from deepface import DeepFace
2
+ from PIL import Image
3
+ import insightface
4
+ import numpy as np
5
+
6
+ def extract_face_prompt(img_path):
7
+ # Load image as numpy array
8
+ img = Image.open(img_path).convert("RGB")
9
+ img_np = np.array(img)
10
+
11
+ # DeepFace analysis
12
+ deepface_result = DeepFace.analyze(
13
+ img_path, actions=['age', 'gender', 'race', 'emotion'], enforce_detection=False
14
+ )
15
+ age = deepface_result['age']
16
+ gender = deepface_result['dominant_gender']
17
+ race = deepface_result['dominant_race']
18
+ emotion = deepface_result['dominant_emotion']
19
+
20
+ # InsightFace analysis
21
+ model = insightface.app.FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider'])
22
+ model.prepare(ctx_id=0)
23
+ faces = model.get(img_np)
24
+
25
+ face_attrs = {}
26
+ if faces:
27
+ face = faces[0]
28
+ face_attrs['glasses'] = face.get('glass', 0) > 0
29
+ face_attrs['beard'] = face.get('beard', 0) > 0
30
+ face_attrs['mustache'] = face.get('mustache', 0) > 0
31
+
32
+ attr_parts = []
33
+ if face_attrs.get('glasses'):
34
+ attr_parts.append("wearing glasses")
35
+ if face_attrs.get('beard'):
36
+ attr_parts.append("has a beard")
37
+ if face_attrs.get('mustache'):
38
+ attr_parts.append("has a mustache")
39
+
40
+ attr_str = ", ".join(attr_parts) if attr_parts else ""
41
+
42
+ prompt = (
43
+ f"A chibi-style digital sticker of a {gender.lower()} with {race} skin, "
44
+ f"{'smiling' if emotion=='happy' else emotion}, approximately {age} years old"
45
+ )
46
+ if attr_str:
47
+ prompt += f", {attr_str}"
48
+
49
+ prompt += ", closeup, cute, expressive face, white outline, transparent background"
50
+ return prompt