예제 #1
0
def update_data(string):
    if string:
        mask = parse_jsonstring(string,
                                io.imread(filename, as_gray=True).shape)
    else:
        raise PreventUpdate
    return array_to_data_url((255 * mask).astype(np.uint8))
예제 #2
0
def maskFromData(string, data, mask_filepath, image_width, image_height,
                 canvas_width, rect_fill):
    """
        Parse the json object file and get the mask from it, then return it
        Parameters :
            string: data in string format 
            data: object that holds the canvas variables
            mask_filepath: file path to save the mask to
            image_width: width of the image
            image_height: height of the image
            canvas_width: width of the canvas
            rect_fill: boolean denoting whether or not to fill rectangle objects in mask
    """

    mask = np.zeros((image_height, image_width))

    # get rectangular mask objects and fill them in the mask,
    # otherwise if parsed it's just the outline
    if 'fill' in rect_fill:
        for object in data['objects']:
            if object["type"] == "rect":

                left = int(object["left"] * image_width / canvas_width)
                top = int(object["top"] * image_width / canvas_width)
                width = int(object["width"] * image_width / canvas_width)
                height = int(object["height"] * image_width / canvas_width)

                rect_mask = getMask(image_width, image_height, left, top,
                                    width, height)
                mask[rect_mask == 0] = 1

    # parse the rest of the mask objects
    mask[parse_jsonstring(string, (image_height, image_width)) == 1] = 1

    plt.imsave(os.getcwd() + mask_filepath, mask, cmap=cm.gray)
예제 #3
0
def remove_background(json_data, image):
    if json_data:
        # ➊ imageが値をもつ場合、それを基に画像のnumpy.ndarrayに変換する
        if image:
            image_array = image_string_to_PILImage(image)
            image_array = np.asarray(image_array)
        # imageが値をもたない場合、imreadで画像を読み込む
        else:
            image_array = io.imread(image_path)
        # ➋ 画像のアレイのサイズを変数shapeに代入する
        shape = image_array.shape[:2]

        # ➌ 書き込みのJSONデータをパースし、ブール値に変換する
        try:
            mask = parse_jsonstring(json_data, shape=shape)
        except IndexError:
            raise PreventUpdate

        if mask.sum() > 0:  # ➍
            seg = superpixel_color_segmentation(image_array, mask)
        else:
            seg = np.ones(shape)
        filled_image = np.copy(image_array)
        filled_image[np.logical_not(seg)] = np.array([255, 255, 255],
                                                     dtype="uint8")  # ➎
        return array_to_data_url(filled_image)  # ➏

    else:
        PreventUpdate
예제 #4
0
def update_segmentation(toggle, string, s, h, w, children, mode):
    if len(children) == 0:
        labs = labels
    else:
        labs = np.asarray(children)
    mask = parse_jsonstring(string, shape=(height, width))
    new_labels = modify_segmentation(labs, mask, img=img, mode=mode)
    return new_labels
예제 #5
0
def segmentation(string):
    if string:
        mask = parse_jsonstring(string, img.shape)
        seg = watershed_segmentation(img, mask)
        src = color.label2rgb(seg, image=img)
    else:
        raise PreventUpdate
    return array_to_data_url(img_as_ubyte(src))
예제 #6
0
def segmentation(string):
    if string:
        mask = parse_jsonstring(string,
                                io.imread(filename, as_gray=True).shape)
        seg = watershed_segmentation(io.imread(filename, as_gray=True), mask)
        src = color.label2rgb(seg, image=io.imread(filename, as_gray=True))
    else:
        raise PreventUpdate
    return array_to_data_url(img_as_ubyte(src))
예제 #7
0
파일: app.py 프로젝트: TedHaley/DAMNIST
def update_data(string):
    pprint(string)

    if string:
        mask = parse_jsonstring(string, (canvas_width, canvas_height))
    else:
        raise PreventUpdate

    return array_to_data_url((255 * mask).astype(np.uint8))
def segmentation(string, content, NDVIcontent):
    global ROIRGB, ROINDVI
    if string:
        data = content.encode("utf8").split(b";base64,")[1]
        NDVIdata = NDVIcontent.encode("utf8").split(b";base64,")[1]
        img = io.BytesIO()
        imgNDVI = io.BytesIO()
        img.write(base64.b64decode(data))
        imgNDVI.write(base64.b64decode(NDVIdata))
        img.seek(0)
        imgNDVI.seek(0)
        i = np.asarray(bytearray(img.read()), dtype=np.uint8)
        i = cv2.imdecode(i, cv2.IMREAD_COLOR)
        iNDVI = np.asarray(bytearray(imgNDVI.read()), dtype=np.uint8)
        iNDVI = cv2.imdecode(iNDVI, cv2.IMREAD_COLOR)
        mask = parse_jsonstring(string, (i.shape[0],i.shape[1]))
        ret,thresh = cv2.threshold(np.array(mask, dtype=np.uint8), 0, 255, cv2.THRESH_BINARY)
        im_floodfill = thresh.copy()
        h, w = thresh.shape[:2]
        m = np.zeros((h+2, w+2), np.uint8)
        cv2.floodFill(im_floodfill, m, (0,0), 255)
        im_floodfill_inv = cv2.bitwise_not(im_floodfill)
        im_out = thresh | im_floodfill_inv
        RGBimg = cv2.bitwise_and(i, i, mask=im_out)
        RGBimg = cv2.cvtColor(RGBimg, cv2.COLOR_BGR2RGB)
        target_size = (RGBimg.shape[1],(RGBimg.shape[0]))
        iNDVI = cv2.resize(iNDVI, target_size)
        NDVIimg = cv2.bitwise_and(iNDVI, iNDVI, mask=im_out)
        NDVIimg = cv2.cvtColor(NDVIimg, cv2.COLOR_BGR2RGB)

        contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
        cnts = sorted(contours, key=lambda c: cv2.contourArea(c), reverse=True)  # finds the largest selection, which is a limitation to multiple selection (will be changed in the future versions)
        (x,y,w,h) = cv2.boundingRect(cnts[0])
        ROIRGB = RGBimg[y:y+h, x:x+w]
        ROINDVI = NDVIimg[y:y+h, x:x+w]
        cv2.imwrite(static_image_route+'RGB_cropped.png', cv2.cvtColor(ROIRGB, cv2.COLOR_RGB2BGR ))
        cv2.imwrite(static_image_route+'CIR_cropped.png', cv2.cvtColor(ROINDVI, cv2.COLOR_RGB2BGR ))
        with ZipFile(static_image_route+'cropped.zip', 'w') as zipObj2:
            zipObj2.write(static_image_route+'RGB_cropped.png')
            zipObj2.write(static_image_route+'CIR_cropped.png')
            location = os.path.join(static_image_route,'cropped.zip')

    else:
        raise PreventUpdate
    return array_to_data_url(img_as_ubyte(ROIRGB)), array_to_data_url(img_as_ubyte(ROINDVI)), html.A(html.Button('Download',style={'display':'block'
                                    ,'position':'relative',
                                    'top': '45%','left': '45%',
                                    'font-size': '16px',
                                    'padding': '8px 12px',
                                    'border-radius': '4px',
                                    'text-align': 'center',
                                    'align':'center',
                                    'color':'black',
                                    'font-family':'Times New Roman',
                                    'textAlign':'center'}), href=location) #, html.Div([html.Button('Save Image', id='saveImg')])
