溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

使用PyQt5怎么實(shí)現(xiàn)一個(gè)數(shù)據(jù)標(biāo)注工具

發(fā)布時(shí)間:2021-04-20 17:03:21 來(lái)源:億速云 閱讀:379 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

使用PyQt5怎么實(shí)現(xiàn)一個(gè)數(shù)據(jù)標(biāo)注工具?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

from PyQt5.QtWidgets import QApplication,QPushButton,QLabel,QMainWindow
from PyQt5.QtGui import QFont,QPixmap
import sys,os
import shutil
 
def copyfile(srcfile, dstfile):#用來(lái)復(fù)制文件,源文件會(huì)保留
 
  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) # 創(chuàng)建路徑
    shutil.copyfile(srcfile, dstfile) # 復(fù)制文件
    print("copy %s -> %s" % (srcfile, dstfile))
class mainForm(QMainWindow):
  def __init__(self):
    super(mainForm, self).__init__()
 
    self.img_path="faces/" #文件夾和py文件要再同一個(gè)目錄下面
    self.img_list=os.listdir(self.img_path) #獲取目錄下的所有文件
    self.idx=0#可以改這里,選擇程序運(yùn)行的時(shí)候第一個(gè)顯示的圖片是哪一個(gè)
 
    self.initUI()
    self.show()
  def initUI(self):
    font=QFont()
    font.setPixelSize(20)#新建一個(gè)字體控件
 
    self.setWindowTitle("label_me")#設(shè)置窗體的標(biāo)題
    self.setGeometry(0,0,900,600)#位置和大小
 
    button_list=["Chandler","Phoebe","Joey","Monica","Rachel","Ross","Others","Thing",]#這里是顯示的按鈕們,也是可能的類(lèi)別數(shù)
 
    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)#動(dòng)態(tài)控件綁定同一個(gè)事件,根據(jù)事件的sender判斷是哪個(gè)按鈕按下
 
    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)#設(shè)置label控件要顯示的圖片
      label_img.setScaledContents(True)
      self.lbl_list.append(label_img)
 
  def get_remainder(self):#打算是要顯示3個(gè)label圖片,如果是到了最后,顯示不了那么多了。
    r=len(self.img_list)-self.idx
    if r>3:
      r=3
    return r
  def clear_lbls(self):#最后的時(shí)候會(huì)用到,不顯示某些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]#獲取剛剛被分類(lèi)的圖片的路徑
    copyfile(self.img_path+current_img_path , dir_path+current_img_path)
    self.idx +=1#下一個(gè)圖片
    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āng)前是第 %d 個(gè)圖片"%self.idx)
 
app=QApplication(sys.argv)
f=mainForm()
sys.exit(app.exec())

關(guān)于使用PyQt5怎么實(shí)現(xiàn)一個(gè)數(shù)據(jù)標(biāo)注工具問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI