Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
try:
|
2 |
+
import detectron2
|
3 |
+
except:
|
4 |
+
import os
|
5 |
+
|
6 |
+
os.system('pip install git+https://github.com/facebookresearch/detectron2.git')
|
7 |
+
|
8 |
+
from matplotlib.pyplot import axis
|
9 |
+
import gradio as gr
|
10 |
+
import requests
|
11 |
+
import numpy as np
|
12 |
+
from torch import nn
|
13 |
+
import requests
|
14 |
+
|
15 |
+
import torch
|
16 |
+
|
17 |
+
from detectron2 import model_zoo
|
18 |
+
from detectron2.engine import DefaultPredictor
|
19 |
+
from detectron2.config import get_cfg
|
20 |
+
from detectron2.utils.visualizer import Visualizer
|
21 |
+
from detectron2.data import MetadataCatalog
|
22 |
+
|
23 |
+
url1 = 'https://cdn.pixabay.com/photo/2014/09/07/21/52/city-438393_1280.jpg'
|
24 |
+
r = requests.get(url1, allow_redirects=True)
|
25 |
+
open("city1.jpg", 'wb').write(r.content)
|
26 |
+
url2 = 'https://cdn.pixabay.com/photo/2016/02/19/11/36/canal-1209808_1280.jpg'
|
27 |
+
r = requests.get(url2, allow_redirects=True)
|
28 |
+
open("city2.jpg", 'wb').write(r.content)
|
29 |
+
|
30 |
+
model_name = 'COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml'
|
31 |
+
|
32 |
+
# model = model_zoo.get(model_name, trained=True)
|
33 |
+
|
34 |
+
cfg = get_cfg()
|
35 |
+
# add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library
|
36 |
+
cfg.merge_from_file(model_zoo.get_config_file(model_name))
|
37 |
+
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model
|
38 |
+
# Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as w ell
|
39 |
+
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(model_name)
|
40 |
+
|
41 |
+
if not torch.cuda.is_available():
|
42 |
+
cfg.MODEL.DEVICE = 'cpu'
|
43 |
+
|
44 |
+
predictor = DefaultPredictor(cfg)
|
45 |
+
|
46 |
+
|
47 |
+
def inference(image):
|
48 |
+
img = np.array(image.resize((1024, 1024)))
|
49 |
+
outputs = predictor(img)
|
50 |
+
|
51 |
+
v = Visualizer(img, MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
|
52 |
+
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
53 |
+
|
54 |
+
return out.get_image()
|
55 |
+
|
56 |
+
|
57 |
+
title = "Detectron2-MaskRCNN X101"
|
58 |
+
description = "demo for Detectron2. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below.\
|
59 |
+
</br><b>Model: COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml</b>"
|
60 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2012.07177'>Simple Copy-Paste is a Strong Data Augmentation Method for Instance Segmentation</a> | <a href='https://github.com/facebookresearch/detectron2/blob/main/MODEL_ZOO.md'>Detectron model ZOO</a></p>"
|
61 |
+
|
62 |
+
gr.Interface(
|
63 |
+
inference,
|
64 |
+
[gr.inputs.Image(type="pil", label="Input")],
|
65 |
+
gr.outputs.Image(type="numpy", label="Output"),
|
66 |
+
title=title,
|
67 |
+
description=description,
|
68 |
+
article=article,
|
69 |
+
examples=[
|
70 |
+
["city1.jpg"],
|
71 |
+
["city2.jpg"]
|
72 |
+
]).launch()
|