File size: 2,273 Bytes
48450a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import paddlehub as hub
import gradio as gr
import requests
import numpy as np
import matplotlib.pyplot as plt

model = hub.Module(name='ocrnet_hrnetw18_cityscapes')

url1 = 'https://cdn.pixabay.com/photo/2014/09/07/21/52/city-438393_1280.jpg'
r = requests.get(url1, allow_redirects=True)
open("city1.jpg", 'wb').write(r.content)
url2 = 'https://cdn.pixabay.com/photo/2016/02/19/11/36/canal-1209808_1280.jpg'
r = requests.get(url2, allow_redirects=True)
open("city2.jpg", 'wb').write(r.content)


colormap = np.zeros((256, 3), dtype=np.uint8)
colormap[0] = [128, 64, 128]
colormap[1] = [244, 35, 232]
colormap[2] = [70, 70, 70]
colormap[3] = [102, 102, 156]
colormap[4] = [190, 153, 153]
colormap[5] = [153, 153, 153]
colormap[6] = [250, 170, 30]
colormap[7] = [220, 220, 0]
colormap[8] = [107, 142, 35]
colormap[9] = [152, 251, 152]
colormap[10] = [70, 130, 180]
colormap[11] = [220, 20, 60]
colormap[12] = [255, 0, 0]
colormap[13] = [0, 0, 142]
colormap[14] = [0, 0, 70]
colormap[15] = [0, 60, 100]
colormap[16] = [0, 80, 100]
colormap[17] = [0, 0, 230]
colormap[18] = [119, 11, 32]

def applyColormap(img,colormap):
    ret = np.zeros((img.shape[0],img.shape[1],3), dtype=np.uint8)
    for y in range(img.shape[0]):
        for x in range(img.shape[1]):
            ret[y,x] = colormap[int(img[y,x])]
    return ret.astype(np.uint8)


def inference(image):
    image = image.resize(size=(512,512))
    img = np.array(image)[:,:,::-1]
    result = model.predict(images=[img], visualization=True)[0]
    result_color = applyColormap(result,colormap)
    return result_color

title = "PaddleHub: OCRNet HRNetw18 Cityscapes"
description = "demo for PaddleHub. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below.\nModel: ocrnet_hrnetw18_cityscapes"
article = "<p style='text-align: center'><a href='https://www.paddlepaddle.org.cn/hubdetail?name=ocrnet_hrnetw18_cityscapes&en_category=ImageSegmentation'>PaddleHub page</a></p>"

gr.Interface(
    inference, 
    [gr.inputs.Image(type="pil", label="Input")], 
    gr.outputs.Image(type="numpy", label="Output"),
    title=title,
    description=description,
    article=article,
    examples=[
            ["city1.jpg"],
            ["city2.jpg"]
        ]).launch()