4

I am designing a GUI to capture an image from picamera, I am using PyQt5 to develop the GUI. I used camera.start_preview() function to see the live preview. After that I used a button named "Take" to capture the image and after capturing the image I showed the image in Qlabel by using self.label_2.setPixmap(QtGui.QPixmap()) command till this my code is working fine. Now I want to set the live preview of PiCamera into the same Qlabel, which I am using to show the captured image. Is it possible to set the live preview inside a QLabel?

After googling I found PIL can be used to stream live preview inside an window, but I have no idea how to implement it.

from PyQt5 import QtCore, QtGui, QtWidgets
from picamera import PiCamera
import time
from time import sleep
import gpiozero
from gpiozero import Button
from PIL import Image
import cups
import os

camera = PiCamera()
camera.sensor_mode = 2
camera.brightness = 70
camera.contrast = 20

photoHeight = 1944
photoWidth = 2592


class clsPicCount(object):
    countpic=0
    def __init__(self):
        clsPicCount.countpic=clsPicCount.countpic+1
        if clsPicCount.countpic > 1:
            clsPicCount.countpic = 0


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1366, 768)
        MainWindow.setMinimumSize(QtCore.QSize(1366, 768))
        MainWindow.setMaximumSize(QtCore.QSize(1366, 768))
        font = QtGui.QFont()
        font.setUnderline(False)
        font.setStrikeOut(False)
        MainWindow.setFont(font)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("Photo.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
        MainWindow.setWindowIcon(icon)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.retake = QtWidgets.QPushButton(self.centralwidget)
        self.retake.setGeometry(QtCore.QRect(320, 680, 211, 71))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.retake.setFont(font)
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap("retake.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
        self.retake.setIcon(icon1)
        self.retake.setIconSize(QtCore.QSize(50, 50))
        self.retake.setObjectName("retake")
        self.take = QtWidgets.QPushButton(self.centralwidget)
        self.take.setGeometry(QtCore.QRect(550, 680, 211, 71))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.take.setFont(font)
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap("take.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
        self.take.setIcon(icon2)
        self.take.setIconSize(QtCore.QSize(60, 60))
        self.take.setObjectName("take")
        self.print = QtWidgets.QPushButton(self.centralwidget)
        self.print.setGeometry(QtCore.QRect(780, 680, 211, 71))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.print.setFont(font)
        icon3 = QtGui.QIcon()
        icon3.addPixmap(QtGui.QPixmap("print.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
        self.print.setIcon(icon3)
        self.print.setIconSize(QtCore.QSize(50, 50))
        self.print.setCheckable(True)
        self.print.setObjectName("print")
        self.settings = QtWidgets.QPushButton(self.centralwidget)
        self.settings.setGeometry(QtCore.QRect(1220, 690, 121, 51))
        font = QtGui.QFont()
        font.setPointSize(8)
        font.setBold(True)
        font.setWeight(75)
        self.settings.setFont(font)
        icon4 = QtGui.QIcon()
        icon4.addPixmap(QtGui.QPixmap("settings.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
        self.settings.setIcon(icon4)
        self.settings.setIconSize(QtCore.QSize(30, 30))
        self.settings.setObjectName("settings")
        self.heading = QtWidgets.QLabel(self.centralwidget)
        self.heading.setGeometry(QtCore.QRect(390, 20, 581, 31))
        self.heading.setObjectName("heading")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(230, 70, 900, 600))
        self.label_2.setText("")
        self.label_2.setPixmap(QtGui.QPixmap("photobooth.jpg"))
        self.label_2.setScaledContents(True)
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(0, 0, 1361, 771))
        self.label_3.setText("")
        self.label_3.setPixmap(QtGui.QPixmap("background.png"))
        self.label_3.setObjectName("label_3")
        self.label_3.raise_()
        self.retake.raise_()
        self.take.raise_()
        self.print.raise_()
        self.settings.raise_()
        self.heading.raise_()
        self.label_2.raise_()
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

       #Defining functions of our buttons

        self.take.clicked.connect(self.startPreview)

        #Initializing Printer

        conn = cups.Connection ()
        printers = conn.getPrinters ()
        printer_name = list(printers)[0]
        for printer in printers:
            print (printer, printers[printer]["device-uri"])

        #Code Block for live preview and image capture


        def startPreview(self):

        p=clsPicCount()
        print(clsPicCount.countpic)

        if clsPicCount.countpic == 1:
            camera.start_preview(fullscreen=False, window=(10,0,320,240))
        else:
            filename = time.strftime("%Y%m%d-%H%M%S")
            print (filename)
            camera.capture('/home/pi/Desktop/%s.jpg'%filename, resize = (photoWidth, photoHeight))
            camera.stop_preview()
            self.label_2.setPixmap(QtGui.QPixmap('/home/pi/Desktop/%s.jpg'%filename))
            self.label_2.setScaledContents(True)
            #self.label_3.raise_()


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "PhotoBooth"))
        self.retake.setText(_translate("MainWindow", "RETAKE"))
        self.take.setText(_translate("MainWindow", "TAKE"))
        self.print.setText(_translate("MainWindow", "PRINT"))
        self.settings.setText(_translate("MainWindow", "SETTINGS"))
        self.heading.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-size:14pt; font-weight:600; color:#ffffff;\">TECH MECH SERVICES PVT LTD.</span></p></body></html>"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241

0 Answers0