Skip to content

Commit

Permalink
1.16
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxmffg committed Nov 23, 2023
1 parent 0bd654c commit 052040f
Show file tree
Hide file tree
Showing 11 changed files with 479 additions and 290 deletions.
166 changes: 95 additions & 71 deletions Redream/Form1.Designer.cs

Large diffs are not rendered by default.

251 changes: 174 additions & 77 deletions Redream/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@
using static Redream.MyFunctions;
using static Redream.API;
using System.Collections.Specialized;
using Emgu.CV.Dai;
using Emgu.CV.OCR;

namespace Redream
{

public enum DeviceType
{
Screenshot,
Webcam
}

public partial class Form1 : Form
{
public string version = "1.16";

List<Button> buttonList = new List<Button>();
List<string> favPromptList = new List<string>();
List<string> favNPromptList = new List<string>();
Expand All @@ -46,6 +54,7 @@ public partial class Form1 : Form
public Form1()
{
InitializeComponent();
this.Text = "Fictiverse : Redream " + version;
Directory.CreateDirectory(FramesPath);

buttonList.Add(button1);
Expand Down Expand Up @@ -219,9 +228,11 @@ private void ToggleScreenshot()

private async void buttonStart_Click(object sender, EventArgs e)
{

if (formSC != null && !formSC.IsDisposed)
{
isStarted = !isStarted;
panelPlayer.Enabled = !isStarted;
ClearMemory();
TogglePlayer(false);
SaveSettings();
Expand All @@ -231,19 +242,13 @@ private async void buttonStart_Click(object sender, EventArgs e)
{
frames.Clear();
//timerFadeOut.Enabled = true;

last64Image = null;
buttonStart.BackColor = Color.FromArgb(25, 85, 35);
buttonStart.Image = Resources.pause_button;

while (isStarted && formSC != null)
{
Bitmap bmp = formSC.TakeScreeenShot();
CurrentCapture = bmp;

string module = control_Settings.SelectedControlNetPreprossessor;
string model = control_Settings.SelectedControlNetModel;

await AutomaticAsync(bmp, module, model);
await AutomaticAsync();
}
}
else
Expand All @@ -267,29 +272,48 @@ public async void Stop()
UpdatePlayer();
}


private async Task AutomaticAsync(Image initimage, string module, string model)
string last64Image = null;
private async Task AutomaticAsync()
{
string script = "http://" + control_Settings.textBoxIP.Text + "/sdapi/v1/img2img";
Image initimage = null;

if (control_Settings.CaptureDevice == DeviceType.Webcam)
initimage = await formSC.CaptureWebcam();
else
initimage = await formSC.TakeScreeenShot();

Bitmap msk = formSC.Mask;
CurrentCapture = (Bitmap)initimage;

Size size = formSC.Size;
if (isNormSize)
if (initimage != null)
{
size = NormalizeSize(size, control_Settings.FitSize);
string script = "http://" + control_Settings.textBoxIP.Text + "/sdapi/v1/img2img";


Bitmap msk = formSC.Mask;

Size size = initimage.Size;
size.Width = size.Width / 8 * 8;
size.Height = size.Height / 8 * 8;


if (isNormSize)
size = NormalizeSize(size, control_Settings.FitSize);

initimage = ResizeImage((Bitmap)initimage.Clone(), size);
msk = ResizeImage((Bitmap)msk.Clone(), size);
}
string base64Image = "data:image/png;base64," + Convert.ToBase64String(ImageToBytes(initimage));
List<string> init_images = new List<string> { base64Image };

string base64Mask = "data:image/png;base64," + Convert.ToBase64String(ImageToBytes(msk));
string base64Image = "data:image/png;base64," + Convert.ToBase64String(ImageToBytes(initimage));

if (last64Image == null)
last64Image = base64Image;

List<string> init_images = new List<string> { base64Image };

int inpainting_fill = strength >= 0.98 ? 3 : 1;
string base64Mask = "data:image/png;base64," + Convert.ToBase64String(ImageToBytes(msk));

var requestBody = new Dictionary<string, object>
int inpainting_fill = strength >= 0.98 ? 3 : 1;

var requestBody = new Dictionary<string, object>
{
{ "init_images", init_images },
{ "mask", base64Mask },
Expand All @@ -308,9 +332,12 @@ private async Task AutomaticAsync(Image initimage, string module, string model)
{ "sampler_index", samplers[samplerIndex] }
};

if (module.ToLower() != "none" || model.ToLower() != "none")
{
var alwaysonScripts = new Dictionary<string, object>
string module = control_Settings.SelectedControlNetPreprossessor;
string model = control_Settings.SelectedControlNetModel;

if (module.ToLower() != "none" || model.ToLower() != "none")
{
var alwaysonScripts = new Dictionary<string, object>
{
{
"controlnet", new Dictionary<string, object>
Expand All @@ -320,59 +347,74 @@ private async Task AutomaticAsync(Image initimage, string module, string model)
{
new Dictionary<string, object>
{
{ "image", init_images[0] },
{ "module", module },
{ "model", model },
{ "control_mode", "ControlNet is more important" }
{ "weight", 0.7f }, // Set weight as float (0.7f)
{ "guidance", 1.0f }, // Set guidance as float (1.0f)
{ "processor_res", 512 },
},
/*
new Dictionary<string, object>
{
{ "image", last64Image },
{ "module", "none" },
{ "model", "diff_control_sd15_temporalnet_fp16 [adc6bd97]" },
{ "weight", 0.7f }, // Set weight as float (0.7f)
{ "guidance", 1.0f } // Set guidance as float (1.0f)
}
*/
}
}
}
}
};
requestBody.Add("alwayson_scripts", alwaysonScripts);
}
requestBody.Add("alwayson_scripts", alwaysonScripts);
}

try
{
string json = JsonConvert.SerializeObject(requestBody);
var client = new HttpClient();
var content = new StringContent(json, Encoding.UTF8, "application/json");
var request = new HttpRequestMessage
try
{
RequestUri = new Uri(script),
Method = HttpMethod.Post,
Content = content
};
var response = await client.SendAsync(request);
dynamic AutomaticResponse = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());

string imgStr = AutomaticResponse.images[0].ToString();
var imgB64 = Convert.FromBase64String(imgStr);
Bitmap bmp;
using (var ms = new MemoryStream(imgB64))
bmp = new Bitmap(Bitmap.FromStream(ms));

ProcessResult(bmp);
}
catch (HttpRequestException ex)
{
Stop();
MessageBox.Show("HttpRequestException occurred: " + ex.Message);
API_failRoutine();
// Handle the exception caused by network or HTTP-related issues
}
catch (UriFormatException ex)
{
Stop();
MessageBox.Show("UriFormatException occurred: " + ex.Message);
API_failRoutine();
// Handle the exception caused by an invalid URL format
}
catch (Exception ex)
{
Stop();
//MessageBox.Show("Other exception occurred: " + ex.Message);
// Handle other types of exceptions
string json = JsonConvert.SerializeObject(requestBody);
var client = new HttpClient();
var content = new StringContent(json, Encoding.UTF8, "application/json");
var request = new HttpRequestMessage
{
RequestUri = new Uri(script),
Method = HttpMethod.Post,
Content = content
};
var response = await client.SendAsync(request);
dynamic AutomaticResponse = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());

string imgStr = AutomaticResponse.images[0].ToString();
var imgB64 = Convert.FromBase64String(imgStr);
Bitmap bmp;
using (var ms = new MemoryStream(imgB64))
bmp = new Bitmap(Bitmap.FromStream(ms));

last64Image = base64Image;
ProcessResult(bmp);
}
catch (HttpRequestException ex)
{
Stop();
MessageBox.Show("HttpRequestException occurred: " + ex.Message);
API_failRoutine();
// Handle the exception caused by network or HTTP-related issues
}
catch (UriFormatException ex)
{
Stop();
MessageBox.Show("UriFormatException occurred: " + ex.Message);
API_failRoutine();
// Handle the exception caused by an invalid URL format
}
catch (Exception ex)
{
Stop();
//MessageBox.Show("Other exception occurred: " + ex.Message);
// Handle other types of exceptions
}
}
}

Expand Down Expand Up @@ -716,9 +758,9 @@ private void buttonCFGScale_Click(object sender, EventArgs e)
private void buttonCFGScale_MouseWheel(object? sender, MouseEventArgs e)
{
if (e.Delta > 0)
SwitchCFGScale(0.5f);
SwitchCFGScale(0.1f);
else if (e.Delta < 0)
SwitchCFGScale(-0.5f);
SwitchCFGScale(-0.1f);
}

public void SwitchCFGScale(float value, bool loop = false)
Expand Down Expand Up @@ -797,7 +839,7 @@ private async void buttonInterrogate_Click(object sender, EventArgs e)
string[] samplers = new string[] {
"Euler a",
"Euler",
"LMS",
"LCM",
"Heun",
"DPM2",
"DPM2 a",
Expand All @@ -813,8 +855,6 @@ private async void buttonInterrogate_Click(object sender, EventArgs e)
"DPM++ 2M Karras",
"DPM++ SDE Karras",
"DDIM",
//"PLMS",
//"UniPC"
};

private void buttonSampler_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -880,7 +920,7 @@ private void buttonClearPromptN_Click(object sender, EventArgs e)


bool isMaskEnabled = true;
private void buttonDefaultSettings_Click(object sender, EventArgs e)
private void buttonMask_Click(object sender, EventArgs e)
{
SwitchMaskButton(!isMaskEnabled);
}
Expand All @@ -889,9 +929,9 @@ private void SwitchMaskButton(bool value)
isMaskEnabled = value;

