import sys
import os
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QVBoxLayout, QWidget, QFileDialog
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
class ImageClassifierApp(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Image Classifier")
self.setGeometry(100, 100, 800, 600)
self.image_label = QLabel(self)
self.image_label.setAlignment(Qt.AlignCenter)
self.image_label.setFixedWidth(640)
self.image_label.setFixedHeight(640)
self.load_button = QPushButton("请选择文件路径", self)
self.load_button.clicked.connect(self.load_images)
self.correct_button = QPushButton("正确", self)
self.correct_button.clicked.connect(self.move_and_load_next)
self.incorrect_button = QPushButton("错误", self)
self.incorrect_button.clicked.connect(self.move_and_load_next)
self.pending_button = QPushButton("待定", self)
self.pending_button.clicked.connect(self.move_and_load_next)
layout = QVBoxLayout()
layout.addWidget(self.image_label)
layout.addWidget(self.load_button)
layout.addWidget(self.correct_button)
layout.addWidget(self.incorrect_button)
layout.addWidget(self.pending_button)
central_widget = QWidget()
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
self.image_paths = []
self.current_index = 0
def load_images(self):
folder_path = QFileDialog.getExistingDirectory(self, "请选择文件路径")
if folder_path:
self.image_paths = [os.path.join(folder_path, filename) for filename in os.listdir(folder_path) if filename.lower().endswith(('.jpg', '.jpeg', '.png', '.gif', '.webp'))]
self.current_index = 0
self.load_current_image()
def load_current_image(self):
if 0 <= self.current_index < len(self.image_paths):
pixmap = QPixmap(self.image_paths[self.current_index])
max_width = self.image_label.maximumWidth()
max_height = self.image_label.maximumHeight()
pixmap = pixmap.scaled(max_width, max_height, Qt.KeepAspectRatio, Qt.SmoothTransformation)
self.image_label.setPixmap(pixmap)
else:
self.image_label.clear()
def move_and_load_next(self):
if self.current_index >= len(self.image_paths):
return
destination_folder = self.sender().text()
source_path = self.image_paths[self.current_index]
filename = os.path.basename(source_path)
root_folder = os.path.dirname(source_path)
destination_folder = os.path.join(root_folder, destination_folder)
destination_path = os.path.join(destination_folder, filename)
os.makedirs(destination_folder, exist_ok=True)
os.rename(source_path, destination_path)
self.current_index += 1
self.load_current_image()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = ImageClassifierApp()
window.show()
sys.exit(app.exec_())