add gfpgan

This commit is contained in:
Qing
2023-03-26 20:42:31 +08:00
parent e7c7896bfa
commit b200db920b
5 changed files with 72 additions and 21 deletions

View File

@@ -598,6 +598,15 @@ export default function Editor() {
} }
}, [runRenderablePlugin]) }, [runRenderablePlugin])
useEffect(() => {
emitter.on(PluginName.GFPGAN, () => {
runRenderablePlugin(PluginName.GFPGAN)
})
return () => {
emitter.off(PluginName.GFPGAN)
}
}, [runRenderablePlugin])
useEffect(() => { useEffect(() => {
emitter.on(PluginName.RealESRGAN, (data: any) => { emitter.on(PluginName.RealESRGAN, (data: any) => {
runRenderablePlugin(PluginName.RealESRGAN, data) runRenderablePlugin(PluginName.RealESRGAN, data)

View File

@@ -4,6 +4,7 @@ import { CursorArrowRaysIcon, GifIcon } from '@heroicons/react/24/outline'
import { import {
BoxModelIcon, BoxModelIcon,
ChevronRightIcon, ChevronRightIcon,
FaceIcon,
HobbyKnifeIcon, HobbyKnifeIcon,
MixIcon, MixIcon,
} from '@radix-ui/react-icons' } from '@radix-ui/react-icons'
@@ -20,6 +21,7 @@ import Button from '../shared/Button'
export enum PluginName { export enum PluginName {
RemoveBG = 'RemoveBG', RemoveBG = 'RemoveBG',
RealESRGAN = 'RealESRGAN', RealESRGAN = 'RealESRGAN',
GFPGAN = 'GFPGAN',
InteractiveSeg = 'InteractiveSeg', InteractiveSeg = 'InteractiveSeg',
MakeGIF = 'MakeGIF', MakeGIF = 'MakeGIF',
} }
@@ -33,6 +35,10 @@ const pluginMap = {
IconClass: BoxModelIcon, IconClass: BoxModelIcon,
showName: 'RealESRGAN 4x', showName: 'RealESRGAN 4x',
}, },
[PluginName.GFPGAN]: {
IconClass: FaceIcon,
showName: 'GFPGAN',
},
[PluginName.InteractiveSeg]: { [PluginName.InteractiveSeg]: {
IconClass: CursorArrowRaysIcon, IconClass: CursorArrowRaysIcon,
showName: 'Interactive Seg', showName: 'Interactive Seg',

View File

@@ -327,28 +327,46 @@ def run_plugin():
return "Plugin not found", 500 return "Plugin not found", 500
origin_image_bytes = files["image"].read() # RGB origin_image_bytes = files["image"].read() # RGB
rgb_np_img, _ = load_img(origin_image_bytes) rgb_np_img, alpha_channel, exif = load_img(origin_image_bytes, return_exif=True)
start = time.time() start = time.time()
res = plugins[name](rgb_np_img, files, form) bgr_res = plugins[name](rgb_np_img, files, form)
logger.info(f"{name} process time: {(time.time() - start) * 1000}ms") logger.info(f"{name} process time: {(time.time() - start) * 1000}ms")
torch_gc() torch_gc()
if name == MakeGIF.name: if name == MakeGIF.name:
filename = form["filename"]
return send_file( return send_file(
io.BytesIO(res), io.BytesIO(bgr_res),
mimetype="image/gif", mimetype="image/gif",
as_attachment=True, as_attachment=True,
attachment_filename=filename, attachment_filename=form["filename"],
) )
if name == RemoveBG.name:
rgb_res = cv2.cvtColor(bgr_res, cv2.COLOR_BGRA2RGBA)
ext = "png"
else: else:
response = make_response( rgb_res = cv2.cvtColor(bgr_res, cv2.COLOR_BGR2RGB)
send_file( ext = get_image_ext(origin_image_bytes)
io.BytesIO(numpy_to_bytes(res, "png")), if alpha_channel is not None:
mimetype=f"image/png", if alpha_channel.shape[:2] != rgb_res.shape[:2]:
alpha_channel = cv2.resize(
alpha_channel, dsize=(rgb_res.shape[1], rgb_res.shape[0])
)
rgb_res = np.concatenate(
(rgb_res, alpha_channel[:, :, np.newaxis]), axis=-1
) )
response = make_response(
send_file(
io.BytesIO(
pil_to_bytes(
Image.fromarray(rgb_res), ext, quality=image_quality, exif=exif
)
),
mimetype=f"image/{ext}",
) )
)
return response return response

View File

@@ -1,4 +1,3 @@
import os
from pathlib import Path from pathlib import Path
import cv2 import cv2

View File

@@ -1,27 +1,46 @@
from pathlib import Path from pathlib import Path
import cv2 import cv2
import pytest
import torch.cuda
from lama_cleaner.plugins import RemoveBG, RealESRGANUpscaler from lama_cleaner.plugins import RemoveBG, RealESRGANUpscaler, GFPGANPlugin
current_dir = Path(__file__).parent.absolute().resolve() current_dir = Path(__file__).parent.absolute().resolve()
save_dir = current_dir / "result" save_dir = current_dir / "result"
save_dir.mkdir(exist_ok=True, parents=True) save_dir.mkdir(exist_ok=True, parents=True)
img_p = current_dir / "bunny.jpeg" img_p = current_dir / "bunny.jpeg"
bgr_img = cv2.imread(str(img_p))
rgb_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB)
def _save(img, name):
cv2.imwrite(str(save_dir / name), img)
def test_remove_bg(): def test_remove_bg():
model = RemoveBG() model = RemoveBG()
img = cv2.imread(str(img_p)) res = model.forward(bgr_img)
res = model.forward(img) _save(res, "test_remove_bg.png")
cv2.imwrite(str(save_dir / "test_remove_bg.png"), res)
def test_upscale(): @pytest.mark.parametrize("device", ["cuda", "cpu"])
model = RealESRGANUpscaler("cpu") def test_upscale(device):
img = cv2.imread(str(img_p)) if device == "cuda" and not torch.cuda.is_available():
res = model.forward(img, 2) return
cv2.imwrite(str(save_dir / "test_upscale_x2.png"), res)
res = model.forward(img, 4) model = RealESRGANUpscaler("realesr-general-x4v3", device)
cv2.imwrite(str(save_dir / "test_upscale_x4.png"), res) res = model.forward(bgr_img, 2)
_save(res, "test_upscale_x2.png")
res = model.forward(bgr_img, 4)
_save(res, "test_upscale_x4.png")
@pytest.mark.parametrize("device", ["cuda", "cpu"])
def test_gfpgan(device):
if device == "cuda" and not torch.cuda.is_available():
return
model = GFPGANPlugin(device)
res = model(rgb_img, None, None)
_save(res, "test_gfpgan.png")