Because its a really big program and it would cost weeks to implement this into node-red completely
I know of no way of doing that but maybe someone else will have an idea
Sure you can. The code you provided is asking the user for an input. This means you run it on a display if I have understood correctly???
Then you have to start the python program in such way so it runs on the display. My example, test.py, with your code, works fine running in a RPi as you can see on the screen shot below. I added a short delay to allow you to see the result since the program closes otherwise when the execution is finished
If you want to give the program the input from NR and see the result returned back, you have to introduce communication with your python program. Inputs can be sent as arguments and return of the result, well I would use MQTT
import time
def main():
user_input = input('Hello User, please write a number: ')
print('Your input was ' + user_input)
if __name__ == '__main__':
main()
time.sleep(5)
[{"id":"1ad354d8.5ea0cb","type":"exec","z":"797a7c58.e82134","command":"export DISPLAY=:0 && lxterminal -e python3 /home/pi/test.py","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":760,"y":450,"wires":[[],[],[]]},{"id":"65c511f.996e8f","type":"inject","z":"797a7c58.e82134","name":"Go","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":450,"wires":[["1ad354d8.5ea0cb"]]}]
@krambriw Walter, what if NR is running on the Pi, but you are connected to on a browser from device B? Won't it open the window on the Pi and not in the dashboard of the browser B?
If I understand the OP he wants the questions to show up in the node-RED Dashboard.
Well, if that's the case, sure is possible, then there has to be two-way communication between the Python program and NR. I would then extend the Python program to use MQTT for this purpose
Well the node-red-node-daemon may be better suited as it provides stdin and stdout so you can both listen and reply to a long running process.
This is a little xample with NR communicating with a python script using mqtt
With "Go" you start the python script, then visible on the display
From the dashboard you pick a number that is sent to the script, returned back and displayed as the result
With "abort" you terminate the python script
[{"id":"ea37930f.f3f81","type":"exec","z":"8627eb61.169ee8","command":"export DISPLAY=:0 && lxterminal -e python3 /home/pi/test.py","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":550,"y":1280,"wires":[[],[],[]]},{"id":"c8a452e7.c8ba7","type":"mqtt in","z":"8627eb61.169ee8","name":"","topic":"from_python","qos":"2","datatype":"auto","broker":"75eba16c.094f9","x":150,"y":1500,"wires":[["e677846b.f968e8","fcc16d87.c21e1"]]},{"id":"1c4bbd7b.ee5dc3","type":"inject","z":"8627eb61.169ee8","name":"Go","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":1280,"wires":[["ea37930f.f3f81"]]},{"id":"e677846b.f968e8","type":"debug","z":"8627eb61.169ee8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":370,"y":1560,"wires":[]},{"id":"9bbac985.bba978","type":"mqtt out","z":"8627eb61.169ee8","name":"","topic":"to_python","qos":"","retain":"","broker":"75eba16c.094f9","x":380,"y":1360,"wires":[]},{"id":"b90a472b.2d32f8","type":"ui_numeric","z":"8627eb61.169ee8","name":"","label":"pick a number","tooltip":"","group":"b367811d.6667b","order":1,"width":0,"height":0,"passthru":true,"topic":"","format":"{{value}}","min":0,"max":10,"step":1,"x":160,"y":1360,"wires":[["9bbac985.bba978"]]},{"id":"fcc16d87.c21e1","type":"ui_text","z":"8627eb61.169ee8","group":"b367811d.6667b","order":2,"width":0,"height":0,"name":"","label":"result","format":"{{msg.payload}}","layout":"row-spread","x":370,"y":1500,"wires":[]},{"id":"ad31a88a.314728","type":"inject","z":"8627eb61.169ee8","name":"","topic":"","payload":"abort","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":1430,"wires":[["9bbac985.bba978"]]},{"id":"75eba16c.094f9","type":"mqtt-broker","z":"","name":"","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"b367811d.6667b","type":"ui_group","z":"","name":"User inputs & results","tab":"f1725e1b.29ba","disp":true,"width":"8","collapse":false},{"id":"f1725e1b.29ba","type":"ui_tab","z":"","name":"NR to Python","icon":"dashboard","disabled":false,"hidden":false}]
And the script....
import time
import paho.mqtt.client as mqtt
abort = False
def on_connect(client, obj, flags, rc):
client.subscribe("to_python", 0)
def on_subscribe(client, userdata, mid, granted_qos):
print ('Subscribed:', userdata, mid, granted_qos)
print ('We are here, waiting for commands...')
def on_message(client, obj, msg):
global abort
print (msg.payload)
if msg.payload.decode("utf-8") == "abort":
abort = True
return
client.publish("from_python", 'Your input was '+str(msg.payload.decode("utf-8")), 1, False)
def main():
client = mqtt.Mosquitto()
client.on_connect = on_connect
client.on_message = on_message
client.on_subscribe = on_subscribe
client.connect("127.0.0.1", 1883, 60)
client.loop_start()
client.publish("from_python", 'Hello user, please pick a number: ', 1, False)
while abort == False:
time.sleep(1)
client.loop_stop()
client.disconnect()
del client
exit(0)
if __name__ == '__main__':
main()
This is helping me understand a lot , its been really helpfull.
So what i need to do is tell NR to start my Python Script, from that script i need to be able to extract who was recognised (as its facial recognition). And get that sent back to NR.
Still figuring it out but feel like im making progress
the "other" way - is for your python script to be running separately all the time (but alongside) ... waiting for an mqtt message to arrive (containing and image buffer) in the on_Message call above - process it and send the answer back (using client.publish) ... And likewise for Node-RED to publish the image out to the required topic and subscribe to the answer topic coming back.
Do you know of any exmaples like this?
Maybe misunderstood, but is this for a lesson you have at college? Or is it for the monitoring of vechiles arriving at he college?
Its a project im doing for college, essentially only allowing authorized persons enter through the barrier
OK understood. If you take the script above, it is a script that will run continously until you "kill it" (if you send a mqtt message "abort" to it, it will terminate gracefully)
Now you can combine this script with the functionality of yours. In your script you read frames from your camera and detects and checks faces. If you get a match you just publish that back to NR via mqtt
What you also could do, I think this is what @dceejay meant, is to feed the script with image data from other sources. You then convert the image data into a buffer in NR and sends it to the script via mqtt. It will arrive in the "on_message" callback function where you could add necessary code or another function call to do the checkup
I wont run my script, only bring me to my script, iv tried "_run" after geany and just get a blank terminal
How would do i run a python script all of the time?
Please understand i am new to all of this
How would do i run a python script all of the time?
Please understand i am new to all of this
My script above will run until you abort it by sending a mqtt message ''abort" (or kill it). Read the python code and you will see, there is a loop at the end
while abort == False:
time.sleep(1)
Ah yes i see that.
But my script wont run, it just brings me to my script in Geany. Im obviously missing something to run it
I need to be able to get it running in the background before i link the python and NR with mqtt
Are you good at python? You'll fix it