Spaces:
Runtime error
Runtime error
Commit
·
79378ee
1
Parent(s):
1edeb9d
update: add simple inference example
Browse files- simple_inference.py +76 -0
simple_inference.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
from typing import List, Union, Optional, Tuple
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from sample import (arg_parse,
|
| 6 |
+
sampling,
|
| 7 |
+
load_fontdiffuer_pipeline)
|
| 8 |
+
|
| 9 |
+
def run_fontdiffuer(source_image,
|
| 10 |
+
character,
|
| 11 |
+
reference_image,
|
| 12 |
+
sampling_step,
|
| 13 |
+
guidance_scale,
|
| 14 |
+
batch_size=1):
|
| 15 |
+
|
| 16 |
+
args.character_input = False if source_image is not None else True
|
| 17 |
+
args.content_character = character
|
| 18 |
+
args.sampling_step = sampling_step
|
| 19 |
+
args.guidance_scale = guidance_scale
|
| 20 |
+
args.batch_size = batch_size
|
| 21 |
+
args.seed = random.randint(0, 10000)
|
| 22 |
+
out_image = sampling(
|
| 23 |
+
args=args,
|
| 24 |
+
pipe=pipe,
|
| 25 |
+
content_image=source_image,
|
| 26 |
+
style_image=reference_image)
|
| 27 |
+
|
| 28 |
+
if out_image is not None:
|
| 29 |
+
out_image.format = 'PNG'
|
| 30 |
+
|
| 31 |
+
return out_image
|
| 32 |
+
|
| 33 |
+
def run_inference(
|
| 34 |
+
source_image_path: Union[str, None],
|
| 35 |
+
character: Union[str, None],
|
| 36 |
+
reference_image_path: str,
|
| 37 |
+
sampling_step: int=50,
|
| 38 |
+
guidance_scale: float=7.5,
|
| 39 |
+
):
|
| 40 |
+
if source_image_path is not None:
|
| 41 |
+
source_image = Image.open(source_image_path).convert('RGB')
|
| 42 |
+
else:
|
| 43 |
+
source_image = None
|
| 44 |
+
|
| 45 |
+
if reference_image_path is not None:
|
| 46 |
+
reference_image = Image.open(reference_image_path).convert('RGB')
|
| 47 |
+
else:
|
| 48 |
+
reference_image = None
|
| 49 |
+
|
| 50 |
+
image = run_fontdiffuer(
|
| 51 |
+
source_image=source_image,
|
| 52 |
+
character=character,
|
| 53 |
+
reference_image=reference_image,
|
| 54 |
+
sampling_step=sampling_step,
|
| 55 |
+
guidance_scale=guidance_scale
|
| 56 |
+
)
|
| 57 |
+
return image
|
| 58 |
+
|
| 59 |
+
if __name__ == '__main__':
|
| 60 |
+
args = arg_parse()
|
| 61 |
+
args.demo = True
|
| 62 |
+
args.ckpt_dir = 'ckpt'
|
| 63 |
+
args.ttf_path = 'ttf/KaiXinSongA.ttf'
|
| 64 |
+
args.device = 'cuda'
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# load fontdiffuer pipeline
|
| 68 |
+
pipe = load_fontdiffuer_pipeline(args=args)
|
| 69 |
+
|
| 70 |
+
image = run_inference(
|
| 71 |
+
character=None,
|
| 72 |
+
source_image_path="figures/ref_imgs/ref_壤.jpg",
|
| 73 |
+
reference_image_path="figures/ref_imgs/ref_欟.jpg"
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
print(image)
|