Convert script python to .json file

I actually was following along from the beginning... but then went away and did life stuff and didn't realise how much I had missed in the interim :blush:

Well, well, let's await the response from the TC and see what is wanted. I think I already know but I don't like to throw out various ideas without knowing the requirements. Or just provide ready-made solutions. Learning by doing is better for all

All yours :stuck_out_tongue_winking_eye:

the total time spent executing the script is 20.48 sec, when i've tried the flow (that you send me) it works just when i exec the script from my raspberry pi ( i run the script in the terminal and every distance has been calculated was sent to node red )
I want to run the script from node red not from the raspberry pi

The project is about to do an app node red to read the distance between camera and the nearly object .
in node red I've get the stream in real time from my camera without exec the script ( voilà the flow that i've been using to get the stream )

Now, i can't get the distance from the object cause i couldn't run the script from node red not from the raspberry pi

Connect debug nodes to all outputs of the exec node that is running the script, give them names so they are identifiable, and feed them into debug nodes set to show Complete Message. Then show us what they say when you try to run the script.

Right, ok

Your script has features that requires access to the display (like cv.imshow) and therefore you cannot run it in background (at least not what I know)

So you can instead start the script from Node-RED like the example here. The script will run visible on the display and you can control it from Node-RED (start and stop the script)

Assuming you have the script in /home/pi

[{"id":"f097b105.f85e5","type":"mqtt in","z":"fcecd26b.a4283","name":"","topic":"distance/result","qos":"2","datatype":"auto","broker":"56c5f0f2.9f483","nl":false,"rap":true,"rh":0,"x":220,"y":660,"wires":[["c76a1e70.41db4"]]},{"id":"c76a1e70.41db4","type":"debug","z":"fcecd26b.a4283","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":470,"y":660,"wires":[]},{"id":"8c8a7d9d.505e2","type":"inject","z":"fcecd26b.a4283","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Stop-Distance-Analyzer","payloadType":"str","x":240,"y":580,"wires":[["db5fe3a4.dbf9"]]},{"id":"db5fe3a4.dbf9","type":"mqtt out","z":"fcecd26b.a4283","name":"","topic":"distance","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"56c5f0f2.9f483","x":460,"y":580,"wires":[]},{"id":"b49ec366.f3671","type":"exec","z":"fcecd26b.a4283","command":"export DISPLAY=:0 && lxterminal -e python3 /home/pi/distance.py","addpay":"","append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":640,"y":500,"wires":[[],[],[]]},{"id":"a4c5ac80.05344","type":"inject","z":"fcecd26b.a4283","name":"Start script","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":200,"y":500,"wires":[["b49ec366.f3671"]]},{"id":"56c5f0f2.9f483","type":"mqtt-broker","name":"","broker":"127.0.0.1","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

Here in this flow it looks like you want to present the image in the dashboard. Have I understood correctly? If so you can easily send the image from script to Node-RED via mqtt. That can be the next step to do when you got the script starting from Node-RED as you want. I can show you the python code needed for that, it's very simple now that you have mqtt working :wink:

i want to : - stream in real time
- detect object in real time
- send distance to the object in real time
the script that i wrote in python is doing all of this but i want an interface in node red
I've tried the flow that u sent me , it shows this error
image

I've tried the same flow with another python script (sending a random number every sec ) and it works , i think that the problem is about the script itself :frowning:

I this script something you can share? I have a few camera streams going, perhaps one of them will be sufficient to test with and see if I can get some form of Node-Red interface working.

distance.txt (4.7 KB)
to run it u should :
1-install the application "ip webcam" on your phone to run the script cause i've been using the phone camera and change the ip of camera
2-install this files: coco.names , yolov3-tiny.cfg yolov3-tiny.weights yolov3.cfg and yolov3.weights

1 Like

I think I bit off more than my little old RPi3 can chew :blush: I spent all this time getting the required import files loaded... only to realise (based on my reading so far) that even tiny-yolo seems to require an RPi4? and even some form of add-on hardware like a Intel Movidius NCS?

I am already running Tensorflow Coco SSD (with allocation warnings that it is exceeding 10% of system memory) which works great, but is tasking my RPi3 if I hit it with too many single image object detections in a row, and if not careful, locking it up requiring a reboot.

I thought this might be in the same range of doable operation, but apparently not :frowning:

So again, sorry, but I think I need to stand back and just watch this topic from a safe distance :telescope: :innocent:

This looks like you have some issue with path to the ref image. It looks like the script cannot find the ref_image in "ref_image = cv.imread("Ref_image.png")". Double check the file name!!
The flow I sent you was an example. It looks like you have other required files etc located in "/home/pi/Desktop". If so, you shall place the script there as well and change the command line in the exec node to the same. You have to have the script in the correct place so it can read all files it requires

image

The script works as expected for me. I modified it just a bit to use a connected usb camera instead, it works fine, it measures the distance and reports it to NR via mqtt without any problems. I then also changed it to use yolov4-tiny files, and have all files in "/home/pi" running on a RPi4, no problems

BTW The script you shared in your last comment is the old one, without the mqtt stuff so not so useful in integration with NR

@Gunner The RPi is a bit weak for this type of exercise but it works if you can accept that the analyze takes a bit longer and that the cpu load is a bit high. Using a Movidius or similar, like a Jetson Nano, where you utilize the powerof GPU's is of course great. I have earlier tried running yolo analyzing also in a RPi3, it works, a bit slow, but you can't have much other stuff running at the same time

i'm using a raspb4 , i've tried to put the script and all the files needed in the same folder but in vain :pleading_face:

The prob is in the path of the ref_image , Thank u so much , IT WORKS !!!

Excellent, that is good news! Just happy it worked, shows the power of mqtt, you can develop many services in Python, run them like this or in background and integrate with Node-RED in a simple & efficient way

1 Like

yes but how can i stop running the script with node red ?

In the flow I sent you, there is an inject node a named "Stop-Distance-Analyzer". If you click on the button trigger, it send the message to your script via mqtt, the scripts recognizes the message, sets th_abort to True, the script terminates the thread and itself. You see it in the python code

and if i want to send the object name with the distance, should i just modif this line in python "client.subscribe("distance", 2)" ?