0

I am new to pyqt5 when I am doing a program to develop a camera software, A click button is given to capture an image. when I press enter it clicks an image and saved in disk suddenly the window is closing with exit code

Process finished with exit code -1073740791 (0xC0000409) The code is given below

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import cv2

cam = cv2.VideoCapture(0)


class camera(QWidget):

def __init__(self):
    super().__init__()
    self.camtitle = 'Camera'
    self.camleft = 0
    self.camtop = 0
    self.camwidth = 640
    self.camheight = 480
    self.camera_button = QPushButton("camera_click", self)
    self.camera_button.move(50, 50)

    self.cameraUI()

def cameraUI(self):
    self.setWindowTitle(self.camtitle)
    self.setGeometry(self.camleft, self.camtop, self.camwidth, self.camheight)
    self.setWindowIcon(QIcon('Logo.png'))
    self.label = QLabel(self)
    self.label.move(100, 100)

    self.camerawindow()

def camerawindow(self):
    # cam = cv2.VideoCapture(0)

    while (True):
        ret, frame = cam.read()

        rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        convertToQtFormat = QImage(rgbImage.data, rgbImage.shape[1], rgbImage.shape[0],
                                   QImage.Format_RGB888)
        convertToQtFormat = QPixmap.fromImage(convertToQtFormat)
        pixmap = QPixmap(convertToQtFormat)
        self.resizeImage = pixmap.scaled(300, 400, Qt.KeepAspectRatio)

        self.label.setPixmap(self.resizeImage)

        self.camera_button.clicked.connect(self.click_picture)
        QApplication.processEvents()

        self.show()

def click_picture(self):  # click picture and close
    img_counter = 0
    while(True):
        ret, frame = cam.read()

        img_name = "opencv_frame_{}.png".format(img_counter)
        cv2.imwrite(img_name, frame)
        printret, frame = cam.read()("{} writtern ".format(img_counter))
        print(printret)
        img_counter += 1


if __name__ == '__main__':
    app1 = QApplication(sys.argv)
    ex2 = camera()
    ex2.show()
    sys.exit(app1.exec_())

Any suggestion is appreciated Thank you!

Aju George
  • 123
  • 1
  • 13
  • what is `printret, frame = cam.read()("{} writtern ".format(img_counter))`? – eyllanesc Sep 03 '18 at 17:23
  • it just return 0 written,1 writtent like so in command window – Aju George Sep 03 '18 at 17:24
  • Execute it in a CMD, the IDEas are not good to find the error, for example I get: `Traceback (most recent call last): File "main.py", line 61, in click_picture printret, frame = cam.read()("{} writtern ".format(img_counter)) TypeError: 'tuple' object is not callable` – eyllanesc Sep 03 '18 at 17:25
  • then remove `("{} writtern ".format(img_counter))` – eyllanesc Sep 03 '18 at 17:25
  • i think no changes gonna happen if i delete those two lines. I am using pycharm – Aju George Sep 03 '18 at 17:27
  • I just tried it changing: `printret, frame = cam.read()("{} writtern ".format(img_counter))` to `printret, frame = cam.read()` and no longer throws that error, please do it, do not just think. PyCharm and other IDEs are not good at catching those mistakes – eyllanesc Sep 03 '18 at 17:29
  • yeah thanku , it worked fine i have one more doubt why close button is not working what changes should i make inorder to change that.. i know it is becouse of the while loop – Aju George Sep 03 '18 at 17:41
  • use a new thread for the part of the camera, see my answer: https://stackoverflow.com/questions/44404349/pyqt-showing-video-stream-from-opencv – eyllanesc Sep 03 '18 at 17:43
  • thank you very much... u r awesome – Aju George Sep 03 '18 at 17:46

0 Answers0