ruiite commited on
Commit
c54e939
·
1 Parent(s): a109310

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ try:
2
+ import detectron2
3
+ except:
4
+ import os
5
+ os.system('pip install git+https://github.com/facebookresearch/detectron2.git')
6
+
7
+ from matplotlib.pyplot import axis
8
+ import gradio as gr
9
+ import requests
10
+ import numpy as np
11
+ from torch import nn
12
+ import requests
13
+
14
+ import torch
15
+ import detectron2
16
+ from detectron2 import model_zoo
17
+ from detectron2.engine import DefaultPredictor
18
+ from detectron2.config import get_cfg
19
+ from detectron2.utils.visualizer import Visualizer
20
+ from detectron2.data import MetadataCatalog
21
+ from detectron2.utils.visualizer import ColorMode
22
+
23
+ model_path = 'model_final.pth'
24
+
25
+ cfg = get_cfg()
26
+ cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
27
+ cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
28
+ cfg.MODEL.ROI_HEADS.NUM_CLASSES = 19
29
+ cfg.MODEL.WEIGHTS = model_path
30
+
31
+ if not torch.cuda.is_available():
32
+ cfg.MODEL.DEVICE='cpu'
33
+
34
+ predictor = DefaultPredictor(cfg)
35
+ my_metadata = MetadataCatalog.get("car_parts_dataset_val")
36
+ my_metadata.thing_classes = ["_background_","back_bumper","back_glass","back_left_door","back_left_light","back_right_door","back_right_light","front_bumper","front_glass","front_left_door","front_left_light","front_right_door","front_right_light","hood","left_mirror","right_mirror","tailgate","trunk","wheel"]
37
+
38
+ def inference(image):
39
+ print(image.height)
40
+
41
+ height = image.height
42
+
43
+ #img = np.array(image.resize((500, height)))
44
+
45
+ img = np.array(image)
46
+ outputs = predictor(img)
47
+ v = Visualizer(img[:, :, ::-1],
48
+ metadata=my_metadata,
49
+ scale=0.5,
50
+ instance_mode=ColorMode.SEGMENTATION # remove the colors of unsegmented pixels. This option is only available for segmentation models
51
+ )
52
+ v = Visualizer(img,scale=1.2)
53
+ #print(outputs["instances"].to('cpu'))
54
+ out = v.draw_instance_predictions(outputs["instances"])
55
+
56
+ return out.get_image()[:, :, ::-1]
57
+
58
+ title = "Detectron2 Car Parts Detection"
59
+ description = "This demo introduces an interactive playground for our trained Detectron2 model."
60
+
61
+ gr.Interface(
62
+ inference,
63
+ [gr.inputs.Image(type="pil", label="Input")],
64
+ gr.outputs.Image(type="numpy", label="Output"),
65
+ title=title,
66
+ description=description,
67
+ examples=[]).launch()