-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageGeneration.mjs
223 lines (185 loc) · 6.53 KB
/
ImageGeneration.mjs
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
// Start the hosted models waking up
function wakeUp() {
var model = new rw.HostedModel({
url: "YourURLGoesHere",
});
console.log("deeplab waking up")
var model = new rw.HostedModel({
url: "YourURLGoesHere",
});
console.log("spadeCoco waking up")
var model = new rw.HostedModel({
url: "YourURLGoesHere",
});
console.log("im2txt waking up")
}
// upload file as a base64 image
function readFile() {
if (this.files && this.files[0]) {
var FR= new FileReader();
FR.addEventListener("load", function(e) {
document.getElementById("imgUpload").src = e.target.result;
//document.getElementById("b64").innerHTML = e.target.result;
});
FR.readAsDataURL( this.files[0] );
}
}
document.getElementById("inp").addEventListener("change", readFile);
document.getElementById('imgUpload').onchange = function (e) {
var loadingImage = loadImage(
e.target.files[0],
function (img) {
document.getElementById("img").src = img.toDataURL();
},
{orientation: 1}
);
//if (!loadingImage) {
// Alternative code ...
//}
};
var originalImage = document.getElementById("imgUpload"),
resetImage = document.getElementById("img");
function readFile() {
if (this.files && this.files[0]) {
var FR= new FileReader();
FR.addEventListener("load", function(e) {
// Here is the code that gets called when the JS first gets its hands on the image.
const rawImageData = e.target.result;
// Set the "original" view:
document.getElementById("imgUpload").src = rawImageData;
// Should be able to do rotation right here.
resetOrientation(rawImageData, 1, function(resetBase64Image) {
console.log( resetBase64Image );
resetImage.src = resetBase64Image;
console.log("rotated image")
});
} );
FR.readAsDataURL( this.files[0] );
}
}
// call file loader when the user picks a file to upload.
document.getElementById("inp").addEventListener("change", readFile);
//rotate orientation
function resetOrientation(srcBase64, srcOrientation, callback) {
var img = new Image();
img.onload = function() {
var width = img.width,
height = img.height,
canvas = document.createElement('canvas'),
ctx = canvas.getContext("2d");
// set proper canvas dimensions before transform & export
if (4 < srcOrientation && srcOrientation < 9) {
canvas.width = height;
canvas.height = width;
} else {
canvas.width = width;
canvas.height = height;
}
// transform context before drawing image
switch (srcOrientation) {
case 2: ctx.transform(-1, 0, 0, 1, width, 0); break;
case 3: ctx.transform(-1, 0, 0, -1, width, height ); break;
case 4: ctx.transform(1, 0, 0, -1, 0, height ); break;
case 5: ctx.transform(0, 1, 1, 0, 0, 0); break;
case 6: ctx.transform(0, 1, -1, 0, height , 0); break;
case 7: ctx.transform(0, -1, -1, 0, height , width); break;
case 8: ctx.transform(0, -1, 1, 0, 0, width); break;
default: break;
}
// draw image
ctx.drawImage(img, 0, 0);
// export base64
callback(canvas.toDataURL('image/jpeg'));
//canvas.toBlob('image/jpeg', 0.15);
};
img.src = srcBase64;
}
// start deeplab model
function deepLabModelSelfie(){
console.log("deeplab loading")
document.getElementById("deepLab")
.setAttribute(
"src", "loadingcircle.gif",
"alt", "Loading",
);
const model = new rw.HostedModel({
url: "YourURLGoesHere",
});
console.log("deeplab loaded")
var result = document.getElementById("img").src;
var image
//console.log(result)
//You can use the info() method to see what type of input object the model expects
//model.info().then(info => console.log(info));
const inputs = {
"image": result
};
model.query(inputs).then(outputs => {
const { image } = outputs;
// this changes a placeholder image rather than creating a new one. setAttribute is for changing a base64 image
document.getElementById("deepLab")
.setAttribute(
"src", image,
"alt", "DeepLab segmentation produced from caption",
);
var img = document.getElementById('deepLab');
//Code for adding a new image instead
// document.body.appendChild(img);
//img.src = event.target.result;
// var originalImage = document.getElementById("deepLab"),
//resetImage = document.getElementById("deepLab");
//resetOrientation(originalImage.src, 5, function(resetBase64Image) {
// resetImage.src = resetBase64Image;
// });
});
};
document.getElementById("deepLab").addEventListener("change", spadeCocoModel);
// run spade COCO
function spadeCocoModel(){
document.getElementById("spadeCoco")
.setAttribute(
"src", "loadingcircle.gif",
"alt", "Loading",
);
var convert = document.getElementById("deepLab").src;
const model = new rw.HostedModel({
url: "YourURLGoesHere",
});
console.log("spadecoco loaded")
// You can use the info() method to see what type of input object the model expects
// model.info().then(info => console.log(info));
const inputs = {
"semantic_map": convert
};
model.query(inputs).then(outputs => {
const { output } = outputs;
// use the outputs in your project
// console.log(output)
document.getElementById("spadeCoco")
.setAttribute(
"src", output,
"alt", "SpadeCoco image produced from segmented work",
);
});
}
document.getElementById("spadeCoco").addEventListener("change", imTxtModel);
//run im2txt
function imTxtModel(){
document.getElementById("imTxt").innerHTML = "loading..."
var image = document.getElementById("spadeCoco").src;
const model = new rw.HostedModel({
url: "YourURLGoesHere",
});
console.log("im2txt loaded")
//// You can use the info() method to see what type of input object the model expects
model.info().then(info => console.log(info));
const inputs = {
"image": image
};
model.query(inputs).then(outputs => {
const { caption } = outputs;
console.log(caption)
document.getElementById("imTxt").innerHTML = caption
// use the outputs in your project
});
}