I wanted to add the webcam image to my main GUI window and that image will send to the email id. If this is not Possible, I also want to save that image and that saved image will send to my email id and On the countdown to 3,2,1, smile it will click the image by webcam. Here, is my code:
import sys
from PyQt5 import QtCore
from PyQt5 import QtWidgets, QtGui
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import cv2, time
DURATION_INT = 5
class MyMainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.time_left_int = DURATION_INT
self.widget_counter_int = 0
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
vbox = QtWidgets.QVBoxLayout()
central_widget.setLayout(vbox)
self.pages_qsw = QtWidgets.QStackedWidget()
vbox.addWidget(self.pages_qsw)
self.time_passed_qll = QtWidgets.QLabel()
vbox.addWidget(self.time_passed_qll)
self.pushButton = QtWidgets.QPushButton()
self.pushButton.setText("Push to start")
self.yesbutton = QtWidgets.QToolButton()
self.yesbutton.setText("yes")
self.Nobutton = QtWidgets.QToolButton()
self.Nobutton.setText("No")
self.imageframe = QtWidgets.QLabel()
self.imageframe.setText("fghkfhh")
vbox.addWidget(self.Nobutton)
vbox.addWidget(self.yesbutton)
vbox.addWidget(self.pushButton)
vbox.addWidget(self.imageframe)
self.pushButton.clicked.connect(self.timer_start)
self.yesbutton.clicked.connect(self.capturing_image)
self.update_gui()
def gmail_alert(self):
email_user = 'user email_id'
email_send = 'receiver email_id'
subject = 'Alert system'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
msg.preamble = "test"
body = 'Hi there, sending this email from Python!'
msg.attach(MIMEText(body, 'plain'))
filename = 'alert.png'
attachment = open(filename, 'rb')
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment;
filename= " + filename)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email_user, 'user email_id password')
server.sendmail(email_user, email_send, text)
server.quit()
def timer_start(self):
self.time_left_int = DURATION_INT
self.my_qtimer = QtCore.QTimer(self)
self.my_qtimer.timeout.connect(self.timer_timeout)
self.my_qtimer.start(1000)
self.update_gui()
def timer_timeout(self):
if self.time_left_int > 0:
self.time_left_int -= 1
else:
self.gmail_alert()
self.update_gui()
def update_gui(self):
self.time_passed_qll.setText((str(self.time_left_int) if self.time_left_int >=1 else "Smile..!"))
def capturing_image(self):
video =cv2.VideoCapture(0)
check, frame = video.read()
print(check)
print(frame)
cv2.imshow("capturing", frame)
video.release()
app = QtWidgets.QApplication(sys.argv)
main_window = MyMainWindow()
main_window.show()
sys.exit(app.exec_()
First of all you do not have to use
cv2.imshow()
inside PyQt since it blocks the python event loop, if you want to show the image of opencv in PyQt you have to convert it to QImage or QPixmap, the next class implements the data acquisition of opencv and it allows to obtain the QImage but it must be executed in a thread.OpencvQt.py
With the above we can show the camera in a QLabel, on the other hand we must convert the QImage to bytes for it we use QByteArray with QBuffer. Another problem that arises is that the sending of email takes a while so the GUI can be blocked so it must be executed in a thread. And finally I added a QDialog where you must enter the mail data.
main.py