Spaces:
Runtime error
Runtime error
Commit
·
ac8f703
1
Parent(s):
d5adff2
Upload 3 files
Browse files- app.py +39 -0
- detection.py +46 -0
- model_final.pth +3 -0
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from detection import *
|
3 |
+
|
4 |
+
def detect_on_image(x):
|
5 |
+
detector = Detector(model_type=x)
|
6 |
+
image_file = st.file_uploader("Upload An Image",type=['png','jpeg','jpg'])
|
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 |
+
if option == "Images":
|
31 |
+
st.title('Instance Segmentation for Images')
|
32 |
+
st.subheader("""
|
33 |
+
This takes an image as an input, and provides image with bounding box and mask as an output.
|
34 |
+
""")
|
35 |
+
detect_on_image('object_detection')
|
36 |
+
|
37 |
+
|
38 |
+
if __name__ == '__main__':
|
39 |
+
main()
|
detection.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from detectron2.engine import DefaultPredictor
|
2 |
+
import detectron2
|
3 |
+
from detectron2.utils.logger import setup_logger
|
4 |
+
setup_logger()
|
5 |
+
from detectron2.utils.video_visualizer import VideoVisualizer
|
6 |
+
from detectron2.config import get_cfg
|
7 |
+
from detectron2.data import MetadataCatalog
|
8 |
+
from detectron2.utils.visualizer import ColorMode, Visualizer
|
9 |
+
from detectron2 import model_zoo
|
10 |
+
from detectron2.data.datasets import register_coco_instances
|
11 |
+
from PIL import Image
|
12 |
+
import PIL
|
13 |
+
import cv2
|
14 |
+
import numpy as np
|
15 |
+
import matplotlib.pyplot as plt
|
16 |
+
|
17 |
+
|
18 |
+
class Detector:
|
19 |
+
|
20 |
+
def __init__(self, model_type = "object_detection"):
|
21 |
+
self.cfg=get_cfg()
|
22 |
+
self.cfg.merge_from_file(model_zoo.get_config_file("COCO-Detection/faster_rcnn_R_50_FPN_1x.yaml")) # load the default configuration
|
23 |
+
self.cfg.MODEL.WEIGHTS = 'model_final.pth'
|
24 |
+
self.cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.8
|
25 |
+
self.cfg.MODEL.ROI_HEADS.NUM_CLASSES = 2
|
26 |
+
self.cfg.MODEL.DEVICE="cpu"
|
27 |
+
|
28 |
+
dataset_name="guns"
|
29 |
+
classes=['guns','Gun']
|
30 |
+
MetadataCatalog.get(dataset_name).set(thing_classes=classes)
|
31 |
+
|
32 |
+
self.predictor = DefaultPredictor(self.cfg)
|
33 |
+
|
34 |
+
def onImage(self, imagePath):
|
35 |
+
image = cv2.imread(imagePath)
|
36 |
+
predictions = self.predictor(image)
|
37 |
+
dataset_name="guns"
|
38 |
+
|
39 |
+
viz = Visualizer(image,MetadataCatalog.get(dataset_name),scale=1)
|
40 |
+
|
41 |
+
output = viz.draw_instance_predictions(predictions['instances'].to('cpu'))
|
42 |
+
filename = 'result.jpg'
|
43 |
+
cv2.imwrite(filename, output.get_image()[:,:,::-1])
|
44 |
+
# cv2.waitKey(0)
|
45 |
+
# cv2.destroyAllWindows()
|
46 |
+
|
model_final.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7689e1052d4376881a28adcc8fc4a0ae135546d8f92e1078fbe58678d8e3aa1a
|
3 |
+
size 330065763
|