Python: How To Capture Frame With Opencv in pyqt5











up vote
2
down vote

favorite












This is the main code that loads a pyqt gui form and it has 2 button one is for



starting webcam and second one is for capturing photos from frame .



I write the first button but i can't write the capture button.



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


I want to capture photos from frames and save it in a folder.



The self.capture.clicked.connect(self.keyPressEvent) is for when we clicking on button.



I should write the function in keyPressEvent def



the capture.is for clicking a button



can someone help me through this?



Edit Note :



    if flag:

QtWidgets.QApplication.beep(i)
img_name = "opencv_frame_{}.png".format()
cv2.imwrite(os.path.join(path,img_name), frame)


I want the condition for loop so that i can save the img_name format with counter but the counter must be number of clicking times










share|improve this question




























    up vote
    2
    down vote

    favorite












    This is the main code that loads a pyqt gui form and it has 2 button one is for



    starting webcam and second one is for capturing photos from frame .



    I write the first button but i can't write the capture button.



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


    I want to capture photos from frames and save it in a folder.



    The self.capture.clicked.connect(self.keyPressEvent) is for when we clicking on button.



    I should write the function in keyPressEvent def



    the capture.is for clicking a button



    can someone help me through this?



    Edit Note :



        if flag:

    QtWidgets.QApplication.beep(i)
    img_name = "opencv_frame_{}.png".format()
    cv2.imwrite(os.path.join(path,img_name), frame)


    I want the condition for loop so that i can save the img_name format with counter but the counter must be number of clicking times










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      This is the main code that loads a pyqt gui form and it has 2 button one is for



      starting webcam and second one is for capturing photos from frame .



      I write the first button but i can't write the capture button.



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


      I want to capture photos from frames and save it in a folder.



      The self.capture.clicked.connect(self.keyPressEvent) is for when we clicking on button.



      I should write the function in keyPressEvent def



      the capture.is for clicking a button



      can someone help me through this?



      Edit Note :



          if flag:

      QtWidgets.QApplication.beep(i)
      img_name = "opencv_frame_{}.png".format()
      cv2.imwrite(os.path.join(path,img_name), frame)


      I want the condition for loop so that i can save the img_name format with counter but the counter must be number of clicking times










      share|improve this question















      This is the main code that loads a pyqt gui form and it has 2 button one is for



      starting webcam and second one is for capturing photos from frame .



      I write the first button but i can't write the capture button.



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


      I want to capture photos from frames and save it in a folder.



      The self.capture.clicked.connect(self.keyPressEvent) is for when we clicking on button.



      I should write the function in keyPressEvent def



      the capture.is for clicking a button



      can someone help me through this?



      Edit Note :



          if flag:

      QtWidgets.QApplication.beep(i)
      img_name = "opencv_frame_{}.png".format()
      cv2.imwrite(os.path.join(path,img_name), frame)


      I want the condition for loop so that i can save the img_name format with counter but the counter must be number of clicking times







      python opencv pyqt pyqt5






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 at 22:00

























      asked Nov 18 at 20:27









      ghostDs

      125




      125
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          keyPressEvent is a method that allows you to capture the keys while the widget has the focus, and in your case it is not necessary, the solution is simple change its name, on the other hand I have improved your code.



          import os
          import cv2
          import numpy as np
          from PyQt5 import QtCore, QtGui, QtWidgets, uic

          class video (QtWidgets.QDialog):
          def __init__(self):
          super(video, self).__init__()
          uic.loadUi('video.ui',self)
          self.startButton.clicked.connect(self.start_webcam)
          self.capture.clicked.connect(self.capture_image)
          self.imgLabel.setScaledContents(True)
          self.capture = None
          self.timer = QtCore.QTimer(self, interval=5)
          self.timer.timeout.connect(self.update_frame)
          self._image_counter = 0

          @QtCore.pyqtSlot()
          def start_webcam(self):
          if self.capture is None:
          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.start()

          @QtCore.pyqtSlot()
          def update_frame(self):
          ret, image=self.capture.read()
          simage = cv2.flip(image, 1)
          self.displayImage(image, True)

          @QtCore.pyqtSlot()
          def capture_image(self):
          flag, frame= self.capture.read()
          path = r'J:Face'
          if flag:
          QtWidgets.QApplication.beep()
          name = "opencv_frame_{}.png".format(self._image_counter)
          cv2.imwrite(os.path.join(path, name), frame)
          self._image_counter += 1

          def displayImage(self, img, window=True):
          qformat = QtGui.QImage.Format_Indexed8
          if len(img.shape)==3 :
          if img.shape[2]==4:
          qformat = QtGui.QImage.Format_RGBA8888
          else:
          qformat = QtGui.QImage.Format_RGB888
          outImage = QtGui.QImage(img, img.shape[1], img.shape[0], img.strides[0], qformat)
          outImage = outImage.rgbSwapped()
          if window:
          self.imgLabel.setPixmap(QtGui.QPixmap.fromImage(outImage))

          if __name__=='__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          window = video()
          window.setWindowTitle('main code')
          window.show()
          sys.exit(app.exec_())





          share|improve this answer























          • thanks for your big help and quick response ,can i ask a little favor? how to loop the capture_image for multiple capturing?
            – ghostDs
            Nov 18 at 21:46










          • make a for loop: for i in range(some_number): flag, frame= self.capture.read() path = r'J:Face' if flag: ...
            – eyllanesc
            Nov 18 at 21:51










          • look at the edit note ,thanks
            – ghostDs
            Nov 18 at 21:57










          • @ghostDs How many images do you want to save the for loop?
            – eyllanesc
            Nov 18 at 22:10










          • as many times as the user click the capture button
            – ghostDs
            Nov 18 at 22:12











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53365109%2fpython-how-to-capture-frame-with-opencv-in-pyqt5%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          keyPressEvent is a method that allows you to capture the keys while the widget has the focus, and in your case it is not necessary, the solution is simple change its name, on the other hand I have improved your code.



          import os
          import cv2
          import numpy as np
          from PyQt5 import QtCore, QtGui, QtWidgets, uic

          class video (QtWidgets.QDialog):
          def __init__(self):
          super(video, self).__init__()
          uic.loadUi('video.ui',self)
          self.startButton.clicked.connect(self.start_webcam)
          self.capture.clicked.connect(self.capture_image)
          self.imgLabel.setScaledContents(True)
          self.capture = None
          self.timer = QtCore.QTimer(self, interval=5)
          self.timer.timeout.connect(self.update_frame)
          self._image_counter = 0

          @QtCore.pyqtSlot()
          def start_webcam(self):
          if self.capture is None:
          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.start()

          @QtCore.pyqtSlot()
          def update_frame(self):
          ret, image=self.capture.read()
          simage = cv2.flip(image, 1)
          self.displayImage(image, True)

          @QtCore.pyqtSlot()
          def capture_image(self):
          flag, frame= self.capture.read()
          path = r'J:Face'
          if flag:
          QtWidgets.QApplication.beep()
          name = "opencv_frame_{}.png".format(self._image_counter)
          cv2.imwrite(os.path.join(path, name), frame)
          self._image_counter += 1

          def displayImage(self, img, window=True):
          qformat = QtGui.QImage.Format_Indexed8
          if len(img.shape)==3 :
          if img.shape[2]==4:
          qformat = QtGui.QImage.Format_RGBA8888
          else:
          qformat = QtGui.QImage.Format_RGB888
          outImage = QtGui.QImage(img, img.shape[1], img.shape[0], img.strides[0], qformat)
          outImage = outImage.rgbSwapped()
          if window:
          self.imgLabel.setPixmap(QtGui.QPixmap.fromImage(outImage))

          if __name__=='__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          window = video()
          window.setWindowTitle('main code')
          window.show()
          sys.exit(app.exec_())





          share|improve this answer























          • thanks for your big help and quick response ,can i ask a little favor? how to loop the capture_image for multiple capturing?
            – ghostDs
            Nov 18 at 21:46










          • make a for loop: for i in range(some_number): flag, frame= self.capture.read() path = r'J:Face' if flag: ...
            – eyllanesc
            Nov 18 at 21:51










          • look at the edit note ,thanks
            – ghostDs
            Nov 18 at 21:57










          • @ghostDs How many images do you want to save the for loop?
            – eyllanesc
            Nov 18 at 22:10










          • as many times as the user click the capture button
            – ghostDs
            Nov 18 at 22:12















          up vote
          1
          down vote



          accepted










          keyPressEvent is a method that allows you to capture the keys while the widget has the focus, and in your case it is not necessary, the solution is simple change its name, on the other hand I have improved your code.



          import os
          import cv2
          import numpy as np
          from PyQt5 import QtCore, QtGui, QtWidgets, uic

          class video (QtWidgets.QDialog):
          def __init__(self):
          super(video, self).__init__()
          uic.loadUi('video.ui',self)
          self.startButton.clicked.connect(self.start_webcam)
          self.capture.clicked.connect(self.capture_image)
          self.imgLabel.setScaledContents(True)
          self.capture = None
          self.timer = QtCore.QTimer(self, interval=5)
          self.timer.timeout.connect(self.update_frame)
          self._image_counter = 0

          @QtCore.pyqtSlot()
          def start_webcam(self):
          if self.capture is None:
          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.start()

          @QtCore.pyqtSlot()
          def update_frame(self):
          ret, image=self.capture.read()
          simage = cv2.flip(image, 1)
          self.displayImage(image, True)

          @QtCore.pyqtSlot()
          def capture_image(self):
          flag, frame= self.capture.read()
          path = r'J:Face'
          if flag:
          QtWidgets.QApplication.beep()
          name = "opencv_frame_{}.png".format(self._image_counter)
          cv2.imwrite(os.path.join(path, name), frame)
          self._image_counter += 1

          def displayImage(self, img, window=True):
          qformat = QtGui.QImage.Format_Indexed8
          if len(img.shape)==3 :
          if img.shape[2]==4:
          qformat = QtGui.QImage.Format_RGBA8888
          else:
          qformat = QtGui.QImage.Format_RGB888
          outImage = QtGui.QImage(img, img.shape[1], img.shape[0], img.strides[0], qformat)
          outImage = outImage.rgbSwapped()
          if window:
          self.imgLabel.setPixmap(QtGui.QPixmap.fromImage(outImage))

          if __name__=='__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          window = video()
          window.setWindowTitle('main code')
          window.show()
          sys.exit(app.exec_())





          share|improve this answer























          • thanks for your big help and quick response ,can i ask a little favor? how to loop the capture_image for multiple capturing?
            – ghostDs
            Nov 18 at 21:46










          • make a for loop: for i in range(some_number): flag, frame= self.capture.read() path = r'J:Face' if flag: ...
            – eyllanesc
            Nov 18 at 21:51










          • look at the edit note ,thanks
            – ghostDs
            Nov 18 at 21:57










          • @ghostDs How many images do you want to save the for loop?
            – eyllanesc
            Nov 18 at 22:10










          • as many times as the user click the capture button
            – ghostDs
            Nov 18 at 22:12













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          keyPressEvent is a method that allows you to capture the keys while the widget has the focus, and in your case it is not necessary, the solution is simple change its name, on the other hand I have improved your code.



          import os
          import cv2
          import numpy as np
          from PyQt5 import QtCore, QtGui, QtWidgets, uic

          class video (QtWidgets.QDialog):
          def __init__(self):
          super(video, self).__init__()
          uic.loadUi('video.ui',self)
          self.startButton.clicked.connect(self.start_webcam)
          self.capture.clicked.connect(self.capture_image)
          self.imgLabel.setScaledContents(True)
          self.capture = None
          self.timer = QtCore.QTimer(self, interval=5)
          self.timer.timeout.connect(self.update_frame)
          self._image_counter = 0

          @QtCore.pyqtSlot()
          def start_webcam(self):
          if self.capture is None:
          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.start()

          @QtCore.pyqtSlot()
          def update_frame(self):
          ret, image=self.capture.read()
          simage = cv2.flip(image, 1)
          self.displayImage(image, True)

          @QtCore.pyqtSlot()
          def capture_image(self):
          flag, frame= self.capture.read()
          path = r'J:Face'
          if flag:
          QtWidgets.QApplication.beep()
          name = "opencv_frame_{}.png".format(self._image_counter)
          cv2.imwrite(os.path.join(path, name), frame)
          self._image_counter += 1

          def displayImage(self, img, window=True):
          qformat = QtGui.QImage.Format_Indexed8
          if len(img.shape)==3 :
          if img.shape[2]==4:
          qformat = QtGui.QImage.Format_RGBA8888
          else:
          qformat = QtGui.QImage.Format_RGB888
          outImage = QtGui.QImage(img, img.shape[1], img.shape[0], img.strides[0], qformat)
          outImage = outImage.rgbSwapped()
          if window:
          self.imgLabel.setPixmap(QtGui.QPixmap.fromImage(outImage))

          if __name__=='__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          window = video()
          window.setWindowTitle('main code')
          window.show()
          sys.exit(app.exec_())





          share|improve this answer














          keyPressEvent is a method that allows you to capture the keys while the widget has the focus, and in your case it is not necessary, the solution is simple change its name, on the other hand I have improved your code.



          import os
          import cv2
          import numpy as np
          from PyQt5 import QtCore, QtGui, QtWidgets, uic

          class video (QtWidgets.QDialog):
          def __init__(self):
          super(video, self).__init__()
          uic.loadUi('video.ui',self)
          self.startButton.clicked.connect(self.start_webcam)
          self.capture.clicked.connect(self.capture_image)
          self.imgLabel.setScaledContents(True)
          self.capture = None
          self.timer = QtCore.QTimer(self, interval=5)
          self.timer.timeout.connect(self.update_frame)
          self._image_counter = 0

          @QtCore.pyqtSlot()
          def start_webcam(self):
          if self.capture is None:
          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.start()

          @QtCore.pyqtSlot()
          def update_frame(self):
          ret, image=self.capture.read()
          simage = cv2.flip(image, 1)
          self.displayImage(image, True)

          @QtCore.pyqtSlot()
          def capture_image(self):
          flag, frame= self.capture.read()
          path = r'J:Face'
          if flag:
          QtWidgets.QApplication.beep()
          name = "opencv_frame_{}.png".format(self._image_counter)
          cv2.imwrite(os.path.join(path, name), frame)
          self._image_counter += 1

          def displayImage(self, img, window=True):
          qformat = QtGui.QImage.Format_Indexed8
          if len(img.shape)==3 :
          if img.shape[2]==4:
          qformat = QtGui.QImage.Format_RGBA8888
          else:
          qformat = QtGui.QImage.Format_RGB888
          outImage = QtGui.QImage(img, img.shape[1], img.shape[0], img.strides[0], qformat)
          outImage = outImage.rgbSwapped()
          if window:
          self.imgLabel.setPixmap(QtGui.QPixmap.fromImage(outImage))

          if __name__=='__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          window = video()
          window.setWindowTitle('main code')
          window.show()
          sys.exit(app.exec_())






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 18 at 22:13

























          answered Nov 18 at 21:23









          eyllanesc

          69.2k93052




          69.2k93052












          • thanks for your big help and quick response ,can i ask a little favor? how to loop the capture_image for multiple capturing?
            – ghostDs
            Nov 18 at 21:46










          • make a for loop: for i in range(some_number): flag, frame= self.capture.read() path = r'J:Face' if flag: ...
            – eyllanesc
            Nov 18 at 21:51










          • look at the edit note ,thanks
            – ghostDs
            Nov 18 at 21:57










          • @ghostDs How many images do you want to save the for loop?
            – eyllanesc
            Nov 18 at 22:10










          • as many times as the user click the capture button
            – ghostDs
            Nov 18 at 22:12


















          • thanks for your big help and quick response ,can i ask a little favor? how to loop the capture_image for multiple capturing?
            – ghostDs
            Nov 18 at 21:46










          • make a for loop: for i in range(some_number): flag, frame= self.capture.read() path = r'J:Face' if flag: ...
            – eyllanesc
            Nov 18 at 21:51










          • look at the edit note ,thanks
            – ghostDs
            Nov 18 at 21:57










          • @ghostDs How many images do you want to save the for loop?
            – eyllanesc
            Nov 18 at 22:10










          • as many times as the user click the capture button
            – ghostDs
            Nov 18 at 22:12
















          thanks for your big help and quick response ,can i ask a little favor? how to loop the capture_image for multiple capturing?
          – ghostDs
          Nov 18 at 21:46




          thanks for your big help and quick response ,can i ask a little favor? how to loop the capture_image for multiple capturing?
          – ghostDs
          Nov 18 at 21:46












          make a for loop: for i in range(some_number): flag, frame= self.capture.read() path = r'J:Face' if flag: ...
          – eyllanesc
          Nov 18 at 21:51




          make a for loop: for i in range(some_number): flag, frame= self.capture.read() path = r'J:Face' if flag: ...
          – eyllanesc
          Nov 18 at 21:51












          look at the edit note ,thanks
          – ghostDs
          Nov 18 at 21:57




          look at the edit note ,thanks
          – ghostDs
          Nov 18 at 21:57












          @ghostDs How many images do you want to save the for loop?
          – eyllanesc
          Nov 18 at 22:10




          @ghostDs How many images do you want to save the for loop?
          – eyllanesc
          Nov 18 at 22:10












          as many times as the user click the capture button
          – ghostDs
          Nov 18 at 22:12




          as many times as the user click the capture button
          – ghostDs
          Nov 18 at 22:12


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53365109%2fpython-how-to-capture-frame-with-opencv-in-pyqt5%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy