davanstrien HF staff commited on
Commit
2186a48
·
1 Parent(s): 7099689
Files changed (1) hide show
  1. app.py +21 -20
app.py CHANGED
@@ -2,36 +2,37 @@ 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
  )
 
2
  from doc_ufcn.main import DocUFCN
3
  import gradio as gr
4
  from doc_ufcn import models
 
 
5
  from PIL import Image
6
+ from PIL import ImageDraw
7
 
8
+ model_path, parameters = models.download_model("generic-page")
9
 
10
+ model = DocUFCN(len(parameters["classes"]), parameters["input_size"], "cpu")
11
+ model.load(model_path, parameters["mean"], parameters["std"])
12
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  def query_image(image):
15
  image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
16
  detected_polygons, probabilities, mask, overlap = model.predict(
17
+ image, raw_output=True, mask_output=True, overlap_output=True
18
+ )
19
+ max_confidence = 0
20
+ max_prediction = None
21
+ for prediction in detected_polygons[1]:
22
+ if prediction["confidence"] > max_confidence:
23
+ max_confidence = prediction["confidence"]
24
+ max_prediction = prediction
25
+ image = Image.fromarray(image)
26
+ img2 = image.copy()
27
+ xy = max_prediction["polygon"]
28
+ draw = ImageDraw.Draw(img2)
29
+ draw.polygon(xy, fill="blue")
30
+ return Image.blend(image, img2, 0.5)
31
+
32
 
33
  demo = gr.Interface(
34
+ query_image,
35
+ inputs=[gr.Image()],
36
  outputs="image",
37
  title="TODO",
38
  )