Spaces:
Runtime error
Runtime error
Ahsen Khaliq
commited on
Commit
·
9c1631e
1
Parent(s):
49e8d07
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.system('pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu102/torch1.9/index.html')
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Install detectron2
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
# clone and install Detic
|
| 9 |
+
os.system("git clone https://github.com/facebookresearch/Detic.git --recurse-submodules")
|
| 10 |
+
os.chdir("Detic")
|
| 11 |
+
os.system("pip install -r requirements.txt")
|
| 12 |
+
|
| 13 |
+
# Some basic setup:
|
| 14 |
+
# Setup detectron2 logger
|
| 15 |
+
import detectron2
|
| 16 |
+
from detectron2.utils.logger import setup_logger
|
| 17 |
+
setup_logger()
|
| 18 |
+
|
| 19 |
+
# import some common libraries
|
| 20 |
+
import sys
|
| 21 |
+
import numpy as np
|
| 22 |
+
import os, json, cv2, random
|
| 23 |
+
from google.colab.patches import cv2_imshow
|
| 24 |
+
|
| 25 |
+
# import some common detectron2 utilities
|
| 26 |
+
from detectron2 import model_zoo
|
| 27 |
+
from detectron2.engine import DefaultPredictor
|
| 28 |
+
from detectron2.config import get_cfg
|
| 29 |
+
from detectron2.utils.visualizer import Visualizer
|
| 30 |
+
from detectron2.data import MetadataCatalog, DatasetCatalog
|
| 31 |
+
|
| 32 |
+
# Detic libraries
|
| 33 |
+
sys.path.insert(0, 'third_party/CenterNet2/projects/CenterNet2/')
|
| 34 |
+
from centernet.config import add_centernet_config
|
| 35 |
+
from detic.config import add_detic_config
|
| 36 |
+
from detic.modeling.utils import reset_cls_test
|
| 37 |
+
|
| 38 |
+
# Build the detector and download our pretrained weights
|
| 39 |
+
cfg = get_cfg()
|
| 40 |
+
add_centernet_config(cfg)
|
| 41 |
+
add_detic_config(cfg)
|
| 42 |
+
cfg.MODEL.DEVICE='cpu'
|
| 43 |
+
cfg.merge_from_file("configs/Detic_LCOCOI21k_CLIP_SwinB_896b32_4x_ft4x_max-size.yaml")
|
| 44 |
+
cfg.MODEL.WEIGHTS = 'https://dl.fbaipublicfiles.com/detic/Detic_LCOCOI21k_CLIP_SwinB_896b32_4x_ft4x_max-size.pth'
|
| 45 |
+
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model
|
| 46 |
+
cfg.MODEL.ROI_BOX_HEAD.ZEROSHOT_WEIGHT_PATH = 'rand'
|
| 47 |
+
cfg.MODEL.ROI_HEADS.ONE_CLASS_PER_PROPOSAL = True # For better visualization purpose. Set to False for all classes.
|
| 48 |
+
predictor = DefaultPredictor(cfg)
|
| 49 |
+
|
| 50 |
+
# Setup the model's vocabulary using build-in datasets
|
| 51 |
+
|
| 52 |
+
BUILDIN_CLASSIFIER = {
|
| 53 |
+
'lvis': 'datasets/metadata/lvis_v1_clip_a+cname.npy',
|
| 54 |
+
'objects365': 'datasets/metadata/o365_clip_a+cnamefix.npy',
|
| 55 |
+
'openimages': 'datasets/metadata/oid_clip_a+cname.npy',
|
| 56 |
+
'coco': 'datasets/metadata/coco_clip_a+cname.npy',
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
BUILDIN_METADATA_PATH = {
|
| 60 |
+
'lvis': 'lvis_v1_val',
|
| 61 |
+
'objects365': 'objects365_v2_val',
|
| 62 |
+
'openimages': 'oid_val_expanded',
|
| 63 |
+
'coco': 'coco_2017_val',
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
vocabulary = 'lvis' # change to 'lvis', 'objects365', 'openimages', or 'coco'
|
| 67 |
+
metadata = MetadataCatalog.get(BUILDIN_METADATA_PATH[vocabulary])
|
| 68 |
+
classifier = BUILDIN_CLASSIFIER[vocabulary]
|
| 69 |
+
num_classes = len(metadata.thing_classes)
|
| 70 |
+
reset_cls_test(predictor.model, classifier, num_classes)
|
| 71 |
+
|
| 72 |
+
os.system("wget https://web.eecs.umich.edu/~fouhey/fun/desk/desk.jpg")
|
| 73 |
+
|
| 74 |
+
def inference(img):
|
| 75 |
+
|
| 76 |
+
im = cv2.imread(img)
|
| 77 |
+
|
| 78 |
+
outputs = predictor(im)
|
| 79 |
+
v = Visualizer(im[:, :, ::-1], metadata)
|
| 80 |
+
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
| 81 |
+
cv2_imshow(out.get_image()[:, :, ::-1])
|
| 82 |
+
|
| 83 |
+
return Image.fromarray(np.uint8(out.get_image())).convert('RGB')
|
| 84 |
+
|
| 85 |
+
title = "Detectron 2"
|
| 86 |
+
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."
|
| 87 |
+
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>"
|
| 88 |
+
|
| 89 |
+
examples = [['desk.jpg']]
|
| 90 |
+
gr.Interface(inference, inputs=gr.inputs.Image(type="filepath"), outputs=gr.outputs.Image(type="pil"),enable_queue=True, title=title,
|
| 91 |
+
description=description,
|
| 92 |
+
article=article,
|
| 93 |
+
examples=examples).launch()
|