Spaces:
Build error
Build error
Commit
·
19ca011
1
Parent(s):
7051702
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
from doc_ufcn.main import DocUFCN
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from doc_ufcn import models
|
| 5 |
+
import numpy as np
|
| 6 |
+
import random
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
model_path, parameters = models.download_model('generic-page')
|
| 11 |
+
|
| 12 |
+
model = DocUFCN(len(parameters['classes']), parameters['input_size'], 'cpu')
|
| 13 |
+
model.load(model_path, parameters['mean'], parameters['std'])
|
| 14 |
+
|
| 15 |
+
def visualize_instance_seg_mask(mask):
|
| 16 |
+
image = np.zeros((mask.shape[0], mask.shape[1], 3))
|
| 17 |
+
labels = np.unique(mask)
|
| 18 |
+
label2color = {label: (random.randint(0, 1), random.randint(0, 255), random.randint(0, 255)) for label in labels}
|
| 19 |
+
for i in range(image.shape[0]):
|
| 20 |
+
for j in range(image.shape[1]):
|
| 21 |
+
image[i, j, :] = label2color[mask[i, j]]
|
| 22 |
+
image = image / 255
|
| 23 |
+
return image
|
| 24 |
+
|
| 25 |
+
def query_image(image):
|
| 26 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 27 |
+
detected_polygons, probabilities, mask, overlap = model.predict(
|
| 28 |
+
image, raw_output=True, mask_output=True, overlap_output=True
|
| 29 |
+
)
|
| 30 |
+
return visualize_instance_seg_mask(overlap)
|
| 31 |
+
|
| 32 |
+
demo = gr.Interface(
|
| 33 |
+
query_image,
|
| 34 |
+
inputs=[gr.Image()],
|
| 35 |
+
outputs="image",
|
| 36 |
+
title="TODO",
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
demo.launch()
|