Spaces:
Runtime error
Runtime error
karolmajek
commited on
Commit
·
48450a5
1
Parent(s):
de3005b
ocrnet_hrnetw18_cityscapes
Browse files
app.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import paddlehub as hub
|
2 |
+
import gradio as gr
|
3 |
+
import requests
|
4 |
+
import numpy as np
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
|
7 |
+
model = hub.Module(name='ocrnet_hrnetw18_cityscapes')
|
8 |
+
|
9 |
+
url1 = 'https://cdn.pixabay.com/photo/2014/09/07/21/52/city-438393_1280.jpg'
|
10 |
+
r = requests.get(url1, allow_redirects=True)
|
11 |
+
open("city1.jpg", 'wb').write(r.content)
|
12 |
+
url2 = 'https://cdn.pixabay.com/photo/2016/02/19/11/36/canal-1209808_1280.jpg'
|
13 |
+
r = requests.get(url2, allow_redirects=True)
|
14 |
+
open("city2.jpg", 'wb').write(r.content)
|
15 |
+
|
16 |
+
|
17 |
+
colormap = np.zeros((256, 3), dtype=np.uint8)
|
18 |
+
colormap[0] = [128, 64, 128]
|
19 |
+
colormap[1] = [244, 35, 232]
|
20 |
+
colormap[2] = [70, 70, 70]
|
21 |
+
colormap[3] = [102, 102, 156]
|
22 |
+
colormap[4] = [190, 153, 153]
|
23 |
+
colormap[5] = [153, 153, 153]
|
24 |
+
colormap[6] = [250, 170, 30]
|
25 |
+
colormap[7] = [220, 220, 0]
|
26 |
+
colormap[8] = [107, 142, 35]
|
27 |
+
colormap[9] = [152, 251, 152]
|
28 |
+
colormap[10] = [70, 130, 180]
|
29 |
+
colormap[11] = [220, 20, 60]
|
30 |
+
colormap[12] = [255, 0, 0]
|
31 |
+
colormap[13] = [0, 0, 142]
|
32 |
+
colormap[14] = [0, 0, 70]
|
33 |
+
colormap[15] = [0, 60, 100]
|
34 |
+
colormap[16] = [0, 80, 100]
|
35 |
+
colormap[17] = [0, 0, 230]
|
36 |
+
colormap[18] = [119, 11, 32]
|
37 |
+
|
38 |
+
def applyColormap(img,colormap):
|
39 |
+
ret = np.zeros((img.shape[0],img.shape[1],3), dtype=np.uint8)
|
40 |
+
for y in range(img.shape[0]):
|
41 |
+
for x in range(img.shape[1]):
|
42 |
+
ret[y,x] = colormap[int(img[y,x])]
|
43 |
+
return ret.astype(np.uint8)
|
44 |
+
|
45 |
+
|
46 |
+
def inference(image):
|
47 |
+
image = image.resize(size=(512,512))
|
48 |
+
img = np.array(image)[:,:,::-1]
|
49 |
+
result = model.predict(images=[img], visualization=True)[0]
|
50 |
+
result_color = applyColormap(result,colormap)
|
51 |
+
return result_color
|
52 |
+
|
53 |
+
title = "PaddleHub: OCRNet HRNetw18 Cityscapes"
|
54 |
+
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"
|
55 |
+
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>"
|
56 |
+
|
57 |
+
gr.Interface(
|
58 |
+
inference,
|
59 |
+
[gr.inputs.Image(type="pil", label="Input")],
|
60 |
+
gr.outputs.Image(type="numpy", label="Output"),
|
61 |
+
title=title,
|
62 |
+
description=description,
|
63 |
+
article=article,
|
64 |
+
examples=[
|
65 |
+
["city1.jpg"],
|
66 |
+
["city2.jpg"]
|
67 |
+
]).launch()
|