Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
try:
|
2 |
+
import detectron2
|
3 |
+
except:
|
4 |
+
import os
|
5 |
+
os.system('pip install git+https://github.com/facebookresearch/detectron2.git')
|
6 |
+
from matplotlib.pyplot import axis
|
7 |
+
import requests
|
8 |
+
import numpy as np
|
9 |
+
from torch import nn
|
10 |
+
|
11 |
+
import requests
|
12 |
+
|
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 |
+
import streamlit as st
|
22 |
+
from detectron2.utils.visualizer import ColorMode
|
23 |
+
import os
|
24 |
+
import cv2
|
25 |
+
from PIL import Image, ImageOps
|
26 |
+
import numpy as np
|
27 |
+
|
28 |
+
model_path = "model_final.pth"
|
29 |
+
|
30 |
+
cfg = get_cfg()
|
31 |
+
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.6
|
32 |
+
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 4
|
33 |
+
cfg.MODEL.WEIGHTS = model_path
|
34 |
+
st.write("""
|
35 |
+
# Car Damage Detection
|
36 |
+
"""
|
37 |
+
)
|
38 |
+
file = st.file_uploader("Please upload an image file(JPG/PNG/JPEG format)", type=["jpg", "png","jpeg"])
|
39 |
+
|
40 |
+
st.set_option('deprecation.showfileUploaderEncoding', False)
|
41 |
+
|
42 |
+
car_metadata = MetadataCatalog.get("test1")
|
43 |
+
car_metadata.thing_classes = ['Damage-car','Damage','Others','Undamage']
|
44 |
+
|
45 |
+
if not torch.cuda.is_available():
|
46 |
+
cfg.MODEL.DEVICE='cpu'
|
47 |
+
|
48 |
+
predictor = DefaultPredictor(cfg)
|
49 |
+
def inference(image):
|
50 |
+
|
51 |
+
img = np.array(image)
|
52 |
+
outputs = predictor(img)
|
53 |
+
v = Visualizer(img[:, :, ::-1],
|
54 |
+
metadata=car_metadata,
|
55 |
+
scale=0.5,
|
56 |
+
instance_mode=ColorMode.IMAGE_BW
|
57 |
+
)
|
58 |
+
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
59 |
+
return out.get_image()
|
60 |
+
if file is None:
|
61 |
+
st.text("Please upload an image file")
|
62 |
+
else:
|
63 |
+
image = Image.open(file).convert('RGB')
|
64 |
+
st.image(image,use_column_width=True)
|
65 |
+
st.write("""
|
66 |
+
# Output!!
|
67 |
+
"""
|
68 |
+
)
|
69 |
+
predictions = inference(image)
|
70 |
+
st.image(predictions,use_column_width=True)
|