温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用PyQt5怎么实现一个数据标注工具

发布时间:2021-04-20 17:03:21 来源:亿速云 阅读:372 作者:Leah 栏目:开发技术

使用PyQt5怎么实现一个数据标注工具?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

from PyQt5.QtWidgets import QApplication,QPushButton,QLabel,QMainWindow
from PyQt5.QtGui import QFont,QPixmap
import sys,os
import shutil
 
def copyfile(srcfile, dstfile):#用来复制文件,源文件会保留
 
  if not os.path.isfile(srcfile):
    print("%s not exist!" % srcfile)
  else:
    f_path, f_name = os.path.split(dstfile) # 分离文件名和路径
    if not os.path.exists(f_path):
      os.makedirs(f_path) # 创建路径
    shutil.copyfile(srcfile, dstfile) # 复制文件
    print("copy %s -> %s" % (srcfile, dstfile))
class mainForm(QMainWindow):
  def __init__(self):
    super(mainForm, self).__init__()
 
    self.img_path="faces/" #文件夹和py文件要再同一个目录下面
    self.img_list=os.listdir(self.img_path) #获取目录下的所有文件
    self.idx=0#可以改这里,选择程序运行的时候第一个显示的图片是哪一个
 
    self.initUI()
    self.show()
  def initUI(self):
    font=QFont()
    font.setPixelSize(20)#新建一个字体控件
 
    self.setWindowTitle("label_me")#设置窗体的标题
    self.setGeometry(0,0,900,600)#位置和大小
 
    button_list=["Chandler","Phoebe","Joey","Monica","Rachel","Ross","Others","Thing",]#这里是显示的按钮们,也是可能的类别数
 
    for idx, label_name in enumerate(button_list):
 
      button=QPushButton(label_name,self)
      button.move(idx*110+20,500)
      button.setFont(font)
      button.setFixedHeight(35)
 
      button.clicked.connect(self.classify)#动态控件绑定同一个事件,根据事件的sender判断是哪个按钮按下
 
    self.lbl_list=[]#存放显示图片的label 的list
    for i in range(self.get_remainder()):
 
      self.pix = QPixmap(self.img_path+self.img_list[self.idx+i])
      label_img = QLabel(self)
      label_img.setGeometry(360*i+10, 400-100*(3-i)+40, 100*(3-i)+40,100*(3-i)+40)
      label_img.setStyleSheet("border:2px solid red")
      label_img.setPixmap(self.pix)#设置label控件要显示的图片
      label_img.setScaledContents(True)
      self.lbl_list.append(label_img)
 
  def get_remainder(self):#打算是要显示3个label图片,如果是到了最后,显示不了那么多了。
    r=len(self.img_list)-self.idx
    if r>3:
      r=3
    return r
  def clear_lbls(self):#最后的时候会用到,不显示某些label
    for i in range(len(self.lbl_list)):
      self.lbl_list[i].hide()
 
  def classify(self):
 
    sender = self.sender()
    dir_path=sender.text()+"/"#获取按钮的text属性
 
    current_img_path=self.img_list[self.idx]#获取刚刚被分类的图片的路径
    copyfile(self.img_path+current_img_path , dir_path+current_img_path)
    self.idx +=1#下一个图片
    img_full_path = [self.img_path + self.img_list[self.idx + i]  for i in range(self.get_remainder())]
 
    self.clear_lbls()
    for i in range(self.get_remainder()):
 
      pix = QPixmap(img_full_path[i])
      self.lbl_list[i].setPixmap(pix)
      self.lbl_list[i].show()
    self.setWindowTitle("当前是第 %d 个图片"%self.idx)
 
app=QApplication(sys.argv)
f=mainForm()
sys.exit(app.exec())

关于使用PyQt5怎么实现一个数据标注工具问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI