Вот мой код
import sys
from PyQt5 import uic, QtWidgets
from PyQt5.QtWidgets import QMessageBox, QApplication, QMainWindow
import os
import cv2
import pandas as pd
import configparser as cf
import numpy as np
import csv
import time
class Form_Login(QMainWindow):
def __init__(self):
super(Form_Login,self).__init__()
uic.loadUi('login.ui',self)
self.btnLogin.clicked.connect(self.showWindow)
self.txtEmployee.textChanged.connect(self.UnlockButton)
self.Load_Shift_comboBox()
def UnlockButton(self):
if len(self.txtEmployee.text()) >=6:
self.btnLogin.setEnabled(True)
else:
self.btnLogin.setEnabled(False)
def showWindow(self):
self.hide()
mainWindow= Form_Main(self)
mainWindow.show()
def Load_Shift_comboBox(self):
Config =cf.ConfigParser()
Config.read("Config.ini")
shift_List= Config.get('Login','turnos_list')
shift_List= shift_List.split(',')
self.cbShift.addItems(shift_List)
class Form_Main(QMainWindow):
points=[]
def __init__(self, parent=None):
super(Form_Main,self).__init__(parent)
uic.loadUi('newInterface.ui',self)
self.btnNew.clicked.connect(self.ClearEntrys)
self.txtCatalogoScan.editingFinished.connect(self.labelCheck)
self.btntakePhoto.clicked.connect(self.takePhoto)
self.btnAddModel.clicked.connect(self.AddingNewModels)
self.btnDelete.clicked.connect(self.cbDeletingModels)
self.btnSave.clicked.connect(self.Save)
self.btnPass.clicked.connect(self.passUnlock)
self.btnPass_2.clicked.connect(self.passUnlock)
self.btnSaveConfig.clicked.connect(self.Save_ConfigValues)
self.btnLock.clicked.connect(self.passLock)
self.btnLock_2.clicked.connect(self.passLock)
self.btnTestCameraSetttings.clicked.connect(self.test_CameraSettings)
self.txtNewCatalog.textChanged.connect(self.Validation_New_Model_EmptyFields)
self.cbItems()
self.btnCerrarSesion.clicked.connect(self.BackToLoginForm)
self.txtSerialScan.setFocus()
self.initial_ConfigValues()
def BackToLoginForm(self):
self.parent().show()
self.close()
def Validation_New_Model_EmptyFields(self):
if len(self.txtNewCatalog.text())>0:
self.btnAddModel.setEnabled(True)
else:
self.btnAddModel.setEnabled(False)
def passUnlock(self):
Pass=self.passFromConfig()
if str(self.txtPass.text() or self.txtPass_2.text() ) == Pass:
self.gbCalibration.setEnabled(True)
self.gbNewModel.setEnabled(True)
self.gbGeneralSettings.setEnabled(True)
self.gbCameraSettings.setEnabled(True)
self.btnSaveConfig.setEnabled(True)
self.txtPass.clear()
self.txtPass_2.clear()
else:
self.popUp_Message('Incorrect Password')
self.txtPass.clear()
def passFromConfig(self):
Config =cf.ConfigParser()
Config.read("Config.ini")
password= Config.get('General','Pass')
return password
def passLock(self):
self.gbCalibration.setEnabled(False)
self.gbNewModel.setEnabled(False)
self.btnSave.setEnabled(False)
self.gbGeneralSettings.setEnabled(False)
self.gbCameraSettings.setEnabled(False)
self.btnSaveConfig.setEnabled(False)
def test_CameraSettings(self):
self.setup_CameraSettings()
resolution= self.cbResolution.currentText()
resolution= resolution.split('x')
self.Resolution_x=int(resolution[0])
self.Resolution_y=int(resolution[1])
self.OpenCamera()
def OpenCamera(self):
self.setup_CameraSettings()
cap = cv2.VideoCapture(self.CameraSource) # video capture source camera (Here webcam of laptop)
cap.set(3, self.Resolution_x) # set the resolution
cap.set(4, self.Resolution_y)
cap.set(cv2.CAP_PROP_AUTOFOCUS, 0) # turn the autofocus off
focus = self.Focus # min: 0, max: 255, increment:5
cap.set(cv2.CAP_PROP_FOCUS, focus)
ret,frame = cap.read() # return a single frame in variable `frame`
if ret== True:
cv2.imshow('Settings Test',frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
self.popUp_Message('Camera Unplugged')
def takePhoto(self):
self.pathToSave() #Lee el path donde tiene que guardar la imagen
self.setup_CameraSettings()
resolution= self.readConfigValues('resolution')
resolution= resolution.split('x')
self.Resolution_x=int(resolution[0])
self.Resolution_y=int(resolution[1])
cap = cv2.VideoCapture(self.CameraSource) # video capture source camera (Here webcam of laptop)
cap.set(3, self.Resolution_x) # set the resolution
cap.set(4, self.Resolution_y)
cap.set(cv2.CAP_PROP_AUTOFOCUS, 0) # turn the autofocus off
focus = self.Focus # min: 0, max: 255, increment:5
cap.set(cv2.CAP_PROP_FOCUS, focus)
ret,frame = cap.read() # return a single frame in variable `frame`
self.img = frame
cv2.imshow('Calibration',frame)
cv2.setMouseCallback('Calibration',self.click_event)
self.points.clear()
cv2.waitKey(0)
cv2.destroyAllWindows()
def pathToSave(self):
dir_name=r"C:\Users\medinam16\Desktop\Practicas\Labels"
suffix = '.png'
fileName=self.cbModel.currentText()
self.fullPath= os.path.join(dir_name,fileName+suffix)
def click_event(self,event,x,y,flag,param):
if event ==cv2.EVENT_LBUTTONDOWN:
cv2.circle(self.img, (x,y),1,(255,255,255),-1)
self.points.append((x,y))
if len(self.points)>=2:
cv2.rectangle(self.img,self.points[0],self.points[1],(255,0,0),1)
x1=self.points[0][0]+2
x2=self.points[1][0]
y1=self.points[0][1]+1
y2=self.points[1][1]
self.img_croped= self.img[y1:y2,x1:x2]
cv2.imshow('Label',self.img_croped)
self.btnSave.setEnabled(True)
cv2.imshow('Calibration',self.img)
def ClearEntrys(self):
self.txtCatalogo.clear()
self.txtSerial.clear()
self.txtDate.clear()
self.txtDelay.clear()
self.txtCatalogoScan.clear()
self.txtSerialScan.clear()
self.btnGO.setStyleSheet('QPushButton {background-color: rgb(175, 175, 175)}')
self.btnGO.setText('')
self.txtSerialScan.setFocus()
def dbData(self,Catalogo,Serial):
from datetime import datetime
self.txtCatalogo.setText(Catalogo)
self.txtSerial.setText(Serial)
self.txtDate.setText(str(datetime.now()))
def colorbutton(self, label):
if label==True:
self.btnGO.setStyleSheet('QPushButton {font-size: 24px;font-family: Arial;color: rgb(255, 255, 255); background-color: rgb(0, 170, 0)}')
self.btnGO.setText('Go')
else:
self.btnGO.setStyleSheet('QPushButton {font-size: 24px;font-family: Arial;color: rgb(255, 255, 255); background-color: rgb(255, 0, 0)}')
self.btnGO.setText('NotGo')
def setup_CameraSettings(self):
self.Focus=self.spinFocus.value()
self.CameraSource= self.spinCameraSource.value()
self.ShowtestedImage= self.checkShowTestedImage.isChecked()
self.ClosePhoto= self.check_ClosePhoto.isChecked()
self.MatchingAccuracy=self.spinAccuracy.value()
def labelCheck(self): ##Funcion Que detecta la etiqueta y delay
inicio = time.time()
catalogo=self.txtCatalogoScan.text()
serial=self.txtSerialScan.text()
self.dbData(catalogo,serial)
self.setup_CameraSettings()
resolution= self.readConfigValues('resolution')
resolution= resolution.split('x')
self.Resolution_x=int(resolution[0])
self.Resolution_y=int(resolution[1])
Img= self.Image_byModel(catalogo)
if Img== 'Error':
self.popUp_Message("The model does not exist!! :'( ")
else:
label=self.ItemToDetect(self.CameraSource,self.Focus,self.Resolution_x,self.Resolution_y,Img,self.MatchingAccuracy,self.ShowtestedImage,self.ClosePhoto)
self.colorbutton(label)
fin = time.time()
self.txtDelay.setText("{:0.2f}".format(fin-inicio))
def initial_ConfigValues(self):
self.spinAccuracy.setValue(self.readConfigValues('accuracy'))
self.spinCameraSource.setValue(self.readConfigValues('camera_source'))
self.spinFocus.setValue(self.readConfigValues('focus'))
self.checkShowTestedImage.setChecked(self.readConfigValues('show_image_tested'))
self.check_ClosePhoto.setChecked(self.readConfigValues('close_image'))
self.checkSerialLog.setChecked(self.readConfigValues('serial_log'))
Resolution= self.readConfigValues('resolution_list')
Resolution= Resolution.split(',')
self.cbResolution.addItems(Resolution)
Screw_list= self.readConfigValues('screw_list')
Screw_list= Screw_list.split(',')
self.cbScrew.addItems(Screw_list)
def Save_ConfigValues(self):
MatchingAccuracy=str(self.spinAccuracy.value())
CameraSource= str(self.spinCameraSource.value())
Focus=str(self.spinFocus.value())
check_ShowtestedImage= str(self.checkShowTestedImage.isChecked())
check_SerialLog= str(self.checkSerialLog.isChecked())
check_close_image= str(self.check_ClosePhoto.isChecked())
resolution= self.cbResolution.currentText()
Config =cf.ConfigParser()
Config.read('Config.ini')
Config.set('General','serial_log',check_SerialLog)
Config.set('Vision','camera_source',CameraSource)
Config.set('Vision','accuracy',MatchingAccuracy)
Config.set('Vision','show_image_tested',check_ShowtestedImage)
Config.set('Vision','close_image',check_close_image)
Config.set('Camera','focus',Focus)
Config.set('Camera','resolution',resolution)
with open('Config.ini', 'w') as configfile:
Config.write(configfile)
self.passLock()
self.popUp_Message()
def readConfigValues(self,Key):
Config =cf.ConfigParser()
Config.read("Config.ini")
if Key== 'camera_source':
return Config.getint('Vision','camera_source')
elif Key== 'accuracy':
return Config.getfloat('Vision','Accuracy')
elif Key== 'show_image_tested':
return Config.getboolean('Vision','show_image_tested')
elif Key== 'close_image':
return Config.getboolean('Vision','close_image')
elif Key== 'resolution_list':
return Config.get('General','resolution_list')
elif Key== 'screw_list':
return Config.get('General','screw_list')
elif Key== 'focus':
return Config.getint('Camera','focus')
elif Key== 'pass':
return Config.get('General','pass')
elif Key== 'serial_log':
return Config.getboolean('General','serial_log')
elif Key== 'close_image':
return Config.getboolean('Vision','close_image')
elif Key== 'resolution':
return Config.get('Camera','resolution')
def writeConfigValues(self,Section,Key,Value):
Config =cf.ConfigParser()
Config.set(Section,Key,Value)
with open('Config.ini', 'w') as configfile:
Config.write(configfile)
def cbItems(self):
self.dataset=pd.read_csv('Modelos.csv')
rows= self.dataset.iloc[:,0].values
self.cbModel.clear()
self.cbModel.addItems(rows)
def AddingNewModels(self):
Modelo=self.txtNewModel.text()
Catalogo=self.txtNewCatalog.text()
Screw=self.cbScrew.currentText()
ds=pd.read_csv('Modelos.csv')
if Modelo in ds.iloc[:,0].values:
model_index = ds.index[ds['Model_ID']==Modelo]
ds.at[model_index,'Model_ID']= Modelo
ds.at[model_index,'Catalog']= Catalogo
ds.at[model_index,'Screw']= Screw
ds.to_csv('Modelos.csv',index=False)
else:
newDF = pd.DataFrame({"Model_ID":[Modelo],
"Catalog":[Catalogo],
'Screw':[Screw]})
ds= ds.append(newDF, ignore_index = True, sort=False)
ds.to_csv('Modelos.csv',index=False)
self.cbItems()
self.ClearNewModelText()
def Save(self):
Model= self.cbModel.currentText()
ds=pd.read_csv('Modelos.csv')
idx = ds.index[ds['Model_ID']==Model]
ds.at[idx,'Path']= self.fullPath
ds.to_csv('Modelos.csv',index=False)
cv2.imwrite(self.fullPath,self.img_croped)
self.passLock()
def cbDeletingModels(self):
Model=self.cbModel.currentText()
ds=pd.read_csv('Modelos.csv')
self.pathToSave()
os.remove(self.fullPath)
ds=ds.drop(ds[ds.Model_ID ==Model].index)
ds.to_csv('Modelos.csv',index=False)
self.cbItems()
self.ClearNewModelText()
self.popUp_Message()
def ClearNewModelText(self):
self.txtNewModel.clear()
self.txtNewCatalog.clear()
def ItemToDetect(self,CameraSource,Focus,Resolution_X,Resolution_Y,Item,Accuracy,showResult,WindowTime=0):
cap = cv2.VideoCapture(CameraSource) #Inicia la grabaacion
cap.set(3, Resolution_X) # set the resolution
cap.set(4, Resolution_Y)
cap.set(cv2.CAP_PROP_AUTOFOCUS, 1) # turn the autofocus off
focus = Focus #self.spinFocus.value() # min: 0, max: 255, increment:5
cap.set(cv2.CAP_PROP_FOCUS, focus)
_, img = cap.read() #Toma la foto
Go= False
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
template = cv2.imread(Item, cv2.IMREAD_GRAYSCALE)
w, h = template.shape[::-1]
result = cv2.matchTemplate(gray_img, template, cv2.TM_CCOEFF_NORMED)
loc = np.where(result >= Accuracy)
for pt in zip(*loc[::-1]):
cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0, 255, 0), 1)
Go=True
if showResult ==False:
cap.release()
return(Go)
elif showResult==True:
cv2.imshow("Camera", img)
cv2.waitKey(WindowTime*1000)
cv2.destroyAllWindows()
cap.release()
return(Go)
def popUp_Message(self,MessageText='Done!'):
msg= QMessageBox()
msg.setWindowTitle('PopUp Message')
msg.setText(MessageText)
msg.setIcon(QMessageBox.Information)
msg.setStyleSheet("font: 12pt")
msg.exec_()
if __name__=="__main__":
app= QtWidgets.QApplication(sys.argv)
main = Form_Login()
main.show()
exit(app.exec_())
Ну, я спроектировал 2 windows в Qtdesigner и загружаю их с python, первый windows - это форма входа только с именем и кнопка, когда кто-то нажимает на кнопку, первый windows скрывается, а второй открывается, проблема возникает, когда я вызываю QmessageBox во второй форме. Затем, когда я нажимаю кнопку ОК в окне сообщений, все приложение закрывается без ошибки.
Я уверен, что ошибка происходит в этой строке: msg.exec_()
функции messageBox:
def popUp_Message(self,MessageText='Done!'):
msg= QMessageBox()
msg.setWindowTitle('PopUp Message')
msg.setText(MessageText)
msg.setIcon(QMessageBox.Information)
msg.setStyleSheet("font: 12pt")
msg.exec_()
Поэтому я попытался изменить последнюю строку exit(app.exec_())
Я удалил exit()
из:
if __name__=="__main__":
app= QtWidgets.QApplication(sys.argv)
main = Form_Login()
main.show()
exit(app.exec_())
но затем приложение вместо закрытия зависает и вылетает (не отвечает)