Esempio n. 1
0
def GetForegroundImgContourInfo(FilePath):
    '''
    딥러닝 모델을 이용하여 배경을 제거한다.
    전경의 외곽선 정보를 리턴한다.
    :param FilePath:
    :return:
    '''
    # 모델 입력 이미지 사이즈는 32의 배수가 되어야 한다.
    ModelInputHeight = 640
    ModelInputWidth = 640
    Model_Path = "Models\\Background\\background.hdf4"

    model = resnet50_unet(n_classes=2, input_height=ModelInputHeight, input_width=ModelInputWidth)

    model.load_weights(Model_Path)

    out = model.predict_segmentation(
        inp=FilePath,
        out_fname="out.png"
    )

    org = cv.imread(FilePath)
    # 모델 아웃풋 사이즈는 입력 사이즈의 절반이다.
    org = cv.resize(org, (320, 320))

    out2 = np.array(out * 1, dtype=np.uint8)
    # threshed = cv.adaptiveThreshold(out2, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 3, 0)
    out2 = cv.cvtColor(out2, cv.COLOR_GRAY2BGR)

    #print(org.shape)
    #print(out2.shape)
    # cv.imshow("FilePath", org)
    # cv.waitKey(0)

    masked = org * out2
    #cv.imshow("masked", masked)
    #cv.waitKey(0)
    maskedImg = cv.resize(masked, (Configuration.ImageSizeForPreprocess, Configuration.ImageSizeForPreprocess))

    # 알약 외곽선을 추출한다.
    highThreshold = 100
    contours, lastIdx = GetPillContour(img_color =maskedImg, highThreshold = highThreshold, showFlag = False)

    if contours is None:
        print("contour is not found!!")

    return contours, lastIdx
Esempio n. 2
0
def train(images, labels, model, width, height, name):

    if model == 'pspnet_101':
        segmentation_model = pspnet_101(n_classes=16,
                                        input_width=width,
                                        input_height=height)
    elif model == 'resnet50_unet':
        segmentation_model = resnet50_unet(n_classes=16,
                                           input_width=width,
                                           input_height=height)
    elif model == 'resnet50_segnet':
        segmentation_model = resnet50_segnet(n_classes=16,
                                             input_width=width,
                                             input_height=height)
    elif model == 'resnet50_pspnet':
        segmentation_model = resnet50_pspnet(n_classes=16,
                                             input_width=width,
                                             input_height=height)
    else:
        return None

    print('Start training')

    checkpoint_directory = f'./checkpoints/{model}_{width}x{height}{name}'
    if not os.path.exists(checkpoint_directory):
        os.makedirs(checkpoint_directory)

    segmentation_model.train(
        train_images=images,
        train_annotations=labels,
        checkpoints_path=
        f'{checkpoint_directory}/{model}_{width}x{height}{name}',
        epochs=10,
        batch_size=1)

    return segmentation_model
Esempio n. 3
0
original_image = "dataset/semantic_drone_dataset/original_images/001.jpg"
label_image_semantic = "dataset/semantic_drone_dataset/label_images_semantic/001.png"

fig, axs = plt.subplots(1,2, figsize=(16, 8), constrained_layout=True)

axs[0].imshow( Image.open(original_image))
# axs.grid(False)

label_image_semantic = Image.open(label_image_semantic)
axs[1].imshow( Image.open(original_image))
# label_image_semantic = np.asarray(label_image_semantic)
axs[1].imshow(label_image_semantic,alpha = 0.6)
# axs[1].grid(False)

n_classes = 23 # Aerial Semantic Segmentation Drone Dataset tree, gras, other vegetation, dirt, gravel, rocks, water, paved area, pool, person, dog, car, bicycle, roof, wall, fence, fence-pole, window, door, obstacle
model = resnet50_unet(n_classes=n_classes ,  input_height=416, input_width=608  )
epochs = 10
model.train( 
    train_images =  "dataset/semantic_drone_dataset/original_images/",
    train_annotations = "dataset/semantic_drone_dataset/label_images_semantic/",
    checkpoints_path = "resnet50_unet" , epochs=epochs)


import time
from PIL import Image
import matplotlib.pyplot as plt
%matplotlib inline

start = time.time()

