Datasets:
EntropyDrop commited on
Commit ·
83048f5
1
Parent(s): 86fc376
update
Browse files- DDJ_real2render/official_api_test.py +207 -0
- DDJ_real2render/real2render.py +3 -3
- DDJ_real2render/test_input/img35.jpg +3 -0
- DDJ_real2render/test_input/img36.jpg +3 -0
- DDJ_real2render/test_output/img34_template41_51_52_53_54_55_2k_test2.png +3 -0
- DDJ_real2render/test_output/img34_template41_51_52_53_54_55_2k_test3.png +3 -0
- DDJ_real2render/test_output/img34_template41_51_52_53_54_55_2k_test4.png +3 -0
- DDJ_real2render/test_output/img34_template41_51_52_53_54_55_2k_test5.png +3 -0
- DDJ_real2render/test_output/img35_aistudio_test1.jpg +3 -0
- DDJ_real2render/test_output/img35_template41_51_2k.png +3 -0
- DDJ_real2render/test_output/img35_template41_51_52_53_54_55_2k_test1.png +3 -0
- DDJ_real2render/test_output/img35_template41_51_52_53_54_55_2k_test5.png +3 -0
- DDJ_real2render/test_output/img36_template41_51_2k.png +3 -0
- DDJ_real2render/test_output/img36_template41_51_2k_official.png +3 -0
- DDJ_real2render/test_output/img36_template41_51_52_2k_official.png +3 -0
- DDJ_real2render/test_output/img36_template41_51_52_2k_official_test2.png +3 -0
- DDJ_real2render/test_output/img36_template41_51_52_2k_official_test3.png +3 -0
- DDJ_real2render/test_output/img36_template41_51_52_2k_official_test4.png +3 -0
- DDJ_real2render/test_output/img36_template41_51_52_4k_official_test5.png +3 -0
DDJ_real2render/official_api_test.py
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
+
import sys
|
| 4 |
+
import argparse
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
import PIL.Image
|
| 7 |
+
from google import genai
|
| 8 |
+
from google.genai import types
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
|
| 11 |
+
# Load .env file from the parent directory
|
| 12 |
+
env_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '.env')
|
| 13 |
+
load_dotenv(env_path)
|
| 14 |
+
|
| 15 |
+
# Default template image path located under template/template5.png
|
| 16 |
+
DEFAULT_TEMPLATE_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'template', 'template5.png')
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _build_template_refs(count):
|
| 20 |
+
"""Build the [图2][图3]...[图N+1] reference string for the prompt."""
|
| 21 |
+
return "".join(f"[图{i}]" for i in range(2, count + 2))
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# Default prompt for real-to-render Minecraft character generation.
|
| 25 |
+
# {template_refs} is replaced at runtime based on the number of templates.
|
| 26 |
+
DEFAULT_PROMPT = """把[图1]中角色生成为参考图片的风格:{template_refs}
|
| 27 |
+
|
| 28 |
+
1. 请严格像素化(最高优先级),允许丢弃细节,texture不能高于Minecraft所支持的分辨率(64x64uvmap),参考{template_refs}。
|
| 29 |
+
|
| 30 |
+
2. 生成角色的尺寸、朝向、姿势必须与{template_refs}完全一致,轮廓与内层或外层皮肤的每个像素完全贴合,不能用超出内外层皮肤的任何元素表达角色的特征,无光影特效。
|
| 31 |
+
|
| 32 |
+
3. 使用容易区分前景的纯色背景。
|
| 33 |
+
|
| 34 |
+
4. 准确的还原包括外貌特征、全身所有服装、各种饰品等(不包括手持物品和披风)"""
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def real2render(
|
| 38 |
+
image_path,
|
| 39 |
+
template_paths=None,
|
| 40 |
+
output_path=None,
|
| 41 |
+
prompt=None,
|
| 42 |
+
aspect_ratio="1:1",
|
| 43 |
+
image_size="2K",
|
| 44 |
+
proxy=None,
|
| 45 |
+
):
|
| 46 |
+
if not os.path.exists(image_path):
|
| 47 |
+
raise FileNotFoundError(f"Real character image '{image_path}' does not exist.")
|
| 48 |
+
|
| 49 |
+
if not template_paths:
|
| 50 |
+
template_paths = [DEFAULT_TEMPLATE_PATH]
|
| 51 |
+
|
| 52 |
+
for i, tp in enumerate(template_paths):
|
| 53 |
+
if not os.path.exists(tp):
|
| 54 |
+
raise FileNotFoundError(f"Template image {i + 2} '{tp}' does not exist.")
|
| 55 |
+
|
| 56 |
+
template_refs = _build_template_refs(len(template_paths))
|
| 57 |
+
prompt_text = (prompt or DEFAULT_PROMPT).format(template_refs=template_refs)
|
| 58 |
+
|
| 59 |
+
print(f"[*] Real Image (Graph 1): {image_path}")
|
| 60 |
+
for i, tp in enumerate(template_paths):
|
| 61 |
+
print(f"[*] Template Image (Graph {i + 2}): {tp}")
|
| 62 |
+
print(f"[*] Image Size: {image_size}, Aspect Ratio: {aspect_ratio}")
|
| 63 |
+
print(f"[*] Prompt:\n{prompt_text}\n")
|
| 64 |
+
|
| 65 |
+
# Load images using PIL
|
| 66 |
+
images = []
|
| 67 |
+
try:
|
| 68 |
+
images.append(PIL.Image.open(image_path))
|
| 69 |
+
for tp in template_paths:
|
| 70 |
+
images.append(PIL.Image.open(tp))
|
| 71 |
+
except Exception as e:
|
| 72 |
+
raise ValueError(f"Failed to load images with PIL: {e}")
|
| 73 |
+
|
| 74 |
+
# Build contents for Gemini generate_content
|
| 75 |
+
# The list contains [image1, image2, ..., prompt_text]
|
| 76 |
+
contents = images + [prompt_text]
|
| 77 |
+
|
| 78 |
+
# Initialize Gemini Client with Proxy and HTTP/2 settings
|
| 79 |
+
import socket
|
| 80 |
+
proxy_url = proxy
|
| 81 |
+
if not proxy_url:
|
| 82 |
+
proxy_url = os.environ.get("HTTPS_PROXY") or os.environ.get("HTTP_PROXY") or os.environ.get("all_proxy") or os.environ.get("ALL_PROXY")
|
| 83 |
+
|
| 84 |
+
# Auto-detect Clash Verge port 7897 if no proxy is specified
|
| 85 |
+
if not proxy_url:
|
| 86 |
+
try:
|
| 87 |
+
with socket.create_connection(("127.0.0.1", 7897), timeout=0.2):
|
| 88 |
+
proxy_url = "http://127.0.0.1:7897"
|
| 89 |
+
print(f"[*] Auto-detected local Clash Verge proxy on {proxy_url}, using it.")
|
| 90 |
+
except Exception:
|
| 91 |
+
pass
|
| 92 |
+
|
| 93 |
+
client_args = {"http2": False}
|
| 94 |
+
if proxy_url:
|
| 95 |
+
client_args["proxy"] = proxy_url
|
| 96 |
+
|
| 97 |
+
client = genai.Client(
|
| 98 |
+
api_key=os.environ.get("GEMINI_API_KEY"),
|
| 99 |
+
http_options=types.HttpOptions(
|
| 100 |
+
client_args=client_args,
|
| 101 |
+
async_client_args=client_args
|
| 102 |
+
)
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Call Gemini Client
|
| 106 |
+
config = types.GenerateContentConfig(
|
| 107 |
+
temperature=1,
|
| 108 |
+
max_output_tokens=32768,
|
| 109 |
+
top_p=0.95,
|
| 110 |
+
system_instruction='你是一个专业的minecraft皮肤绘手',
|
| 111 |
+
response_modalities=['image', 'text'],
|
| 112 |
+
image_config=types.ImageConfig(
|
| 113 |
+
aspect_ratio=aspect_ratio,
|
| 114 |
+
image_size=image_size
|
| 115 |
+
)
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# Call Gemini Client with retries (up to 5 attempts) to handle transient proxy/network issues
|
| 119 |
+
max_retries = 5
|
| 120 |
+
response = None
|
| 121 |
+
for attempt in range(1, max_retries + 1):
|
| 122 |
+
try:
|
| 123 |
+
response = client.models.generate_content(
|
| 124 |
+
model='models/gemini-3-pro-image',
|
| 125 |
+
contents=contents,
|
| 126 |
+
config=config
|
| 127 |
+
)
|
| 128 |
+
break
|
| 129 |
+
except Exception as e:
|
| 130 |
+
if attempt == max_retries:
|
| 131 |
+
raise e
|
| 132 |
+
print(f"[!] Attempt {attempt} failed: {e}. Retrying in 2 seconds...")
|
| 133 |
+
import time
|
| 134 |
+
time.sleep(2)
|
| 135 |
+
|
| 136 |
+
# Save the output image to the specified location
|
| 137 |
+
# Find the image part in the response
|
| 138 |
+
image_data = None
|
| 139 |
+
for part in response.candidates[0].content.parts:
|
| 140 |
+
if part.inline_data:
|
| 141 |
+
if image_data is None:
|
| 142 |
+
image_data = part.inline_data.data
|
| 143 |
+
else:
|
| 144 |
+
if part.text:
|
| 145 |
+
print(f"[Info] Text response from Gemini: {part.text}")
|
| 146 |
+
else:
|
| 147 |
+
print(f"[Info] Non-image part: {part}")
|
| 148 |
+
|
| 149 |
+
if not image_data:
|
| 150 |
+
raise ValueError("No image was returned from Gemini API.")
|
| 151 |
+
|
| 152 |
+
import time
|
| 153 |
+
if not output_path:
|
| 154 |
+
output_path = f"official_api_test_{int(time.time())}.png"
|
| 155 |
+
|
| 156 |
+
# Make sure output directory exists
|
| 157 |
+
output_dir = os.path.dirname(output_path)
|
| 158 |
+
if output_dir and not os.path.exists(output_dir):
|
| 159 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 160 |
+
|
| 161 |
+
with open(output_path, "wb") as f:
|
| 162 |
+
f.write(image_data)
|
| 163 |
+
|
| 164 |
+
return output_path
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
def main():
|
| 168 |
+
parser = argparse.ArgumentParser(description="Convert real character photo to Minecraft render using Gemini Official API.")
|
| 169 |
+
parser.add_argument("image", help="Path to local real character photo (Graph 1).")
|
| 170 |
+
parser.add_argument(
|
| 171 |
+
"-t", "--template",
|
| 172 |
+
action="append",
|
| 173 |
+
default=None,
|
| 174 |
+
help="Path to a reference template image. May be specified multiple times "
|
| 175 |
+
"(e.g. -t t1.png -t t2.png -t t3.png). "
|
| 176 |
+
"Defaults to the built-in template if omitted."
|
| 177 |
+
)
|
| 178 |
+
parser.add_argument("-o", "--output", help="Output path for the generated image.")
|
| 179 |
+
parser.add_argument("-p", "--prompt", default=None,
|
| 180 |
+
help="Prompt for image generation. Use {template_refs} as placeholder for reference image tags.")
|
| 181 |
+
parser.add_argument("-a", "--aspect-ratio", default="1:1",
|
| 182 |
+
choices=["1:1", "2:3", "3:2", "3:4", "4:3", "4:5", "5:4", "9:16", "16:9", "21:9"],
|
| 183 |
+
help="Aspect ratio.")
|
| 184 |
+
parser.add_argument("-s", "--image-size", default="1K", choices=["1K", "2K", "4K"], help="Resolution size (default: 2K).")
|
| 185 |
+
parser.add_argument("-x", "--proxy", default=None,
|
| 186 |
+
help="Proxy URL (e.g., http://127.0.0.1:7897). If not provided, it will check environment variables and auto-detect Clash Verge.")
|
| 187 |
+
args = parser.parse_args()
|
| 188 |
+
|
| 189 |
+
try:
|
| 190 |
+
output_file = real2render(
|
| 191 |
+
image_path=args.image,
|
| 192 |
+
template_paths=args.template,
|
| 193 |
+
output_path=args.output,
|
| 194 |
+
prompt=args.prompt,
|
| 195 |
+
aspect_ratio=args.aspect_ratio,
|
| 196 |
+
image_size=args.image_size,
|
| 197 |
+
proxy=args.proxy
|
| 198 |
+
)
|
| 199 |
+
print(f"[+] Task completed successfully. Output saved to: {output_file}")
|
| 200 |
+
except Exception as e:
|
| 201 |
+
print(f"[!] Execution failed: {e}")
|
| 202 |
+
sys.exit(1)
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
if __name__ == '__main__':
|
| 206 |
+
main()
|
| 207 |
+
|
DDJ_real2render/real2render.py
CHANGED
|
@@ -17,15 +17,15 @@ def _build_template_refs(count):
|
|
| 17 |
|
| 18 |
# Default prompt for real-to-render Minecraft character generation.
|
| 19 |
# {template_refs} is replaced at runtime based on the number of templates.
|
| 20 |
-
DEFAULT_PROMPT = """把[图1]中角色
|
| 21 |
|
| 22 |
1. 包含内层/外层贴图的双图层模型,不能有额外元素。texture不能高于Minecraft所支持的分辨率,参考{template_refs}。
|
| 23 |
|
| 24 |
-
2. 生成
|
| 25 |
|
| 26 |
3. 使用容易区分前景的纯色背景。
|
| 27 |
|
| 28 |
-
4.
|
| 29 |
|
| 30 |
|
| 31 |
def real2render(
|
|
|
|
| 17 |
|
| 18 |
# Default prompt for real-to-render Minecraft character generation.
|
| 19 |
# {template_refs} is replaced at runtime based on the number of templates.
|
| 20 |
+
DEFAULT_PROMPT = """把[图1]中角色生成为参考图片的风格:{template_refs}
|
| 21 |
|
| 22 |
1. 包含内层/外层贴图的双图层模型,不能有额外元素。texture不能高于Minecraft所支持的分辨率,参考{template_refs}。
|
| 23 |
|
| 24 |
+
2. 生成角色的尺寸、朝向、姿势必须与{template_refs}完全一致,轮廓与内层或外层皮肤完全贴合,不能用超出内外层皮肤的任何元素表达角色的特征,无光影特效。
|
| 25 |
|
| 26 |
3. 使用容易区分前景的纯色背景。
|
| 27 |
|
| 28 |
+
4. 准确的还原包括外貌特征、全身所有服装、各种饰品等(不包括手持物品和披风)"""
|
| 29 |
|
| 30 |
|
| 31 |
def real2render(
|
DDJ_real2render/test_input/img35.jpg
ADDED
|
Git LFS Details
|
DDJ_real2render/test_input/img36.jpg
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img34_template41_51_52_53_54_55_2k_test2.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img34_template41_51_52_53_54_55_2k_test3.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img34_template41_51_52_53_54_55_2k_test4.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img34_template41_51_52_53_54_55_2k_test5.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img35_aistudio_test1.jpg
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img35_template41_51_2k.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img35_template41_51_52_53_54_55_2k_test1.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img35_template41_51_52_53_54_55_2k_test5.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img36_template41_51_2k.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img36_template41_51_2k_official.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img36_template41_51_52_2k_official.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img36_template41_51_52_2k_official_test2.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img36_template41_51_52_2k_official_test3.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img36_template41_51_52_2k_official_test4.png
ADDED
|
Git LFS Details
|
DDJ_real2render/test_output/img36_template41_51_52_4k_official_test5.png
ADDED
|
Git LFS Details
|