【问题标题】:Issue in terminating a loop终止循环的问题
【发布时间】:2020-12-24 12:41:26
【问题描述】:

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

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()

非常感谢您的帮助

【问题讨论】:

    标签: python python-2.7 loops


    【解决方案1】:

    您缺少分配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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-18
      • 1970-01-01
      • 2010-09-13
      • 2021-08-24
      • 2021-06-30
      • 2015-02-10
      相关资源
      最近更新 更多