예제 #9
0
파일: app.py 프로젝트: rzhao97/captcha
def show_draw(string):
    if not string:
        raise PreventUpdate

    # Convert image string to displayable image
    mask = parse_jsonstring(string, shape)
    mask = (255 * mask).astype(np.uint8)
    mask = cv2.resize(mask, (400, 100), interpolation=cv2.INTER_AREA)
    mask = cv2.threshold(mask, 0, 255,
                         cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

    captcha_img = array_to_data_url(mask)

    # Show image and save image string
    return captcha_img, string
예제 #10
0
def classify(alteration, image):

    prevent_update(image)

    mask = parse_jsonstring(alteration)
    with open('bug.txt', 'w') as f:
        f.write(str(mask))
    image2 = array_to_data_url((255 * mask).astype(np.uint8))

    if alteration:
        return str(
            google_classify(
                combine(convert_base46(image), convert_base46(image2))))

    return str(google_classify(convert_base46(image)))
예제 #11
0
def update_figure_upload(string, image, algorithm):
    print("update figure")
    if string:
        if image is None:
            im = img
            image = img
        else:
            im = image_string_to_PILImage(image)
            im = np.asarray(im)
        shape = im.shape[:2]
        mask = parse_jsonstring(string, shape=shape)
        if mask.sum() > 0:
            seg = segmentation_generic(im, mask, mode=algorithm)
        else:
            seg = np.zeros(shape)
        return image_with_contour(im, seg, shape=shape)
    else:
        raise PreventUpdate
예제 #12
0
def update_figure_upload(string, image):
    if string:
        if image is None:
            im = img_app3
        else:
            im = image_string_to_PILImage(image)
            im = np.asarray(im)
        shape = im.shape[:2]
        try:
            mask = parse_jsonstring(string, shape=shape)
        except IndexError:
            raise PreventUpdate
        if mask.sum() > 0:
            seg = superpixel_color_segmentation(im, mask)
        else:
            seg = np.ones(shape)
        fill_value = 255 * np.ones(3, dtype=np.uint8)
        dat = np.copy(im)
        dat[np.logical_not(seg)] = fill_value
        return array_to_data_url(dat)
    else:
        raise PreventUpdate
예제 #13
0
def update_data(string):
    if string:

        try:
            mask = parse_jsonstring(string,
                                    shape=(canvas_height, canvas_width))
        except:
            return "Out of Bounding Box, click clear button and try again"
        # np.savetxt('data.csv', mask) use this to save the canvas annotations as a numpy array
        # Invert True and False
        mask = (~mask.astype(bool)).astype(int)

        image_string = array_to_data_url((255 * mask).astype(np.uint8))

        # this is from canvas.utils.image_string_to_PILImage(image_string)
        img = Image.open(BytesIO(base64.b64decode(image_string[22:])))

        text = "{}".format(
            pytesseract.image_to_string(img, lang="eng", config="--psm 6"))
        return text
    else:
        raise PreventUpdate
def segmentation(string, content, NDVIcontent):
    if string:
        data = content.encode("utf8").split(b";base64,")[1]
        NDVIdata = NDVIcontent.encode("utf8").split(b";base64,")[1]
        img = io.BytesIO()
        imgNDVI = io.BytesIO()
        img.write(base64.b64decode(data))
        imgNDVI.write(base64.b64decode(NDVIdata))
        img.seek(0)
        imgNDVI.seek(0)
        i = np.asarray(bytearray(img.read()), dtype=np.uint8)
        i = cv2.imdecode(i, cv2.IMREAD_COLOR)
        iNDVI = np.asarray(bytearray(imgNDVI.read()), dtype=np.uint8)
        iNDVI = cv2.imdecode(iNDVI, cv2.IMREAD_COLOR)
        mask = parse_jsonstring(string, (i.shape[0],i.shape[1]))
        ret,thresh = cv2.threshold(np.array(mask, dtype=np.uint8), 0, 255, cv2.THRESH_BINARY)
        im_floodfill = thresh.copy()
        h, w = thresh.shape[:2]
        m = np.zeros((h+2, w+2), np.uint8)
        cv2.floodFill(im_floodfill, m, (0,0), 255)
        im_floodfill_inv = cv2.bitwise_not(im_floodfill)
        im_out = thresh | im_floodfill_inv
        RGBimg = cv2.bitwise_and(i, i, mask=im_out)
        RGBimg = cv2.cvtColor(RGBimg, cv2.COLOR_BGR2RGB)
        target_size = (RGBimg.shape[1],(RGBimg.shape[0]))
        iNDVI = cv2.resize(iNDVI, target_size)
        NDVIimg = cv2.bitwise_and(iNDVI, iNDVI, mask=im_out)
        NDVIimg = cv2.cvtColor(NDVIimg, cv2.COLOR_BGR2RGB)

        contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
        cnts = sorted(contours, key=lambda c: cv2.contourArea(c), reverse=True)  # finds the largest selection, which is a limitation to multiple selection (will be changed in the future versions)
        (x,y,w,h) = cv2.boundingRect(cnts[0])
        ROIRGB = RGBimg[y:y+h, x:x+w]
        ROINDVI = NDVIimg[y:y+h, x:x+w]
    else:
        raise PreventUpdate
    return array_to_data_url(img_as_ubyte(ROIRGB)), array_to_data_url(img_as_ubyte(ROINDVI)) 
예제 #15
0
def update_data(string):
    if string:
        mask = parse_jsonstring(string, shape)
    else:
        raise PreventUpdate
    return array_to_data_url((255 * mask).astype(np.uint8))
예제 #16
0
파일: receiver.py 프로젝트: rzhao97/captcha
from dash_canvas.utils import array_to_data_url, parse_jsonstring

#from src.predict import *

shape = (200, 800)
chars = '0123456789' + string.ascii_lowercase

model = keras.models.load_model('models/4cnn_draw_model.h5')

print('receiving string with length:', len(sys.argv[1]))

# Get image string from app.py
string = sys.argv[1]

# Convert image string to numpy array
mask = parse_jsonstring(string, shape)
img = (255 * mask).astype(np.uint8)
img = cv2.resize(img, (200, 50), interpolation=cv2.INTER_AREA)

# Use model to predict image
img = img / 255.0
onehotpred = np.array(model.predict(img.reshape(1, 50, 200, 1))).reshape(5, 36)
pred = ''

# Find prediction for each character output
for i in onehotpred:
    c = chars[np.argmax(i)]
    pred += c

# Return prediction on app.py
print(pred)