First of all, my English isn’t good, so I apologize with my grammar error
I have a problem with my PyQT5 app with Tensorflow CNN model.
when I export my app with this command “pyinstaller –onefile app.py”, I don’t have any problem.
But, when exporting my app with command “pyinstaller –onefile –windowed app.py”, error is occured.
error message is “‘NoneType’ object has no attribute ‘write'”
my source codes don’t have any ‘write’. I can’t understand error.
Below is my source code. plz check my code and solve my problem.
Thank you
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout, QFileDialog, QHBoxLayout
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
from PIL import Image
import numpy as np
import tensorflow as tf
from tensorflow import keras
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # TensorFlow의 로그 레벨을 WARNING으로 설정
class EyeDiseaseClassifier(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('반려견 안구 질환 예측기')
self.layout = QVBoxLayout()
self.uploadButton = QPushButton('이미지 업로드', self)
self.uploadButton.clicked.connect(self.uploadImage)
self.layout.addWidget(self.uploadButton)
self.imageLabel = QLabel(self)
self.imageLabel.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.imageLabel)
self.resultLabel = QLabel('예측 결과가 여기 표시됩니다.', self)
self.resultLabel.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.resultLabel)
self.predictButton = QPushButton('검사하기', self)
self.predictButton.clicked.connect(self.predict)
self.layout.addWidget(self.predictButton)
self.backButton = QPushButton('초기 화면으로 돌아가기', self)
self.backButton.clicked.connect(self.reset)
self.layout.addWidget(self.backButton)
self.setLayout(self.layout)
self.model = keras.models.load_model('best-cnn-model(ver1.0).h5') # 모델 로드
self.imagePath = None
def uploadImage(self):
options = QFileDialog.Options()
options |= QFileDialog.ReadOnly
fileName, _ = QFileDialog.getOpenFileName(self, "이미지 선택", "", "Images (*.png *.xpm *.jpg *.jpeg);;All Files (*)", options=options)
if fileName:
self.imagePath = fileName
img = Image.open(fileName)
img = img.convert('RGB') # RGB 포맷으로 변환
img = img.resize((128, 128)) # 크기 조정
self.uploaded_image = np.array(img) # 정규화하지 않고 배열로 변환
pixmap = QPixmap(fileName)
self.imageLabel.setPixmap(pixmap.scaled(400, 400, Qt.KeepAspectRatio))
self.resultLabel.setText('')
def predict(self):
try:
if self.imagePath:
img = Image.open(self.imagePath)
img = img.resize((128, 128)) # 모델의 입력 크기에 맞게 조정
img = np.array(img) # 정규화하지 않고 배열로 변환
img = np.expand_dims(img, axis=0)
prediction = self.model.predict(img)
confidence = prediction[0][0]
confidence_percentage = round(confidence * 100, 2)
if confidence > 0.5:
self.resultLabel.setText(f"{confidence_percentage}% 확률로 안구 질환이 의심됩니다. {self.imagePath}")
else:
self.resultLabel.setText(f"{100 - confidence_percentage}% 확률로 반려견의 눈이 건강하다고 예측됩니다. {self.imagePath}")
except Exception as e:
self.resultLabel.setText(f"예측 중 오류가 발생했습니다: {e}, n type(img): {type(img)}, type(self.model): {type(self.model)}, self.imagePath: {type(self.imagePath)}")
def reset(self):
self.imagePath = None
self.imageLabel.clear()
self.resultLabel.setText('예측 결과가 여기 표시됩니다.')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = EyeDiseaseClassifier()
ex.show()
sys.exit(app.exec_())
When there is no error, with command “pyinstaller –onefile app.py
When there is error, with command “pyinstaller –onefile –windowed app.py
adding except phrase.
Dotorimuk is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.