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"
示例#2
0
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 main(ann_file_path,images_path,epochs,ped_percentage,val_images_path , val_ann_file, preproType):
    # train set
    train_set = OurTrainDataset()
    train_set.load_dataset(images_path, ann_file_path,ped_percentage, preproType) # ,ped_percentage
    train_set.prepare()
    print('Train images: %d' % len(train_set.image_ids))

    # val set
    val_set = OurALLTestDataset()
    val_set.load_dataset(val_images_path, val_ann_file,preproType)
    val_set.prepare()
    print('Validation images: %d' % len(val_set.image_ids))

    # prepare config
    config = TrainConfig()

    # define the model
    model = MaskRCNN(mode='training', model_dir='/home/test/data/trained_models/', config=config)

    # load weights (mscoco)
    model.load_weights('/home/test/data/mask_rcnn_coco.h5', by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc",  "mrcnn_bbox", "mrcnn_mask"])

    print('START TRAINING')
    
    # train weights (output layers or 'heads')
    model.train(train_set, val_set, learning_rate=config.LEARNING_RATE, epochs=epochs, layers='all')

    print('Training DONE')
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))
示例#5
0
文件: finder.py 项目: Sorong/ML_Waldo
    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..")
示例#6
0
# define the prediction configuration
class PredictionConfig(Config):
    # define the name of the configuration
    NAME = "underline_cfg"

    NUM_CLASSES = 1 + 1
    # 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
示例#7
0
class CommaConfig(coco.CocoConfig):
    NAME = 'comma.ai'

    GPU_COUNT = 1
    IMAGES_PER_GPU = batch_size

    IMAGE_MIN_DIM = 256
    IMAGE_MAX_DIM = 512

    MEAN_PIXEL = mean


if __name__ == '__main__':
    config = CommaConfig()
    model = MaskRCNN(mode="inference", model_dir="logs", config=config)
    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)
示例#8
0
    GPU_COUNT = 1
    NUM_CLASSES = 81


config = Config()
config.display()

ROOT_DIR = os.getcwd()
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")

print(COCO_MODEL_PATH)
if not os.path.exists(COCO_MODEL_PATH):
    Mask_RCNN.mrcnn.utils.download_trained_weights(COCO_MODEL_PATH)

model = MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=Config())
model.load_weights(COCO_MODEL_PATH, by_name=True)


def get_cars(boxes, class_ids):
    cars = []
    for i, box in enumerate(boxes):
        if class_ids[i] in [3, 8, 6]:
            cars.append(box)
    return np.array(cars)


def compute_overlaps(parked_car_boxes, car_boxes):
    new_car_boxes = []
    for box in car_boxes:
        y1 = box[0]