def upload(): # style_path = request.files['style_file'] # content_path = request.files['image_file'] # best, best_loss = run_style_transfer( # content_path, style_path, num_iterations=500) # Image.fromarray(best) # im = Image.fromarray(best) # styled = im.save('styled.jpg') image_file = request.files['image_file'] model = os.path.join(os.path.abspath(''), 'rcnn_model.pkl') show_objects = load_object(image_file, model) # contour_outlines = show_selection(raw_input, filename, show_objects) storage_client = storage.Client(project='amli-245518') bucket = storage_client.get_bucket(CLOUD_STORAGE_BUCKET) destination_blob_name = 'test.jpg' blob = bucket.blob(destination_blob_name) blob.upload_from_filename(style_file.filename) print("Send file") return render_template('upload.html')
def upload(): transfer_option = request.form.get('transfer_select') if transfer_option == 'whole': style = request.files['style_file'] # style_path = request.files['style_file'] style_name = secure_filename(style.filename) style_path = os.path.join('static/style_images', style_name) style.save(style_path) # url = upload_to_gcloud(style_path) content = request.files['image_file'] content_name = secure_filename(content.filename) content_path = os.path.join('static/input_images', content_name) content.save(content_path) content_img_name = os.path.basename(content_path)[:-4] style_img_name = os.path.basename(style_path)[:-4] # content_path = request.path['image_file'] # print(style_path) # best, best_loss = run_style_transfer( # content_path, style_path, num_iterations=1) # style = "images/style_images/statue_of_liberty_sq.jpg" # content = "images/content_images/image.jpg" test = "arbitrary_image_stylization_with_weights \ --checkpoint=arbitrary_style_transfer/model.ckpt \ --output_dir=outputs \ --style_images_paths=" + style_path + "\ --content_images_paths=" + content_path + "\ --image_size=256 \ --content_square_crop=False \ --style_image_size=256 \ --style_square_crop=False \ --logtostderr" os.system(test) path = os.path.join( os.path.abspath('static/out'), '%s_stylized_%s_%d.jpg' % (content_img_name, style_img_name, 0)) # print(path) # im=Image.fromarray(best) # im.save('static/out/styled.jpg') # styled_file=os.path.join( # os.path.abspath(''), 'static/out/styled.jpg') url = upload_to_gcloud_name( path, '%s_stylized_%s_%d.jpg' % (content_img_name, style_img_name, 0)) # return render_template('upload.html') return render_template('upload.html', image_url=url) elif transfer_option == 'object': # # Upload style image first style_path = request.files['style_file'] global STYLE_URL STYLE_URL = upload_to_gcloud(style_path) content_path = request.files['image_file'] content_path_copy = request.files['image_file'] global CONTENT_URL CONTENT_URL = upload_to_gcloud(content_path_copy) # print(content_path_copy) # content_path = request.files['image_file'] # content_path_copy = content_path # print(content_path_copy) ROOT_DIR = os.path.abspath("../") MaskRCNN_DIR = os.path.abspath("../Mask_RCNN") # global CONTENT_URL # CONTENT_URL = upload_to_gcloud(content_path_copy) # sys.path.append(os.path.join(MaskRCNN_DIR, "samples/coco/")) # sys.path.append(MaskRCNN_DIR) # To find local version of the library MODEL_DIR = os.path.join(MaskRCNN_DIR, "samples/coco/") COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5") config = InferenceConfig() detection_model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) detection_model.load_weights(COCO_MODEL_PATH, by_name=True) global RESULTS global SHOW_OBJECTS # content = download_from_gcloud(os.path.basename(CONTENT_URL)) RESULTS, SHOW_OBJECTS = load_object(CONTENT_URL, detection_model) # load_object(image_file, detection_model) # config = InferenceConfig() # model = modellib.MaskRCNN( # mode="inference", model_dir=MODEL_DIR, config=config) # model.load_weights(COCO_MODEL_PATH, by_name=True) # results, show_objects = load_object(image_file) # RESULTS = results # SHOW_OBJECTS = show_objects # contour_outlines = show_selection(raw_input, filename, show_objects) url = upload_to_gcloud_name(SHOW_OBJECTS, 'all_objects.jpg') return render_template('object.html', image_url=url)
from google.cloud import storage import os from flask import request, send_file import tempfile from object_detection import load_object style_file = os.path.join(os.path.abspath(''), 'stylize.jpg') model = os.path.join(os.path.abspath(''), 'rcnn_model.pkl') show_objects = load_object(style_file, model) # style_file = request.files['stylize.jpg'] # style_file = os.path.abspath('../Flask-STWA/stylize.jpg') # print(os.path.join(os.path.abspath(''), 'stylize.jpg')) CLOUD_STORAGE_BUCKET = 'style-input-images-1' def upload(): style_file = os.path.join(os.path.abspath(''), 'stylize.jpg') CLOUD_STORAGE_BUCKET = 'style-input-images-1' storage_client = storage.Client(project='amli-245518') bucket = storage_client.get_bucket(CLOUD_STORAGE_BUCKET) destination_blob_name = 'stylize.jpg' blob = bucket.blob(destination_blob_name) blob.upload_from_filename(style_file) def download(): CLOUD_STORAGE_BUCKET = 'style-input-images-1' style_file = 'stylize.jpg' # style_file = os.path.abspath('../Flask-STWA/stylize.jpg')
def upload(): transfer_option = request.form.get('transfer_select') global STYLE_URL, CONTENT_URL global RESULTS, SHOW_OBJECTS global LOCATION global SELECTION if transfer_option == 'whole': style = request.files['style_file'] style_name = secure_filename(style.filename) style_path = os.path.join('static/style_images', style_name) style.save(style_path) content = request.files['image_file'] content_name = secure_filename(content.filename) content_path = os.path.join('static/input_images', content_name) content.save(content_path) content_img_name = os.path.basename(content_path)[:-4] style_img_name = os.path.basename(style_path)[:-4] test = "arbitrary_image_stylization_with_weights \ --checkpoint=arbitrary_style_transfer/model.ckpt \ --output_dir=static/out \ --style_images_paths=" + style_path + "\ --content_images_paths=" + content_path + "\ --image_size=512 \ --content_square_crop=False \ --style_image_size=512 \ --style_square_crop=False \ --logtostderr" os.system(test) path = 'static/out/' + ('%s_stylized_%s_0.jpg' % (content_img_name, style_img_name)) return render_template('upload.html', image_url=path) elif transfer_option == 'object': # # Upload style image first SELECTION = 'object' style = request.files['style_file'] style_name = secure_filename(style.filename) style_path = os.path.join('static/style_images', style_name) style.save(style_path) STYLE_URL = style_path content = request.files['image_file'] content_name = secure_filename(content.filename) content_path = os.path.join('static/input_images', content_name) content.save(content_path) CONTENT_URL = content_path RESULTS, SHOW_OBJECTS = load_object(CONTENT_URL, detection_model) return render_template('object.html', image_url=SHOW_OBJECTS) elif transfer_option == 'inverse': SELECTION = 'inverse' # # Upload style image first style = request.files['style_file'] style_name = secure_filename(style.filename) style_path = os.path.join('static/style_images', style_name) style.save(style_path) STYLE_URL = style_path content = request.files['image_file'] content_name = secure_filename(content.filename) content_path = os.path.join('static/input_images', content_name) content.save(content_path) CONTENT_URL = content_path RESULTS, SHOW_OBJECTS = load_object(CONTENT_URL, detection_model) return render_template('object.html', image_url=SHOW_OBJECTS)
def upload(): transfer_option = request.form.get('transfer_select') # Set global variable to access across different pages global STYLE_URL, CONTENT_URL global RESULTS, SHOW_OBJECTS global LOCATION global SELECTION # Directly transform the whole image if transfer_option == 'whole': style = request.files['style_file'] content = request.files['image_file'] STYLE_URL, CONTENT_URL = upload_style_content_images(style, content) content_img_name = os.path.basename(CONTENT_URL)[:-4] style_img_name = os.path.basename(STYLE_URL)[:-4] # Run 100% style transfer with arbitrary_image_stylization model out = "arbitrary_image_stylization_with_weights \ --checkpoint=arbitrary_style_transfer/model.ckpt \ --output_dir=static/final \ --style_images_paths=" + STYLE_URL + "\ --content_images_paths=" + CONTENT_URL + "\ --image_size=512 \ --content_square_crop=False \ --style_image_size=512 \ --style_square_crop=False \ --logtostderr" os.system(out) path = 'static/final/' + ('%s_stylized_%s_0.jpg' % (content_img_name, style_img_name)) return render_template('upload.html', image_url=path) # Transform the whole image with different weights of transfer elif transfer_option == 'adjust': style = request.files['style_file'] content = request.files['image_file'] STYLE_URL, CONTENT_URL = upload_style_content_images(style, content) content_img_name = os.path.basename(CONTENT_URL)[:-4] style_img_name = os.path.basename(STYLE_URL)[:-4] # Run different weights of style transfer from 20% to 100% INTERPOLATION_WEIGHTS = '[0.2,0.4,0.6,0.8,1.0]' output = "arbitrary_image_stylization_with_weights \ --checkpoint=arbitrary_style_transfer/model.ckpt \ --output_dir=static/final \ --style_images_paths=" + STYLE_URL + "\ --content_images_paths=" + CONTENT_URL + "\ --image_size=512 \ --content_square_crop=False \ --style_image_size=512 \ --style_square_crop=False \ --interpolation_weights=" + INTERPOLATION_WEIGHTS + "\ --logtostderr" os.system(output) changed_paths = [] for i in range(5): changed_paths.append('static/final/' + ('%s_stylized_%s_%d.jpg' % (content_img_name, style_img_name, i))) return render_template('wholeOptions.html', image_url=changed_paths) # Object Detection elif transfer_option == 'object': SELECTION = 'object' style = request.files['style_file'] content = request.files['image_file'] STYLE_URL, CONTENT_URL = upload_style_content_images(style, content) # Run Object Detection RESULTS, SHOW_OBJECTS = load_object(CONTENT_URL, detection_model) return render_template('object.html', image_url=SHOW_OBJECTS) # Inverse Object Detection elif transfer_option == 'inverse': SELECTION = 'inverse' style = request.files['style_file'] content = request.files['image_file'] STYLE_URL, CONTENT_URL = upload_style_content_images(style, content) # Run Object Detection RESULTS, SHOW_OBJECTS = load_object(CONTENT_URL, detection_model) return render_template('object.html', image_url=SHOW_OBJECTS)