diff --git a/docs/ability/flux-schnell-faceid.md b/docs/ability/flux-schnell-faceid.md new file mode 100644 index 0000000000000000000000000000000000000000..3440a9173f3c15efa9977b23365617ea6b5cc43b --- /dev/null +++ b/docs/ability/flux-schnell-faceid.md @@ -0,0 +1,95 @@ +# Flux人脸迁移模型功能和原理简介 + +[Flux模型的人脸迁移](https://ai.gitee.com/serverless-api?model=flux-1-schnell&operation=134)在原功能基础上,将原图中的人脸迁移到生成图像上。给定一张带有一个人的图像,模型会提取该图像人物面部,并在依据提示词生成图像的过程中依据该面部生成对应的人物全貌。 + +该模型用处广泛,可以将人物放置到任意模型生成能力的场景中。只要你能想到的场景详细表述并给出你的肖像图,那么生成的图片将仿佛你置身于该场景中。 + +:::info +概念说明 + “**人脸迁移**”一般是指将人脸从原图中换到另一张图中的人上。 + “**生成图像**”在本教程中指根据文本提示词生成的图片。 +::: + +# 步骤一:输入token并定义请求函数 + +首先获取你的[token](https://ai.gitee.com/dashboard/settings/tokens),然后可定义请求函数如下: + +```python +import requests +import base64 +from PIL import Image +from io import BytesIO + +API_URL = "https://ai.gitee.com/v1/images/face-migration" +headers = { + "Authorization": "Bearer " +} + +def query(payload): + files = { + "image": (payload["image"], open(payload["image"], "rb")) + } + data = {key: payload[key] for key in payload if key not in files} + response = requests.post(API_URL, headers=headers, files=files, data=data) + return response.json() +``` + +# 步骤二:请求并获得数据结果 + +请求方式如下,不过需要注意提示词(prompt)必须是**英文**才行。 + +```python +output = query({ + "model": "flux-1-schnell", + "image": "v2-d31d9e5ab74c7d4bbf4ccacde1497a18_r.jpg", + "size": "1024x1024", + "guidance_scale": 4, + "num_inference_steps": 4, + "id_weight": 1, + "id_timestep_start": 0, + "prompt": "Portrait painting, delicate sketch style, colored painting, ultra-high-definition, ultra-high pixel count" +}) +``` + +参数说明: + +* `model`:模型名,此处固定。 +* `image`:本地的图片路径,事实上在函数中可以看到是以二进制的形式传输。 +* `size`:生成图像的尺寸。 +* `guidance_scale`:提示词引导系数,越大则生成图片对提示词的遵从度越高。 +* `num_inference_steps`:生成图片的步数,该模型4步即可生成效果极佳的图片。 +* `id_weight`:id参考图对生成图片的影响度,越大则人脸生成的越像,可以适当调整。 +* `id_timestep_start`:开始在生图时嵌入人脸的步数,越小则在越早的步数中开始生成人脸,就越像。可适当调整。 +* `prompt`:生成图片的提示词,flux支持长文本复杂的提示词,越详细越好,可以放心大胆的写。 + +教程使用的id图像如下: + +![input](../../static\img\serverless-api\flux-schnell-faceid-input.jpg) + +# 步骤三:解码并保存结果 + +得到的结果为json,格式如下: + +```json +{ + "data": [ + { + "b64_json": "" + } + ], + "created": "" +} +``` + +数据结果为base64编码的图像,因此需要解码后才能保存,解包、解码如下: + +```python +b64data = output['data'][0]['b64_json'] +img = Image.open(BytesIO(base64.b64decode(b64data))) +img.save("/path/to/save/res.jpg") +img.show() +``` + +结果如下图,效果非常好了: + +![output](../../static\img\serverless-api\flux-schnell-faceid-res.jpg) diff --git a/static/img/serverless-api/flux-schnell-faceid-input.jpg b/static/img/serverless-api/flux-schnell-faceid-input.jpg new file mode 100644 index 0000000000000000000000000000000000000000..1f6391f516941a1b401f995e98fa57d1127ff107 Binary files /dev/null and b/static/img/serverless-api/flux-schnell-faceid-input.jpg differ diff --git a/static/img/serverless-api/flux-schnell-faceid-res.jpg b/static/img/serverless-api/flux-schnell-faceid-res.jpg new file mode 100644 index 0000000000000000000000000000000000000000..dd773277343ad2fa6dfb2203695db69ab06e9c2c Binary files /dev/null and b/static/img/serverless-api/flux-schnell-faceid-res.jpg differ