title: OpenCV error when trying to capture frame every seconds
body: <p>I am trying to capture an image every 5 seconds in Opencv using my webcam however I am getting an error whenever I try.</p>
  <p>Python Code:</p>
  <pre class="lang-py prettyprint-override"><code>def imgcap():
      cap = cv2.VideoCapture(0)
      framerate = cap.get(5)
      x=1
  
      while(True):
          # Capture frame-by-frame
          ret, frame = cap.read()
          if ret:
              # Our operations on the frame come here
  
              filename = 'Captures/capture' +  str(int(x)) + &quot;.png&quot;
              x=x+1
              cv2.imwrite(filename, frame)
              time.sleep(5)
              if cv2.waitKey(1) &amp; 0xFF == ord('q'):
                  break
          else:
              print(&quot;Ret False&quot;)
  
      # When everything done, release the capture
      cap.release()
      cv2.destroyAllWindows()
  
  imgcap()
  
  imgcap()
  </code></pre>
  <p>Error I am Getting:</p>
  <pre><code>  File &quot;vision.py&quot;, line 30, in &lt;module&gt;
      imgcap()
    File &quot;vision.py&quot;, line 21, in imgcap
      cv2.imwrite(filename, frame)
  cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-oduouqig\opencv\modules\imgcodecs\src\loadsave.cpp:753: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'
  </code></pre>