You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi there, I am trying to get your PremTools work in my mobile application made in Unity, therefore all codes are written in C#. But I have no idea what should I do about required response parameter called "Mask". I firstly generated an image based of a prompt and then I am trying to remove background of that image. Generating image itself works, so I am stuck at removing the background part, which is why I am here. This is how my code looks like if it will help:
// Set the model parameters
yield return sdc.SetModelAsync(modelsList[selectedModel]);
// Generate the image
HttpWebRequest httpWebRequest = null;
try
{
// Make a HTTP POST request to the Stable Diffusion server
httpWebRequest = (HttpWebRequest)WebRequest.Create(sdc.settings.StableDiffusionServerURL + sdc.settings.PbremPredictAPI);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
// Send the generation parameters along with the POST request
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
byte[] inputImgBytes = texture2D.EncodeToPNG();
string inputImgString = Convert.ToBase64String(inputImgBytes);
SDPremPredict sd = new SDPremPredict();
sd.img = inputImgString;
// Serialize the input parameters
string json = JsonConvert.SerializeObject(sd);
// Send to the server
streamWriter.Write(json);
}
}
catch (Exception e)
{
Debug.LogError(e.Message + "\n\n" + e.StackTrace);
}
// Read the output of generation
if (httpWebRequest != null)
{
// Wait that the generation is complete before procedding
Task<WebResponse> webResponse = httpWebRequest.GetResponseAsync();
while (!webResponse.IsCompleted)
{
if (sdc.settings.useAuth && !sdc.settings.user.Equals("") && !sdc.settings.pass.Equals(""))
UpdateGenerationProgressWithAuth();
else
UpdateGenerationProgress();
yield return new WaitForSeconds(0.5f);
}
// Stream the result from the server
var httpResponse = webResponse.Result;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
// Decode the response as a JSON string
string result = streamReader.ReadToEnd();
// Deserialize the JSON string into a data structure
SDResponsePremPredict json = JsonConvert.DeserializeObject<SDResponsePremPredict>(result);
// If no image, there was probably an error so abort
if (json.img == null || json.img.Length == 0)
{
generating = false;
yield break;
}
// Decode the image from Base64 string into an array of bytes
byte[] imageData = Convert.FromBase64String(json.img);
// Write it in the specified project output folder
using (FileStream imageFile = new FileStream(filename, FileMode.Create))
{
yield return imageFile.WriteAsync(imageData, 0, imageData.Length);
NativeGallery.SaveImageToGallery(filename, Application.productName + " Captures", name);
AssetDatabase.StopAssetEditing();
AssetDatabase.SaveAssets();
}
try
{
// Read back the image into a texture
if (File.Exists(filename))
{
texture = new Texture2D(2, 2);
texture.LoadImage(imageData);
texture.Apply();
LoadIntoMaterial(texture);
generatingFinished = true;
}
}
catch (Exception e)
{
Debug.LogError(e.Message + "\n\n" + e.StackTrace);
}
}
}
generating = false;
yield return null;
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there, I am trying to get your PremTools work in my mobile application made in Unity, therefore all codes are written in C#. But I have no idea what should I do about required response parameter called "Mask". I firstly generated an image based of a prompt and then I am trying to remove background of that image. Generating image itself works, so I am stuck at removing the background part, which is why I am here. This is how my code looks like if it will help:
IEnumerator GenerateTransparentAsync(Texture2D texture2D)
{
Beta Was this translation helpful? Give feedback.
All reactions