|
import gradio as gr |
|
|
|
import torch |
|
from fastai.vision.all import * |
|
from PIL import ImageFilter, ImageEnhance, ImageDraw |
|
from diffusers.utils import make_image_grid |
|
from tqdm import tqdm |
|
from diffusers import AutoPipelineForInpainting, LCMScheduler, DDIMScheduler |
|
from diffusers import StableDiffusionControlNetInpaintPipeline, ControlNetModel |
|
import numpy as np |
|
from PIL import Image |
|
from datetime import datetime |
|
|
|
preferred_dtype = torch.float32 |
|
preferred_device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
|
def label_func(fn): return path/"labels"/f"{fn.stem}_P{fn.suffix}" |
|
|
|
segmodel = load_learner("camvid-512.pkl") |
|
|
|
working_size = (512, 512) |
|
|
|
default_inpainting_prompt = "watercolor of a leafy pedestrian mall at golden hour with multiracial genderqueer joggers and bicyclists and wheelchair users talking and laughing" |
|
|
|
seg_vocabulary = ['Animal', 'Archway', 'Bicyclist', 'Bridge', 'Building', 'Car', |
|
'CartLuggagePram', 'Child', 'Column_Pole', 'Fence', 'LaneMkgsDriv', |
|
'LaneMkgsNonDriv', 'Misc_Text', 'MotorcycleScooter', 'OtherMoving', |
|
'ParkingBlock', 'Pedestrian', 'Road', 'RoadShoulder', 'Sidewalk', |
|
'SignSymbol', 'Sky', 'SUVPickupTruck', 'TrafficCone', |
|
'TrafficLight', 'Train', 'Tree', 'Truck_Bus', 'Tunnel', |
|
'VegetationMisc', 'Void', 'Wall'] |
|
|
|
ban_cars_mask = np.array([0, 0, 0, 0, 0, 1, |
|
0, 0, 1, 0, 1, |
|
1, 1, 0, 0, |
|
1, 0, 1, 1, 1, |
|
1, 0, 1, 1, |
|
1, 0, 0, 0, 1, |
|
0, 1, 0], dtype=np.uint8) |
|
|
|
def get_seg_mask(img): |
|
mask = segmodel.predict(img)[0] |
|
return mask |
|
|
|
|
|
def app(img, prompt): |
|
start_time = datetime.now().timestamp() |
|
img = img.resize(working_size) |
|
mask = ban_cars_mask[get_seg_mask(img)] |
|
overlay_img = Image.fromarray(np.stack([img[:, :, 0], mask / 2, img[:,:,2]], axis=-1)) |
|
end_time = datetime.now().timestamp() |
|
draw = ImageDraw.Draw(overlay_img) |
|
draw.text((10, 10), f"Duration: {int(1000 * (end_time - start_time))}ms\n<{prompt}>", fill=(255, 255, 255)) |
|
return overlay_img |
|
|
|
|
|
|
|
iface = gr.Interface(app, [gr.Image(), gr.Textbox(value=default_inpainting_prompt)], "image") |
|
iface.launch() |
|
|