def main(): args = _get_args() image_path = args.image_path class_names = {1: 'mask', 2: 'no_mask', 3: 'mask_worn_incorrectly'} inf_config = InferenceConfig() model = MaskRCNN(mode='inference', config=inf_config, model_dir='./') model_path = model.find_last() model.load_weights(model_path, by_name=True) for filename in os.listdir(image_path): if filename.split('.')[-1] not in ['jpg', 'png', 'jpeg']: continue img = skimage.io.imread(image_path + filename) img_arr = np.array(img) img_arr = img_arr[:, :, :3] results = model.detect([img_arr], verbose=1) r = results[0] display_instances(img, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'], figsize=(10, 10))
def test_api_post(): path = "C:\\Users\\CAU\\Desktop\\capstone\\text_recognition\demo_image" if os.path.exists(path): for file in os.scandir(path): os.remove(file.path) imagefile = request.files['image'] filename = werkzeug.utils.secure_filename(imagefile.filename) print("\nReceived image File name : " + imagefile.filename) imagefile.save("./text_detection/test/" + filename) # time.sleep(5) detection.run_detection() # time.sleep(5) img_files, img_bbox = load_files() crop_img(img_files, img_bbox) pred_str = recognition.run_recognition() # underline detection cfg = PredictionConfig() # define the model model = MaskRCNN(mode='inference', model_dir='./', config=cfg) # load model weights model_path = 'mask_rcnn_underline_cfg_0020.h5' model.load_weights(model_path, by_name=True) temp = cv2.imread("./text_detection/test/androidFlask.jpg") yhat = model.detect([temp], verbose=0)[0] print(len(yhat['rois'])) # [l, t], [r, t], [r, b], [l, b] for i, file in enumerate(img_files): txt = pd.read_csv(img_bbox[i], header=None) df = pd.DataFrame(columns=["x1", "y1", "x2", "y2", "x3", "y3", "x4", "y4", "result_text"]) # compare for i, bb in enumerate(txt.values): x1, y1, x2, y2, x3, y3, x4, y4 = bb # textbb = [x1, y1, x3, y3] for underline in yhat['rois']: uy1, ux1, uy2, ux2 = underline if (ux1 + ux2) / 2 > x1 and (ux1 + ux2) / 2 < x3 and y1 < uy1 and uy1 < y3: df = df.append({"x1": x1, "y1": y1, "x2": x2, "y2": y2, "x3": x3, "y3": y3, "x4": x4, "y4": y4, "result_text": pred_str[i]}, ignore_index=True) temp = cv2.rectangle(temp, (x1, y1), (x3, y3), (0, 0, 255), 3) # top-left corner and bottom-right corner of rectangle. df.to_csv("./result.csv") cv2.imwrite("./result.jpg", temp) from keras import backend as K K.clear_session() cuda.select_device(0) cuda.close() del model return "done"
def run_main(): detection.run_detection() img_files, img_bbox = load_files() crop_img(img_files, img_bbox) pred_str = recognition.run_recognition() print("recog done") from Mask_RCNN.mrcnn.model import MaskRCNN cfg = PredictionConfig() # define the model model = MaskRCNN(mode='inference', model_dir='./', config=cfg) # load model weights model_path = 'mask_rcnn_underline_cfg_0019.h5' model.load_weights(model_path, by_name=True) temp = cv2.imread("./text_detection/test/androidFlask.jpg") yhat = model.detect([temp], verbose=0)[0] # [l, t], [r, t], [r, b], [l, b] for i, file in enumerate(img_files): txt = pd.read_csv(img_bbox[i], header=None) df = pd.DataFrame(columns=[ "x1", "y1", "x2", "y2", "x3", "y3", "x4", "y4", "result_text" ]) # compare #temp = [] for i, bb in enumerate(txt.values): x1, y1, _, _, x3, y3, _, _ = bb # textbb = [x1, y1, x3, y3] for underline in yhat['rois']: uy1, ux1, uy2, ux2 = underline if (ux1 + ux2) / 2 > x1 and ( ux1 + ux2) / 2 < x3 and y1 < uy1 and uy1 < y3: print(pred_str[i]) for num, _col in enumerate(list(df)[:-1]): df[_col] = txt[num] df["result_text"] = pred_str df.to_csv("./result.csv")
def find(self, imgpath, outputname): model = MaskRCNN(mode="inference", config=self.config, model_dir=self.config.MODEL_PATH) print("weights_path: ", self.weights_path) model.load_weights(self.weights_path, by_name=True) image = skimage.io.imread(imgpath) masks = model.detect([image], verbose=1)[0] gray = skimage.color.gray2rgb(skimage.color.rgb2gray(image)) * 255 mask_filter = (np.sum(masks["masks"], -1, keepdims=True) >= 1) if mask_filter.shape[0] > 0: waldo = np.where(mask_filter, image, gray).astype(np.uint8) img = Image.fromarray(waldo, 'RGB') outpath = os.path.join(os.getcwd(), os.path.basename(self.weights_path)[:-3]) if not os.path.exists(outpath): os.makedirs(outpath) img.save(os.path.join(outpath, outputname + ".jpg")) else: print("Can't find Waldo. Hmm..")
# simplify GPU config GPU_COUNT = 1 IMAGES_PER_GPU = 1 # create config cfg = PredictionConfig() # define the model model = MaskRCNN(mode='inference', model_dir='./', config=cfg) # load model weights model_path = 'mask_rcnn_underline_cfg_0019.h5' model.load_weights(model_path, by_name=True) temp = cv2.imread("./text_detection/test/androidFlask.jpg") yhat = model.detect([temp], verbose=0)[0] print(yhat['rois']) # plot first few images pyplot.imshow(temp) # plot all masks # for j in range(len(yhat['rois'])): # pyplot.imshow(list(yhat['rois'][j]), cmap='gray', alpha=0.3) ax = pyplot.gca() # plot each box # print(len(yhat['rois'])) for box in yhat['rois']: # get coordinates y1, x1, y2, x2 = box # calculate width and height of the box width, height = x2 - x1, y2 - y1 # create the shape
model.load_weights('models/mask_rcnn_coco.h5', by_name=True) for image_paths in [train_paths, test_paths]: output_dir = os.path.join(*image_paths[0].split('/')[:2], 'seg') os.makedirs(output_dir, exist_ok=True) for i in tqdm(range(0, len(image_paths), batch_size)): batch = [] for j in range(batch_size): if i + j >= len(image_paths): batch.append(batch[-1]) else: image = cv2.cvtColor(cv2.imread(image_paths[i + j]), cv2.COLOR_BGR2RGB) batch.append(image) batch_detections = model.detect(batch) for j in range(batch_size): mask = np.zeros((256, 512)) detections = batch_detections[j] for k in range(len(detections['scores'])): if 1 <= detections['class_ids'][k] <= 9: mask[detections['masks'][:, :, k]] = 255 output_name = os.path.split(image_paths[i + j])[-1] cv2.imwrite(os.path.join(output_dir, output_name), mask)
alpha = 0.6 video_capture = cv2.VideoCapture(VIDEO_SOURCE) video_FourCC = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') video_fps = video_capture.get(cv2.CAP_PROP_FPS) video_size = (int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) out = cv2.VideoWriter("out.avi", video_FourCC, video_fps, video_size) while video_capture.isOpened(): success, frame = video_capture.read() overlay = frame.copy() if not success: break rgb_image = frame[:, :, ::-1] results = model.detect([rgb_image], verbose=0) cars = get_cars(results[0]['rois'], results[0]['class_ids']) overlaps = compute_overlaps(parked_car_boxes, cars) for parking_area, overlap_areas in zip(parked_car_boxes, overlaps): max_IoU_overlap = np.max(overlap_areas) if max_IoU_overlap < 0.15: cv2.fillPoly(overlay, [np.array(parking_area)], (71, 27, 92)) free_space = True cv2.addWeighted(overlay, alpha, frame, 1 - alpha, 0, frame) cv2.imshow('output', frame) out.write(frame) if cv2.waitKey(1) & 0xFF == ord('q'): break