ruiite commited on
Commit
110601a
1 Parent(s): a395cb8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
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
+
22
+ model_path = 'model_final.pth'
23
+
24
+ cfg = get_cfg()
25
+ cfg.merge_from_file("./configs/detectron2/faster_rcnn_R_50_FPN_3x.yaml")
26
+ cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
27
+ cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1
28
+ cfg.MODEL.WEIGHTS = model_path
29
+
30
+ if not torch.cuda.is_available():
31
+ cfg.MODEL.DEVICE='cpu'
32
+
33
+ predictor = DefaultPredictor(cfg)
34
+
35
+ def inference(image):
36
+ # print(image.height)
37
+
38
+ # height = image.height
39
+
40
+ # img = np.array(image.resize((500, height)))
41
+ outputs = predictor(img)
42
+
43
+ v = Visualizer(img,scale=1.2)
44
+ out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
45
+
46
+ return out.get_image()
47
+
48
+ title = "Detectron2 Car damage Detection"
49
+ description = "This demo introduces an interactive playground for our trained Detectron2 model."
50
+
51
+ gr.Interface(
52
+ inference,
53
+ [gr.inputs.Image(type="pil", label="Input")],
54
+ gr.outputs.Image(type="numpy", label="Output"),
55
+ title=title,
56
+ description=description,
57
+ examples=[]).launch()