使用while循环从函数中获取输出以在其他地方使用(python)

使用while循环从函数中获取输出以在其他地方使用(python),python,function,while-loop,return,break,Python,Function,While Loop,Return,Break,我有一个功能,我从视频图像中检测一个点,并在帧上画一个点。现在我需要点在其他地方的x,y位置,但由于while循环,我无法从函数中获得所需的信息。由于下面的代码是现在,该函数只返回视频停止后的最后一个已知值。我还尝试将return语句放入while循环中,但由于return语句,循环中断。我说的是xy_端,我需要在函数之外的某个地方实时使用它(因此不要将所有值存储在列表中,然后再显示列表)。 有人能帮我吗? 代码是用python编写的 def det_point(folder,fn,model)

我有一个功能,我从视频图像中检测一个点,并在帧上画一个点。现在我需要点在其他地方的x,y位置,但由于while循环,我无法从函数中获得所需的信息。由于下面的代码是现在,该函数只返回视频停止后的最后一个已知值。我还尝试将return语句放入while循环中,但由于return语句,循环中断。我说的是xy_端,我需要在函数之外的某个地方实时使用它(因此不要将所有值存储在列表中,然后再显示列表)。 有人能帮我吗? 代码是用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

我建议您使用线程安全队列。在您的情况下,您可以将队列传递到
det_point
,这将把值推送到队列上。然后,您可以在另一个线程中运行使用者,以使用
det_point
放入队列中的值

python队列库提供了一个很好的示例,说明如何启动调用使用者的线程


在您的情况下,需要从
det_point
输出值的函数将替换辅助函数。此外,我会将队列作为参数传递给工作线程,而不是使用全局变量。

这是什么语言?Python,很抱歉没有提到这一点
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')