Spaces:
Runtime error
Runtime error
siddhantgore
commited on
Commit
•
7d9724b
1
Parent(s):
f4fe79b
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
if image_file is not None:
|
8 |
-
file_details = {"FileName":image_file.name,"FileType":image_file.type}
|
9 |
-
st.write(file_details)
|
10 |
-
img = Image.open(image_file)
|
11 |
-
st.image(img, caption='Uploaded Image.')
|
12 |
-
with open(image_file.name,mode = "wb") as f:
|
13 |
-
f.write(image_file.getbuffer())
|
14 |
-
st.success("Saved File")
|
15 |
-
detector.onImage(image_file.name)
|
16 |
-
img_ = Image.open("result.jpg")
|
17 |
-
st.image(img_, caption='Proccesed Image.')
|
18 |
-
|
19 |
-
|
20 |
-
def main():
|
21 |
-
with st.expander("About the App"):
|
22 |
-
st.markdown( '<p style="font-size: 30px;"><strong>Welcome to my Instance Segmentation App!</strong></p>', unsafe_allow_html= True)
|
23 |
-
|
24 |
-
|
25 |
-
option = st.selectbox(
|
26 |
-
'What Type of File do you want to work with?',
|
27 |
-
('Images', ' '))
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
import requests
|
11 |
+
|
12 |
+
|
13 |
+
import torch
|
14 |
+
import detectron2
|
15 |
+
from detectron2 import model_zoo
|
16 |
+
from detectron2.engine import DefaultPredictor
|
17 |
+
from detectron2.config import get_cfg
|
18 |
+
from detectron2.utils.visualizer import Visualizer
|
19 |
+
from detectron2.data import MetadataCatalog
|
20 |
import streamlit as st
|
21 |
+
from detectron2.utils.visualizer import ColorMode
|
22 |
+
import os
|
23 |
+
import cv2
|
24 |
+
from PIL import Image, ImageOps
|
25 |
+
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
model_path = "model_final.pth"
|
28 |
+
|
29 |
+
cfg = get_cfg()
|
30 |
+
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.6
|
31 |
+
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 4
|
32 |
+
cfg.MODEL.WEIGHTS = model_path
|
33 |
+
st.write("""
|
34 |
+
# Car Damage Detection
|
35 |
+
"""
|
36 |
+
)
|
37 |
+
file = st.file_uploader("Please upload an image file(JPG/PNG/JPEG format)", type=["jpg", "png","jpeg"])
|
38 |
|
39 |
+
st.set_option('deprecation.showfileUploaderEncoding', False)
|
40 |
+
|
41 |
+
car_metadata = MetadataCatalog.get("test1")
|
42 |
+
car_metadata.thing_classes = ['Damage-car','Damage','Others','Undamage']
|
43 |
+
|
44 |
+
if not torch.cuda.is_available():
|
45 |
+
cfg.MODEL.DEVICE='cpu'
|
46 |
+
|
47 |
+
predictor = DefaultPredictor(cfg)
|
48 |
+
def inference(image):
|
49 |
+
#height = image.height
|
50 |
|
51 |
+
#img = np.array(image.resize((800, height)))
|
52 |
+
img = np.array(image)
|
53 |
+
outputs = predictor(img)
|
54 |
+
v = Visualizer(img[:, :, ::-1],
|
55 |
+
metadata=car_metadata,
|
56 |
+
scale=0.5,
|
57 |
+
instance_mode=ColorMode.IMAGE_BW
|
58 |
+
)
|
59 |
+
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
60 |
+
return out.get_image()
|
61 |
+
if file is None:
|
62 |
+
st.text("Please upload an image file")
|
63 |
+
else:
|
64 |
+
image = Image.open(file).convert('RGB')
|
65 |
+
st.image(image,use_column_width=True)
|
66 |
+
st.write("""
|
67 |
+
# Output!!
|
68 |
+
"""
|
69 |
+
)
|
70 |
+
predictions = inference(image)
|
71 |
+
st.image(predictions,use_column_width=True)
|