Spaces:
Sleeping
Sleeping
Weicheng HE
commited on
Commit
·
173e0d7
1
Parent(s):
854a8ac
updates
Browse files
app.py
CHANGED
|
@@ -1,7 +1,44 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
# check pytorch installation:
|
| 3 |
+
import torch, torchvision
|
| 4 |
+
# Some basic setup:
|
| 5 |
+
# Setup detectron2 logger
|
| 6 |
+
import detectron2
|
| 7 |
+
from detectron2.utils.logger import setup_logger
|
| 8 |
|
| 9 |
+
# import some common libraries
|
| 10 |
+
import numpy as np
|
| 11 |
+
import os, json, cv2, random
|
| 12 |
|
| 13 |
+
# import some common detectron2 utilities
|
| 14 |
+
from detectron2 import model_zoo
|
| 15 |
+
from detectron2.engine import DefaultPredictor
|
| 16 |
+
from detectron2.config import get_cfg
|
| 17 |
+
from detectron2.utils.visualizer import Visualizer
|
| 18 |
+
from detectron2.data import MetadataCatalog, DatasetCatalog
|
| 19 |
+
from PIL import Image
|
| 20 |
+
|
| 21 |
+
cfg = get_cfg()
|
| 22 |
+
cfg.MODEL.DEVICE='cpu'
|
| 23 |
+
cfg.merge_from_file(model_zoo.get_config_file("COCO-Detection/faster_rcnn_R_101_FPN_3x.yaml"))
|
| 24 |
+
cfg.MODEL.WEIGHTS = "model_final.pth"
|
| 25 |
+
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model
|
| 26 |
+
predictor = DefaultPredictor(cfg)
|
| 27 |
+
def inference(img):
|
| 28 |
+
im = cv2.imread(img.name)
|
| 29 |
+
outputs = predictor(im)
|
| 30 |
+
v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
|
| 31 |
+
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
| 32 |
+
return Image.fromarray(np.uint8(out.get_image())).convert('RGB')
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
title = "Detectron 2"
|
| 36 |
+
description = "Gradio demo for Detectron 2: A PyTorch-based modular object detection library. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
|
| 37 |
+
article = "<p style='text-align: center'><a href='https://ai.facebook.com/blog/-detectron2-a-pytorch-based-modular-object-detection-library-/' target='_blank'>Detectron2: A PyTorch-based modular object detection library</a> | <a href='https://github.com/facebookresearch/detectron2' target='_blank'>Github Repo</a></p>"
|
| 38 |
+
|
| 39 |
+
examples = [['input.jpg']]
|
| 40 |
+
|
| 41 |
+
gr.Interface(inference, inputs=gr.inputs.Image(type="file"), outputs=gr.outputs.Image(type="pil"),enable_queue=True, title=title,
|
| 42 |
+
description=description,
|
| 43 |
+
article=article,
|
| 44 |
+
examples=examples).launch()
|
input.jpg
ADDED
|