Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
try:
|
2 |
+
import detectron2
|
3 |
+
except:
|
4 |
+
import os
|
5 |
+
os.system('pip install git+https://github.com/facebookresearch/detectron2.git')
|
6 |
+
|
7 |
+
import cv2
|
8 |
+
|
9 |
+
from matplotlib.pyplot import axis
|
10 |
+
import gradio as gr
|
11 |
+
import requests
|
12 |
+
import numpy as np
|
13 |
+
from torch import nn
|
14 |
+
import requests
|
15 |
+
|
16 |
+
import torch
|
17 |
+
|
18 |
+
from detectron2 import model_zoo
|
19 |
+
from detectron2.engine import DefaultPredictor
|
20 |
+
from detectron2.config import get_cfg
|
21 |
+
from detectron2.utils.visualizer import Visualizer
|
22 |
+
from detectron2.data import MetadataCatalog
|
23 |
+
|
24 |
+
|
25 |
+
model_path = "https://huggingface.co/dbmdz/detectron2-model/resolve/main/model_final.pth"
|
26 |
+
|
27 |
+
cfg = get_cfg()
|
28 |
+
cfg.merge_from_file("./configs/detectron2/faster_rcnn_R_50_FPN_3x.yaml")
|
29 |
+
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 2
|
30 |
+
cfg.MODEL.WEIGHTS = model_path
|
31 |
+
|
32 |
+
my_metadata = MetadataCatalog.get("dbmdz_coco_all")
|
33 |
+
my_metadata.thing_classes = ["Illumination", "Illustration"]
|
34 |
+
|
35 |
+
if not torch.cuda.is_available():
|
36 |
+
cfg.MODEL.DEVICE = "cpu"
|
37 |
+
|
38 |
+
|
39 |
+
def inference(image_url, image, min_score):
|
40 |
+
if image_url:
|
41 |
+
r = requests.get(image_url)
|
42 |
+
if r:
|
43 |
+
im = np.frombuffer(r.content, dtype="uint8")
|
44 |
+
im = cv2.imdecode(im, cv2.IMREAD_COLOR)
|
45 |
+
else:
|
46 |
+
# Model expect BGR!
|
47 |
+
im = image[:,:,::-1]
|
48 |
+
|
49 |
+
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = min_score
|
50 |
+
predictor = DefaultPredictor(cfg)
|
51 |
+
|
52 |
+
outputs = predictor(im)
|
53 |
+
|
54 |
+
v = Visualizer(im, my_metadata, scale=1.2)
|
55 |
+
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
56 |
+
|
57 |
+
return out.get_image()
|
58 |
+
|
59 |
+
|
60 |
+
title = "DBMDZ Detectron2 Model Demo"
|
61 |
+
description = "This demo introduces an interactive playground for our trained Detectron2 model. <br>The model was trained on manually annotated segments from digitized books to detect Illustration or Illumination segments on a given page."
|
62 |
+
article = '<p>Detectron model is available from our repository <a href="">here</a> on the Hugging Face Model Hub.</p>'
|
63 |
+
|
64 |
+
gr.Interface(
|
65 |
+
inference,
|
66 |
+
[gr.inputs.Textbox(label="Image URL", placeholder="https://api.digitale-sammlungen.de/iiif/image/v2/bsb10483966_00008/full/500,/0/default.jpg"),
|
67 |
+
gr.inputs.Image(type="numpy", label="Input Image"),
|
68 |
+
gr.Slider(minimum=0.0, maximum=1.0, value=0.5, label="Minimum score"),
|
69 |
+
],
|
70 |
+
gr.outputs.Image(type="pil", label="Output"),
|
71 |
+
title=title,
|
72 |
+
description=description,
|
73 |
+
article=article,
|
74 |
+
examples=[]).launch()
|