mattyamonaca / starline Goto Github PK
View Code? Open in Web Editor NEWStrict coloring machine for line drawings.
License: Apache License 2.0
Strict coloring machine for line drawings.
License: Apache License 2.0
starline.py
の replace_color
関数の高速化の提案です:
https://github.com/mattyamonaca/starline/blob/main/starline.py#L30-L75
現在のアルゴリズムは以下のような流れになっています:
color_1
で塗られているピクセルを列挙するcolor_1
や color_2
ではない色で塗られているピクセルがあるなら、その色で上書きするこのアルゴリズムは color_1
の外側の領域から徐々に浸食していくイメージです。ただ、この方法だと全ピクセルの走査が何度も行われるので、時間がかかってしまいます。
そこで、幅優先探索を使って color_1
の境界付近のセルを順に上書きしていく方法を提案します。これにより、一回の走査で対象のピクセルを上書きできるようになります。
手元の環境では、元のアルゴリズムで replace_color
に約 60 秒かかっていた入力が、この新しい方法では 0.6 秒ほどに短縮されました。
以下が提案するコードです。一部高速化のためのトリッキーなコードがありますが、根幹は基本的な幅優先探索です (これがないと 4 秒ほどになりました)
https://github.com/koyumeishi/starline/blob/main/starline.py#L30-L99
https://github.com/mattyamonaca/starline/blob/main/starline.py#L193-L199
starline.py
の generate_distant_colors
関数に関して、「consolidated_lab
と distance_threshold
以上の距離があるランダムな色を取得する」機能を期待しています。しかし、現状のコードではループが一度しか実行されず、即座に break
されてしまうため、実質的にランダムな色が指定されているだけになっています。
(24bit 空間からランダムな色を得ているので実用上の問題はないと思いますが)
以下に修正案を示します:
https://github.com/koyumeishi/starline/blob/main/starline.py#L216-L241
consolidated_lab
と random_lab
の間の最小距離を最大化する色を返すようにしました。また、バグフィックス後の挙動を確認したところ、distance_threshold = 100 は少し大きすぎるようでしたので、もう少し小さい値にしてもいいかもしれません。
https://github.com/koyumeishi/starline/blob/main/starline.py#L330
ゴリ押しで動かしたら動きました。すごいです。
Ubuntuだとapp.pyは動きませんでした。これはGradioのClickで正常にイベントを拾えず、関数を呼び出せていないためだと思われます。
次のようなCLIバージョンを作って動かしました。
import sys
from starline import process
from utils import load_cn_model, load_cn_config, randomname
from convertor import pil2cv, cv2pil
from sd_model import get_cn_pipeline, generate, get_cn_detector
import cv2
import os
import numpy as np
from PIL import Image
path = os.getcwd()
output_dir = f"{path}/output"
input_dir = f"{path}/input"
cn_lineart_dir = f"{path}/controlnet/lineart"
load_cn_model(cn_lineart_dir)
load_cn_config(cn_lineart_dir)
pos_prompt="brown hair, brown eyes, white T-shirt"
neg_prompt=""
input_image=Image.open("/home/alfredplpl/Downloads/IMG_0617.PNG")
org_line_image=input_image
image = pil2cv(input_image)
image = cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA)
alpha_th=100
index = np.where(image[:, :, 3] == 0)
image[index] = [255, 255, 255, 255]
input_image = cv2pil(image)
pipe = get_cn_pipeline()
detectors = get_cn_detector(input_image.resize((1024, 1024), Image.Resampling.BICUBIC))
gen_image = generate(pipe, detectors, pos_prompt, neg_prompt)
output = process(gen_image.resize((image.shape[1], image.shape[0]), Image.Resampling.BICUBIC) , org_line_image, alpha_th)
output = output.resize((image.shape[1], image.shape[0]) , Image.Resampling.BICUBIC)
output = Image.alpha_composite(output, org_line_image)
name = randomname(10)
output.save(f"{output_dir}/output_{name}.png")
#output = pil2cv(output)
file_name = f"{output_dir}/output_{name}.png"
また、足りなかったPythonパッケージは以下の通りでした。
# Must
pip install chardet transformers
# Option
pip install accelerate
Image.ANTIALIASは最近deplicatedしたのでなくなりました。代わりにImage.Resampling.BICUBICを入れました。
大体の開発者はわかると思いますが、利用者はわからないかもしれません。
git clone https://github.com/mattyamonaca/starline.git
cd starline
conda create -n starline python=3.10
conda activate starline
conda install pytorch torchvision pytorch-cuda=12.1 -c pytorch -c nvidia
pip install -r requirements.txt
のようなやつですね。
stable diffusion を動かさずに、用意された以下の入力を使って修正する機能を提案します:
具体的には、以下のようにコマンドラインから実行できるようにします
https://github.com/koyumeishi/starline/blob/main/starline.py#L349-L403
$ python starline.py -c color.png -l line.png
この方法で実行できると、重い stable diffusion を動かさなくても、簡単に画像の修正ができてうれしい気がします。
$ python starline.py --help
usage: starline [-h] -c COLORED_IMAGE -l LINEART_IMAGE [-o OUTPUT_DIR] [-a ALPHA_TH] [-t THICKNESS]
Starline
options:
-h, --help show this help message and exit
-c COLORED_IMAGE, --colored_image COLORED_IMAGE
colored image
-l LINEART_IMAGE, --lineart_image LINEART_IMAGE
lineart image
-o OUTPUT_DIR, --output_dir OUTPUT_DIR
output directory
-a ALPHA_TH, --alpha_th ALPHA_TH
alpha threshold
-t THICKNESS, --thickness THICKNESS
line thickness
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.