def process(): input_path = generate_random_filename(upload_directory, "jpg") output_path = generate_random_filename(result_directory, "jpg") try: url = request.json["url"] download(url, input_path) with tf.Session(config=tf.ConfigProto( allow_soft_placement=True)) as sess: gan = UGATIT(sess, args) gan.build_model() gan.test_endpoint_init() gan.test_endpoint(input_path, output_path) callback = send_file(output_path, mimetype='image/jpeg') return callback, 200 except: traceback.print_exc() return {'message': 'input error'}, 400 finally: clean_all([input_path, output_path])
def index(): # set session for image results if "file_urls" not in session: session['file_urls'] = [] # list to hold our uploaded image urls file_urls = session['file_urls'] if request.method == 'POST': file_obj = request.files for f in file_obj: file = request.files.get(f) # convert string of image data to uint8 nparr = np.fromfile(file, np.uint8) # decode image img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) # parse arguments args = parse_args() if args is None: exit() # open session with tf.Session(config=tf.ConfigProto( allow_soft_placement=True)) as sess: gan = UGATIT(sess, args) # build graph gan.build_model() # show network architecture show_all_variables() # do some fancy processing here.... fake_img = gan.test_endpoint(img) # save the file with to our photos folder filename = str(uuid.uuid1()) + '.png' cv2.imwrite('uploads/' + filename, fake_img) # append image urls file_urls.append(photos.url(filename)) session['file_urls'] = file_urls return "uploading..." return render_template('index.html')
def runner(args): # open session with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess: gan = UGATIT(sess, args) # build graph gan.build_model() # show network architecture show_all_variables() gan.test_endpoint_init() while True: time.sleep(5) messages = get_messages_from_queue() total_msg = len(messages) print("[INFO] Retrieved " + str(total_msg) + " messages") # email service email = EmailService() for message in messages: total_msg = total_msg - 1 print("[INFO] " + str(total_msg) + " messages remain in queue") try: try: body = json.loads(message['Body']) print(body) bucket = body['bucket_name'] bucket_key = body['bucket_key'] bucket_cropped_key = body.get('bucket_cropped_key', None) file_name = body['file_name'] email_addr = body['email'] crop = body['crop'] # Crop params x = crop['x'] y = crop['y'] width = crop['width'] height = crop['height'] except Exception as e: print() print("ERROR: Parsing message") print(e) raise e if bucket_cropped_key is None: try: image = download_image(bucket, bucket_key) except Exception as e: print("ERROR: Downloading Image") print(e) raise e try: crop_img = image[y:y + height, x:x + width] # Change color space crop_img = cv2.cvtColor(crop_img, cv2.COLOR_RGB2BGR) except Exception as e: print("ERROR: Cropping Image") print(e) raise e try: # Resize image crop_img = cv2.resize(crop_img, dsize=(256, 256)) except Exception as e: print("ERROR: Resizing Image") print(e) raise e else: try: crop_img = download_image(bucket, bucket_cropped_key) crop_img = cv2.cvtColor(crop_img, cv2.COLOR_RGB2BGR) except Exception as e: print("ERROR: Downloading Cropped Image") print(e) raise e try: # do some fancy processing here.... fake_img = gan.test_endpoint(crop_img) except Exception as e: print("ERROR: Prosccing image with GAN") print(e) raise e try: # Upload to S3 image_url = upload_image(fake_img, file_name) except Exception as e: print("ERROR: Uploading image to S3") print(e) raise e try: # Send Email email.send_email(email_addr, image_url) except Exception as e: print("ERROR: Failed to send email") print(e) raise e except Exception as e: print('FATAL ERROR') print(e)