Spaces:
Running
Running
app: major refactoring (add new possibility to provide both image link OR upload an image, add min score slider, fix various preprocessing issues)
Browse files
app.py
CHANGED
@@ -4,6 +4,8 @@ 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
|
@@ -24,7 +26,6 @@ model_path = "https://huggingface.co/dbmdz/detectron2-model/resolve/main/model_f
|
|
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 |
|
@@ -32,31 +33,41 @@ 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=
|
36 |
|
37 |
-
predictor = DefaultPredictor(cfg)
|
38 |
|
39 |
-
def inference(image):
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
|
|
43 |
|
44 |
-
|
45 |
-
outputs = predictor(img)
|
46 |
|
47 |
-
v = Visualizer(
|
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 manually annotated segments from digitized books to detect Illustration or Illumination segments on a given page.
|
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.
|
59 |
-
|
|
|
|
|
|
|
60 |
title=title,
|
61 |
description=description,
|
62 |
article=article,
|
|
|
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
|
|
|
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 |
|
|
|
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,
|