0

I have a function where I detect a point from a video image and draw a dot on the frame. Now I need the x,y position of the dot elsewhere, but I can't get the information I need out of the function because of the while loop. As the code below is right now, the function only returns the last known value after the video stops. I also tried to put the return statement in the while loop, but the loop breaks because of the return statement. I'm talking about the xy_side that I need somewhere out of the function and I need it at real time (so not storing all the values in a list and showing the list afterwards). Can someone help me? The code is written with python.

def det_point(folder,fn,model):
    cap = cv2.VideoCapture("./" + folder + "/" + fn)
    red = (0, 0, 255)
    while(cap.isOpened()):
        ret, frame = cap.read()
        crds = detect_point_prop(frame,model)
        cntr_crds = float_to_int(crds[0])
        start_crds = float_to_int(crds[1])
        end_crds = float_to_int(crds[2])
        frame = cv2.circle(frame, cntr_crds, 3, red, 5)
        frame = cv2.rectangle(frame, start_crds, end_crds, green, 5)
        cv2.imshow("Image", frame)
        xy_side = cntr_crds
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    return  xy_side
JarneH
  • 33
  • 5

1 Answers1

0

I would suggest you use a thread-safe queue. In your case, you could pass the queue to det_point, which would push values onto the queue. You could then run a consumer in another thread to use the values that were put in the queue by det_point.

The python queue library has a good example of how to start a thread that will call a consumer.

https://docs.python.org/3/library/queue.html#queue.Queue.join

import threading, queue

q = queue.Queue()

def worker():
    while True:
        item = q.get()
        print(f'Working on {item}')
        print(f'Finished {item}')
        q.task_done()

# turn-on the worker thread
threading.Thread(target=worker, daemon=True).start()

# send thirty task requests to the worker for item in range(30):
q.put(item) print('All task requests sent\n', end='')

# block until all tasks are done q.join() print('All work completed')

In your case, the function requiring the output values from det_point would replace the worker function. Additionally, I would pass the queue as an argument to the worker thread rather than using a global variable.

jisrael18
  • 719
  • 3
  • 10