input_image = "dataset/semantic_drone_dataset/original_images/001.jpg"
Esempio n. 4
0
    def process(self):
        try:
            page_grp, self.image_grp = self.output_file_grp.split(',')
        except ValueError:
            page_grp = self.output_file_grp
            self.image_grp = FALLBACK_IMAGE_GRP
            LOG.info(
                "No output file group for images specified, falling back to '%s'",
                FALLBACK_IMAGE_GRP)
        oplevel = self.parameter['operation_level']

        model = None
        if self.parameter['use_deeplr']:

            model_weights = self.parameter['seg_weights']
            if not Path(model_weights).is_file():
                LOG.error("""\
                    Segementation model weights file was not found at '%s'. Make sure the `seg_weights` parameter
                    points to the local model weights path.
                    """ % model_weights)
                sys.exit(1)

            model = resnet50_unet(n_classes=self.parameter['classes'],
                                  input_height=self.parameter['height'],
                                  input_width=self.parameter['width'])
            model.load_weights(model_weights)
            LOG.info('Segmentation Model loaded')

        for (n, input_file) in enumerate(self.input_files):
            page_id = input_file.pageId or input_file.ID

            pcgts = page_from_file(self.workspace.download_file(input_file))
            metadata = pcgts.get_Metadata()
            metadata.add_MetadataItem(
                MetadataItemType(
                    type_="processingStep",
                    name=self.ocrd_tool['steps'][0],
                    value=TOOL,
                    Labels=[
                        LabelsType(  #externalRef="parameters",
                            Label=[
                                LabelType(type_=name,
                                          value=self.parameter[name])
                                for name in self.parameter.keys()
                            ])
                    ]))

            page = pcgts.get_Page()
            LOG.info("INPUT FILE %s", input_file.pageId or input_file.ID)

            if self.parameter['use_deeplr']:
                page_image, page_xywh, page_image_info = self.workspace.image_from_page(
                    page, page_id, feature_filter='binarized,deskewed,cropped')
            else:
                page_image, page_xywh, page_image_info = self.workspace.image_from_page(
                    page,
                    page_id,
                    feature_selector='binarized,deskewed,cropped')

            if oplevel == 'page':
                self._process_segment(page_image, page, page_xywh, page_id,
                                      input_file, n, model)
            else:
                LOG.warning('Operation level %s, but should be "page".',
                            oplevel)
                break

            # Use input_file's basename for the new file -
            # this way the files retain the same basenames:
            file_id = input_file.ID.replace(self.input_file_grp,
                                            self.output_file_grp)
            if file_id == input_file.ID:
                file_id = concat_padded(self.output_file_grp, n)
            self.workspace.add_file(
                ID=file_id,
                file_grp=page_grp,  #self.output_file_grp,
                pageId=input_file.pageId,
                mimetype=MIMETYPE_PAGE,
                local_filename=os.path.join(self.output_file_grp,
                                            file_id + '.xml'),
                content=to_xml(pcgts).encode('utf-8'),
                force=self.parameter['force'])
Esempio n. 5
0
# 결과 이미지가 입력의 반으로 줄어듦(이유는 모름)
# 따라서 모델에다가 입력 크기를 원본보다 두배로 설정하면 결과가 원본 이미지와 같은 크기가 나옴.
# 크기는 32의 배수가 되어야 함. assert input_height%32 == 0
# 학습시 색 정보가 중요 한 것 같음.
# 배경과 전경의 색이 비슷하면 결과가 좋지 않음.

#from keras_segmentation.models.unet import vgg_unet
from keras_segmentation.models.unet import resnet50_unet

#model = vgg_unet(n_classes=2 ,  input_height=640, input_width=640)
model = resnet50_unet(n_classes=2, input_height=640, input_width=640)

model.train(
    train_images="E:\\Study\\Pill\\SegTest\\Data\\firstDis2\\Training\\Src\\",
    train_annotations=
    "E:\\Study\\Pill\\SegTest\\Data\\firstDis2\\Training\\Mask\\",
    checkpoints_path=
    "E:\\Study\\Pill\\SegTest\\Data\\firstDis2\\Model\\Resnet50",
    epochs=5)
Esempio n. 6
0
import os, io, base64
from keras_segmentation.models.unet import resnet50_unet
from flask import Flask, request, jsonify
import numpy as np
import cv2

application = Flask(__name__)
model = resnet50_unet(n_classes=2, input_height=256, input_width=512)
model.load_weights("58.h5")


@application.route('/localOcr', methods=['POST'])
def recognition():
    imgBase64 = str(request.get_data()).replace("b'image=", "")  #直接数据的原始结构
    imgBase64 = base64.b64decode(imgBase64)
    imgBase64 = np.frombuffer(imgBase64, np.uint8)
    imgBase64 = cv2.imdecode(imgBase64, cv2.IMREAD_ANYCOLOR)
    out = model.predict_segmentation(inp=imgBase64, out_fname="3.png")
    return jsonify(out.tolist())


if __name__ == "__main__":
    application.run(host='0.0.0.0', port=80, debug=True, use_reloader=False)