PyQt crahses when trying to transfer data using sockets - python

I am trying to make a simple video chat application with 2 participants. Here the JoinClient is the a PyQt UI for entering code and accessing the meeting. This window is then redirected to the CallClient where the actual meeting takes place. I am using cv2.VideoCapture(0) to get a video frame and then using PIL and PyQt I am processing that frame and signalling the Qpixmap of the same. However, I am sending a raw frame to the server, which is then sent to the second user by the server itself. I am doing something similar with audio using pyaudio and audio streams to input and output audio. Before adding the socket programming, the application works just fine, but as I add the socket programming the application after opening the CallClient stops responding. I am unsure as to why this happens or how I could fix it.
client.py
from PyQt5 import QtCore, QtGui, QtWidgets
import meetingui
import joinui
from PIL.ImageQt import ImageQt
from PIL import Image
import cv2
import pyaudio
import network as net
class Worker(QtCore.QObject):
finished = QtCore.pyqtSignal()
cam_signal = QtCore.pyqtSignal(bool)
mic_signal = QtCore.pyqtSignal(bool)
p1video_feed_signal = QtCore.pyqtSignal(QtGui.QPixmap)
p2video_feed_signal = QtCore.pyqtSignal(QtGui.QPixmap)
def __init__(self, parent=None):
QtCore.QObject.__init__(self, parent=parent)
self.leave_call = False
self.camera_status = False
self.mic_status = False
self.audio_interface = pyaudio.PyAudio()
self.chunk = 2048
self.sample_format = pyaudio.paInt16
self.channels = 2
self.fs = 44100
def run(self, name, code):
network = net.Network({'name': name, 'code': code})
person = {}
while not self.leave_call:
# Get self mic data
if self.mic_status:
audio_data = self.stream.read(self.chunk)
person['audio'] = audio_data
else:
person['audio'] = None
# get self camera data
if self.camera_status and self.video_feed.isOpened():
check, self.p1frame = self.video_feed.read()
if check:
self.p1frame = cv2.cvtColor(self.p1frame, cv2.COLOR_BGR2RGB)
self.p1frame = cv2.flip(self.p1frame, 1)
self.PILp1frame = Image.fromarray(self.p1frame).convert('RGB')
self.Qtp1frame = ImageQt(self.PILp1frame)
self.p1video_feed_signal.emit(QtGui.QPixmap.fromImage(self.Qtp1frame))
person['video_frame'] = self.p1frame
else:
person['video_frame'] = None
else:
person['video_frame'] = None
# send self data and receive person2 data
p2 = network.send(person)
if p2:
if p2['audio']:
self.stream.write(p2['audio'])
if p2['video_frame']:
self.PILp2frame = Image.fromarray(p2['video_frame']).convert('RGB')
self.Qtp2frame = ImageQt(self.PILp2frame)
self.p2video_feed_signal.emit(QtGui.QPixmap.fromImage(self.Qtp2frame))
else:
self.p2video_feed_signal.emit(QtGui.QPixmap('Icons\\img_avatar.png'))
else:
break
self.finished.emit()
def leave(self):
self.audio_interface.terminate()
self.leave_call = True
def toggle_cam(self):
if not self.camera_status:
self.video_feed = cv2.VideoCapture(0)
self.camera_status = True
else:
self.camera_status = False
self.video_feed.release()
self.p1video_feed_signal.emit(QtGui.QPixmap('Icons\\img_avatar.png'))
self.cam_signal.emit(self.camera_status)
def toggle_mic(self):
if not self.mic_status:
self.stream = self.audio_interface.open(format=self.sample_format,
channels=self.channels,
rate=self.fs,
frames_per_buffer=self.chunk,
input=True,
output=True)
self.mic_status = True
else:
self.mic_status = False
self.stream.stop_stream()
self.stream.close()
self.mic_signal.emit(self.mic_status)
class CallClient(QtWidgets.QWidget, meetingui.Ui_MeetingWindow):
stop_signal = QtCore.pyqtSignal()
toggle_cam_signal = QtCore.pyqtSignal()
toggle_mic_signal = QtCore.pyqtSignal()
def __init__(self, code, name, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setupUi(self)
self.P1_name = name
self.MeetCode = code
self.setWindowTitle(f'Video Call App (Meeting Code - {code})')
# Signals and Slots
self.thread = QtCore.QThread()
self.worker = Worker()
self.stop_signal.connect(self.worker.leave)
self.toggle_cam_signal.connect(self.worker.toggle_cam)
self.toggle_mic_signal.connect(self.worker.toggle_mic)
self.worker.moveToThread(self.thread)
self.thread.started.connect(lambda: self.worker.run(self.P1_name, self.MeetCode))
self.worker.cam_signal.connect(lambda status: self.handleCam(status))
self.worker.mic_signal.connect(lambda status: self.handleMic(status))
self.worker.p1video_feed_signal.connect(lambda video_feed: self.setP1Frames(video_feed))
self.worker.p2video_feed_signal.connect(lambda video_feed: self.setP2Frames(video_feed))
self.worker.finished.connect(self.thread.quit)
self.worker.finished.connect(self.worker.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
self.thread.finished.connect(self.close)
self.thread.start()
self.leaveBtn.clicked.connect(self.stop_thread)
self.cameraBtn.clicked.connect(self.toggle_cam)
self.micBtn.clicked.connect(self.toggle_mic)
def stop_thread(self):
self.stop_signal.emit()
self.close()
def toggle_cam(self):
self.toggle_cam_signal.emit()
def toggle_mic(self):
self.toggle_mic_signal.emit()
def handleCam(self, cam_status):
if cam_status:
cam_icon = QtGui.QIcon()
cam_icon.addPixmap(QtGui.QPixmap("Icons\\icons8-camera-96.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.cameraBtn.setIcon(cam_icon)
self.cameraBtn.setStyleSheet(
"border:none;border-radius: 35px;padding: 10px;background-color: rgb(255, 255, 255);")
else:
cam_icon = QtGui.QIcon()
cam_icon.addPixmap(QtGui.QPixmap("Icons\\no-cam-icon.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.cameraBtn.setIcon(cam_icon)
self.cameraBtn.setStyleSheet(
"border:none;border-radius: 35px;padding: 10px;background-color: rgb(204, 0, 0);")
def handleMic(self, mic_status):
if mic_status:
mic_icon = QtGui.QIcon()
mic_icon.addPixmap(QtGui.QPixmap("Icons\\icons8-microphone-96.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.micBtn.setIcon(mic_icon)
self.micBtn.setStyleSheet(
"border:none;border-radius: 35px;padding: 10px;background-color: rgb(255, 255, 255);")
else:
mic_icon = QtGui.QIcon()
mic_icon.addPixmap(QtGui.QPixmap("Icons\\no-mic-icon.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.micBtn.setIcon(mic_icon)
self.micBtn.setStyleSheet(
"border:none;border-radius: 35px;padding: 10px;background-color: rgb(204, 0, 0);")
def setP1Frames(self, pixmap):
self.Person1_Self.setPixmap(pixmap)
def setP2Frames(self, pixmap):
self.Person2_Opposite.setPixmap(pixmap)
class JoinClient(QtWidgets.QWidget, joinui.Ui_JoinClient):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setupUi(self)
self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.joinbtn.clicked.connect(self.connect_to_meeting)
self.x_btn.clicked.connect(self.btn_close_clicked)
self.minus_btn.clicked.connect(self.btn_minus_clicked)
def mousePressEvent(self, event):
self.start = self.mapToGlobal(event.pos())
self.pressing = True
def mouseMoveEvent(self, event):
if self.pressing:
self.end = self.mapToGlobal(event.pos())
self.movement = self.end - self.start
self.setGeometry(self.mapToGlobal(self.movement).x(),
self.mapToGlobal(self.movement).y(),
self.width(),
self.height())
self.start = self.end
def mouseReleaseEvent(self, QMouseEvent):
self.pressing = False
def btn_close_clicked(self):
self.close()
def btn_minus_clicked(self):
self.showMinimized()
def connect_to_meeting(self):
print(self.username_edit.text())
print(self.code_edit.text())
self.meetClient = CallClient(self.code_edit.text(), self.username_edit.text())
self.meetClient.show()
self.close()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
main_client = JoinClient()
main_client.show()
sys.exit(app.exec_())
network.py
import socket
import pickle
class Network:
def __init__(self, data: dict):
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server = "192.168.1.38"
self.port = 5555
self.addr = (self.server, self.port)
self.connect(data)
def connect(self, data: dict):
try:
self.client.connect(self.addr)
self.client.send(pickle.dumps(data))
print('connected :D')
except:
pass
def send(self, data):
try:
self.client.send(pickle.dumps(data))
return pickle.loads(self.client.recv(2048*500))
except socket.error as e:
print(e)
server.py
import socket
import threading
from models import Meeting
import pickle
server = "192.168.1.38"
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((server, port))
except socket.error as e:
str(e)
s.listen(2)
print("Waiting for a connection, Server Started")
meetings = []
def threaded_client(conn):
initials = pickle.loads(conn.recv(2048))
currentMeeting = None
currentMeetingIndex = 0
p = 'p1'
for meeting in meetings:
if initials['code'] == meeting.code:
currentMeeting = meeting
p = 'p2'
currentMeetingIndex = meetings.index(currentMeeting)
meetings[currentMeetingIndex].P2 = {'name': initials['name'], 'video_frame': None, 'audio': None}
if not currentMeeting:
currentMeeting = Meeting({'name': initials['name'], 'video_frame': None, 'audio': None}, {'name': None, 'video_frame': None, 'audio': None}, initials['code'])
meetings.append(currentMeeting)
currentMeetingIndex = meetings.index(currentMeeting)
reply = ""
while True:
try:
data = pickle.loads(conn.recv(2048*500))
if not data:
print("Disconnected")
if p == 'p1':
meetings[currentMeetingIndex].P1 = {'name': None, 'video_frame': None, 'audio': None}
else:
meetings[currentMeetingIndex].P2 = {'name': None, 'video_frame': None, 'audio': None}
break
else:
if p == 'p1':
meetings[currentMeetingIndex].P1['video_frame'] = data['video_frame']
meetings[currentMeetingIndex].P1['audio'] = data['audio']
reply = meetings[currentMeetingIndex].P2
else:
meetings[currentMeetingIndex].P2['video_frame'] = data['video_frame']
meetings[currentMeetingIndex].P2['audio'] = data['audio']
reply = meetings[currentMeetingIndex].P1
print("Received: ", data)
print("Sending : ", reply)
conn.sendall(pickle.dumps(reply))
except:
break
if not currentMeeting.P1['name'] and not currentMeeting.P2['name']:
meetings.remove(currentMeeting)
print('meet ended')
print("Lost connection")
print(meetings)
conn.close()
while True:
conn, addr = s.accept()
print("Connected to:", addr)
if conn:
new_thread = threading.Thread(target=threaded_client, args=(conn,))
new_thread.start()

One of the inconvenient things about TCP is that it is a streaming protocol, not a packet protocol. The bytes get transferred eventually, but it doesn't honor your packet boundaries. If you send a 2048-byte chunk and a 1024-byte chunk, depending on network traffic you might receive 500 bytes, then 1000 bytes, then 600, then the last 972. You cannot assume that you are receiving a complete pickle, nor that you are receiving only one -- they could be combined. You must send some kind of header to allow the other end to reconstruct the packets.

Related

PyQt5 : Destroyed while thread is still running

Tried to add exceptions, what should the program (client) do when the server is disconnected.
The idea is simple. If the server is turned off - the client should try to connect to the server until the server is turned on.
When server is turn off - program try to connect. But when I turned server on, client is connected, and after that connection drops, and in client app I saw that - QThread: Destroyed while thread is still running
This is simple example of what I want (minimal reproducible example for client):
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import socket
class ListeningThread(QtCore.QThread):
mysignal = QtCore.pyqtSignal(str)
def __init__(self, server_socket, parent=None):
QtCore.QThread.__init__(self, parent)
self.server_socket = server_socket
self.message = None
def run(self):
try:
while True:
self.message = self.server_socket.recv(4096)
self.mysignal.emit(self.message.decode('utf-8'))
except:
Push()
class Push(QtWidgets.QMainWindow):
def __init__(self):
super(Push, self).__init__()
print('now connecting...')
self.connect_server()
def connect_server(self):
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.server_socket.connect(('127.0.0.1', 5555))
self.message_monitor = ListeningThread(self.server_socket)
self.message_monitor.mysignal.connect(self.init_UI)
self.message_monitor.start()
except:
Push()
def init_UI(self, message):
print(message)
app =QtWidgets.QApplication([])
application = Push()
sys.exit(app.exec_())
I tried that in ListeningThread:
class ListeningThread(QtCore.QThread):
mysignal = QtCore.pyqtSignal(str)
def __init__(self, server_socket, parent=None):
QtCore.QThread.__init__(self, parent)
self.server_socket = server_socket
self.message = None
def run(self):
try:
while not self.isInterruptionRequested():
self.message = self.server_socket.recv(4096)
self.mysignal.emit(self.message.decode('utf-8'))
except:
print('error in thread')
self.requestInterruption()
self.wait()
Push()
But the problem is still the same.
I think I should close the thread before it starts in Push.connect_server but idk how.
For minimal reproducible example u can use that server:
import socket
import threading
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('127.0.0.1', 5555))
server_socket.listen()
clients = []
def accept_socket():
while True:
client_socket, addr = server_socket.accept()
print(f'connection from {addr}')
print(client_socket)
if client_socket not in clients:
clients.append(client_socket)
def sending_message(clients, data):
for client_socket in clients:
try:
client_socket.send(data.encode("utf-8"))
except:
pass
accept_thread = threading.Thread(target= accept_socket)
accept_thread.start()
while True:
data = input('Enter_message:\n')
sending_message(clients, data)
The problem is that when an exception occurs you are creating a new "Push" object that has a limited scope since it is a local variable so it will be destroyed, and objects such as the thread will also be destroyed, and that is what it indicates. the error message.
Instead of complicating your life with the handling of threads (IMO they are the last option) you can use a QTcpSocket that allows to handle the sockets in a simple way through signals and that uses the Qt eventloop.
from functools import cached_property
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, QtNetwork
class Client(QtCore.QObject):
messageChanged = QtCore.pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent)
self.socket.stateChanged.connect(self.handle_state_changed)
self.socket.errorOccurred.connect(self.handle_error_occurred)
self.socket.readyRead.connect(self.handle_ready_read)
#cached_property
def socket(self):
return QtNetwork.QTcpSocket()
def try_connect(self):
self.socket.connectToHost("127.0.0.1", 5555)
def handle_state_changed(self, state):
print(f"state: {state}")
if state == QtNetwork.QAbstractSocket.UnconnectedState:
print("disconnected")
QtCore.QTimer.singleShot(1000, self.try_connect)
elif state == QtNetwork.QAbstractSocket.ConnectedState:
print("connected")
def handle_error_occurred(self, error):
print(f"error code {error}, message: {self.socket.errorString()}")
def handle_ready_read(self):
codec = QtCore.QTextCodec.codecForName("UTF-8")
message = codec.toUnicode(self.socket.readAll())
self.messageChanged.emit(message)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.client.messageChanged.connect(self.handle_message_changed)
self.client.try_connect()
#cached_property
def client(self):
return Client()
def handle_message_changed(self, message):
print(f"client message: {message}")
def main():
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()

I wanted to add capturing image by using webcam to my PyQt gui window and webcam image will come on gui window

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
import cv2
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
class Capture(QtCore.QObject):
started = QtCore.pyqtSignal()
frameReady = QtCore.pyqtSignal(np.ndarray)
def __init__(self, parent=None):
super(Capture, self).__init__(parent)
self._frame = None
self.m_timer = QtCore.QBasicTimer()
self.m_videoCapture = cv2.VideoCapture()
#QtCore.pyqtSlot()
def start(self, cam=0):
if self.m_videoCapture is not None:
self.m_videoCapture.release()
self.m_videoCapture = cv2.VideoCapture(cam)
if self.m_videoCapture.isOpened():
self.m_timer.start(0, self)
self.started.emit()
#QtCore.pyqtSlot()
def stop(self):
self.m_timer.stop()
def __del__(self):
self.m_videoCapture.release()
def frame(self):
return self.m_frame
def timerEvent(self, event):
if event.timerId() != self.m_timer.timerId():
return
ret, val = self.m_videoCapture.read()
if not ret:
self.m_timer.stop()
return
self.m_frame = val
self.frameReady.emit(self.m_frame)
frame = QtCore.pyqtProperty(np.ndarray, fget=frame, notify=frameReady, user=True)
class Converter(QtCore.QObject):
imageReady = QtCore.pyqtSignal(QtGui.QImage)
def __init__(self, parent=None):
super(Converter, self).__init__(parent)
self.m_frame = np.array([])
self.m_timer = QtCore.QBasicTimer()
self.m_processAll = True
self.m_image = QtGui.QImage()
def queue(self, frame):
self.m_frame = frame
if not self.m_timer.isActive():
self.m_timer.start(0, self)
def process(self, frame):
w, h, _ = frame.shape
rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.m_image = QtGui.QImage(rgbImage.data, h, w, QtGui.QImage.Format_RGB888)
self.imageReady.emit(QtGui.QImage(self.m_image))
def timerEvent(self, event):
if event.timerId() != self.m_timer.timerId():
return
self.process(self.m_frame)
self.m_timer.stop()
def processAll(self):
return self.m_processAll
def setProcessAll(self, _all):
self.m_processAll = _all
def processFrame(self, frame):
if self.m_processAll:
self.process(frame)
else:
self.queue(frame)
def image(self):
return self.m_image
image = QtCore.pyqtProperty(QtGui.QImage, fget=image, notify=imageReady, user=True)
processAll = QtCore.pyqtProperty(bool, fget=processAll, fset=setProcessAll)
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
import sys
import threading
from PyQt5 import QtCore, QtGui, QtWidgets
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
from OpencvQt import Capture, Converter
config = {
"DURATION_INT": 5
}
def send_email(user, pwd, recipient, subject, body, image_payload):
msg = MIMEMultipart()
msg['From'] = user
msg['To'] = recipient
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
part = MIMEBase('application', 'octet-stream')
part.set_payload(image_payload)
encoders.encode_base64(part)
filename = QtCore.QDateTime.currentDateTime().toString()+ '.png'
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(user, pwd)
server.sendmail(user, recipient, text)
server.quit()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QVBoxLayout(central_widget)
self.view = QtWidgets.QLabel()
self.btn_start = QtWidgets.QPushButton("Start")
self.btn_stop = QtWidgets.QPushButton("Stop")
self.btn_send = QtWidgets.QPushButton("Send Email")
self.label_time = QtWidgets.QLabel()
lay.addWidget(self.view, alignment=QtCore.Qt.AlignCenter)
lay.addWidget(self.btn_start)
lay.addWidget(self.btn_stop)
lay.addWidget(self.btn_send)
lay.addWidget(self.label_time, alignment=QtCore.Qt.AlignCenter)
self.view.setFixedSize(640, 400)
self.show()
self.init_camera()
self.init_email()
def init_camera(self):
self.capture = Capture()
self.converter = Converter()
captureThread = QtCore.QThread(self)
converterThread = QtCore.QThread(self)
self.converter.setProcessAll(False)
captureThread.start()
converterThread.start()
self.capture.moveToThread(captureThread)
self.converter.moveToThread(converterThread)
self.capture.frameReady.connect(self.converter.processFrame)
self.converter.imageReady.connect(self.setImage)
self.capture.started.connect(lambda: print("started"))
self.btn_start.clicked.connect(self.capture.start)
self.btn_stop.clicked.connect(self.capture.stop)
#QtCore.pyqtSlot(QtGui.QImage)
def setImage(self, image):
self.view.setPixmap(QtGui.QPixmap.fromImage(image))
def init_email(self):
timeline = QtCore.QTimeLine(config["DURATION_INT"]*1000, self)
timeline.frameChanged.connect(self.onFrameChanged)
timeline.setFrameRange(0, config["DURATION_INT"])
timeline.setDirection(QtCore.QTimeLine.Backward)
self.btn_send.clicked.connect(timeline.start)
d = EmailDialog(self)
if d.exec_() == EmailDialog.Accepted:
self._info = d.get_data()
def onFrameChanged(self, frame):
if frame !=0:
self.label_time.setNum(frame)
else:
self.label_time.setText("Smile...!")
QtWidgets.QApplication.beep()
image = QtGui.QImage(self.converter.image)
ba = QtCore.QByteArray()
buff = QtCore.QBuffer(ba)
image.save(buff, "PNG")
th = threading.Thread(target=send_email, args=(*self._info, ba))
th.start()
def closeEvent(self, event):
self.capture.stop()
super(MainWindow, self).closeEvent(event)
class EmailDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super(EmailDialog, self).__init__(parent)
lay = QtWidgets.QFormLayout(self)
self.from_le = QtWidgets.QLineEdit()
self.pass_le = QtWidgets.QLineEdit(echoMode=QtWidgets.QLineEdit.Password)
self.to_le = QtWidgets.QLineEdit()
self.subject_le = QtWidgets.QLineEdit()
self.body_te = QtWidgets.QTextEdit()
self.buttonBox = QtWidgets.QDialogButtonBox()
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
lay.addRow("From: ", self.from_le)
lay.addRow("Password: ", self.pass_le)
lay.addRow("To: ", self.to_le)
lay.addRow("Subject: ", self.subject_le)
lay.addRow("Body: ", self.body_te)
lay.addRow(self.buttonBox)
self.from_le.textChanged.connect(self.enable_button)
self.pass_le.textChanged.connect(self.enable_button)
self.to_le.textChanged.connect(self.enable_button)
self.enable_button()
def enable_button(self):
disabled = self.from_le.text() == "" or self.pass_le.text() == "" or self.to_le.text() == ""
self.buttonBox.setDisabled(disabled)
def get_data(self):
return self.from_le.text(), self.pass_le.text(), self.to_le.text(), self.subject_le.text(), self.body_te.toPlainText()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
sys.exit(app.exec_())

Updating QPixmap crashes application in PyQt5

In the code below, when I load the GridscanCanvas' pixmap with the function get_core_data it works perfectly fine, no crashes, and the image is then displayed in my app.
However, when I want to update that same pixmap in the function get_square_data, it crashes every time.
Both times, the original image is an np.array and the only difference between them is the size of the array. I've commented on the code where it crashes.
I'm very lost, since both functions are nearly identical. Any help would be greatly appreciated!
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import os
import numpy as np
from mdoc_class import Gridscan_mdoc
from mrc_class import Gridscan_mrc
from em_ss_find_gridsquares import find_gridsquares
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setGeometry(100,100,1000,1000)
self.main_widget = QWidget(self)
self.setCentralWidget(self.main_widget)
self.gridscan_canvas = GridscanCanvas(self)
self.init_menu()
self.pixel_size = 92.7
self.gridsquare_length = 85000
self.spacing_length = 40000
self.show()
def init_menu(self):
bar = self.menuBar()
self.file = bar.addMenu('File')
load_mdoc_action = QAction('Load mdoc', self)
self.file.addAction(load_mdoc_action)
self.file.triggered.connect(self.file_respond)
find_squares_action = QAction('Find squares', self)
self.file.addAction(find_squares_action)
def file_respond(self, q):
signal = q.text()
if signal == 'Load mdoc':
mdoc_file = QFileDialog.getOpenFileName(self, 'Open File', os.getenv('Home'),
"MDOC(*.mdoc);;AllFiles(*.*)")[0]
if mdoc_file:
try:
self.load_core_data = LoadCoreData(mdoc_file)
except Exception as e:
print(e)
return
try:
self.load_core_data.new_output.connect(self.get_core_data)
except Exception as e:
print(e)
return
try:
self.load_core_data.start()
except Exception as e:
print(e)
return
elif signal == 'Find squares':
try:
self.find_squares = FindGridsquares(self.mrc.gridscan, self.pixel_size, self.gridsquare_length,
self.spacing_length)
except Exception as e:
print(e)
return
try:
self.find_squares.new_output.connect(self.get_square_data)
except Exception as e:
print(e)
return
try:
self.find_squares.start()
except Exception as e:
print(e)
return
else:
return
def get_core_data(self, core_data):
self.mrc = core_data['mrc']
self.mdoc = core_data['mdoc']
# This here works fine
self.gridscan_canvas.pixmap = QPixmap(QImage(self.mrc.gridscan, self.mrc.gridscan.shape[1],
self.mrc.gridscan.shape[0], QImage.Format_Grayscale8))
self.gridscan_canvas.update()
def get_square_data(self, square_data):
self.centres = square_data['centres']
self.aligned_centres = square_data['aligned_centres']
self.aligned_gridscan = square_data['aligned_gridscan']
self.rot_angle = square_data['rot_angle']
self.sqrd_centres = square_data['sqrd_centres']
# This here crashes is where it crashes
self.gridscan_canvas.pixmap = QPixmap(QImage(self.aligned_gridscan, self.aligned_gridscan.shape[1],
self.aligned_gridscan.shape[0], QImage.Format_Grayscale8))
self.gridscan_canvas.update()
class GridscanCanvas(QWidget):
DELTA = 10
def __init__(self, *args, **kwargs):
super(GridscanCanvas, self).__init__(*args, **kwargs)
self.draggin_idx = -1
self.points_old = np.array([[v * 5, v * 5] for v in range(75)], dtype=np.float)
self.width = 700
self.height = 700
self.setFixedSize(self.width, self.height)
self.pixmap = None
self.points = None
self.scaling_factor = [1, 1]
def paintEvent(self, e):
qp = QPainter()
qp.begin(self)
if self.pixmap is not None:
self.pixmap = self.pixmap.scaled(self.width, self.height, transformMode=Qt.SmoothTransformation)
qp.drawPixmap(0, 0, self.width, self.height, self.pixmap)
else:
self.scaling_factor = [1, 1]
if self.points is not None:
self.drawPoints(qp)
qp.end()
def drawPoints(self, qp):
pen = QPen()
pen.setWidth(5)
pen.setColor(Qt.red)
qp.setPen(pen)
for x,y in self.points:
qp.drawPoint(x,y)
class LoadCoreData(QThread):
new_output = pyqtSignal(object)
def __init__(self, mdoc_filepath):
QThread.__init__(self)
self.isRunning = True
self.mdoc_filepath = mdoc_filepath
def __del__(self):
self.wait()
def run(self):
mdoc = Gridscan_mdoc(self.mdoc_filepath)
mrc = Gridscan_mrc(mdoc)
output = {'mdoc': mdoc, 'mrc': mrc}
self.new_output.emit(output)
class FindGridsquares(QThread):
new_output = pyqtSignal(object)
def __init__(self, gridscan, pixel_size, gridsquare_length, spacing_length):
QThread.__init__(self)
self.isRunning = True
self.gridscan = gridscan
self.pixel_size = pixel_size
self.gridsquare_length = gridsquare_length
self.spacing_length = spacing_length
def __del__(self):
self.wait()
def run(self):
centres, aligned_centres, aligned_gridscan, rot_angle, sqrd_centres =find_gridsquares(self.gridscan, self.pixel_size, self.gridsquare_length, self.spacing_length)
output = {'centres': centres, 'aligned_centres': aligned_centres, 'aligned_gridscan': aligned_gridscan, 'rot_angle': rot_angle, 'sqrd_centres': sqrd_centres}
self.new_output.emit(output)
app = QApplication([])
window = MainWindow()
app.exec_()

Passing data from a QThread to another QThread

I have a situation where a Gui is active and a QThread.
The QThread is getting and saving data in the background all the time, it should not stop doing this.
Now I want to process the latest data from the QThread without interrupting the QThread or freezing the Gui.
So I think I need another thread to do this?!
How can I pass its data to another thread and do something with it?
import sys, random, time
from PyQt5 import QtWidgets
from PyQt5.QtCore import *
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
self.setWindowTitle('Window')
#getData
self.myGetData = getData()
self.myGetData.start()
self.show()
class getData(QThread):
#This Thread should run all the time
def run(self):
while True:
myNumber = random.randint(0, 100)
#He 'processData Thread'!! Do Something with mynumber!!
time.sleep(1)
class processData(QThread):
def processNumber(self, myNumber):
#Extremly complex code will execute while 'getData' is doing its thing.
newNumber = myNumber * 10
return newNumber
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Ui()
sys.exit(app.exec_())
Found a lot of examples on how to pass data from a QThread to the interface (signals).. But not the other way around.
The signals are confusing me a bit in this case..
I would like to propose do next:
import sys, random, time
from PyQt5 import QtWidgets
from PyQt5.QtCore import *
from PyQt5 import Qt #+
class getData(QThread):
#This Thread should run all the time
threadSignalGetData = pyqtSignal(int)
def __init__(self, myNumber):
super().__init__()
self.myNumber = myNumber
def run(self):
#myNumber = 0
while True:
#He 'processData Thread'!! Do Something with mynumber!!
Qt.QThread.msleep(1000)
self.myNumber += 1
self.threadSignalGetData.emit(self.myNumber)
class MsgBoxGetData(Qt.QDialog):
def __init__(self):
super().__init__()
layout = Qt.QVBoxLayout(self)
self.label = Qt.QLabel("")
layout.addWidget(self.label)
close_btn = Qt.QPushButton("Close window GetData")
layout.addWidget(close_btn)
close_btn.clicked.connect(self.close)
self.setGeometry(900, 65, 400, 80)
self.setWindowTitle('MsgBox GetData')
self.setStyleSheet("""QLabel{
font-family:'Consolas';
color: green;
font-size: 16px;}""")
class processData(QThread):
threadSignalProcessData = pyqtSignal(int, int)
def __init__(self, myNumber):
super().__init__()
self.newNumber = myNumber
def run(self):
flagProcessData = True
while flagProcessData:
#Extremly complex code will execute while 'getData' is doing its thing.
newNumber = self.newNumber * 10
self.threadSignalProcessData.emit(self.newNumber, newNumber)
flagProcessData = False
class MsgBoxProcessData(Qt.QDialog):
def __init__(self):
super().__init__()
layout = Qt.QVBoxLayout(self)
self.label = Qt.QLabel("")
layout.addWidget(self.label)
close_btn = Qt.QPushButton("Close window ProcessData")
layout.addWidget(close_btn)
close_btn.clicked.connect(self.close)
self.setGeometry(900, 200, 400, 80)
self.setWindowTitle('MsgBox ProcessData')
self.setStyleSheet("""QLabel{
font-family:'Consolas';
color: red;
font-size: 24px;}""")
class Ui(Qt.QWidget):
def __init__(self, parent=None):
super(Ui, self).__init__(parent)
layout = Qt.QVBoxLayout(self)
self.lbl = Qt.QLabel("process GUI")
layout.addWidget(self.lbl)
self.btnA = Qt.QPushButton("Start getData")
layout.addWidget(self.btnA)
self.btnB = Qt.QPushButton("Start processData")
layout.addWidget(self.btnB)
self.setGeometry(550, 65, 300, 300)
self.setWindowTitle('Window')
self.btnA.clicked.connect(self.usingGetData)
self.btnB.clicked.connect(self.usingProcessData)
self.myNumber = 0
self.newNumber = None
self.msgGetData = MsgBoxGetData()
self.threadGetData = None
self.msgProcessData = MsgBoxProcessData()
self.threadProcessData = None
self.counter = 0
self.timer = Qt.QTimer()
self.timer.setInterval(1000)
# -------- timeout -------> def recurring_timer(self):
self.timer.timeout.connect(self.recurring_timer)
self.timer.start()
self.setStyleSheet("""QLabel{
font-family:'Consolas';
color: blue;
font-size: 20px;}""")
self.show()
def recurring_timer(self):
self.counter += 1
self.lbl.setText("process GUI: %d" % self.counter)
# ---- getData(QThread) -----------------------------------------------------#
def usingGetData(self):
if self.threadGetData is None:
self.threadGetData = getData(self.myNumber)
self.threadGetData.threadSignalGetData.connect(self.on_threadSignalGetData)
self.threadGetData.finished.connect(self.finishedGetData)
self.threadGetData.start()
self.btnA.setText("Stop getData(QThread)")
else:
self.threadGetData.terminate()
self.threadGetData = None
self.btnA.setText("Start getData(QThread)")
def finishedGetData(self):
self.threadGetData = None
self.btnA.setText("Start getData(QThread)")
def on_threadSignalGetData(self, value):
self.myNumber = value
self.msgGetData.label.setText(str(self.myNumber))
if not self.msgGetData.isVisible():
self.msgGetData.show()
# --END-- getData(QThread) -------------------#
# ---- processData(QThread) -----------------------------------------------------#
def usingProcessData(self):
if self.threadProcessData is None:
self.threadProcessData = processData(self.myNumber)
self.threadProcessData.threadSignalProcessData.connect(self.on_threadSignalProcessData)
self.threadProcessData.finished.connect(self.finishedProcessData)
self.threadProcessData.start()
self.btnB.setText("Stop processData(QThread)")
else:
self.threadProcessData.terminate()
self.threadProcessData = None
self.btnB.setText("Start processData(QThread)")
def finishedProcessData(self):
self.threadProcessData = None
self.btnB.setText("Start processData(QThread)")
def on_threadSignalProcessData(self, value1, value2):
self.newNumber = value2
self.msgProcessData.label.setText(str(value1)+" * 10 = "+str(self.newNumber))
if not self.msgProcessData.isVisible():
self.msgProcessData.show()
# --END-- processData(QThread) -------------------#
# -------------------- closeEvent --------------------------------------- #
def closeEvent(self, event):
reply = Qt.QMessageBox.question\
(self, 'Question',
"QUIT ?",
Qt.QMessageBox.Yes,
Qt.QMessageBox.No)
if reply == Qt.QMessageBox.Yes:
# close getData(QThread)
if self.threadGetData:
self.threadGetData.terminate()
self.msgGetData.close()
# close processData(QThread)
if self.threadProcessData:
self.threadProcessData.terminate()
self.msgProcessData.close()
#event.accept()
super().closeEvent(event)
else:
event.ignore()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Ui()
sys.exit(app.exec_())

python 2.7 threading GUI and socket

Lately I have been working on my python chat project, the server works well but when I use the threading model to run the gui of the client and the select I have a problem with it.
I am able to send data to the server and the server returns the data to the clients but the clients can't print the data.
This is the code of the client :
import socket
import select
from Tkinter import *
import thread
class client(object):
def __init__(self):
self.client_socket = socket.socket()
self.messages =[]
def connect(self):
self.client_socket.connect(('127.0.0.1', 1234))
def getgui(self, window):
self.root = window
def run(self):
while True:
self.rlist, self.wlist, self.xlist = select.select([self.client_socket], [self.client_socket], [])
for current_socket in self.rlist:
data = current_socket.recv(1024)
self.root.print_on_list(data)
def send_to_server(self, word):
self.client_socket.send(word)
class rootgui():
def __init__(self,client_socket):
self.client_socket = client_socket
self.root = Tk()
self.root.geometry('600x700')
self.root.minsize(600, 700)
self.root.maxsize(600, 700)
self.root.title('chat')
self.frame = Frame(self.root)
self.frame.pack()
self.scrollbar = Scrollbar(self.frame)
self.scrollbar.pack(side = RIGHT, fill = Y)
self.chatlist = Listbox(self.frame, yscrollcommand = self.scrollbar.set, width = 80, height = 25 )
self.chatlist.pack(side = LEFT)
self.leftframe = Frame(self.root)
self.leftframe.pack(side = LEFT)
self.send_button = Button(self.leftframe, text = 'send', bg = 'green', padx = 60, pady = 70, command = self.send)
self.send_button.pack()
self.rightframe = Frame(self.root)
self.rightframe.pack(side = RIGHT)
self.text = StringVar()
self.input_box = Entry(self.rightframe, width =55, textvariable = self.text )
self.input_box.pack()
self.scrollbar.config(command = self.chatlist.yview)
def mainloop(self):
self.root.mainloop()
def send(self):
t = self.text.get()
self.client_socket.send_to_server(t)
return
def print_on_list(self, data):
self.chatlist.insert(END, data+'\r\n')
def main():
client_socket = client()
client_socket.connect()
window = rootgui(client_socket)
client_socket.getgui(window)
thread.start_new_thread(window.mainloop())
thread.start_new_thread(client_socket.run())
if __name__ == '__main__':
main()
This is the code of the server:
import socket
import select
class server(object):
''' init function '''
def __init__(self):
self.server_socket = socket.socket()
self.open_client_sockets = []
'''this function send a message to all of the connected clients'''
def send_messages(self, message):
for client_socket in self.open_client_sockets:
client_socket.send(message)
''' this function get a port , it bind the server socket and set the listen to 5 sockets '''
def connection(self):
self.server_socket.bind(('0.0.0.0', 1234))
self.server_socket.listen(5)
''' this function use the select library and read / write from / to the client socket '''
def run(self):
while True:
self.rlist, self.wlist, self.xlist = select.select([self.server_socket]+self.open_client_sockets, self.open_client_sockets, [])
for current_socket in self.rlist:
if current_socket is self.server_socket:
(new_socket, address) = self.server_socket.accept()
self.open_client_sockets.append(new_socket)
print 'new member : ' + str(address)
else:
try:
data = current_socket.recv(1024)
self.send_messages(data)
break
except socket.error:
self.open_client_sockets.remove(current_socket)
print 'Connection with client closed.'
if __name__ == '__main__':
server_socket = server()
server_socket.connection()
server_socket.run()

Categories