如何通过单击 GUI 上的按钮将图像获取到同一目录文件夹中的另一个 .py 文件

How can i get image by click button on GUI into another .py file in the same directory folder

main.py:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPixmap, QImage
import cv2
import os
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):

        self.loadimg.clicked.connect(self.load_img)

    def load_img(self):
        load_img_1, filter = QtWidgets.QFileDialog.getOpenFileName(None, "Select Image",
                                                                    options=QtWidgets.QFileDialog.DontUseNativeDialog)
        img = cv2.imread(load_img_1)

        if img is None:
            self.imglabel.setText("Cannot load the input image.")
        else:
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            img_ = QImage(img.data, img.shape[1], img.shape[0], QImage.Format_RGB888)
            self.imglabel.setPixmap(QPixmap.fromImage(img_))
            pixmap = QPixmap.fromImage(img_)
            pixmap = pixmap.scaled(self.imglabel.width(), self.imglabel.height())
        if load_img_1:
            pixmap = QtGui.QPixmap(load_img_1)
            pixmap = pixmap.scaled(self.imglabel.width(), self.imglabel.height(),QtCore.Qt.KeepAspectRatio)
            self.imglabel.setPixmap(pixmap)
            self.imglabel.setAlignment(QtCore.Qt.AlignCenter)
            self.imglabel.setScaledContents(True)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

classify.py:

image = cv2.imread("/")

我是 python 和机器学习的初学者。我的学习有些问题。 我在 main.py 中制作了一个 GUI,当我通过在 gui def load_img(self): 上单击按钮加载图像时,我希望图像的路径 link 到 image = cv2.imread("/")classify.py 中,但我不知道该怎么做。此外,我想如何在 GUI 中的文本标签上显示分类结果,我也不知道如何。 所以,我需要你的帮助,并告诉我如何做到这一点。 非常感谢。

classify.py中将代码放入带参数的函数中,即。 path and then you canimportit and run function with your image filename as parametr. If function will return image thenmain`可以获取并显示。

classify.py

def run(path)

    image = cv2.imread(path)

    # ... machine learning code ...

    return image

main.py - 参见 importimage = clasify.run(filename)

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPixmap, QImage
import cv2
import os

import classify # filename without .py

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):

        self.loadimg.clicked.connect(self.load_img)

    def load_img(self):
        load_img_1, filter = QtWidgets.QFileDialog.getOpenFileName(None, "Select Image",
                                                                    options=QtWidgets.QFileDialog.DontUseNativeDialog)

        #img = cv2.imread(load_img_1)
        img = classify.run(load_img_1) # use it


        if img is None:
            self.imglabel.setText("Cannot load the input image.")
        else:
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            img_ = QImage(img.data, img.shape[1], img.shape[0], QImage.Format_RGB888)
            self.imglabel.setPixmap(QPixmap.fromImage(img_))
            pixmap = QPixmap.fromImage(img_)
            pixmap = pixmap.scaled(self.imglabel.width(), self.imglabel.height())
        if load_img_1:
            pixmap = QtGui.QPixmap(load_img_1)
            pixmap = pixmap.scaled(self.imglabel.width(), self.imglabel.height(),QtCore.Qt.KeepAspectRatio)
            self.imglabel.setPixmap(pixmap)
            self.imglabel.setAlignment(QtCore.Qt.AlignCenter)
            self.imglabel.setScaledContents(True)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())