Skip to content

Commit b1e163f

Browse files
Add files via upload
1 parent 6612af4 commit b1e163f

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

Detector.py

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Created on Mon Apr 11 12:59:39 2022
5+
"""
6+
7+
import cv2 as cv
8+
import json
9+
from detectron2.engine import DefaultPredictor
10+
from detectron2.config import get_cfg
11+
from detectron2.utils.visualizer import Visualizer
12+
from detectron2.utils.visualizer import ColorMode
13+
from detectron2 import model_zoo
14+
from detectron2.data import MetadataCatalog, DatasetCatalog
15+
from detectron2.data.datasets import register_coco_instances
16+
from detectron2.modeling import build_model
17+
import torch
18+
import numpy as np
19+
from PIL import Image
20+
import time
21+
22+
23+
24+
25+
26+
class Detector:
27+
28+
def __init__(self):
29+
30+
# set model and test set
31+
self.model = 'mask_rcnn_R_50_FPN_3x.yaml'
32+
33+
# obtain detectron2's default config
34+
self.cfg = get_cfg()
35+
36+
# load values from a file
37+
# self.cfg.merge_from_file("test.yaml")
38+
self.cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/"+self.model))
39+
40+
# set device to cpu
41+
#self.cfg.MODEL.DEVICE = "cuda"
42+
self.cfg.MODEL.DEVICE = "cpu"
43+
44+
# get weights
45+
# self.cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/"+self.model)
46+
self.cfg.MODEL.WEIGHTS = "/home/iovision/return_img/model_final.pth"
47+
#self.cfg.MODEL.WEIGHTS = "/home/appuser/return_img_repo/model_final.pth"
48+
49+
# set the testing threshold for this model
50+
51+
self.cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7
52+
53+
#self.cfg.DATASETS.TEST = ("fold1")
54+
55+
# build model from weights
56+
# self.cfg.MODEL.WEIGHTS = self.convert_model_for_inference()
57+
self.cfg.MODEL.ROI_HEADS.NUM_CLASSES = 5
58+
self.predictor = DefaultPredictor(self.cfg)
59+
60+
# build model and convert for inference
61+
def convert_model_for_inference(self):
62+
63+
# build model
64+
model = build_model(self.cfg)
65+
66+
# save as checkpoint
67+
torch.save(model.state_dict(), 'checkpoint.pth')
68+
69+
# return path to inference model
70+
return 'checkpoint.pth'
71+
72+
# detectron model
73+
# adapted from detectron2 colab notebook: https://colab.research.google.com/drive/16jcaJoc6bCFAQ96jDe2HwtXj7BMD_-m5
74+
def inference(self, file):
75+
start= time.time()
76+
77+
im = cv.imread(file)
78+
outputs = self.predictor(im)
79+
# with open(self.curr_dir+'/data.txt', 'w') as fp:
80+
# json.dump(outputs['instances'], fp)
81+
# # json.dump(cfg.dump(), fp)
82+
83+
# get metadata
84+
MetadataCatalog.get("mydataset").thing_classes = ['short_sleeved_shirt', 'long_sleeved_shirt', 'long_sleeved_outwear', 'shorts', 'trousers']
85+
# visualise
86+
v = Visualizer(im[:, :, ::-1], metadata=MetadataCatalog.get("mydataset"), scale=1.2)
87+
v = v.draw_instance_predictions(outputs["instances"].to("cpu"))
88+
# get image
89+
img1 = cv.cvtColor(v.get_image()[:, :, ::-1], cv.COLOR_BGR2RGB)
90+
img = Image.fromarray(np.uint8(img1))
91+
end = time.time()
92+
a= end - start
93+
# write to jpg
94+
# cv.imwrite('img.jpg',v.get_image())
95+
96+
return img, a

0 commit comments

Comments
 (0)