Python Forum
Python Threading - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python Threading (/thread-705.html)



Python Threading - Amro_Al - Oct-30-2016

I am a beginner in programming but I was able to develop a tracking program in python. It finds and tracks a T shape in a video and return its coordinates (cX, cY) and Orientation (angle). The code itself is working, however, I want to put it on a loop or thread that gives me an update of the values of those variables (cX, cY, angle). I am supposed to send this information every 30 ms to an Arduino through a port (or any other method, I am not really fussy about how). All I need is to see the variables even if they are saved in 


I know that this is possible with threading but as a beginner, I have never used threading. The more I read about it the more confused I got. everywhere I read it says threading is not for beginners. I tried understanding how it works but I am getting nowhere. 
The code is about 180 lines so instead of copying it all here, I will just put the main function that calls on the different ones used for the detection and tracking. If you need the whole code the please tell me.


def main ():
    loadShape()
    camera = cv2.VideoCapture("IR-Tracking.mov")
    total_start_time = time.time()
    fps_start_time = time.time()
    fps = 0
    total_fps = 0
    while makeIteration(camera):
        time_now = time.time()
        fps = fps + 1
        if time_now - fps_start_time > 1:
           print "FPS:", fps
           fps_start_time = time_now
           fps = 0

    seconds = int(time.time() - total_start_time)
    print "Total matches:",total_matches, "in", seconds, "seconds"

if __name__ == '__main__':
    main()
If it helps, I am using python 2.7 and not 3.

Hope anyone can help me. thnax


RE: Python Threading - Ofnuts - Oct-30-2016

Why do you need threading? If you can locate the T in less than 30ms and if you don't need millisecond accuracy on the Arduino notification and if the Arduino comms are quick then this is superfluous

Threading is complicated when your threads write to the same variables. But here you would have one thread that periodically extracts the 'T' position and makes a tuples out of it, and a second thread that runs every 30ms, gets the tuple, and sends it to the Arduino. Not that using a tuple here is already a thread-aware tecnique, if you were using three different variables for X,Y,A, the Arduino thread could be reading them exactly when the video analysis thread updates them and obtain X,Y from the previous extract and A from the new extract. Using a tuple(*) ensures that all three values aere collected together.

(*) and not a list... the tuple contents are immutable...


RE: Python Threading - Amro_Al - Oct-30-2016

(Oct-30-2016, 10:48 AM)Ofnuts Wrote: Why do you need threading? If you can locate the T in less than 30ms and if you don't need millisecond accuracy on the Arduino notification and if the Arduino comms are quick then this is superfluous

Threading is complicated when your threads write to the same variables. But here you would have one thread that periodically extracts the 'T' position and makes a tuples out of it, and a second thread that runs every 30ms, gets the tuple, and sends it to the Arduino. Not that using a tuple here is already a thread-aware tecnique, if you were using three different variables for X,Y,A, the Arduino thread could be reading them exactly when the video analysis thread updates them and obtain X,Y from the previous extract and A from the new extract. Using a tuple(*) ensures that all three values aere collected together.

(*) and not a list... the tuple contents are immutable...

that is a great idea. I never really thought of it as 2 different threads. I understand the simplicity behind it. 

As I am new to programming in general though, please bare with me if this question is too stupid or has an answer that is too obvious, but a tuple would be updated every time the variables are calculated right?. It isn't accumulative, as in it will not stack them up. It will always have the last calculated variables only. right? 

Finally, will I be using Queues? I would really appreciate it if you could point me in the right direction with examples or maybe a link to a good tutorial. I feel way over my head in this part.

Thanx


RE: Python Threading - Ofnuts - Oct-30-2016

(Oct-30-2016, 12:50 PM)Amro_Al Wrote: that is a great idea. I never really thought of it as 2 different threads. I understand the simplicity behind it. 

As I am new to programming in general though, please bare with me if this question is too stupid or has an answer that is too obvious, but a tuple would be updated every time the variables are calculated right?. It isn't accumulative, as in it will not stack them up. It will always have the last calculated variables only. right? 

Finally, will I be using Queues? I would really appreciate it if you could point me in the right direction with examples or maybe a link to a good tutorial. I feel way over my head in this part.

Thanx

1) One of the two threads is the main code flow... you just start a second thread to feed the Arduino on a regular basis.

2) You get a new tuple each time, there is no update of the tuple itself. But the variable that is shared between the threads is a tuple reference, and this reference is updated to point to the new tuple. And yes, when you look at it you only get the last instance of the calculated values.

3) queues are not necessary unless you have a high variation of execution time in the consumer side (Arduino thread) and don't want to lose an item.