---作者吴疆,未经允许,严禁转载,违权必究---
---欢迎指正,需要源码和文件可站内私信联系---
-----------点击此处链接至博客园原文-----------
功能说明:这几天在给数据集打标签,需要标注车辆方向等信息,如“正前(0)”、“正后(1)”、“正侧(2)”、“前侧(3)”、“后侧(4)”,一张张图像键盘输入实在烧脑累手,索性写个小程序解放双手,记录一下
运行环境:Windows7、Python37、PyQt5
一、效果预览
使用说明:点击开始标注将加载数据集第一张图片,文本框中显示当前待标注的图像名称,依此对每张车辆图片做出“正前”、“正后”、“正侧”、“前侧”、“后侧”的标注,label信息将记录在txt中
二、主要代码分享
(标注时执行主代码)show_single_channel.py
# -*- coding:utf-8 -*- # Author: Jiang Wu # based on PyQt5、Python3 from test import Ui_MainWindow # test.py中Ui_MainWindow窗体类可被继承 from PyQt5 import QtCore,QtGui,uic,QtWidgets from PyQt5.QtCore import * from PyQt5.QtGui import * import sys,os import cv2 import glob import time # 在PyQt中,所有class都是由Qobject派生而来的 class MyApp(QtWidgets.QMainWindow,Ui_MainWindow): # 继承test.py中Ui_MainWindow窗体类 def __init__(self,parent=None): # 为None表明当前QWidget对象(MyApp)为最上层的窗体(主窗体) super(MyApp,self).__init__(parent) self.setupUi(self) # 向txt中写入,更换路径需要修改!!!!!! self.images_path = glob.glob("E:\dataset\stanford_car\cars_test\*.jpg") self.images_path = sorted(self.images_path) self.images_num = len(self.images_path) self.Button_1.clicked.connect(self.show_vis_1) self.Button_2.clicked.connect(self.show_vis_2) self.Button_3.clicked.connect(self.show_vis_3) self.Button_4.clicked.connect(self.show_vis_4) self.Button_5.clicked.connect(self.show_vis_5) self.Button_6.clicked.connect(self.show_vis_6) self.image_path_index = 0 self.label_1.setScaledContents(True) self.image_name = "" # 向txt中写入,更换路径需要修改!!!!!! self.f = open("E:\qt_2camera_tradet\\back_up\coding_1901\qt_show_single\car_viewpoint_test.txt", "w") def show_vis_1(self): # 向txt中写入,更换路径需要修改 self.f.write("/home/sever/stanford_car/test/{0} 0\n".format(self.image_name)) self.image_path_index += 1 if self.image_path_index >= self.images_num: self.textEdit.setPlainText("数据集标注完成!") time.sleep(10) exit() else: image_path = self.images_path[self.image_path_index] self.image_name = image_path.split("\\")[-1] self.textEdit.setPlainText(self.image_name) self.label_1.setPixmap(QPixmap(image_path)) def show_vis_2(self): # 向txt中写入,更换路径需要修改 self.f.write("/home/sever/stanford_car/test/{0} 1\n".format(self.image_name)) self.image_path_index += 1 if self.image_path_index >= self.images_num: self.textEdit.setPlainText("数据集标注完成!") time.sleep(10) exit() else: image_path = self.images_path[self.image_path_index] self.image_name = image_path.split("\\")[-1] self.textEdit.setPlainText(self.image_name) self.label_1.setPixmap(QPixmap(image_path)) def show_vis_3(self): # 向txt中写入,更换路径需要修改 self.f.write("/home/sever/stanford_car/test/{0} 2\n".format(self.image_name)) self.image_path_index += 1 if self.image_path_index >= self.images_num: self.textEdit.setPlainText("数据集标注完成!") time.sleep(10) exit() else: image_path = self.images_path[self.image_path_index] self.image_name = image_path.split("\\")[-1] self.textEdit.setPlainText(self.image_name) self.label_1.setPixmap(QPixmap(image_path)) def show_vis_4(self): # 向txt中写入,更换路径需要修改 self.f.write("/home/sever/stanford_car/test/{0} 3\n".format(self.image_name)) self.image_path_index += 1 if self.image_path_index >= self.images_num: self.textEdit.setPlainText("数据集标注完成!") time.sleep(10) exit() else: image_path = self.images_path[self.image_path_index] self.image_name = image_path.split("\\")[-1] self.textEdit.setPlainText(self.image_name) self.label_1.setPixmap(QPixmap(image_path)) def show_vis_5(self): # 向txt中写入,更换路径需要修改 self.f.write("/home/sever/stanford_car/test/{0} 4\n".format(self.image_name)) self.image_path_index += 1 if self.image_path_index >= self.images_num: self.textEdit.setPlainText("数据集标注完成!") time.sleep(10) exit() else: image_path = self.images_path[self.image_path_index] self.image_name = image_path.split("\\")[-1] self.textEdit.setPlainText(self.image_name) self.label_1.setPixmap(QPixmap(image_path)) def show_vis_6(self): image_path = self.images_path[self.image_path_index] self.image_name = image_path.split("\\")[-1] self.textEdit.setPlainText(self.image_name) self.label_1.setPixmap(QPixmap(image_path)) if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) window = MyApp() window.show() sys.exit(app.exec_())
UI界面test.py
界面转译代码test.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'test.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(1200, 700) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.label_1 = QtWidgets.QLabel(self.centralwidget) self.label_1.setGeometry(QtCore.QRect(30, 30, 800, 600)) self.label_1.setObjectName("label_1") self.Button_1 = QtWidgets.QPushButton(self.centralwidget) self.Button_1.setGeometry(QtCore.QRect(980, 260, 75, 23)) self.Button_1.setObjectName("Button_1") self.Button_2 = QtWidgets.QPushButton(self.centralwidget) self.Button_2.setGeometry(QtCore.QRect(980, 310, 75, 23)) self.Button_2.setObjectName("Button_2") self.Button_3 = QtWidgets.QPushButton(self.centralwidget) self.Button_3.setGeometry(QtCore.QRect(980, 360, 75, 23)) self.Button_3.setObjectName("Button_3") self.Button_4 = QtWidgets.QPushButton(self.centralwidget) self.Button_4.setGeometry(QtCore.QRect(980, 410, 75, 23)) self.Button_4.setObjectName("Button_4") self.Button_5 = QtWidgets.QPushButton(self.centralwidget) self.Button_5.setGeometry(QtCore.QRect(980, 460, 75, 23)) self.Button_5.setObjectName("Button_5") self.textEdit = QtWidgets.QTextEdit(self.centralwidget) self.textEdit.setGeometry(QtCore.QRect(890, 110, 251, 31)) self.textEdit.setObjectName("textEdit") self.Button_6 = QtWidgets.QPushButton(self.centralwidget) self.Button_6.setGeometry(QtCore.QRect(970, 170, 91, 61)) self.Button_6.setObjectName("Button_6") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 1200, 23)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.label_1.setText(_translate("MainWindow", "显示")) self.Button_1.setText(_translate("MainWindow", "正前")) self.Button_2.setText(_translate("MainWindow", "正后")) self.Button_3.setText(_translate("MainWindow", "正侧")) self.Button_4.setText(_translate("MainWindow", "前侧")) self.Button_5.setText(_translate("MainWindow", "后侧")) self.Button_6.setText(_translate("MainWindow", "开始标注"))