代码之家  ›  专栏  ›  技术社区  ›  bear

在MacOS中保存照片时,照片应用程序会冻结并需要很长时间才能保存

  •  0
  • bear  · 技术社区  · 2 年前

    这是我的代码:

        import os
        import cv2
        import uuid
        import time
    
    
        camera = cv2.VideoCapture(0)
    
        POZ_Dosya = os.path.join("veriler", "pozitif")
        NEG_Dosya = os.path.join("veriler", "negatif")
        Temel_Dosya = os.path.join("veriler", "temel")
        os.makedirs(POZ_Dosya, exist_ok=True)
        os.makedirs(NEG_Dosya, exist_ok=True)
        os.makedirs(Temel_Dosya, exist_ok=True)
    
        target_size = 250
    
        while camera.isOpened():
            ret, frame = camera.read()
    
            scale_percent = 33 
            width = int(frame.shape[1] * scale_percent / 100)
            height = int(frame.shape[0] * scale_percent / 100)
            dim = (width, height)
            frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)
    
            height, width, _ = frame.shape
    
            start_row = max(0, int((height - target_size) / 2))
            end_row = min(height, start_row + target_size)
            start_col = max(0, int((width - target_size) / 2))
            end_col = min(width, start_col + target_size)
    
            frame = frame[start_row:end_row, start_col:end_col, :]
            frame = cv2.resize(frame, (target_size, target_size))
            cv2.imshow("a", frame)
            
            if cv2.waitKey(1) & 0xFF == ord("a"):
                time.sleep(10)
                imagename = os.path.join(Temel_Dosya, '{}.jpg'.format(uuid.uuid1()))
                cv2.imwrite(imagename, frame)
            
            if cv2.waitKey(1) & 0xFF == ord("p"):
                pass
            
            if cv2.waitKey(1) & 0xFF == ord("q"):
                break
            
        camera.release()
        cv2.destroyAllWindows()
    
    
    

    My computer

    我该如何解决这个问题。

    提前谢谢。

    我试图为数据集拍摄大量照片,但保存时会冻结,需要很长时间才能保存。我添加了

     "editor.formatOnSave": false,
     "editor.codeActionsOnSave": {
            "source.organizeImports": "never"
    

    到我的设置.js上的VS代码仍然保持着

    2 回复  |  直到 2 年前
        1
  •  0
  •   Muhammad Nabeegh    2 年前

    看起来冻结问题可能与您正在使用的事实有关 cv2.imshow 和 cv2.waitKey 在循环中,关键事件处理可能会导致延迟。此外 time.sleep(10) 保存图像后可能会导致冻结。

    以下是一些优化并可能解决冻结问题的建议:

    1. 移动 cv2.imshow 循环外: 在循环外显示图像,以减少每次迭代中更新显示的开销。

      while camera.isOpened():
          # ... (your existing code)
          cv2.imshow("a", frame)
      cv2.destroyAllWindows()
      
    2. 避免多重 cv2.waitKey 电话: 将您的关键事件检查合并为一个 cv2.waitKey 调用以防止重复检查。

      while camera.isOpened():
          # ... (your existing code)
          key = cv2.waitKey(1) & 0xFF
          if key == ord("a"):
              time.sleep(10)
              imagename = os.path.join(Temel_Dosya, '{}.jpg'.format(uuid.uuid1()))
              cv2.imwrite(imagename, frame)
          elif key == ord("p"):
              pass
          elif key == ord("q"):
              break
          cv2.imshow("a", frame)
      cv2.destroyAllWindows()
      
    3. 仅在必要时检查关键事件: 您可以将关键事件处理移动到要捕获图像的特定条件,而不是在每次迭代中检查关键事件。

      while camera.isOpened():
          # ... (your existing code)
          cv2.imshow("a", frame)
      
          key = cv2.waitKey(1) & 0xFF
          if key == ord("a"):
              time.sleep(10)
              imagename = os.path.join(Temel_Dosya, '{}.jpg'.format(uuid.uuid1()))
              cv2.imwrite(imagename, frame)
          elif key == ord("q"):
              break
      
      cv2.destroyAllWindows()
      

    请确保测试这些更改,看看冻结问题是否仍然存在。如果问题仍然存在,您可能需要探索更具体的分析和调试技术,以确定代码中的瓶颈。