if (isMaskEnabled)
buttonDefaultSettings.BackColor = Color.FromArgb(25, 85, 35);
buttonMask.BackColor = Color.FromArgb(25, 85, 35);
else
buttonDefaultSettings.BackColor = Color.FromArgb(85, 35, 25);
buttonMask.BackColor = Color.FromArgb(85, 35, 25);

if (formSC != null)
formSC.IsMaskEnabled = isMaskEnabled;
Expand Down Expand Up @@ -985,6 +1025,8 @@ private void pictureBox1_Click(object sender, EventArgs e)





bool isPlaying;
int currentFrame = 0;
private void buttonPlay_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -1041,9 +1083,64 @@ private void buttonDelete_Click(object sender, EventArgs e)
}
}

private void buttonSaveFrames_Click(object sender, EventArgs e)
private async void buttonSaveFrames_Click(object sender, EventArgs e)
{
buttonSaveFrames.Image = Resources.hourglass24;
if (frames.Count > 0)
{
// Create and configure the FolderBrowserDialog
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
folderBrowserDialog.Description = "Select Saving Path";

// Show the dialog and check if the user selected a folder
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
string savingPath = folderBrowserDialog.SelectedPath;
string folderName = "Redream_" + DateTime.Now.ToFileTime().ToString().Substring(0, 12);
// Create the folder if it doesn't exist
string folderPath = Path.Combine(savingPath, folderName);
Directory.CreateDirectory(folderPath);

await SaveFrames(folderPath);

Process.Start("explorer.exe", folderPath);
}
}
buttonSaveFrames.Image = Resources.save1;
}

private Task SaveFrames(string path)
{
for (int i = 0; i < frames.Count; i++)
{
string fileName = (i + 1).ToString("0000") + ".jpg";
string filePath = Path.Combine(path, fileName);
frames[i].Save(filePath);
}
return Task.CompletedTask;
}

private void buttonBrowse_Click(object sender, EventArgs e)
{
Process.Start("explorer.exe", FramesPath);
}

bool isWebcamEnabled;
public void ToggleDevice(DeviceType value)
{
//isWebcamEnabled = value;
if (value == DeviceType.Screenshot)
{
buttonScreenshot.Image = Resources.selection;
}
else
{
buttonScreenshot.Image = Resources.webcam;
SwitchMaskButton(true);
}

}


}
}
2 changes: 1 addition & 1 deletion Redream/Form1.resx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing"">Blue</data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
Expand Down
Loading

0 comments on commit 052040f

Please sign in to comment.