title: How To Capture Frame With Opencv
body: <p>This is the main code that loads a pyqt gui form and it has 2 button one is for </p>
  
  <p>starting webcam and second one is for capturing photos from frame .</p>
  
  <p>I write the first button but i can't write the capture button.</p>
  
  <pre><code>import sys
  import cv2
  import numpy as np
  from PyQt5.QtCore import QTimer
  from PyQt5.QtGui import  QImage,QPixmap
  from PyQt5.QtWidgets import QApplication , QDialog
  from PyQt5.uic import loadUi
  
  img_counter = 0
  
  class video (QDialog):
      def __init__(self):
          super(video, self).__init__()
          loadUi('video.ui',self)
          self.image=None
          self.startButton.clicked.connect(self.start_webcam)
          self.capture.clicked.connect(self.keyPressEvent)
  
      def start_webcam(self):
          self.capture =cv2.VideoCapture(0)
          self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
          self.capture.set(cv2.CAP_PROP_FRAME_WIDTH,640)
  
          self.timer=QTimer(self)
          self.timer.timeout.connect(self.update_frame)
          self.timer.start(5)
  
      def update_frame(self):
          ret,self.image=self.capture.read()
          self.image=cv2.flip(self.image,1)
          self.displayImage(self.image,1)
  
      def keyPressEvent(self):
          flag, frame= self.capture.read()
          path = 'J:\Face'
          cv2.imwrite(os.path.join(path,'wakka.jpg'), frame)
  
      def displayImage(self,img,window=1):
          qformat=QImage.Format_Indexed8
          if len(img.shape)==3 :
              if img.shape[2]==4:
                  qformat=QImage.Format_RGBA8888
              else:
                  qformat=QImage.Format_RGB888
  
          outImage=QImage(img,img.shape[1],img.shape[0],img.strides[0],qformat)
  
          outImage=outImage.rgbSwapped()
  
  
          if window==1:
              self.imgLabel.setPixmap(QPixmap.fromImage(outImage))
              self.imgLabel.setScaledContents(True)
  
  if __name__=='__main__':
      app=QApplication(sys.argv)
      window=video()
      window.setWindowTitle('main code')
      window.show()
      sys.exit(app.exec_())
  </code></pre>
  
  <p>I want to capture photos from frames and save it in a folder.</p>
  
  <p>The self.capture.clicked.connect(self.keyPressEvent) is for when we clicking on button.</p>
  
  <p>I should write the function in keyPressEvent def</p>
  
  <p>the capture.is for clicking a button </p>
  
  <p>can someone help me through this?</p>
  
  <p><strong>Edit Note</strong> : </p>
  
  <pre><code>    if flag:
  
           QtWidgets.QApplication.beep(i)
           img_name = "opencv_frame_{}.png".format()
           cv2.imwrite(os.path.join(path,img_name), frame)
  </code></pre>
  
  <p>I want the condition for loop so that i can save the  <strong>img_name</strong> format with counter but the counter must be number of clicking times</p>