终止循环的问题

Issue in terminating a loop

我有以下代码读取比文件夹中更多的帧。当从文件夹中读取所有图像而不是再次进入循环并再次读取帧时,我面临停止的问题我需要停止这个循环。

IMAGE_FOLDER="frames"
filenames = [f"{IMAGE_FOLDER}/frame{i}.jpg" for i in range(1, 2522)]
filenames = ["{}/frame{}.jpg".format(IMAGE_FOLDER,i) for i in range(1, 2522)]
SERVER_A_ADDRESS = "tcp://localhost:5555"
context = zmq.Context()
socket_server_a = context.socket(zmq.PUSH)
socket_server_a.connect(SERVER_A_ADDRESS)
destination = {
 "currentSocket": socket_server_a,}
running = True
endpoint_responses = 0
frames= 0
def send_frame(frame, frame_requests):
 global destination, running
 try:
    frame = cv2.resize(frame, (224, 224))
    encoded, buffer = cv2.imencode('.jpg', frame)
    jpg_as_text = base64.b64encode(buffer)
    print("{} ( i ) :  Sending frame {} to {}...".format(time.strftime('%H:%M:%S'),frame_requests))
    destination["currentSocket"].send(jpg_as_text)
 
 except Exception as Error:
    print("{} ( ! ) :  Encountered Error at frame {}\n\n> KILLING CONNECTIONS\n\nERROR MESSAGE:  {Error}".format(time.strftime('%H:%M:%S'),frame_requests,Error))
    running = False

def main():
 global destination, running, frames
 interval = 1 / 10
 while running:
    for img in filenames:
        frames = cv2.imread(img)
        frames += 1
        print("{} ( i ) :  frames count = {}".format(time.strftime('%H:%M:%S'),frames))
        threading.Thread(target=send_frame, args=(frame, frames)).start()
        time.sleep(interval)
 destination["currentSocket"].close()
if __name__ == "__main__":
  main()

非常感谢帮助

您缺少分配 running = Fasle 一个循环已完成。

试试这个:

def main():
 global destination, running, frames
 interval = 1 / 10
 while running:
    for img in filenames:
        frames = cv2.imread(img)
        frames += 1
        print("{} ( i ) :  frames count = {}".format(time.strftime('%H:%M:%S'),frames))
        threading.Thread(target=send_frame, args=(frame, frames)).start()
        time.sleep(interval)
     running = False
 destination["currentSocket"].close()