How can I extract a key with different values? - javascript

I have several same keys and multiple values I just want to parse JSON with keys and values and extract device_id, As you can see all device_id is the same but the value of them is different.
How can I parse.json and have just device_id: values?
My file is like this :
[
{
"id": "c19ca393.e14b1",
"type": "tab",
"label": "Flow 1",
"disabled": false,
"info": ""
},
{
"id": "a15871bb.7c3b2",
"type": "input-distributer",
"z": "c19ca393.e14b1",
"name": "input-distributer",
"device_id": "93e71ba7-fb56-5592-a5f6-d855203dd7ae",
"device_name": "nmos-cpp_node_10-20-130-24:6131",
"x": 280,
"y": 260,
"wires": [
[
"6548d2cb.45bc1c"
],
[
"7cf8bc7e.204a84"
]
]
},
{
"id": "421da76b.670be8",
"type": "output-distributer",
"z": "c19ca393.e14b1",
"name": "output-distributer",
"device_id": "93e71ba7-fb56-5592-a5f6-d855203dd7ae",
"device_name": "nmos-cpp_node_10-20-130-24:6131",
"x": 620,
"y": 260,
"wires": [
]
},
{
"id": "6548d2cb.45bc1c",
"type": "video-switcher",
"z": "c19ca393.e14b1",
"name": "video-switcher",
"device_id": "93e71ba7-fb56-5592-a5f6-d855203dd7ae",
"device_name": "nmos-cpp_node_10-20-130-24:6131",
"x": 480,
"y": 120,
"wires": [
[
"421da76b.670be8"
]
]
},
{
"id": "7cf8bc7e.204a84",
"type": "audio-switcher",
"z": "c19ca393.e14b1",
"name": "audio-switcher",
"device_id": "fe21abdf-706f-5c7b-adb8-2507e145e820",
"device_name": "nmos-cpp_node_10-20-130-24:6121",
"x": 440,
"y": 360,
"wires": [
[
"421da76b.670be8"
]
]
}
]
If I wanted to grab all the values of the device id, How is it possible?

You can try this for getting device_id values
data.map(element => { // data refers to your array
console.log(element['device_id']);
});
since in first object device_id is not there so it will print undefined and you can apply a check on that.

You can add a check if device_id exists or not and the you can .map it.
Try this:
let arr = [{"id":"c19ca393.e14b1","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"a15871bb.7c3b2","type":"input-distributer","z":"c19ca393.e14b1","name":"input-distributer","device_id":"93e71ba7-fb56-5592-a5f6-d855203dd7ae","device_name":"nmos-cpp_node_10-20-130-24:6131","audio_services":"{\"node-input-audio_services1\":\"c250498b-52ee-5162-b450-e15905912e8c\",\"audio_ip_1\":\"1.2.3.4\",\"audio_port_1\":\"1234\"}","video_services":"{\"node-input-video_services1\":\"c250498b-52ee-5162-b450-e15905912e8c\",\"video_ip_1\":\"5.6.7.8\",\"video_port_1\":\"5678\"}","x":280,"y":260,"wires":[["6548d2cb.45bc1c"],["7cf8bc7e.204a84"]]},{"id":"421da76b.670be8","type":"output-distributer","z":"c19ca393.e14b1","name":"output-distributer","device_id":"93e71ba7-fb56-5592-a5f6-d855203dd7ae","device_name":"nmos-cpp_node_10-20-130-24:6131","audio_services":"{\"node-input-audio_services1\":\"7d010b65-b9d8-5561-a622-189b1c06d3f9\"}","video_services":"{\"node-input-video_services1\":\"7d010b65-b9d8-5561-a622-189b1c06d3f9\"}","x":620,"y":260,"wires":[]},{"id":"6548d2cb.45bc1c","type":"video-switcher","z":"c19ca393.e14b1","name":"video-switcher","device_id":"93e71ba7-fb56-5592-a5f6-d855203dd7ae","device_name":"nmos-cpp_node_10-20-130-24:6131","video_senders":"{\"node-input-senders_services1\":\"c250498b-52ee-5162-b450-e15905912e8c\",\"senders_ip_1\":\"9.0.1.2\",\"senders_port_1\":\"9012\"}","video_receivers":"{\"node-input-receivers_services1\":\"7d010b65-b9d8-5561-a622-189b1c06d3f9\"}","x":480,"y":120,"wires":[["421da76b.670be8"]]},{"id":"7cf8bc7e.204a84","type":"audio-switcher","z":"c19ca393.e14b1","name":"audio-switcher","device_id":"fe21abdf-706f-5c7b-adb8-2507e145e820","device_name":"nmos-cpp_node_10-20-130-24:6121","audio_senders":"{\"node-input-senders_services1\":\"7408e1be-a227-5d0a-a22b-294bcf22017c\",\"senders_ip_1\":\"0.0.0.0\",\"senders_port_1\":\"0000\"}","audio_receivers":"{\"node-input-receivers_services1\":\"d08f3a50-41fc-5020-9c6c-175d91891aef\"}","x":440,"y":360,"wires":[["421da76b.670be8"]]}]
let device_ids = arr.filter(o => o.device_id).map(obj => obj.device_id);
console.log(device_ids);

After you opened the file and get the data from it, iterate through array and check if object has property device_id obj.hasOwnProperty("device_id") , if it's present, print it:
// edit after comment ---------
async function getFileContent() {
fs.readFile("flow_demo_1.json", (err, data) => {
if (err) {
console.log(err);
} else {
console.log(data.toString());
return data
}
})
}
// const fileContent = await getFileContent();
// const myObj = JSON.parse(fileContent)
// ---------
const myObj = [{
"id": "c19ca393.e14b1",
"type": "tab",
"label": "Flow 1",
"disabled": false,
"info": ""
},
{
"id": "a15871bb.7c3b2",
"type": "input-distributer",
"z": "c19ca393.e14b1",
"name": "input-distributer",
"device_id": "93e71ba7-fb56-5592-a5f6-d855203dd7ae",
"device_name": "nmos-cpp_node_10-20-130-24:6131",
"audio_services": "{\"node-input-audio_services1\":\"c250498b-52ee-5162-b450-e15905912e8c\",\"audio_ip_1\":\"1.2.3.4\",\"audio_port_1\":\"1234\"}",
"video_services": "{\"node-input-video_services1\":\"c250498b-52ee-5162-b450-e15905912e8c\",\"video_ip_1\":\"5.6.7.8\",\"video_port_1\":\"5678\"}",
"x": 280,
"y": 260,
"wires": [
[
"6548d2cb.45bc1c"
],
[
"7cf8bc7e.204a84"
]
]
},
{
"id": "421da76b.670be8",
"type": "output-distributer",
"z": "c19ca393.e14b1",
"name": "output-distributer",
"device_id": "93e71ba7-fb56-5592-a5f6-d855203dd7ae",
"device_name": "nmos-cpp_node_10-20-130-24:6131",
"audio_services": "{\"node-input-audio_services1\":\"7d010b65-b9d8-5561-a622-189b1c06d3f9\"}",
"video_services": "{\"node-input-video_services1\":\"7d010b65-b9d8-5561-a622-189b1c06d3f9\"}",
"x": 620,
"y": 260,
"wires": [
]
},
{
"id": "6548d2cb.45bc1c",
"type": "video-switcher",
"z": "c19ca393.e14b1",
"name": "video-switcher",
"device_id": "93e71ba7-fb56-5592-a5f6-d855203dd7ae",
"device_name": "nmos-cpp_node_10-20-130-24:6131",
"video_senders": "{\"node-input-senders_services1\":\"c250498b-52ee-5162-b450-e15905912e8c\",\"senders_ip_1\":\"9.0.1.2\",\"senders_port_1\":\"9012\"}",
"video_receivers": "{\"node-input-receivers_services1\":\"7d010b65-b9d8-5561-a622-189b1c06d3f9\"}",
"x": 480,
"y": 120,
"wires": [
[
"421da76b.670be8"
]
]
},
{
"id": "7cf8bc7e.204a84",
"type": "audio-switcher",
"z": "c19ca393.e14b1",
"name": "audio-switcher",
"device_id": "fe21abdf-706f-5c7b-adb8-2507e145e820",
"device_name": "nmos-cpp_node_10-20-130-24:6121",
"audio_senders": "{\"node-input-senders_services1\":\"7408e1be-a227-5d0a-a22b-294bcf22017c\",\"senders_ip_1\":\"0.0.0.0\",\"senders_port_1\":\"0000\"}",
"audio_receivers": "{\"node-input-receivers_services1\":\"d08f3a50-41fc-5020-9c6c-175d91891aef\"}",
"x": 440,
"y": 360,
"wires": [
[
"421da76b.670be8"
]
]
}
]
for (let obj of myObj) {
if (obj.hasOwnProperty("device_id")) {
console.log("device_id: " + obj.device_id)
}
}

finally found my answer :
var fileContent = fs.readFileSync('flow_demo_1.json', 'utf8');
console.log(fileContent);
const myObj = JSON.parse(fileContent)
for (let obj of myObj) {
if (obj.hasOwnProperty("device_id")) {
console.log("device_id: " + obj.device_id);
}
}
Thanks every one.

Related

Select items that have at least one digit from object in javascript

{
"images": [
{
"key": "ASDV1-01.jpg",
"image_location": "image1.jpg",
"data": {
"documentid": "CE44DBAC-59B2-4178-8392-0141FB2F58DF",
"scandate": "Feb 1 2018 12:05PM",
"F08": "1",
"F09": "",
"F10": "101076",
"F11": ""
},
"crops": {
"F08": {
"rectangle": {
"left": 690,
"top": 2111,
"width": 597,
"height": 121
}
},
"F09": {},
"F10": {
"rectangle": {
"left": 653,
"top": 821,
"width": 653,
"height": 243
}
},
"F11": {}
}
},
{
"key": "ASDV1-01.jpg",
"image_location": "image.png",
"crops": {
"F05": {
"rectangle": {
"left": 0,
"top": 808,
"width": 624,
"height": 243
}
}
},
"metadata": [
{
"name": "colors",
"data": {
"dimensions": {
"width": 2000,
"height": 2600
},
"coordinates": {
"width": {
"x": {
"lat": 4,
"long": [12, 345]
},
"y": {
"lat": {
"x" : [12,345],
"y": "234"
},
"long": 123
}
}
}
}
}
]
},
{
"key": "ASDV1-02.jpg",
"image_location": "image.png"
}
]
}
My main goal is to loop through this and return a new object containing items that have at least one digit so in the end i will have something similar to this example. The new object should be a new JSON file:
{
"key": "ASDV1-01.jpg",
"data": {
"documentid": "CE44DBAC-59B2-4178-8392-0141FB2F58DF",
"scandate": "Feb 1 2018 12:05PM",
"F08": "1",
"F10": "101076",
}
}
Of course, this should be applied for the whole object. Tried to do this with regex syntax but with no use. Thank you in advance
By digit you mean like 0 or a? (then it would be a character) you can loop trough the whole Json and check for each key if the corresponding value is len() >= 1 if it is, then add it to a new Object. You can also use a filter like in this example (example for getting all odd numbers):
const odd = x => x % 2 === 1;
[1, 2, 3].filter(x => x % 2 === 1);
Have a look here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
loop through whole array of images and test() key if it contains any digit with /\d/ regex
let data = [
{ "key": "no-digit.jpg" },
{ "key": "has2-digits7.jpg" },
{ "key": "again-no-digit.jpg" }
];
data = data.filter(item => /\d/.test(item.key));
console.log(data);

Sorting a chart axis based on the dynamically updated axis order in another chart

In this VegaLite spec the y-axis order of the bottom-most barplot is updated as the data of that plot is filtered based on the selection in the scatter plot. How can I achieve the same resorting behavior for both the blue and orange bars in the top-most bar plot where I have layered the same barplot together with another chart?
I have tried toggling the axis between shared and independent and switching the order of the layer, but that didn't do it. Conceptually I can imagine using a calculate transform to define a new field that is based on the selection and used as the sort order key, but I can't figure out how to write this vega expression string.
Here is that Altair code if anyone prefers to solve it that way:
import altair as alt
import pandas as pd
data={
'Term': ['algorithm','learning','learning','algorithm','algorithm','learning'],
'Freq_x': [1330,1153,504.42,296.69,177.59,140.35],
'Total': [1330, 1353,1353.7,1330.47,1330.47,1353.7],
'Category': ['Default', 'Default', 'Topic1', 'Topic1', 'Topic2', 'Topic2'],
'logprob': [30.0, 27.0, -5.116, -5.1418, -5.4112, -5.5271],
'loglift': [30.0, 27.0, 0.0975, 0.0891, -0.1803, -0.3135],
'saliency_ind': [0, 3, 76, 77, 181, 186],
'x': [None,None,-0.0080,-0.0080,-0.0053,-0.0053],
'y': [None,None,-0.0056,-0.0056, 0.0003,0.0003],
'topics': [None,None, 1.0, 1.0, 2.0, 2.0],
'cluster': [None,None, 1.0, 1.0, 1.0, 1.0],
'Freq_y': [None,None,20.39,20.39,14.18,14.18]}
df=pd.DataFrame(data)
pts = alt.selection(type="single", fields=['Category'], empty='none')
points = alt.Chart(df).mark_circle().encode(
x='mean(x)',
y='mean(y)',
size='Freq_y',
detail='Category',
color=alt.condition(pts, alt.value('#F28E2B'), alt.value('#4E79A7'))
).add_selection(pts)
bars = alt.Chart(df).mark_bar().encode(
x='Freq_x',
y=alt.Y('Term'),
)
bars2 = alt.Chart(df).mark_bar(color='#F28E2B').encode(
x='Freq_x',
y=alt.Y('Term', sort='-x'),
).transform_filter(
pts
)
(points | (bars + bars2) & bars2)
The issue with your spec was that in layers you performed filter transform which created a separate data for each layers. Sorting was working at each level of layer but since both the layers data were separate so each layer was getting sorted inpendently.
So instead of having a filter transform, I tried to manual filter using calculate transform and created a filtered_freq_x field which is later used on 2nd layer and performed sorting using this on window. So with this my data becomes same for both layers, just few fields were added and used.
Let me know if this works for you not. Below is the spec config and editor:
{
"config": {"view": {"continuousWidth": 200, "continuousHeight": 300}},
"hconcat": [
{
"mark": "circle",
"encoding": {
"color": {
"condition": {"value": "#F28E2B", "selection": "selector046"},
"value": "#4E79A7"
},
"detail": {"type": "nominal", "field": "Category"},
"size": {"type": "quantitative", "field": "Freq_y"},
"x": {"type": "quantitative", "aggregate": "mean", "field": "x"},
"y": {"type": "quantitative", "aggregate": "mean", "field": "y"}
},
"selection": {
"selector046": {
"type": "single",
"fields": ["Category"],
"empty": "none"
}
}
},
{
"vconcat": [
{
"transform": [
{
"joinaggregate": [
{"field": "Freq_x", "op": "max", "as": "max_Fx"}
]
},
{
"calculate": "selector046['Category'] ? selector046['Category'] : []",
"as": "filterCategory"
},
{
"calculate": "indexof(datum.filterCategory,datum['Category']) > -1 ? datum['Freq_x'] : null",
"as": "filtered_Freq_x"
},
{
"sort": [{"field": "filtered_Freq_x", "order": "descending"}],
"window": [{"op": "rank", "as": "Sorted"}]
}
],
"height": 50,
"layer": [
{
"mark": "bar",
"encoding": {
"x": {"type": "quantitative", "field": "Freq_x"},
"y": {
"type": "nominal",
"field": "Term",
"sort": {"field": "Sorted"}
}
}
},
{
"mark": {"type": "bar", "color": "#F28E2B"},
"encoding": {
"x": {"type": "quantitative", "field": "filtered_Freq_x"},
"y": {
"type": "nominal",
"field": "Term",
"sort": {"field": "Sorted"}
}
}
}
]
},
{
"mark": {"type": "bar", "color": "#F28E2B"},
"encoding": {
"x": {"type": "quantitative", "field": "Freq_x"},
"y": {"type": "nominal", "field": "Term", "sort": "-x"}
},
"transform": [{"filter": {"selection": "selector046"}}]
}
]
}
],
"data": {"name": "data-d807cd22b94d04d6f1543201cfe5f45e"},
"$schema": "https://vega.github.io/schema/vega-lite/v4.8.1.json",
"datasets": {
"data-d807cd22b94d04d6f1543201cfe5f45e": [
{
"Term": "algorithm",
"Freq_x": 1330,
"Total": 1330,
"Category": "Default",
"logprob": 30,
"loglift": 30,
"saliency_ind": 0,
"x": null,
"y": null,
"topics": null,
"cluster": null,
"Freq_y": null
},
{
"Term": "learning",
"Freq_x": 1153,
"Total": 1353,
"Category": "Default",
"logprob": 27,
"loglift": 27,
"saliency_ind": 3,
"x": null,
"y": null,
"topics": null,
"cluster": null,
"Freq_y": null
},
{
"Term": "learning",
"Freq_x": 504.42,
"Total": 1353.7,
"Category": "Topic1",
"logprob": -5.116,
"loglift": 0.0975,
"saliency_ind": 76,
"x": -0.008,
"y": -0.0056,
"topics": 1,
"cluster": 1,
"Freq_y": 20.39
},
{
"Term": "algorithm",
"Freq_x": 296.69,
"Total": 1330.47,
"Category": "Topic1",
"logprob": -5.1418,
"loglift": 0.0891,
"saliency_ind": 77,
"x": -0.008,
"y": -0.0056,
"topics": 1,
"cluster": 1,
"Freq_y": 20.39
},
{
"Term": "algorithm",
"Freq_x": 177.59,
"Total": 1330.47,
"Category": "Topic2",
"logprob": -5.4112,
"loglift": -0.1803,
"saliency_ind": 181,
"x": -0.0053,
"y": 0.0003,
"topics": 2,
"cluster": 1,
"Freq_y": 14.18
},
{
"Term": "learning",
"Freq_x": 140.35,
"Total": 1353.7,
"Category": "Topic2",
"logprob": -5.5271,
"loglift": -0.3135,
"saliency_ind": 186,
"x": -0.0053,
"y": 0.0003,
"topics": 2,
"cluster": 1,
"Freq_y": 14.18
}
]
}
}

AWS recognition face API: Crop teeth section (mouth) from face

I'm cropping teeth section (mouth) from face based on x,y co-ordinates received from AWS recognition face API
this code is working and cropping the teeth section like
but I need only teeth section to be cropped.
AWS recognition API image response
[
{
"BoundingBox": {
"Width": 0.4604368805885315,
"Height": 0.7760819792747498,
"Left": 0.28602713346481323,
"Top": 0.07381705939769745
},
"AgeRange": {
"Low": 48,
"High": 66
},
"Smile": {
"Value": true,
"Confidence": 99.91497802734375
},
"Eyeglasses": {
"Value": false,
"Confidence": 98.94174194335938
},
"Sunglasses": {
"Value": false,
"Confidence": 99.84471130371094
},
"Gender": {
"Value": "Male",
"Confidence": 99.57334899902344
},
"Beard": {
"Value": false,
"Confidence": 73.63420867919922
},
"Mustache": {
"Value": false,
"Confidence": 96.08769226074219
},
"EyesOpen": {
"Value": true,
"Confidence": 98.94685363769531
},
"MouthOpen": {
"Value": true,
"Confidence": 99.7721939086914
},
"Emotions": [
{
"Type": "HAPPY",
"Confidence": 99.75701904296875
},
{
"Type": "SURPRISED",
"Confidence": 0.10713297128677368
},
{
"Type": "CONFUSED",
"Confidence": 0.056786004453897476
},
{
"Type": "CALM",
"Confidence": 0.02734198607504368
},
{
"Type": "ANGRY",
"Confidence": 0.020567195490002632
},
{
"Type": "DISGUSTED",
"Confidence": 0.01198340579867363
},
{
"Type": "SAD",
"Confidence": 0.011844608001410961
},
{
"Type": "FEAR",
"Confidence": 0.007329543586820364
}
],
"Landmarks": [
{
"Type": "eyeLeft",
"X": 0.4020455777645111,
"Y": 0.3627050220966339
},
{
"Type": "eyeRight",
"X": 0.6262026429176331,
"Y": 0.379489928483963
},
{
"Type": "mouthLeft",
"X": 0.40419745445251465,
"Y": 0.6104526519775391
},
{
"Type": "mouthRight",
"X": 0.5907381772994995,
"Y": 0.6247860193252563
},
{
"Type": "nose",
"X": 0.49532997608184814,
"Y": 0.48828810453414917
},
{
"Type": "leftEyeBrowLeft",
"X": 0.32399997115135193,
"Y": 0.3045051097869873
},
{
"Type": "leftEyeBrowRight",
"X": 0.38662829995155334,
"Y": 0.27300384640693665
},
{
"Type": "leftEyeBrowUp",
"X": 0.4492948651313782,
"Y": 0.2880849540233612
},
{
"Type": "rightEyeBrowLeft",
"X": 0.578127920627594,
"Y": 0.29742100834846497
},
{
"Type": "rightEyeBrowRight",
"X": 0.6459962725639343,
"Y": 0.29183030128479004
},
{
"Type": "rightEyeBrowUp",
"X": 0.7144292593002319,
"Y": 0.3330812454223633
},
{
"Type": "leftEyeLeft",
"X": 0.3629233241081238,
"Y": 0.3603385388851166
},
{
"Type": "leftEyeRight",
"X": 0.4457237124443054,
"Y": 0.36826738715171814
},
{
"Type": "leftEyeUp",
"X": 0.4013364613056183,
"Y": 0.3494759500026703
},
{
"Type": "leftEyeDown",
"X": 0.40179359912872314,
"Y": 0.37347349524497986
},
{
"Type": "rightEyeLeft",
"X": 0.5811655521392822,
"Y": 0.3783351182937622
},
{
"Type": "rightEyeRight",
"X": 0.6668664813041687,
"Y": 0.38298410177230835
},
{
"Type": "rightEyeUp",
"X": 0.6265660524368286,
"Y": 0.36624279618263245
},
{
"Type": "rightEyeDown",
"X": 0.6238686442375183,
"Y": 0.39007559418678284
},
{
"Type": "noseLeft",
"X": 0.4562915861606598,
"Y": 0.5203639268875122
},
{
"Type": "noseRight",
"X": 0.5394821166992188,
"Y": 0.5265129804611206
},
{
"Type": "mouthUp",
"X": 0.4932428300380707,
"Y": 0.5806143283843994
},
{
"Type": "mouthDown",
"X": 0.48947831988334656,
"Y": 0.6564671397209167
},
{
"Type": "leftPupil",
"X": 0.4020455777645111,
"Y": 0.3627050220966339
},
{
"Type": "rightPupil",
"X": 0.6262026429176331,
"Y": 0.379489928483963
},
{
"Type": "upperJawlineLeft",
"X": 0.28082960844039917,
"Y": 0.37847602367401123
},
{
"Type": "midJawlineLeft",
"X": 0.3077985942363739,
"Y": 0.6443988680839539
},
{
"Type": "chinBottom",
"X": 0.48529136180877686,
"Y": 0.7894702553749084
},
{
"Type": "midJawlineRight",
"X": 0.7061411738395691,
"Y": 0.6732134819030762
},
{
"Type": "upperJawlineRight",
"X": 0.77140212059021,
"Y": 0.4138971269130707
}
],
"Pose": {
"Roll": 3.0064163208007812,
"Yaw": -2.569990634918213,
"Pitch": 8.883845329284668
},
"Quality": {
"Brightness": 76.55046844482422,
"Sharpness": 94.08262634277344
},
"Confidence": 99.99818420410156
}
]
Node Js code for crop using gm ImageMagick library
const init = async () => {
try {
console.info("Process Started");
const size = await getImageSize("passport-photo.jpeg");
console.info("get Image Size: ", size);
const faceDetails = await getFaceDetailsFromImage();
// Uploded image width height
const imageWidth = size.width;
const imageHeight = size.height;
// Face detail width height
const faceDetailWidth = Math.round(faceDetails[0].BoundingBox.Width * imageWidth);
const faceDetailHeight = Math.round(faceDetails[0].BoundingBox.Height * imageHeight);
// Coordinates for the mouth
const faceDetailMouthLeft = faceDetails[0].Landmarks.filter(o => o.Type === "mouthLeft");
const faceDetailMouthRight = faceDetails[0].Landmarks.filter(o => o.Type === "mouthRight");
const faceDetailMouthUp = faceDetails[0].Landmarks.filter(o => o.Type === "mouthUp");
const faceDetailMouthDown = faceDetails[0].Landmarks.filter(o => o.Type === "mouthDown");
// Find x and y point from where the cropping needs to be started
const xPoint = Math.round(faceDetailMouthLeft[0].X * imageWidth);
const yPoint = Math.round(faceDetailMouthUp[0].Y * imageHeight);
// Width height for which image needs to be cut from start index
const width = ((faceDetailMouthRight[0].X - faceDetailMouthLeft[0].X) * imageWidth)
const height = ((faceDetailMouthDown[0].Y - faceDetailMouthUp[0].Y) * imageHeight)
console.log("xPoint:" + xPoint + ", yPoint:" + yPoint + ", faceDetailWidth:" + faceDetailWidth + ", faceDetailHeight:" + faceDetailHeight)
gm('passport-photo.jpeg')
// Invoke crop function
.crop(width, height, xPoint, yPoint, true)
// Process and Write the image
.write("crop5.png", function (err) {
console.error(err);
if (!err) console.log('done');
});
} catch (error){
console.error(error);
}
}
init();
original image
Your calculations for the mouth co-ordinates appear to be correct.
However, I see you are using:
.crop(width, height, xPoint, yPoint, true)
As you say, this "accepts the parameter as a percentage value", which probably isn't what you want.

how to modify properties of an nested object?

I have the following nested object and I need to leave the "alias" property blank and the "group" property set to true for all "entries" and "exits". I also need to delete the whole "parameters" object.
Would there be a way to do it all in one function? I've tried to apply the delete Object method but it doesn't work as it's an indexed object.
{
"1": {
"x": 114,
"y": 135,
"properties": {
"id": 1,
"entries": {
"entry_0": {
"id": 1,
"alias": "do",
"group": false
}
},
"exits": {
"exit_0": {
"id": 1,
"alias": "re",
"group": false
}
},
"parameters": {
"parameter_0": {
"id": 3,
"group": false
}
},
"order": 1
}
},
"2": {
"x": 700,
"y": 104,
"properties": {
"id": 1
"entries": {
"entry_0": {
"id": 1
"alias": "do"
"group": false
}
},
"exits": {
"exyt_0": {
"id": 1
"alias": "re"
"group": false
}
},
"parameters": {
"parameter_0": {
"id": 3
"alias": "mi"
"group": false
}
},
"order": 2
}
}
}
the desired nested object would be the following
{
"1": {
"x": 114,
"y": 135,
"properties": {
"id": 1,
"entries": {
"entry_0": {
"id": 1,
"alias": "",
"group": true
}
},
"exits": {
"exit_0": {
"id": 1,
"alias": "",
"group": true
}
},
"order": 1
}
},
"2": {
"x": 700,
"y": 104,
"properties": {
"id": 1
"entries": {
"entry_0": {
"id": 1
"alias": ""
"group": true
}
},
"exits": {
"exyt_0": {
"id": 1
"alias": ""
"group": true
}
},
"order": 2
}
}
}
what I've tried is the following, managing to delete the "parameters" object but I can't access the "label" property of each "entry" and "exit
const nedtedObjectsValues = Object.values(nestedObjects);
for (object of nedtedObjectsValues) {
delete object.properties.parameters;
}
if anyone can give me an idea of how to approach this function.
Thank you in advance.
In JavaScript, to reference numeric object properties, you need to use the square brackets syntax:
object.1 // bad
object[1] // good
You can delete numeric property like this:
delete object[1];

how to stop setInterval in JS in node-red..?

I'm trying to code a function node in node-red which will give output as increasing numbers by 1 for every second when get msg.paylaod true from inject node and stop when get msg.payload "false" from another inject node.
it starts giving output but don't stop when payload "false" is injected.
Node code:
[
{
"id":"b6c9b219.90a478",
"type":"function",
"z":"a3d6aff.bd4935",
"name":"",
"func":"var i = 1;\nfunction increment(){\n i = i + 1;\n msg={payload:i};\n node.send(msg);\n \n}\nif(msg.payload===true){\nvar interval = setInterval( increment, 1000);\nif(msg.payload===false){\n clearInterval(interval);\n}\n}\n\n",
"outputs":1,
"noerr":0,
"x":700.5,
"y":581,
"wires":[
[
"12f2090c.587347"
]
]
},
{
"id":"12f2090c.587347",
"type":"debug",
"z":"a3d6aff.bd4935",
"name":"",
"active":true,
"tosidebar":true,
"console":false,
"tostatus":false,
"complete":"true",
"x":826.5,
"y":473,
"wires":[
]
},
{
"id":"26abbbf.05cf944",
"type":"inject",
"z":"a3d6aff.bd4935",
"name":"",
"topic":"",
"payload":"true",
"payloadType":"bool",
"repeat":"",
"crontab":"",
"once":false,
"onceDelay":0.1,
"x":654.88330078125,
"y":767.2833251953125,
"wires":[
[
"b6c9b219.90a478"
]
]
},
{
"id":"880341a0.dcab2",
"type":"inject",
"z":"a3d6aff.bd4935",
"name":"",
"topic":"",
"payload":"false",
"payloadType":"bool",
"repeat":"",
"crontab":"",
"once":false,
"onceDelay":0.1,
"x":643.88330078125,
"y":824.0999755859375,
"wires":[
[
"b6c9b219.90a478"
]
]
}
]
Code of function node:
var i = 1;
function increment(){
i = i + 1;
msg={value:i};
node.send(msg);
if(msg.payload===false){
clearInterval(interval);
}
}
if(msg.payload===true){
var interval = setInterval( increment, 1000);
}
The short answer is you don't (easily).
The better way to do this is by having the function send all the messages in the sequence at once using the format for sending multiple messages. Then use the delay node to rate limit the stream so the messages are released once a second.
If you REALLY need (you really don't) to do it the way you have it then you need to save the internal timer in the context and retrieve it in the increment function to call clearInterval() on it.
I solved it like this. please take look at my node code. also please suggest if it can be done some other better way.
[
{
"id": "b6c9b219.90a478",
"type": "function",
"z": "a3d6aff.bd4935",
"name": "",
"func": "var i = 0;\nfunction increment(){\n i = i + 1;\n msg={value:i};\n node.send(msg);\n if(global.get(\"a\")===\"off\"){\n clearInterval(interval);\n }\n}\n\nif(msg.payload===true){\nvar interval = setInterval( increment, 1000);\n}",
"outputs": 1,
"noerr": 0,
"x": 450.5000305175781,
"y": 508,
"wires": [
[
"12f2090c.587347"
]
]
},
{
"id": "12f2090c.587347",
"type": "debug",
"z": "a3d6aff.bd4935",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"x": 699.5,
"y": 686.9999694824219,
"wires": []
},
{
"id": "26abbbf.05cf944",
"type": "inject",
"z": "a3d6aff.bd4935",
"name": "",
"topic": "",
"payload": "true",
"payloadType": "bool",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 305.8833312988281,
"y": 430.2833557128906,
"wires": [
[
"b6c9b219.90a478",
"4fdb56dd.8ef"
]
]
},
{
"id": "880341a0.dcab2",
"type": "inject",
"z": "a3d6aff.bd4935",
"name": "",
"topic": "",
"payload": "false",
"payloadType": "bool",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 207.88333129882812,
"y": 666.0999450683594,
"wires": [
[
"4fdb56dd.8ef",
"b6c9b219.90a478"
]
]
},
{
"id": "4fdb56dd.8ef",
"type": "function",
"z": "a3d6aff.bd4935",
"name": "",
"func": "if(msg.payload===false){\n global.set(\"a\",\"off\");\n}\nif (msg.payload===true){\n global.set(\"a\",\"on\");\n}\nmsg={payload:global.get(\"a\")}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 202.26666259765625,
"y": 554.2332458496094,
"wires": [
[]
]
}
]

Categories

Resources