-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.cs
More file actions
831 lines (724 loc) · 28.6 KB
/
Copy pathMain.cs
File metadata and controls
831 lines (724 loc) · 28.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
using Emgu.CV.CvEnum;
using Emgu.CV;
using System.Diagnostics;
using Emgu.CV.Structure;
using Microsoft.VisualBasic.Logging;
using Python.Runtime;
using System.IO;
namespace Codeformer_Dotnet
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
string dll = Environment.GetEnvironmentVariable("PYTHONNET_PYDLL");
if (dll != null)
{
PythonEngine.Initialize();
PythonEngine.BeginAllowThreads();
}
comboBox1.SelectedIndex = 0;
}
public static bool isCodeFormer = false;
public static int upscale = 4;
public static float codeformer_fidelity = 1f;
public static bool face_upsample = true;
public static bool background_enhance = true;
static string videoFilePath = string.Empty;
public static string FileName = string.Empty;
public static int frameCount = 0;
static string inputImagePath = $@"{Directory.GetCurrentDirectory()}\frames";
static string outFolderPath = Directory.GetCurrentDirectory() + "\\outCode";
private bool enhanced = false;
private bool codeFormer = false;
Process process;
double fps;
bool hardStop = false;
private FileSystemWatcher watcher;
bool convertInprogress = false;
private const int MinZoom = 10;
private const int MaxZoom = 200;
private const int ZoomStep = 10;
private int currentZoom = 100;
private Point mouseDownLocation;
private bool mouseDragging;
bool isPlaying = false;
bool isEnhanceAll = false;
private DateTime startTime;
private DateTime endTime;
private void Main_Load(object sender, EventArgs e)
{
PythonEngine.Initialize();
trackBar1.Enabled = false;
pictureBox1.Visible = true;
pictureBox2.Visible = false;
CreateDirectory();
//// Create a new FileSystemWatcher
//watcher = new FileSystemWatcher(outFolderPath);
//// Set the notification filters
//watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
//// Subscribe to the events
////watcher.Changed += OnChanged;
//watcher.Created += OnChanged;
////watcher.Deleted += OnChanged;
//watcher.EnableRaisingEvents = true;
}
private async void videoSelectBtn_Click(object sender, EventArgs e)
{
ShowProcessing();
int value = trackBar1.Value;
//button8.Visible = false;
//CreateDirectory();
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "MP4 files (*.mp4, *.mov)|*.mp4|All files (*.*)|*.*";
openFileDialog.Title = "Select a Video File";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
videoFilePath = openFileDialog.FileName;
//button1.Enabled = false;
FileName = openFileDialog.SafeFileName;
//button2.BackColor = Color.Green;
trackBar1.Enabled = true;
trackBar1.Value = value;
//button9.Visible = true;
CreateDirectory();
await ExtractAllFrames();
}
}
}
private async void button2_Click(object sender, EventArgs e)
{
ShowProcessing();
await RunCodeFormer();
}
private async Task RunCodeFormer()
{
int index = trackBar1.Value;
if (!File.Exists(GetInputImageWithIndex(index)))
{
if (pictureBox1.InvokeRequired)
{
BeginInvoke(new Action(() =>
{
pictureBox1.Image = null;
}));
}
else
{
pictureBox1.Image = null;
}
if (pictureBox2.InvokeRequired)
{
BeginInvoke(new Action(() =>
{
pictureBox2.Image = null;
}));
}
else
{
pictureBox2.Image = null;
}
return;
}
await Task.Run(() =>
{
using (Py.GIL())
{
if (isCodeFormer)
{
dlog($"started Codeformer for : {GetInputImageWithIndex(index)} || {DateTime.Now.ToString("HH:mm:ss")}");
dynamic cfClrModule = Py.Import("cf_clr");
cfClrModule.run_codeformer(GetInputImageWithIndex(index), background_enhance, face_upsample, upscale, codeformer_fidelity);
}
else
{
dlog($"started GFPGAN for : {GetInputImageWithIndex(index)} || {DateTime.Now.ToString("HH:mm:ss")}");
dynamic cfClrModule = Py.Import("gf");
cfClrModule.restore_image(GetInputImageWithIndex(index));
}
LoadEnhImage(GetInputImageWithIndex(index));
LoadOrgImage(GetInputImageWithIndex(index));
dlog($"Enhanced: {GetInputImageWithIndex(index)} || {DateTime.Now.ToString("HH:mm:ss")}");
}
});
}
private async Task EnhanceAll()
{
await Task.Run(() =>
{
using (Py.GIL())
{
isEnhanceAll = true;
startTime = DateTime.Now;
string[] imageFiles = Directory.GetFiles(inputImagePath, "*.*", SearchOption.AllDirectories)
.Where(file => file.ToLower().EndsWith(".jpg") || file.ToLower().EndsWith(".png") || file.ToLower().EndsWith(".jpeg"))
.ToArray();
if (isCodeFormer)
{
dlog($"started Codeformer for : {inputImagePath} || {DateTime.Now.ToString("HH:mm:ss")}");
dynamic cfClrModule = Py.Import("cf_clr");
foreach (var item in imageFiles)
{
//cfClrModule.run_codeformer(item, background_enhance, face_upsample, upscale, codeformer_fidelity);
cfClrModule.restore_image(item);
BeginInvoke(new Action(() =>
{
LoadEnhImage(GetOutputImageWithIndex(trackBar1.Value));
LoadOrgImage(GetOutputImageWithIndex(trackBar1.Value));
dlog($"Enhanced: {GetInputImageWithIndex(trackBar1.Value)} || {DateTime.Now.ToString("HH:mm:ss")}");
}));
if (hardStop)
{
hardStop = false;
isEnhanceAll = false;
break;
}
BeginInvoke(new Action(() =>
{
if (trackBar1.Value < trackBar1.Maximum)
{
trackBar1.Value++;
}
}));
}
}
else
{
dlog($"started GFPGAN for : {inputImagePath} || {DateTime.Now.ToString("HH:mm:ss")}");
dynamic cfClrModule = Py.Import("gf");
foreach (var item in imageFiles)
{
//cfClrModule.run_codeformer(item, background_enhance, face_upsample, upscale, codeformer_fidelity);
cfClrModule.restore_image(item);
BeginInvoke(new Action(() =>
{
LoadEnhImage(GetOutputImageWithIndex(trackBar1.Value));
LoadOrgImage(GetOutputImageWithIndex(trackBar1.Value));
dlog($"Enhanced: {GetInputImageWithIndex(trackBar1.Value)} || {DateTime.Now.ToString("HH:mm:ss")}");
}));
if (hardStop)
{
hardStop = false;
isEnhanceAll = false;
break;
}
BeginInvoke(new Action(() =>
{
if (trackBar1.Value < trackBar1.Maximum)
{
trackBar1.Value++;
}
}));
}
}
endTime = DateTime.Now;
BeginInvoke(new Action(() =>
{
videoSelectBtn.Enabled = true;
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button5.Visible = false;
button6.Visible = true;
}));
TimeSpan timeSpan = endTime - startTime;
int hours = timeSpan.Hours;
int minutes = timeSpan.Minutes;
int seconds = timeSpan.Seconds;
dlog($"Total Time: {hours} hours, {minutes} minutes, {seconds} seconds");
}
});
}
private async Task ExtractAllFrames()
{
await Task.Run(async () =>
{
// Path to the input video file
string inputVideoPath = videoFilePath;
// Path to the output frames directory
string outputFramesDirectory = inputImagePath;
// Initialize VideoCapture object
using (VideoCapture videoCapture = new VideoCapture(inputVideoPath))
{
// Get video information
fps = videoCapture.Get(CapProp.Fps);
frameCount = (int)videoCapture.Get(CapProp.FrameCount);
double durationInSeconds = frameCount / fps;
BeginInvoke(new Action(() =>
{
trackBar1.Maximum = frameCount;
}));
dlog($"Total Frames: {frameCount}");
dlog($"Frames Per Second (FPS): {fps}");
dlog($"Duration: {durationInSeconds} seconds");
// Create output directory if not exists
if (!Directory.Exists(outputFramesDirectory))
{
Directory.CreateDirectory(outputFramesDirectory);
}
// Extract frames
//for (int i = 0; i < frameCount; i++)
//{
// // Set the frame position
// videoCapture.Set(CapProp.PosFrames, i);
// // Read the frame
// Mat frame = new Mat();
// videoCapture.Read(frame);
// // Save the frame as an image file
// string frameFilePath = $"{outputFramesDirectory}/frame_{i:D8}.png";
// CvInvoke.Imwrite(frameFilePath, frame);
// if (i == 0)
// {
// LoadImage(0);
// }
//}
await ExtractAllFramesFFMPEG();
BeginInvoke(new Action(() =>
{
button4.Visible = true;
}));
while (true)
{
if (File.Exists(GetInputImageWithIndex(1)))
{
LoadImage(1);
break;
}
}
}
});
}
private async Task ExtractAllFramesFFMPEG()
{
await Task.Run(() =>
{
// Execute FFmpeg command to extract frames
process = new Process();
process.StartInfo.FileName = "ffmpeg";
process.StartInfo.Arguments = $"-i \"{videoFilePath}\" -vf fps={fps} \"{inputImagePath}\\frame_%08d.png\"";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true; // Prevent opening a new console window
//process.OutputDataReceived += (sender, e) => {
//};
//process.ErrorDataReceived += (sender, e) => dlog(e.Data != null ? e.Data : "");
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
});
}
private void LoadImage(int index)
{
LoadOrgImage(GetInputImageWithIndex(index));
LoadEnhImage(GetOutputImageWithIndex(index));
}
private void LoadOrgImage(string src)
{
if (pictureBox1.InvokeRequired)
{
BeginInvoke(new Action(() =>
{
pictureBox1.ImageLocation = src;
}));
}
else
{
pictureBox1.ImageLocation = src;
}
}
private void LoadEnhImage(string src)
{
if (pictureBox2.InvokeRequired)
{
BeginInvoke(new Action(() =>
{
pictureBox2.ImageLocation = src;
}));
}
else
{
pictureBox2.ImageLocation = src;
}
}
private string GetFrameName(int index)
{
return $"frame_{index:00000000}.png";
}
private string GetInputImageWithIndex(int index)
{
return $"{inputImagePath}/frame_{index:00000000}.png";
}
private string GetOutputImageWithIndex(int index)
{
return $"{outFolderPath}/frame_{index:00000000}.png";
}
private string GetDeletedFramesPath(int index)
{
return $"{outFolderPath}/Temp_frame_{index:00000000}.png";
}
private void OnChanged(object sender, FileSystemEventArgs e)
{
// Handle file or directory changes here
Console.WriteLine($"Change detected: {e.ChangeType} - {e.FullPath}");
dlog($"Enhanced: {e.ChangeType} - {e.FullPath} || {DateTime.Now.ToString("HH:mm:ss")}");
if (!isEnhanceAll)
{
BeginInvoke(new Action(() =>
{
LoadOrgImage(GetInputImageWithIndex(trackBar1.Value));
}));
}
}
public void ShowLoading()
{
if (pictureBox1.InvokeRequired)
{
BeginInvoke(new Action(() =>
{
pictureBox1.Image = new Bitmap($@"{Directory.GetCurrentDirectory()}\loading1.gif");
}));
}
else
{
pictureBox1.Image = new Bitmap($@"{Directory.GetCurrentDirectory()}\loading1.gif");
}
if (pictureBox2.InvokeRequired)
{
BeginInvoke(new Action(() =>
{
pictureBox2.Image = new Bitmap($@"{Directory.GetCurrentDirectory()}\loading1.gif");
}));
}
else
{
pictureBox2.Image = new Bitmap($@"{Directory.GetCurrentDirectory()}\loading1.gif");
}
}
private void dlog(string msg)
{
if (textBox1.InvokeRequired)
{
textBox1.Invoke(new Action<string>(dlog), $"> {msg + Environment.NewLine}");
return;
}
else
{
textBox1.AppendText($"> {msg + Environment.NewLine}");
}
}
private void CreateDirectory()
{
if (!Directory.Exists(outFolderPath))
{
Directory.CreateDirectory(outFolderPath);
dlog("Results directory created successfully.");
}
else
{
Directory.Delete(outFolderPath, true);
Directory.CreateDirectory(outFolderPath);
dlog("Results directory created successfully.");
}
if (!Directory.Exists(inputImagePath))
{
Directory.CreateDirectory(inputImagePath);
dlog("Frames directory created successfully.");
}
else
{
Directory.Delete(inputImagePath, true);
Directory.CreateDirectory(inputImagePath);
dlog("Frames directory created successfully.");
}
}
public void ShowProcessing()
{
if (pictureBox1.InvokeRequired)
{
BeginInvoke(new Action(() =>
{
pictureBox1.Image = new Bitmap($@"{Directory.GetCurrentDirectory()}\loading2.gif");
}));
}
else
{
pictureBox1.Image = new Bitmap($@"{Directory.GetCurrentDirectory()}\loading2.gif");
}
if (pictureBox2.InvokeRequired)
{
BeginInvoke(new Action(() =>
{
pictureBox2.Image = new Bitmap($@"{Directory.GetCurrentDirectory()}\loading2.gif");
}));
}
else
{
pictureBox2.Image = new Bitmap($@"{Directory.GetCurrentDirectory()}\loading2.gif");
}
}
private async void button1_Click(object sender, EventArgs e)
{
isPlaying = !isPlaying;
if (isPlaying)
{
button1.Text = "Pause";
}
else
{
button1.Text = "Play";
}
int index = 0;
index = trackBar1.Value;
if (isPlaying)
{
await Task.Run(() =>
{
while (true)
{
Thread.Sleep(Convert.ToInt32(fps));
if (!isPlaying) break;
string frameName = GetFrameName(index);
string frameFileName = Path.Combine(inputImagePath, frameName);
BeginInvoke(new Action(() =>
{
trackBar1.Value = index;
}));
if (!File.Exists(GetInputImageWithIndex(index)) && !File.Exists(GetOutputImageWithIndex(index)))
{
index = 1;
}
else if (File.Exists(GetOutputImageWithIndex(index)) && !File.Exists(GetInputImageWithIndex(index)))
{
LoadOrgImage(GetOutputImageWithIndex(index));
}
else
{
LoadOrgImage(GetInputImageWithIndex(index));
}
if (index < frameCount)
{
index++;
}
else
{
index = 1;
}
}
});
}
}
private void trackBar1_MouseUp(object sender, MouseEventArgs e)
{
if (!convertInprogress)
{
codeFormer = false;
enhanced = false;
//button3.BackColor = Color.Red;
//button4.BackColor = Color.Red;
pictureBox1.Visible = true;
pictureBox2.Visible = false;
//button8.Visible = false;
int value = trackBar1.Value;
LoadImage(value);
//await Task.Run(() =>
//{
// try
// {
// ShowLoading();
// dlog($"Loading {value}");
// if (value < frameCount)
// {
// using (VideoFileReader reader = new VideoFileReader())
// {
// reader.Open(videoFilePath);
// //frameCount = Convert.ToInt32(reader.FrameCount);
// Bitmap frame = reader.ReadVideoFrame(value);
// LoadImage((Image)frame.Clone());
// string frameName = GetFrameName(value);
// string frameFileName = Path.Combine(inputImagePath, frameName);
// if (!File.Exists(frameFileName))
// frame.Save(frameFileName, System.Drawing.Imaging.ImageFormat.Png);
// frame.Dispose();
// }
// // execute codeformer
// //ApplyCodeFormer(GetInputImageWithIndex(trackbarValue));
// // Load images
// LoadImage(value);
// dlog($"Loaded {value}");
// }
// }
// catch (Exception ex)
// {
// dlog(ex.Message);
// }
//});
}
//else
//{
// timer1.Enabled = false;
//}
}
private void button3_Click(object sender, EventArgs e)
{
enhanced = !enhanced;
if (enhanced)
{
if (!File.Exists(GetOutputImageWithIndex(trackBar1.Value)))
{
enhanced = false;
button3.Text = "Show Enhanced";
MessageBox.Show("Please use option 'Enhance' to enhance this frame!", "Frame not found !");
}
else
{
button3.Text = "Show Original";
LoadOrgImage(GetOutputImageWithIndex(trackBar1.Value));
LoadEnhImage(GetOutputImageWithIndex(trackBar1.Value));
}
}
else
{
button3.Text = "Show Enhanced";
LoadOrgImage(GetInputImageWithIndex(trackBar1.Value));
LoadEnhImage(GetInputImageWithIndex(trackBar1.Value));
}
}
private void pictureBox2_Click(object sender, EventArgs e)
{
}
private async void button4_Click(object sender, EventArgs e)
{
ShowProcessing();
trackBar1.Value = 1;
videoSelectBtn.Enabled = false;
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Visible = true;
await EnhanceAll();
}
private void button5_Click(object sender, EventArgs e)
{
hardStop = true;
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
isPlaying = false;
process.Kill();
}
catch (Exception ex)
{
dlog(ex.Message);
}
}
private async void button6_Click(object sender, EventArgs e)
{
videoSelectBtn.Enabled = false;
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
await ConvertTovideo();
}
private async Task ConvertTovideo()
{
await Task.Run(() =>
{
try
{
using (process = new Process())
{
process.StartInfo.FileName = $"ffmpeg";
process.StartInfo.Arguments = $"-framerate {fps} -i {outFolderPath}/frame_%08d.png -c:v libx264 -pix_fmt yuv420p \"{FileName}\"";
dlog($"{process.StartInfo.FileName} {process.StartInfo.Arguments}");
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
// Asynchronously read the output and error streams
Task<string> outputTask = process.StandardOutput.ReadToEndAsync();
Task<string> errorTask = process.StandardError.ReadToEndAsync();
// Optionally wait for the process to exit
process.WaitForExit();
BeginInvoke(new Action(() =>
{
videoSelectBtn.Enabled = true;
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button5.Enabled = true;
button6.Enabled = true;
}));
// Get the output and error results
string output = outputTask.Result;
string error = errorTask.Result;
Console.WriteLine("Process exited with code: " + process.ExitCode);
dlog("Process exited with code: " + process.ExitCode);
Console.WriteLine(output);
dlog(output);
if (error != null && error != string.Empty && error != "")
{
Console.WriteLine("Error: " + error);
dlog("Error: " + error);
}
}
}
catch (Exception ex)
{
dlog(ex.Message);
}
});
}
private void pictureBox2_DoubleClick(object sender, EventArgs e)
{
if (!enhanced)
{
Maximize maximize = new Maximize(inputImagePath, fps, trackBar1.Value, frameCount);
maximize.Show();
}
else
{
Maximize maximize = new Maximize(outFolderPath, fps, trackBar1.Value, frameCount);
maximize.Show();
}
}
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
if (!enhanced)
{
Maximize maximize = new Maximize(inputImagePath, fps, trackBar1.Value, frameCount);
maximize.Show();
}
else
{
Maximize maximize = new Maximize(outFolderPath, fps, trackBar1.Value, frameCount);
maximize.Show();
}
}
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
if(comboBox1.SelectedIndex == 0)
{
isCodeFormer = false;
Text = $"Video Enhancer - GFPGAN";
}
else
{
isCodeFormer = true;
Text = $"Video Enhancer - Codeformer";
}
}
}
}