Skip to content

Instantly share code, notes, and snippets.

@Steve-Tech
Last active March 4, 2022 10:42
Show Gist options
  • Save Steve-Tech/7f39ab2cfc2778263274070033a5a354 to your computer and use it in GitHub Desktop.
Save Steve-Tech/7f39ab2cfc2778263274070033a5a354 to your computer and use it in GitHub Desktop.
Python Gaugan Web API

Interact with the GauGAN2 web demo using python

Inspired by DoodleChaos who would've made something similar for this video.

Usage

Import with from gaugan import gaugan

<> = optional argument

gaugan(input png name, <output jpg name>, <caption>)

kwargs affect the request data, to enable the caption for example, (they have to be strings, booleans don't work) gaugan("segmentation.png", "gaugan_output.jpg", "Something amazing", enable_caption="true") or change style gaugan("segmentation.png", style_name="random")

I actually have no idea what most of the options do, but if you work them out post it in the comments.

import time
import base64
import requests
from random import randint
def gaugan(input:str, output:str="gaugan_output.jpg", caption:str="", **kwargs):
segmentation = b"data:image/png;base64," + base64.b64encode(open(input, "rb").read())
name = f"{time.strftime('%d/%m/%Y')},{int(time.time() * 1000)}-{randint(0, 1000000000)}"
data = {
"name": name, "masked_segmap": segmentation, "masked_edgemap": None, "masked_image": None, "caption": caption,
"style_name": "5", "enable_seg": "true", "enable_edge": "false", "enable_caption": "true" if caption else "false", "enable_image": "false", "use_model2": "false"
} | kwargs
print("Request Name:", name)
req1 = requests.post("http://ec2-54-184-13-84.us-west-2.compute.amazonaws.com:443/gaugan2_infer", data=data)
print("gaugan2_infer:", req1.status_code)
if req1.ok and req1.json()["success"] == True:
req2 = requests.post("http://ec2-54-184-13-84.us-west-2.compute.amazonaws.com:443/gaugan2_receive_output", data={"name": name})
print("gaugan2_receive_output:", req2.status_code)
open(output, "wb").write(req2.content)
print("Image saved!")
if __name__ == "__main__":
gaugan("segmentation.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment