Created
March 29, 2020 21:01
-
-
Save Unitech/e5ef3f015bef7021da9322dd8947355f to your computer and use it in GitHub Desktop.
Communication node<>python via JSON
This file contains hidden or 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
process.env.PYTHONUNBUFFERED = '1' | |
const spawn = require('child_process').spawn | |
const path = require('path') | |
var pythonExec = function(algo_file, params, cb) { | |
var init_data = params.init_data | |
var receivedData = params.receivedData | |
var dataString = '' | |
var py = spawn('python3', ['-u', algo_file], { | |
env: process.env, | |
shell : true | |
}) | |
py.on('error', (error) => { | |
cb(error) | |
}) | |
py.stderr.on('data', (data) => { | |
console.error(`[ERR] ${data}`) | |
}) | |
py.stdout.on('data', function(data){ | |
if (data == 'ready\n') { | |
py.stdin.write(JSON.stringify(init_data)) | |
py.stdin.write('\n'); | |
return | |
} | |
data = data.toString() | |
receivedData(data) | |
}) | |
py.stdout.on('end', function(){ | |
py.stdin.end() | |
cb(null, dataString) | |
}) | |
} | |
if (require.main === module) { | |
var init_data = { | |
"params": { | |
"percent": 0.9, | |
"kernel": 2 | |
} | |
} | |
pythonExec('./worker.py', { | |
init_data: init_data, | |
receivedData: (data) => { | |
console.log('Received Data cb') | |
console.log(data) | |
} | |
}, (err, res) => { | |
console.log('Script ended') | |
console.log(`Error = ${err}`) | |
}) | |
} | |
This file contains hidden or 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
import cv2 | |
import numpy as np | |
import time | |
from detector import MotionDetector | |
from packer import pack_images | |
import json | |
import os | |
import sys | |
import numpy as np | |
def read_in_v2(): | |
k = 0 | |
try: | |
buff = '' | |
while True: | |
buff += sys.stdin.read(1) | |
if buff.endswith('\n'): | |
return json.loads(buff[:-1]) | |
buff = '' | |
k = k + 1 | |
except KeyboardInterrupt: | |
sys.stdout.flush() | |
pass | |
return json.loads(lines[0]) | |
def processing(): | |
print('ready') | |
obj = read_in_v2() | |
print(json.dumps(obj)) | |
time.sleep(4) | |
print(json.dumps(obj)) | |
if __name__ == "__main__": | |
processing() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment