ShoppableImage

node v6.17.1
version: 4.0.0
endpointsharetweet
var endpoint = require("@runkit/runkit/json-endpoint/1.0.0") var request = require("request") // peer dependency var requestPromise = require("request-promise") var jsonQuery = require('json-query') const {JSONPath} = require('jsonpath-plus'); var cld= require('cloudinary@1.21.0').v2; //var JSONPath = require("jsonpath-plus") var cldCloudName = process.env.cldCloudName; var cldKey = process.env.cldKey; var cldSecret = process.env.cldSecret; var apiResourceUrl = "https://" + cldKey + ":" + cldSecret + "@api.cloudinary.com/v1_1/" + cldCloudName +"/resources/image/upload/"; var apiUploadUrl = "https://" + cldKey + ":" + cldSecret + "@api.cloudinary.com/v1_1/" + cldCloudName +"/image/upload"; var apiMetadataUrl = "https://" + cldKey + ":" + cldSecret + "@api.cloudinary.com/v1_1/" + cldCloudName +"/image/metadata"; const boundingBoxCropLocation = 'hackathon/boundingboxcrops'; endpoint(module.exports, async function(request) { console.log(request.query); console.log("Processing the request"); var publicId; publicId = request.query.public_id; var update = (request.query.update === 'true'); //publicId = 'hackathon/shopthelook/m0efw4bisvisgxe3qsyy'; //publicId = 'hackathon/shopthelook/tv22qbcd8vqal6om7e8m'; console.log('Received public Id :' + publicId); var jsonResponse; var detectedTags; var detectedBoundaries; var detectedObjects; //Get the bounding boxes based on objects detected try { console.log('executing request'); var options = { method: 'POST', uri: apiResourceUrl+publicId, body: { }, json: true // Automatically parses the JSON string in the response }; jsonResponse = await requestPromise(options); console.log('executed request'); //detectedTags = jsonQuery('info.detection.object_detection.data.cld-fashion.tags', {data: jsonResponse}); //See if hotspots are already present and update is not called. hotSpotMetaData = JSONPath({path: "metadata.hotspots", json:jsonResponse}); if(hotSpotMetaData){ console.log('found hotspot metadata'); if(!update){ console.log('update is false'); //Need to do this since the metadata is an escaped string return JSON.parse(hotSpotMetaData); } } detectedTags = JSONPath({path: "info.detection.object_detection.data.cld-fashion.tags.*~", json:jsonResponse}); //console.log(detectedTags); detectedBoundaries = JSONPath({path: "info.detection.object_detection.data.cld-fashion.tags.*..bounding-box", json:jsonResponse}); //Consolidate detected objects and upload crops var detectedObjects = []; for (let i=0; i< detectedTags.length; i++) { var detectedObject = new Object(); detectedObject.objectfound = detectedTags[i]; detectedObject.x = detectedBoundaries[i][0]; detectedObject.y = detectedBoundaries[i][1]; detectedObject.w = detectedBoundaries[i][2]; detectedObject.h = detectedBoundaries[i][3]; var cropUploadUrl = 'https://res.cloudinary.com/'+ cldCloudName + '/image/upload/c_crop,' + 'x_'+ parseInt(detectedObject.x) +',y_'+ parseInt(detectedObject.y) +',w_'+ parseInt(detectedObject.w) +',h_'+ parseInt(detectedObject.h) + '/'+ publicId; var options = { method: 'POST', uri: apiUploadUrl, body: { "file": cropUploadUrl, "folder": boundingBoxCropLocation, "upload_preset": 'shoppableimage', }, json: true // Automatically parses the JSON string in the response }; try { console.log("uploading:" + cropUploadUrl); jsonResponse = await requestPromise(options); detectedObject.cropimage = jsonResponse.asset_id; } catch(e) { console.log("Error while uploading:" + cropUploadUrl); console.log(e); } detectedObjects.push(detectedObject); } //Configure the sdk cld.config({ cloud_name: cldCloudName, api_key: cldKey, api_secret: cldSecret, //cname : 'product-demos-res.cloudinary.com', }); console.log(cld.config()); console.log("Cld sdk is ready"); await (cld.uploader.update_metadata('hotspots=' + JSON.stringify(detectedObjects), [publicId], function(error, result) { console.log(result, error)})) console.log("Meta data was updated"); } catch(e) { console.log('error'); console.log(e); return {error: e} } //return detectedBoundaries[0][0]; return detectedObjects; } )
Loading…

no comments

    sign in to comment