karolmajek commited on
Commit
afaa4ff
·
1 Parent(s): 1c7aee4

initial :fire:

Browse files
Files changed (2) hide show
  1. app.py +68 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from matplotlib.pyplot import axis
2
+ import gradio as gr
3
+ import requests
4
+ import numpy as np
5
+ from torch import nn
6
+ from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
7
+ import requests
8
+
9
+ import torch
10
+
11
+ from detectron2 import model_zoo
12
+ from detectron2.engine import DefaultPredictor
13
+ from detectron2.config import get_cfg
14
+ from detectron2.utils.visualizer import Visualizer
15
+ from detectron2.data import MetadataCatalog
16
+
17
+
18
+ url1 = 'https://cdn.pixabay.com/photo/2014/09/07/21/52/city-438393_1280.jpg'
19
+ r = requests.get(url1, allow_redirects=True)
20
+ open("city1.jpg", 'wb').write(r.content)
21
+ url2 = 'https://cdn.pixabay.com/photo/2016/02/19/11/36/canal-1209808_1280.jpg'
22
+ r = requests.get(url2, allow_redirects=True)
23
+ open("city2.jpg", 'wb').write(r.content)
24
+
25
+ model_name='COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml'
26
+
27
+ # model = model_zoo.get(model_name, trained=True)
28
+
29
+ cfg = get_cfg()
30
+ # add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library
31
+ cfg.merge_from_file(model_zoo.get_config_file(model_name))
32
+ cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model
33
+ # Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as w ell
34
+ cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(model_name)
35
+
36
+ if not torch.cuda.is_available():
37
+ cfg.MODEL.DEVICE='cpu'
38
+
39
+ predictor = DefaultPredictor(cfg)
40
+
41
+
42
+ def inference(image):
43
+ img = np.array(image.resize((1024,1024)))
44
+ outputs = predictor(img)
45
+
46
+ v = Visualizer(img, MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
47
+ out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
48
+
49
+ return out.get_image()
50
+
51
+
52
+
53
+ title = "Detectron2-MaskRCNN X101"
54
+ 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.\
55
+ </br><b>Model: COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml</b>"
56
+ 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>"
57
+
58
+ gr.Interface(
59
+ inference,
60
+ [gr.inputs.Image(type="pil", label="Input")],
61
+ gr.outputs.Image(type="numpy", label="Output"),
62
+ title=title,
63
+ description=description,
64
+ article=article,
65
+ examples=[
66
+ ["city1.jpg"],
67
+ ["city2.jpg"]
68
+ ]).launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ opencv-python-headless
2
+ pyyaml==5.1
3
+ torch
4
+ git+https://github.com/facebookresearch/detectron2.git