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. when i press button the image is not saved and also it closes the program i couldn't figure out the mistake i made
The error is
Process finished with exit code -1073740791 (0xC0000409)
My code is
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import cv2
cap = cv2.VideoCapture(0)
class Thread(QThread):
changePixmap = pyqtSignal(QImage)
def run(self):
while (True):
ret, frame = cap.read()
rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
convertToQtFormat = QImage(rgbImage.data, rgbImage.shape[1], rgbImage.shape[0], QImage.Format_RGB888)
p = convertToQtFormat.scaled(640, 480, Qt.KeepAspectRatio)
self.changePixmap.emit(p)
class camera(QWidget):
def __init__(self):
super().__init__()
self.title = 'Camera'
self.left = 0
self.top = 0
self.width = 640
self.height = 480
self.cameraUI()
@pyqtSlot(QImage)
def setImage(self, image):
self.label.setPixmap(QPixmap.fromImage(image))
def cameraUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.resize(1800, 1200)
# create a label
self.label = QLabel(self)
self.label.move(100, 120)
self.label.resize(640, 480)
camera_button = QPushButton("camera_click", self)
camera_button.move(50, 50)
camera_button.clicked.connect(self.click_picture)
th = Thread(self)
th.changePixmap.connect(self.setImage)
th.start()
self.show()
def click_picture(self):
while (True):
frame= cap.read()
img_name = "image.png"
cv2.imwrite(img_name,frame)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = camera()
ex.show()
sys.exit(app.exec_())
Any suggestion is appretiated
It looks like a threading issue. Since your thread is using the camera, you probably cannot read from it in your main thread. Try not starting the thread, since it doesn't seem it's being used otherwise, and it is not stated in your goal. If instead you want to get frames continously and just save them when the button is clicked, you'll have to write a slot that is connected to the signal you're emitting in the thread, then put in some logic to save when the button is clicked.
Also, It looks like you have a infinite loop in click_picture, and the reading syntax in that method should look like: flag, frame = cap.read()
.
If you're interested in a project using a similar approach, check this out (disclaimer, I'm one of the authors.): https://github.com/natedileas/ImageRIT/blob/master/Server/qt_CameraWidget.py
User contributions licensed under CC BY-SA 3.0