Created
December 2, 2019 10:00
-
-
Save deepconsc/251445f3ea9f135d43b7c76b090cb1c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random as r | |
import math | |
cfg = get_cfg() | |
cfg.merge_from_file("./detectron2_repo/configs/COCO-Detection/retinanet_R_50_FPN_3x.yaml") | |
# detector threshold | |
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 | |
cfg.MODEL.WEIGHTS = 'output/model_final.pth' | |
cfg.DATASETS.TEST = ("plates", ) | |
predictor = DefaultPredictor(cfg) | |
# get images with glob function to filelist to iterate through. Use any directory for testing, with your prefered format of | |
# images: .png, .jpg, .jpeg. | |
filelist = glob.glob('*.jpg') | |
for i in range(10): | |
img = cv2.imread(filelist[i]) | |
# prediction | |
outputs = predictor(img) | |
### uncomment the below script in case you want to see the detector visualisations | |
### v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TEST[0]), scale=1.2) | |
### v = v.draw_instance_predictions(outputs["instances"].to("cpu")) | |
### cv2_imshow(v.get_image()[:, :, ::-1]) | |
# getting prediction bboxes from model outputs | |
boxes = outputs['instances'].pred_boxes.tensor.cpu().numpy()[0] | |
x2 = math.ceil(boxes[0]) | |
x1 = math.ceil(boxes[1]) | |
y2 = math.ceil(boxes[2]) | |
y1 = math.ceil(boxes[3]) | |
crop_img = img[x1:y1,x2:y2] | |
#crop_img = cv2.resize(crop_img, (500,250)) | |
# showing original image | |
cv2_imshow(img) | |
# showing cropped number plate | |
cv2_imshow(crop_img) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment