I'm trying to open an image and compare it with a dataset to tell the type of the image. so the size of the loaded image should be the same as the images from the dataset
I tried writing a function to resize the image before comparing it with the dataset but it doesn't work and shows an error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize
here's the code for loading the image and then prepare it to be compared to the images from the dataset:
class MainWindow(QMainWindow): def __init__(self): super(MainWindow,self).__init__() loadUi('Main_GUI.ui',self) self.open_button.clicked.connect(self.browsimages) # adding the funtionality of the "open_button" button , and then create a function named brownimages # the above code is something like that: when the Open button is clicked go to function browsimages and do it self.predict_button.clicked.connect(self.predict) def browsimages(self): fileName, _ = QtWidgets.QFileDialog.getOpenFileName(None, "Select Image", "", "Image Files (*.png *.jpg jpeg " ".bmp)") if fileName: pixmap = QtGui.QPixmap(fileName) pixmap = pixmap.scaled(self.imageLbl.width(), self.imageLbl.height(), QtCore.Qt.KeepAspectRatio) self.imageLbl.setPixmap(pixmap) self.imageLbl.setAlignment(QtCore.Qt.AlignCenter) def predict(self): filepath = self.image_name.text def prepare(filepath): IMG_SIZE1 = 64 # 50 in txt-based IMG_SIZE2 = 64 # 50 in txt-based img_array = cv2.imread(str(filepath)) # read in the image, convert to grayscale new_array = cv2.resize(img_array, (IMG_SIZE1, IMG_SIZE2)) # resize image to match model's expected sizing return new_array.reshape(-3, IMG_SIZE1, IMG_SIZE2, 3) # return the image with shaping that TF wants. model = tf.keras.models.load_model("flowers99.model") predictions = model.predict([prepare(self.image_name.text)]) result = np.where(predictions[0] == 1) if result[0] == 0: self.Prediction_Result.setText("daisy") elif result[0] == 1: self.Prediction_Result.setText("dandelion") elif result[0] == 2: self.Prediction_Result.setText("rose") elif result[0] == 3: self.Prediction_Result.setText("sunflower") else: self.Prediction_Result.text("tulip")
Where in the "browsimages" the image should be loaded then when pressing predict it showed compare it with the dataset and then determine its type.enter image description here
The error statement: "
I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations: AVX AVX2 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. I tensorflow/core/common_runtime/process_util.cc:146] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance. <class 'cv2.error'> OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-kh7iq4w7\opencv\modules\imgproc\src\resize.cpp:4051: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize' <traceback object at 0x000001EE804E90C0> Traceback (most recent call last): File "D:/gui_program/main.py", line 48, in predict predictions = model.predict([prepare(self.image_name.text)]) File "D:/gui_program/main.py", line 44, in prepare new_array = cv2.resize(img_array, (IMG_SIZE1, IMG_SIZE2)) # resize image to match model's expected sizing cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-kh7iq4w7\opencv\modules\imgproc\src\resize.cpp:4051: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize' Process finished with exit code 1
"
https://stackoverflow.com/questions/67238901/is-there-a-solution-for-resizing-the-image-to-compare-it-with-a-dataset-in-pyqt5 April 24, 2021 at 10:50AM
没有评论:
发表评论