-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathSuperResolution.lpr
More file actions
224 lines (208 loc) · 6.76 KB
/
Copy pathSuperResolution.lpr
File metadata and controls
224 lines (208 loc) · 6.76 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
program SuperResolution;
(*
Coded by Joao Paulo Schwarz Schuler.
https://github.com/joaopauloschuler/neural-api/tree/master/examples/SuperResolution
This command line tool increases image resolution from an input image file.
*)
{$mode objfpc}{$H+}
uses {$IFDEF UNIX} cthreads, {$ENDIF}
Classes,
SysUtils,
CustApp,
neuralnetwork,
neuralvolume,
Math,
neuraldatasets,
neuralfit,
neuralgeneric,
FPImage,
usuperresolutionexample,
FPWriteBMP, FPWritePCX, FPWriteJPEG, FPWritePNG,
FPWritePNM, FPWriteTGA, FPWriteTiff;
type
{ TSuperResolutionExample }
TSuperResolutionExample = class(TCustomApplication)
protected
procedure DoRun; override;
procedure RunAlgo(inputFile, outputFile: string);
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
procedure WriteHelp; virtual;
end;
{ TSuperResolutionExample }
procedure TSuperResolutionExample.DoRun;
var
inputFile, outputFile: string;
begin
// quick check parameters
// parse parameters
if HasOption('h', 'help') then
begin
WriteHelp;
end;
if HasOption('i', 'input') then
begin
inputFile := GetOptionValue('i', 'input');
end
else
begin
WriteHelp;
Terminate;
Exit;
end;
outputFile := 'superresolution.png';
if HasOption('o', 'output') then
begin
outputFile := GetOptionValue('o', 'output');
end;
if FileExists(inputFile) then
begin
RunAlgo(inputFile, outputFile);
end
else
begin
WriteLn('File can''t be found: ', inputFile);
end;
Terminate;
Exit;
end;
const
csTileBorder = 4;
procedure TSuperResolutionExample.RunAlgo(inputFile, outputFile: string);
var
NN: TNNet;
InputImgVol, OutputImgVol: TNNetVolume;
InputTile, OutputTile: TNNetVolume;
MaxTileX, MaxTileY: integer;
TileXCnt, TileYCnt: integer;
TileStartX, TileStartY: integer;
LocalDestX, LocalDestY: integer;
LocalOriginX, LocalOriginY: integer;
LocalLenX, LocalLenY: integer;
LocalTileSizeX, LocalTileSizeY: integer;
begin
InputImgVol := TNNetVolume.Create();
OutputImgVol := TNNetVolume.Create();
WriteLn('Loading input file: ', inputFile);
LoadImageFromFileIntoVolume(inputFile, InputImgVol);
WriteLn('Input image size: ', InputImgVol.SizeX,' x ', InputImgVol.SizeY,' x ',InputImgVol.Depth);
InputImgVol.Divi(64);
InputImgVol.Sub(2);
WriteLn('Creating Neural Network...');
if InputImgVol.SizeX * InputImgVol.SizeY <= 128*128 then
begin
WriteLn('Resizing...');
NN := CreateResizingNN(InputImgVol.SizeX, InputImgVol.SizeY, csExampleFileName);
NN.Compute(InputImgVol);
NN.GetOutput(OutputImgVol);
end
else
begin
LocalTileSizeX := GetMaxDivisor(InputImgVol.SizeX-csTileBorder*2, 128);
LocalTileSizeY := GetMaxDivisor(InputImgVol.SizeY-csTileBorder*2, 128);
WriteLn('Resizing with tiles. Tile size is: ', LocalTileSizeX, 'x', LocalTileSizeY, ' .');
NN := CreateResizingNN(LocalTileSizeX+csTileBorder*2, LocalTileSizeY+csTileBorder*2, csExampleFileName);
InputTile := TNNetVolume.Create(LocalTileSizeX+csTileBorder*2, LocalTileSizeY+csTileBorder*2, 3);
OutputTile := TNNetVolume.Create(InputTile.SizeX*2, InputTile.SizeY*2, 3);
MaxTileX := ( (InputImgVol.SizeX-csTileBorder*2) div LocalTileSizeX) - 1;
MaxTileY := ( (InputImgVol.SizeY-csTileBorder*2) div LocalTileSizeY) - 1;
OutputImgVol.Resize(InputImgVol.SizeX*2, InputImgVol.SizeY*2, 3);
WriteLn('Resizing with tiles to: ', OutputImgVol.SizeX,' x ', OutputImgVol.SizeY,' x ',OutputImgVol.Depth);
OutputImgVol.Fill(0);
for TileXCnt := 0 to MaxTileX do
begin
for TileYCnt := 0 to MaxTileY do
begin
TileStartX := TileXCnt*LocalTileSizeX;
TileStartY := TileYCnt*LocalTileSizeY;
InputTile.CopyCropping
(
InputImgVol,
TileStartX,
TileStartY,
LocalTileSizeX+csTileBorder*2,
LocalTileSizeY+csTileBorder*2
);
NN.Compute(InputTile);
NN.GetOutput(OutputTile);
LocalDestX := TileXCnt*LocalTileSizeX*2+csTileBorder*2;
LocalDestY := TileYCnt*LocalTileSizeY*2+csTileBorder*2;
LocalOriginX := csTileBorder*2;
LocalOriginY := csTileBorder*2;
LocalLenX := LocalTileSizeX*2;
LocalLenY := LocalTileSizeY*2;
if ((TileXCnt = 0) or (TileXCnt = MaxTileX)) then
begin
LocalLenX := LocalLenX + csTileBorder*2;
if (TileXCnt = 0) then
begin
LocalOriginX := 0;
LocalDestX := 0;
end;
end;
if ((TileYCnt = 0) or (TileYCnt = MaxTileY)) then
begin
LocalLenY := LocalLenY + csTileBorder*2;
if (TileYCnt = 0) then
begin
LocalOriginY := 0;
LocalDestY := 0;
end;
end;
OutputImgVol.AddArea
(
{DestX=}LocalDestX,
{DestY=}LocalDestY,
{OriginX=}LocalOriginX,
{OriginY=}LocalOriginY,
{LenX=}LocalLenX,
{LenY=}LocalLenY,
OutputTile
);
end;
end;
InputTile.Free;
OutputTile.Free;
end;
OutputImgVol.Add(2);
OutputImgVol.Mul(64);
WriteLn('Saving output file: ', outputFile);
if Not(SaveImageFromVolumeIntoFile(OutputImgVol, outputFile)) then
begin
WriteLn('Saving has failed: ', outputFile);
end;
OutputImgVol.Free;
InputImgVol.Free;
NN.Free;
end;
constructor TSuperResolutionExample.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
StopOnException := True;
end;
destructor TSuperResolutionExample.Destroy;
begin
inherited Destroy;
end;
procedure TSuperResolutionExample.WriteHelp;
begin
WriteLn
(
'Increase Image Resolution from an image file',sLineBreak,
'Command Line Example: SuperResolution -i myphoto.png -o myphoto-big.png', sLineBreak,
' -h : displays this help. ', sLineBreak,
' -i : defines input file. ', sLineBreak,
' -o : defines output file.', sLineBreak,
' More info at:', sLineBreak,
' https://github.com/joaopauloschuler/neural-api/tree/master/examples/SuperResolution', sLineBreak
);
end;
var
Application: TSuperResolutionExample;
begin
Application := TSuperResolutionExample.Create(nil);
Application.Title:='Super Resolution Command Line Example';
Application.Run;
Application.Free;
end.