Last active
June 8, 2020 20:25
-
-
Save Snazzah/98747d640d1b092ce2ab887788a518d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Pix2Pix Module, Snazzah | |
let request = require("superagent"); | |
let fs = require("fs"); | |
let path = require("path"); | |
let URLs = { | |
edges2shoes: "https://pix2pix.affinelayer.com/edges2shoes_AtoB", | |
edges2handbags: "https://pix2pix.affinelayer.com/edges2handbags_AtoB", | |
facades: "https://pix2pix.affinelayer.com/facades_BtoA", | |
edges2cats: "https://pix2pix.affinelayer.com/edges2cats_AtoB", | |
} | |
module.exports = { | |
edges2shoes: function(file){return this.process(file, URLs.edges2shoes)}, | |
edges2handbags: function(file){return this.process(file, URLs.edges2handbags)}, | |
facades: function(file){return this.process(file, URLs.facades)}, | |
edges2cats: function(file){return this.process(file, URLs.edges2cats)}, | |
process: function(file, url){ | |
return new Promise((resolve, reject)=>{ | |
this.resolveBuffer(file).then(buffer => { | |
request.post(url) | |
.set("Content-Type", "image/png") | |
//.send(this.b64ToBin(buffer.toString('base64'))) | |
.send(buffer) | |
.end((err, res) => { | |
if (err) return reject(err); | |
//resolve("data:image\/png;base64," + res.body.toString('base64')) | |
resolve(res.body) | |
}) | |
}) | |
}) | |
}, | |
resolveBuffer: function(resource){ | |
if (resource instanceof Buffer) return Promise.resolve(resource); | |
if (typeof resource === 'string') { | |
return new Promise((resolve, reject) => { | |
if (/^https?:\/\//.test(resource)) { | |
const req = request.get(resource).set('Content-Type', 'blob'); | |
req.end((err, res) => { | |
if (err) return reject(err); | |
if (!(res.body instanceof Buffer)) return reject(new TypeError('The response body isn\'t a Buffer.')); | |
return resolve(res.body); | |
}); | |
} else { | |
const file = path.resolve(resource); | |
fs.stat(file, (err, stats) => { | |
if (err) return reject(err); | |
if (!stats || !stats.isFile()) return reject(new Error(`The file could not be found: ${file}`)); | |
fs.readFile(file, (err2, data) => { | |
if (err2) reject(err2); else resolve(data); | |
}); | |
return null; | |
}); | |
} | |
}); | |
} | |
} | |
} | |
// Based on code from Affinelayer; http://affinelayer.com/pixsrv/index.html | |
/* | |
xhr.open("POST", this.config.generate_url, true) | |
xhr.setRequestHeader("Content-Type", "image/png") | |
xhr.responseType = "arraybuffer" | |
xhr.timeout = 45000 | |
xhr.onreadystatechange = () => { | |
if (xhr.readyState == 4) { | |
request_in_progress = false | |
update() | |
if (xhr.status == 200) { | |
console.log("request complete", xhr.status) | |
var output_bin = new Uint8Array(xhr.response) | |
var output_b64 = bin_to_b64(output_bin) | |
var output = new Image() | |
output.src = "data:image\/png;base64," + output_b64 | |
output.onload = () => { | |
// browsers besides chrome need to wait for the image to load | |
this.output.drawImage(output, 0, 0) | |
update() | |
} | |
} else { | |
last_request_failed = true | |
} | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Goodness Gracious