Skip to content

Instantly share code, notes, and snippets.

@rowntreerob
Created May 8, 2023 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rowntreerob/c16780e2e728af16aff6992ddf9f73f1 to your computer and use it in GitHub Desktop.
Save rowntreerob/c16780e2e728af16aff6992ddf9f73f1 to your computer and use it in GitHub Desktop.
//routes w streams MUST be above this !! content type application/octet-stream
// client side posted photo as http w binary request.body (no form, just the file)
// middleware pushes inbound http readstream to an obj in .body
app.use(express.raw({
inflate: true,
limit: '9mb',
type: 'application/octet-stream'}))
// path or route chosen by client upload is "labelai" && fileName of <img>
app.options('/labelai', cors());
app.post('/labelai/:fname',
cors(),
verifyToken, // JWT secure on the api
wrapAsync(async(req, resp, next) => {
const filename = req.params.fname;
let imgnew = await resizeImage(req.body);
let base64 = imgnew.toString('base64')
// scaledown the img as upload from phonecamera = fullSz
// base64-encode downscaled img for openai classification
// roboflow - openai project .com/${photoClassificationModel}
// https://app.roboflow.com/project/org311-clip-photos/2
let res = await axios.post('https://classify.roboflow.com/org311-clip-photos/2?api_key=1f7N'
,base64
, { headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
resp.set({'Content-Type': 'application/json'});
resp.end(JSON.stringify({ data: res.data })); //json response w openAi classification see below:
}
)
)
...
import sharp from 'sharp';
async function resizeImage(img) {
try {
return sharp(img)
.resize({
width: 800
})
.toBuffer();
} catch (error) {
console.log(error);
}
}
/* Open Ai classification response == "Garbage"
"data":{
"time":0.19188385199959157,
"image":{
"width":800,
"height":600
},
"predictions":[{
"class":"garbage",
"confidence":0.6223},
{
"class":"graffiti",
"confidence":0.1344},
{
"class":"encampment",
"confidence":0.1305},
{
"class":"mural",
"confidence":0.1128}
],
"top":"garbage",
"confidence":0.6223
}
*/
<!-- client side to capture photo and to upload as binary file (.jpeg | .png)
JWT is auth for serverSide api - standard "bearer token"
api hosted on "railway"
-->
<label for="files">Select Photo </label>
<input id="files" type="file" accept="image/png, image/jpeg" style="height: 320px; font-size:18px">
const uploader = document.getElementById('files');
var file = uploader.files[0]
const upload = (file) => {
let name = file.name;
let url1 = "https://openAiImg-production.up.railway.app/labelai/" + name;
console.log(url1)
fetch(url1, {
method: 'POST',
headers: {
"application":"openAiImg-production.up.railway.app",
"content-type":"application/octet-stream",
"authorization":"Bearer epZ3ogNhLw"
},
body: file
}).then(
response => response.json()
).then(
resJson => {
const ans = resJson.data.predictions
bubble_fn_uploadResp(JSON.stringify(ans))
}
).catch(
error => console.log(error.message)
);
};
var resp = upload(file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment