Spaces:
Running
Running
app: add initial gradio version
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
try:
|
2 |
+
import detectron2
|
3 |
+
except:
|
4 |
+
import os
|
5 |
+
os.system('pip install git+https://github.com/facebookresearch/detectron2.git')
|
6 |
+
|
7 |
+
from matplotlib.pyplot import axis
|
8 |
+
import gradio as gr
|
9 |
+
import requests
|
10 |
+
import numpy as np
|
11 |
+
from torch import nn
|
12 |
+
import requests
|
13 |
+
|
14 |
+
import torch
|
15 |
+
|
16 |
+
from detectron2 import model_zoo
|
17 |
+
from detectron2.engine import DefaultPredictor
|
18 |
+
from detectron2.config import get_cfg
|
19 |
+
from detectron2.utils.visualizer import Visualizer
|
20 |
+
from detectron2.data import MetadataCatalog
|
21 |
+
|
22 |
+
|
23 |
+
model_path = "https://huggingface.co/dbmdz/detectron2-model/resolve/main/model_final.pth"
|
24 |
+
|
25 |
+
cfg = get_cfg()
|
26 |
+
cfg.merge_from_file("./configs/detectron2/faster_rcnn_R_50_FPN_3x.yaml")
|
27 |
+
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.8
|
28 |
+
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 2
|
29 |
+
cfg.MODEL.WEIGHTS = model_path
|
30 |
+
|
31 |
+
my_metadata = MetadataCatalog.get("dbmdz_coco_all")
|
32 |
+
my_metadata.thing_classes = ["Illumination", "Illustration"]
|
33 |
+
|
34 |
+
if not torch.cuda.is_available():
|
35 |
+
cfg.MODEL.DEVICE='cpu'
|
36 |
+
|
37 |
+
predictor = DefaultPredictor(cfg)
|
38 |
+
|
39 |
+
def inference(image):
|
40 |
+
print(image.height)
|
41 |
+
|
42 |
+
height = image.height
|
43 |
+
|
44 |
+
img = np.array(image.resize((500, height)))
|
45 |
+
outputs = predictor(img)
|
46 |
+
|
47 |
+
v = Visualizer(img, my_metadata, scale=1.2)
|
48 |
+
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
49 |
+
|
50 |
+
return out.get_image()
|
51 |
+
|
52 |
+
title = "DBMDZ Detectron2 Model Demo"
|
53 |
+
description = "This demo introduces an interactive playground for our trained Detectron2 model. <br>The model was trained on image from digitized books to detect Illustration or Illumination segments on a given page. Classification threshold is set to 0.8."
|
54 |
+
article = '<p>Detectron model is available from our repository <a href="">here</a> on the Hugging Face Model Hub.</p>'
|
55 |
+
|
56 |
+
gr.Interface(
|
57 |
+
inference,
|
58 |
+
[gr.inputs.Image(type="pil", label="Input")],
|
59 |
+
gr.outputs.Image(type="numpy", label="Output"),
|
60 |
+
title=title,
|
61 |
+
description=description,
|
62 |
+
article=article,
|
63 |
+
examples=[]).launch()
|