File size: 2,029 Bytes
9c93bcf c3ee9a5 9c93bcf c3ee9a5 5b6b34d c3ee9a5 |
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 |
import os
os.system('!python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu113/torch1.10/index.html')
os.system('!git clone -b add_dit_inference_bis https://github.com/NielsRogge/unilm.git')
import argparse
import cv2
from ditod import add_vit_config
from detectron2.config import get_cfg
from detectron2.utils.visualizer import ColorMode, Visualizer
from detectron2.data import MetadataCatalog
from detectron2.engine import DefaultPredictor
# Step 1: instantiate config
cfg = get_cfg()
add_vit_config(cfg)
cfg.merge_from_file("cascade_dit_base.yaml")
# Step 2: add model weights URL to config
cfg.MODEL.WEIGHTS = https://layoutlm.blob.core.windows.net/dit/dit-fts/publaynet_dit-b_mrcnn.pth
# Step 3: set device
# TODO also support GPU
cfg.MODEL.DEVICE='cpu'
# Step 4: define model
predictor = DefaultPredictor(cfg)
def analyze_image(img):
md = MetadataCatalog.get(cfg.DATASETS.TEST[0])
if cfg.DATASETS.TEST[0]=='icdar2019_test':
md.set(thing_classes=["table"])
else:
md.set(thing_classes=["text","title","list","table","figure"])
output = predictor(img)["instances"]
v = Visualizer(img[:, :, ::-1],
md,
scale=1.0,
instance_mode=ColorMode.SEGMENTATION)
result = v.draw_instance_predictions(output.to("cpu"))
result_image = result.get_image()[:, :, ::-1]
return result_image
title = "Interactive demo: Document Layout Analysis with DiT"
description = "This is a demo for Microsoft's Document Image Transformer (DiT)."
examples =[['publaynet_example.jpeg']]
iface = gr.Interface(fn=analyze_image,
inputs=gr.inputs.Image(type="numpy"),
outputs=gr.outputs.Image(type="numpy", label="analyzed image"),
title=title,
description=description,
article=article,
examples=examples,
enable_queue=True)
iface.launch(debug=True) |