def update_uploaded_image(list_of_contents, n_clicks, filename, style, style_weight, storage, session_id): print("update_uploaded_image") storage = json.loads(storage) # if an image was loaded if list_of_contents is not None: # if the callback was triggered by the upload-image module if "upload-image" in dash.callback_context.triggered[0].get('prop_id'): print('list of contents') storage["filename"] = filename string = list_of_contents.split(";base64,")[-1] storage["image_signature"] = string[0:200] storage["image_string"] = list_of_contents return [ html.Img(id="image", src=list_of_contents), html.Div(id="div-storage", children=json.dumps(storage), style={"display": "none"}) ] else: print('not list of contents') if style is not None: print('style') # CREATE the new image with selected style string = list_of_contents.split(";base64,")[-1] image = drc.b64_to_pil(string) image_path = f'images/input/{filename}' image.save(image_path) print('image') stylized_image = stylize_image(image_path, style, style_weight) print('stylized_image') b64_stylized_image = drc.pil_to_b64(Image.open(stylized_image)) print('b64_stylized_image') # CALL MODEL return [ html.Img( id="image", src=drc.HTML_IMG_SRC_PARAMETERS + drc.pil_to_b64(Image.open('test_save_image.png'))), html.Div(id="div-storage", children=json.dumps(storage), style={"display": "none"}), html.Div( id="div-style-text", children=[ f'Style : {style} with weight {style_weight} applied' ]) ] else: return [ html.Img(id="image", src=drc.HTML_IMG_SRC_PARAMETERS + drc.pil_to_b64(Image.open('test_save_image.png'))), html.Div(id="div-storage", children=json.dumps(storage), style={"display": "none"}) ]
def update_image(contents, input_1): if contents != None: string = input_1.split(';base64,')[-1] im_pil = drc.b64_to_pil(string) im_pil = im_pil.resize((128, 128), Image.ANTIALIAS) if im_pil.mode != "RGB": im_pil = im_pil.convert("RGB") # Convert image to numpy array img_array = np.asarray(im_pil) img_array = (img_array - 127.5) / 127.5 img2 = img_array #img2 = np.zeros((img_array.shape[0],img_array.shape[1],3)) #img2[:,:,0] = img_array #img2[:,:,1] = img_array #img2[:,:,2] = img_array # Create payload payload = {'instances': [img2.tolist()]} # Predict image #res = requests.post('http://localhost:8080/v1/models/color_sketch_model:predict', json=payload) res = requests.post( 'http://color-sketch_model-development_1:8080/v1/models/color_sketch_model:predict', json=payload) res = res.json() res = res['predictions'][0] res = ((np.array(res) + 1) / 2.0) * 255.0 # Encode prediction pred_pil = Image.fromarray(np.uint8(res)) pred_pil = pred_pil.resize((300, 300), Image.ANTIALIAS) enc_pred = drc.pil_to_b64(pred_pil) return 'data:image/png;base64, ' + enc_pred
def update_image(content, input_1): if content != None: string = input_1.split(';base64,')[-1] im_pil = drc.b64_to_pil(string) im_pil = im_pil.resize((300, 300), Image.ANTIALIAS) enc_img = drc.pil_to_b64(im_pil) return 'data:image/png;base64, ' + enc_img
import dash_html_components as html import json import plotly.graph_objs as go import dash_reusable_components as drc from PIL import Image, ImageFilter, ImageDraw, ImageEnhance BUCKET_NAME = 'bucketeer-dash-image-processing' # [filename, image_signature, action_stack] STORAGE_PLACEHOLDER = json.dumps({ 'filename': None, 'image_signature': None, 'action_stack': [] }) IMAGE_STRING_PLACEHOLDER = drc.pil_to_b64( Image.open('images/default.jpg').copy(), enc_format='jpeg') GRAPH_PLACEHOLDER = dcc.Graph(id='interactive-image', style={'height': '80vh'}) # Maps process name to the Image filter corresponding to that process FILTERS_DICT = { 'blur': ImageFilter.BLUR, 'contour': ImageFilter.CONTOUR, 'detail': ImageFilter.DETAIL, 'edge_enhance': ImageFilter.EDGE_ENHANCE, 'edge_enhance_more': ImageFilter.EDGE_ENHANCE_MORE, 'emboss': ImageFilter.EMBOSS, 'find_edges': ImageFilter.FIND_EDGES, 'sharpen': ImageFilter.SHARPEN, 'smooth': ImageFilter.SMOOTH, 'smooth_more': ImageFilter.SMOOTH_MORE
# drc = importlib.import_module("apps.dash-iamge-processing.dash_reusable_components") # APP_PATH = str(pathlib.Path(__file__).parent.resolve()) # [filename, image_signature, action_stack] STORAGE_PLACEHOLDER = json.dumps({ "filename": None, "image_signature": None, "action_stack": [] }) IMAGE_STRING_PLACEHOLDER = drc.pil_to_b64( Image.open(os.path.join(APP_PATH, os.path.join("images", "default.jpg"))).copy(), enc_format="jpeg", ) GRAPH_PLACEHOLDER = dcc.Graph( id="interactive-image", figure={ "data": [], "layout": { "autosize": True, "paper_bgcolor": "#272a31", "plot_bgcolor": "#272a31", "margin":
from PIL import Image, ImageFilter, ImageDraw, ImageEnhance # APP_PATH = str(pathlib.Path(__file__).parent.resolve()) # [filename, image_signature, action_stack] STORAGE_PLACEHOLDER = json.dumps({ "filename": None, "image_signature": None, "action_stack": [] }) IMAGE_STRING_PLACEHOLDER = drc.pil_to_b64( Image.open( os.path.join(APP_PATH, os.path.join("input/test/autistic", "001.jpg"))).copy(), enc_format="jpeg", ) GRAPH_PLACEHOLDER = dcc.Graph( id="interactive-image", figure={ "data": [], "layout": { "autosize": True, "paper_bgcolor": "#272a31", "plot_bgcolor": "#272a31", "margin":