File size: 1,547 Bytes
19ca011
 
 
 
 
2186a48
19ca011
2186a48
19ca011
2186a48
 
19ca011
 
 
 
 
2186a48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19ca011
 
2186a48
 
19ca011
d366b83
 
86a804b
80c5fcc
fe6a22f
 
80c5fcc
e2e9def
19ca011
 
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
import cv2
from doc_ufcn.main import DocUFCN
import gradio as gr
from doc_ufcn import models
from PIL import Image
from PIL import ImageDraw

model_path, parameters = models.download_model("generic-page")

model = DocUFCN(len(parameters["classes"]), parameters["input_size"], "cpu")
model.load(model_path, parameters["mean"], parameters["std"])


def query_image(image):
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    detected_polygons, probabilities, mask, overlap = model.predict(
        image, raw_output=True, mask_output=True, overlap_output=True
    )
    max_confidence = 0
    max_prediction = None
    for prediction in detected_polygons[1]:
        if prediction["confidence"] > max_confidence:
            max_confidence = prediction["confidence"]
            max_prediction = prediction
    image = Image.fromarray(image)
    img2 = image.copy()
    xy = max_prediction["polygon"]
    draw = ImageDraw.Draw(img2)
    draw.polygon(xy, fill="blue")
    return Image.blend(image, img2, 0.5)


demo = gr.Interface(
    query_image,
    inputs=[gr.Image()],
    outputs="image",
    title="doc-ufcn Page Detection Demo",
    description="A demo showing the top prediction from the [Teklia/doc-ufcn-generic-page]("
    "https://huggingface.co/Teklia/doc-ufcn-generic-page) model. This generic page detection model predicts single pages from document images.",
    examples=[
        "v2_bsb00046516_00100_full_full_0_default.jpg",
        "512px-Page_from_the_Arthurian_Romances_illuminated_manuscript.jpg",
    ],
)

demo.launch()