Spaces:
Sleeping
Sleeping
| try: | |
| import detectron2 | |
| except: | |
| import os | |
| os.system('pip install git+https://github.com/facebookresearch/detectron2.git') | |
| from matplotlib.pyplot import axis | |
| import gradio as gr | |
| import requests | |
| import numpy as np | |
| from torch import nn | |
| import requests | |
| import torch | |
| from detectron2 import model_zoo | |
| from detectron2.engine import DefaultPredictor | |
| from detectron2.config import get_cfg | |
| from detectron2.utils.visualizer import Visualizer | |
| from detectron2.data import MetadataCatalog | |
| model_path = "https://huggingface.co/dbmdz/detectron2-model/resolve/main/model_final.pth" | |
| cfg = get_cfg() | |
| cfg.merge_from_file("./configs/detectron2/faster_rcnn_R_50_FPN_3x.yaml") | |
| cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.8 | |
| cfg.MODEL.ROI_HEADS.NUM_CLASSES = 2 | |
| cfg.MODEL.WEIGHTS = model_path | |
| my_metadata = MetadataCatalog.get("dbmdz_coco_all") | |
| my_metadata.thing_classes = ["Illumination", "Illustration"] | |
| if not torch.cuda.is_available(): | |
| cfg.MODEL.DEVICE='cpu' | |
| predictor = DefaultPredictor(cfg) | |
| def inference(image): | |
| print(image.height) | |
| height = image.height | |
| img = np.array(image.resize((500, height))) | |
| outputs = predictor(img) | |
| v = Visualizer(img, my_metadata, scale=1.2) | |
| out = v.draw_instance_predictions(outputs["instances"].to("cpu")) | |
| return out.get_image() | |
| title = "DBMDZ Detectron2 Model Demo" | |
| 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." | |
| article = '<p>Detectron model is available from our repository <a href="">here</a> on the Hugging Face Model Hub.</p>' | |
| gr.Interface( | |
| inference, | |
| [gr.inputs.Image(type="pil", label="Input")], | |
| gr.outputs.Image(type="numpy", label="Output"), | |
| title=title, | |
| description=description, | |
| article=article, | |
| examples=[]).launch() | |