Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from datasets import load_dataset
|
3 |
+
import uuid
|
4 |
+
import numpy as np
|
5 |
+
import cv2
|
6 |
+
import gradio as gr
|
7 |
+
from huggingface_hub import snapshot_download
|
8 |
+
from insightface.app import FaceAnalysis
|
9 |
+
from PIL import Image
|
10 |
+
import json
|
11 |
+
|
12 |
+
# 定义保存路径
|
13 |
+
save_path = "./examples/xiangxiang_man"
|
14 |
+
|
15 |
+
# 清空目标路径(如果存在)
|
16 |
+
if os.path.exists(save_path):
|
17 |
+
for file_name in os.listdir(save_path):
|
18 |
+
file_path = os.path.join(save_path, file_name)
|
19 |
+
if os.path.isfile(file_path):
|
20 |
+
os.remove(file_path)
|
21 |
+
print(f"Cleared existing files in {save_path}")
|
22 |
+
else:
|
23 |
+
os.makedirs(save_path, exist_ok=True)
|
24 |
+
print(f"Created directory: {save_path}")
|
25 |
+
|
26 |
+
# 加载数据集
|
27 |
+
dataset = load_dataset("svjack/Prince_Xiang_iclight_v2")
|
28 |
+
|
29 |
+
# 遍历数据集并保存图片
|
30 |
+
for example in dataset["train"]:
|
31 |
+
# 获取图片数据
|
32 |
+
image = example["image"]
|
33 |
+
|
34 |
+
# 生成唯一的文件名(使用 uuid)
|
35 |
+
file_name = f"{uuid.uuid4()}.png"
|
36 |
+
file_path = os.path.join(save_path, file_name)
|
37 |
+
|
38 |
+
# 保存图片
|
39 |
+
image.save(file_path)
|
40 |
+
print(f"Saved {file_path}")
|
41 |
+
|
42 |
+
print("All images have been saved.")
|
43 |
+
|
44 |
+
# Download face encoder
|
45 |
+
snapshot_download(
|
46 |
+
"fal/AuraFace-v1",
|
47 |
+
local_dir="models/auraface",
|
48 |
+
)
|
49 |
+
|
50 |
+
# Initialize FaceAnalysis
|
51 |
+
app = FaceAnalysis(
|
52 |
+
name="auraface",
|
53 |
+
providers=["CUDAExecutionProvider", "CPUExecutionProvider"],
|
54 |
+
root=".",
|
55 |
+
)
|
56 |
+
app.prepare(ctx_id=0, det_size=(640, 640))
|
57 |
+
|
58 |
+
def get_embedding(image):
|
59 |
+
"""
|
60 |
+
Get the embedding of a single image.
|
61 |
+
Parameters:
|
62 |
+
- image: PIL Image object.
|
63 |
+
Returns:
|
64 |
+
- A numpy array representing the embedding of the face in the image.
|
65 |
+
"""
|
66 |
+
# Convert PIL image to OpenCV format
|
67 |
+
cv2_image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
68 |
+
|
69 |
+
# Get face information
|
70 |
+
face_info = app.get(cv2_image)
|
71 |
+
|
72 |
+
if len(face_info) > 0:
|
73 |
+
# Return the embedding of the first detected face
|
74 |
+
return face_info[0].normed_embedding.tolist() # Convert to list
|
75 |
+
else:
|
76 |
+
return None
|
77 |
+
|
78 |
+
def display_embedding(image):
|
79 |
+
"""
|
80 |
+
Display the embedding of a single image as a JSON object.
|
81 |
+
Parameters:
|
82 |
+
- image: PIL Image object.
|
83 |
+
Returns:
|
84 |
+
- A JSON object with the embedding (nested list) or an empty list if no face is detected.
|
85 |
+
"""
|
86 |
+
embedding = get_embedding(image)
|
87 |
+
if embedding is not None:
|
88 |
+
return json.dumps({"embedding": embedding}) # Wrap in a list and convert to JSON
|
89 |
+
else:
|
90 |
+
return json.dumps({"embedding": []}) # Return empty list as JSON
|
91 |
+
|
92 |
+
# 获取数据集中的图片路径
|
93 |
+
import pathlib
|
94 |
+
example_images = list(map(str, pathlib.Path(save_path).rglob("*.png")))
|
95 |
+
|
96 |
+
# 创建Gradio界面
|
97 |
+
iface = gr.Interface(
|
98 |
+
fn=display_embedding,
|
99 |
+
inputs=gr.Image(type="pil"),
|
100 |
+
outputs="json",
|
101 |
+
title="面部图片嵌入计算",
|
102 |
+
description="上传一张图片,计算其嵌入向量。",
|
103 |
+
examples=example_images[:3], # 使用数据集中的前3张图片作为示例
|
104 |
+
)
|
105 |
+
|
106 |
+
# 启动Gradio应用
|
107 |
+
iface.launch(share=True)
|