File size: 1,639 Bytes
a042988
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from deepface import DeepFace
from PIL import Image
import insightface
import numpy as np

def extract_face_prompt(img_path):
    # Load image as numpy array
    img = Image.open(img_path).convert("RGB")
    img_np = np.array(img)

    # DeepFace analysis
    deepface_result = DeepFace.analyze(
        img_path, actions=['age', 'gender', 'race', 'emotion'], enforce_detection=False
    )
    age = deepface_result['age']
    gender = deepface_result['dominant_gender']
    race = deepface_result['dominant_race']
    emotion = deepface_result['dominant_emotion']

    # InsightFace analysis
    model = insightface.app.FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider'])
    model.prepare(ctx_id=0)
    faces = model.get(img_np)

    face_attrs = {}
    if faces:
        face = faces[0]
        face_attrs['glasses'] = face.get('glass', 0) > 0
        face_attrs['beard'] = face.get('beard', 0) > 0
        face_attrs['mustache'] = face.get('mustache', 0) > 0

    attr_parts = []
    if face_attrs.get('glasses'):
        attr_parts.append("wearing glasses")
    if face_attrs.get('beard'):
        attr_parts.append("has a beard")
    if face_attrs.get('mustache'):
        attr_parts.append("has a mustache")

    attr_str = ", ".join(attr_parts) if attr_parts else ""

    prompt = (
        f"A chibi-style digital sticker of a {gender.lower()} with {race} skin, "
        f"{'smiling' if emotion=='happy' else emotion}, approximately {age} years old"
    )
    if attr_str:
        prompt += f", {attr_str}"

    prompt += ", closeup, cute, expressive face, white outline, transparent background"
    return prompt