溫馨提示×

溫馨提示×

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

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

Python編寫一個(gè)驗(yàn)證碼圖片數(shù)據(jù)標(biāo)注GUI程序附源碼

發(fā)布時(shí)間:2020-08-28 14:38:27 來源:腳本之家 閱讀:393 作者:州的先生 欄目:開發(fā)技術(shù)

做驗(yàn)證碼圖片的識別,不論是使用傳統(tǒng)的ORC技術(shù),還是使用統(tǒng)計(jì)機(jī)器學(xué)習(xí)或者是使用深度學(xué)習(xí)神經(jīng)網(wǎng)絡(luò),都少不了從網(wǎng)絡(luò)上采集大量相關(guān)的驗(yàn)證碼圖片做數(shù)據(jù)集樣本來進(jìn)行訓(xùn)練。

采集驗(yàn)證碼圖片,可以直接使用Python進(jìn)行批量下載,下載完之后,就需要對下載下來的驗(yàn)證碼圖片進(jìn)行標(biāo)注。一般情況下,一個(gè)驗(yàn)證碼圖片的文件名就是圖片中驗(yàn)證碼的實(shí)際字符串。

在不借助工具的情況下,我們對驗(yàn)證碼圖片進(jìn)行上述標(biāo)注的流程是:

1、打開圖片所在的文件夾;
2、選擇一個(gè)圖片;
3、鼠標(biāo)右鍵重命名;
4、輸入正確的字符串;
5、保存

州的先生親身體驗(yàn),一個(gè)驗(yàn)證碼完成數(shù)據(jù)的標(biāo)注,大概需要10到20秒。大量的時(shí)間浪費(fèi)在了重復(fù)地進(jìn)行鼠標(biāo)右鍵重命名操作了。于是,使用Qt的Python封裝包——PyQt5,編寫了一個(gè)小工具,方便進(jìn)行驗(yàn)證碼圖片的數(shù)據(jù)標(biāo)注,節(jié)省時(shí)間,珍惜生命。

程序的運(yùn)行如下動圖所示:

Python編寫一個(gè)驗(yàn)證碼圖片數(shù)據(jù)標(biāo)注GUI程序附源碼

下面我們來了解一下如何編寫這個(gè)驗(yàn)證碼圖片數(shù)據(jù)標(biāo)注程序。

首先,我們來構(gòu)建一個(gè)圖形界面。這個(gè)圖形界面里面包含了一個(gè)圖像展示控件、一個(gè)文本輸入控件、四個(gè)按鈕控件?;诖?,我們選擇三個(gè)布局來排列圖形界面的布局。圖形界面窗口中的核心控件是一個(gè)QWidget(),其布局層設(shè)置為網(wǎng)格布局QGridLayout()。在其中放置三個(gè)控件:圖像展示控件QWidget()、文本輸入控件QLineText()、四個(gè)按鈕組QWidget()。

同時(shí),圖像展示控件QWidget()用水平布局層QHBoxLayout()包含一個(gè)QLabel()標(biāo)簽來占位;按鈕組控件QWidget()用一個(gè)垂直布局層QVBoxLayout()將4個(gè)按鈕控件QPushButton()添加進(jìn)去。最后,代碼如下所示:

class ImgTag(QtWidgets.QMainWindow):
 def __init__(self):
 super().__init__()
 self.setWindowTitle("驗(yàn)證碼圖片標(biāo)注 州的先生 zmister.com")
 # 主控件和主控件布局
 self.main_widget = QtWidgets.QWidget()
 self.main_layout = QtWidgets.QGridLayout()
 self.main_widget.setLayout(self.main_layout)

 # 圖像展示控件
 self.img_widget = QtWidgets.QWidget()
 self.img_layout = QtWidgets.QHBoxLayout()
 self.img_widget.setLayout(self.img_layout)
 # 標(biāo)簽占位
 self.img_view = QtWidgets.QLabel("請選擇一個(gè)文件夾!")
 self.img_view.setAlignment(QtCore.Qt.AlignCenter)
 self.img_layout.addWidget(self.img_view)

 # 圖像標(biāo)注控件
 self.img_input = QtWidgets.QLineEdit()

 # 控制按鈕控件
 self.opera_widget = QtWidgets.QWidget()
 self.opera_layout = QtWidgets.QVBoxLayout()
 self.opera_widget.setLayout(self.opera_layout)
 # 各個(gè)按鈕
 self.select_img_btn = QtWidgets.QPushButton("選擇目錄")
 self.previous_img_btn = QtWidgets.QPushButton("上一張")
 self.previous_img_btn.setEnabled(False)
 self.next_img_btn = QtWidgets.QPushButton("下一張")
 self.next_img_btn.setEnabled(False)
 self.save_img_btn = QtWidgets.QPushButton("保存")
 self.save_img_btn.setEnabled(False)
 # 添加按鈕到布局
 self.opera_layout.addWidget(self.select_img_btn)
 self.opera_layout.addWidget(self.previous_img_btn)
 self.opera_layout.addWidget(self.next_img_btn)
 self.opera_layout.addWidget(self.save_img_btn)

 # 將控件添加到主控件布局層
 self.main_layout.addWidget(self.img_widget,0,0,4,4)
 self.main_layout.addWidget(self.opera_widget,0,4,5,1)
 self.main_layout.addWidget(self.img_input,4,0,1,4)

 # 狀態(tài)欄
 self.img_total_current_label = QtWidgets.QLabel()
 self.img_total_label = QtWidgets.QLabel()
 self.statusBar().addPermanentWidget(self.img_total_current_label)
 self.statusBar().addPermanentWidget(self.img_total_label, stretch=0) # 在狀態(tài)欄添加永久控件

 # 設(shè)置UI界面核心控件
 self.setCentralWidget(self.main_widget)

運(yùn)行上述代碼,我們可以得到以下如下圖所示的圖形界面:

Python編寫一個(gè)驗(yàn)證碼圖片數(shù)據(jù)標(biāo)注GUI程序附源碼

下面,我們?yōu)檫@個(gè)靜態(tài)的圖形界面添加事件響應(yīng)。

二、選擇目錄讀取文件

首先,我們來實(shí)現(xiàn)“選擇目錄”按鈕的功能。這個(gè)按鈕點(diǎn)擊之后,需要打開文件夾選擇框,然后在選擇一個(gè)文件夾之后,自動讀取文件夾內(nèi)的圖片文件,并將第一張圖片顯示到圖形展示控件上。

在這里,我們通過QFileDialog.getExistingDirectory()來實(shí)現(xiàn)調(diào)用文件夾對話框,其會返回所選擇文件夾路徑的字符串。然后通過os模塊的listdir()方法,獲取文件夾下所有的文件,對其進(jìn)行遍歷,提取出圖片文件,將這些圖片文件添加到一個(gè)新的列表中。代碼如下所示:

# 選擇目錄按鈕
def select_img_click(self):
 self.dir_path = QtWidgets.QFileDialog.getExistingDirectory(self,'選擇文件夾')
 # print(self.dir_path)
 dir_list = os.listdir(self.dir_path)
 img_list = []
 for dir in dir_list:
 suffix_list = ['jpg','png','jpeg','bmp',]
 if dir.split('.')[-1].lower() in suffix_list:
  img_list.append(dir)

接著,我們繼續(xù)遍歷這個(gè)列表,生成一個(gè)圖片的索引字典,用于記錄每個(gè)圖片的順序信息,方便進(jìn)行上一張、下一張按鈕的切換操作。

# 圖像文件索引字典
self.img_index_dict = dict()
for i,d in enumerate(img_list):
 self.img_index_dict[i] = d
self.current_index = 0 # 當(dāng)前的圖像索引
# 當(dāng)前圖片文件路徑
self.current_filename = os.path.join(
 self.dir_path,self.img_index_dict[self.current_index]
)

然后,借助QImage()類實(shí)例化一個(gè)Qt的圖像,在圖像占位標(biāo)簽中通過setPixmap設(shè)置顯示圖像。

# 實(shí)例化一個(gè)圖像
image = QtGui.QImage(self.current_filename)
self.img_width = image.width() # 圖片寬度
self.img_height = image.height() # 圖片高度
self.img_scale = 1
self.image = image.scaled(self.img_width*self.img_scale,self.img_height*self.img_scale)

# 在img_view控件中顯示圖像
self.img_view.setPixmap(QtGui.QPixmap.fromImage(self.image))

接著再設(shè)置文本輸入框的內(nèi)容、獲取文本輸入框的焦點(diǎn)并全選文本輸入框的內(nèi)容:

# 設(shè)置img_input控件文本內(nèi)容
self.img_input.setText(self.current_text)
self.img_input.setFocus() # 獲取輸入框焦點(diǎn)
self.img_input.selectAll() # 全選文本

最后在狀態(tài)欄設(shè)置圖片數(shù)量的信息,包括當(dāng)前圖片和圖片總數(shù):

# 設(shè)置狀態(tài)欄 圖片數(shù)量信息
self.img_total_current_label.setText("{}".format(self.current_index+1))
self.img_total_label.setText("/{total}".format(total=len(img_list)))

以上這些代碼都是寫在select_img_click()方法操作。在完成select_img_click()這個(gè)方法的編寫后,我們將其綁定到“選擇目錄”的點(diǎn)擊信號上:

self.select_img_btn.clicked.connect(self.select_img_click)

這樣,就實(shí)現(xiàn)了選擇目錄,并顯示目錄中的第一張圖片的功能。效果如下動圖所示:

Python編寫一個(gè)驗(yàn)證碼圖片數(shù)據(jù)標(biāo)注GUI程序附源碼

下面,我們再來實(shí)現(xiàn)下一張圖片的按鈕功能

三、切換下一張圖片

要切換下一張圖片,我們首先需要將當(dāng)前顯示的圖片重命名為文本輸入框中的內(nèi)容:

# 下一張圖片
def next_img_click(self):
 # 修改當(dāng)前圖像文件名
 new_tag = self.img_input.text() # 獲取當(dāng)前輸入框內(nèi)容
 current_img = self.img_index_dict[self.current_index] # 獲取當(dāng)前圖片名稱
 try:
 os.rename(
  os.path.join(self.dir_path,current_img),
  os.path.join(self.dir_path,new_tag+'.'+current_img.split('.')[-1])
 ) # 修改文件名
 self.img_index_dict[self.current_index] = new_tag+'.'+current_img.split('.')[-1]
 except FileExistsError as e: # 同名文件異常
 print(repr(e))
 QtWidgets.QMessageBox.information(
  self, '提示', '已存在同名文件!',
  QtWidgets.QMessageBox.Ok
 )

接下來,將圖片當(dāng)前索引變量值加1,通過這個(gè)索引值獲取到下一張圖片的文件名,再按照之前的方式將其讀取為圖像并顯示在標(biāo)簽占位控件上,同時(shí)更新狀態(tài)欄的信息:

# 當(dāng)前圖像索引加1
self.current_index += 1
if self.current_index in self.img_index_dict.keys():
 # 當(dāng)前圖片文件路徑
 self.current_filename = os.path.join(
 self.dir_path, self.img_index_dict[self.current_index]
 )
 # 實(shí)例化一個(gè)圖像
 image = QtGui.QImage(self.current_filename)
 self.img_width = image.width() # 圖片寬度
 self.img_height = image.height() # 圖片高度
 self.img_scale = 1
 self.image = image.scaled(self.img_width * self.img_scale, self.img_height * self.img_scale)

 # 在img_view控件中顯示圖像
 self.img_view.setPixmap(QtGui.QPixmap.fromImage(self.image))
 # 當(dāng)前文件名
 self.current_text = self.img_index_dict[self.current_index].split('.')[0]
 # 設(shè)置img_input控件文本內(nèi)容
 self.img_input.setText(self.current_text)
 self.img_input.setFocus() # 獲取輸入框焦點(diǎn)
 self.img_input.selectAll() # 全選文本

 # 設(shè)置狀態(tài)欄
 self.img_total_current_label.setText(str(self.current_index+1))
else:
 self.current_index -=1
 QtWidgets.QMessageBox.information(
 self,'提示','所有圖片已標(biāo)注完!',
 QtWidgets.QMessageBox.Ok
 )

這樣,調(diào)用next_img_click()方法,我們就可以切換下一張圖片。我們將其綁定在“下一張”按鈕、“保存”按鈕和文本輸入框的回車信號上,就可以實(shí)現(xiàn)點(diǎn)擊“下一張”按鈕、“保存”按鈕或是在標(biāo)注完一個(gè)數(shù)據(jù)后直接回車就能切換到下一張圖片:

self.next_img_btn.clicked.connect(self.next_img_click)
self.save_img_btn.clicked.connect(self.next_img_click)
self.img_input.returnPressed.connect(self.next_img_click) # 回車事件綁定

這樣,切換下一張圖片的功能也實(shí)現(xiàn)了,其效果如下動圖所示:

Python編寫一個(gè)驗(yàn)證碼圖片數(shù)據(jù)標(biāo)注GUI程序附源碼

四、切換上一張圖片

有時(shí)候我們需要返回前面標(biāo)注的圖片,這時(shí)候切換上一張圖片的功能也是很有必要的。切換上一張圖片的邏輯與切換下一張圖片的邏輯基本一致,只是需要將圖像的索引值減1:

# 上一張圖片
def previous_img_click(self):
 # 修改當(dāng)前圖像文件名
 new_tag = self.img_input.text() # 獲取當(dāng)前輸入框內(nèi)容
 current_img = self.img_index_dict[self.current_index] # 獲取當(dāng)前圖片名稱
 try:
 os.rename(
  os.path.join(self.dir_path, current_img),
  os.path.join(self.dir_path, new_tag + '.' + current_img.split('.')[-1])
 ) # 修改文件名
 self.img_index_dict[self.current_index] = new_tag + '.' + current_img.split('.')[-1]
 except FileExistsError as e: # 同名文件異常
 print(repr(e))
 QtWidgets.QMessageBox.information(
  self, '提示', '已存在同名文件!',
  QtWidgets.QMessageBox.Ok
 )

 # 當(dāng)前圖像索引加1
 self.current_index -= 1
 if self.current_index in self.img_index_dict.keys():
 # 當(dāng)前圖片文件路徑
 self.current_filename = os.path.join(
  self.dir_path, self.img_index_dict[self.current_index]
 )
 # 實(shí)例化一個(gè)圖像
 image = QtGui.QImage(self.current_filename)
 self.img_width = image.width() # 圖片寬度
 self.img_height = image.height() # 圖片高度
 self.img_scale = 1
 self.image = image.scaled(self.img_width * self.img_scale, self.img_height * self.img_scale)

 # 在img_view控件中顯示圖像
 self.img_view.setPixmap(QtGui.QPixmap.fromImage(self.image))
 # 當(dāng)前文件名
 self.current_text = self.img_index_dict[self.current_index].split('.')[0]
 # 設(shè)置img_input控件文本內(nèi)容
 self.img_input.setText(self.current_text)
 self.img_input.setFocus() # 獲取輸入框焦點(diǎn)
 self.img_input.selectAll() # 全選文本

 # 設(shè)置狀態(tài)欄
 self.img_total_current_label.setText(str(self.current_index + 1))
 else:
 self.current_index += 1
 QtWidgets.QMessageBox.information(
  self, '提示', '圖片列表到頂了!',
  QtWidgets.QMessageBox.Ok
 )

可以看到,這和切換下一張圖片的代碼幾乎是一致的,因?yàn)槠浜诵倪壿嫳緛砭褪且粯拥?,我們將“上一張”按鈕的點(diǎn)擊信號綁定在這個(gè)方法上,就可以實(shí)現(xiàn)切換上一張圖片的功能了:

self.previous_img_btn.clicked.connect(self.previous_img_click)

其效果如下動圖所示:

Python編寫一個(gè)驗(yàn)證碼圖片數(shù)據(jù)標(biāo)注GUI程序附源碼

五、圖片縮放

到這里,我們的驗(yàn)證碼圖片數(shù)據(jù)標(biāo)注程序基本上已經(jīng)完成了,但是突然發(fā)現(xiàn),有些驗(yàn)證碼圖片很變態(tài),它的干擾線和干擾點(diǎn)簡直讓人無法看清它到底是什么字符,這樣的情況下可能需要把圖片放大或縮小一點(diǎn),方便我們確認(rèn)驗(yàn)證碼圖片上的信息,所以,我們的程序還需要一個(gè)圖片縮放功能。最終,我們實(shí)現(xiàn)的效果是,按住Ctrl+鼠標(biāo)滾輪,滾輪向上,圖片放大,滾輪向下,圖片縮小。這是通過重寫鼠標(biāo)滾輪事件來實(shí)現(xiàn)的:

# 重寫鼠標(biāo)滾輪事件
def wheelEvent(self, event):
 # 如果按住了Ctrl
 if event.modifiers() == QtCore.Qt.ControlModifier:
 try:
  delta = event.angleDelta().y()
  if delta > 0:
  self.img_scale += 0.25
  self.image_scaled = self.image.scaled(self.img_width * self.img_scale, self.img_height * self.img_scale)
  self.img_view.setPixmap(QtGui.QPixmap.fromImage(self.image_scaled))
  self.statusBar().showMessage("當(dāng)前圖片縮放比例為:{}%".format(self.img_scale * 100))
  elif delta < 0:
  if self.img_scale > 0.25:
   self.img_scale -= 0.25
   self.image_scaled = self.image.scaled(self.img_width * self.img_scale, self.img_height * self.img_scale)
   self.img_view.setPixmap(QtGui.QPixmap.fromImage(self.image_scaled))
   self.statusBar().showMessage("當(dāng)前圖片縮放比例為:{}%".format(self.img_scale * 100))
 except Exception as e:
  print(traceback.print_exc())
  print(repr(e))

最后,這樣圖片縮放的功能也實(shí)現(xiàn)了,其效果如下所示:

Python編寫一個(gè)驗(yàn)證碼圖片數(shù)據(jù)標(biāo)注GUI程序附源碼

六、程序完整代碼

以上,我們的圖片驗(yàn)證碼數(shù)據(jù)標(biāo)注程序就完全編寫好了,基于此,我們可以進(jìn)一步使用Pyinstaller等打包工具,將其打包為二進(jìn)制的可執(zhí)行文件,方便傳播使用。

源碼下載地址:鏈接: https://pan.baidu.com/s/1FadzPC2FoIJNPMCmpYBKRg 提取碼: e4w4

總結(jié)

以上所述是小編給大家介紹的Python編寫一個(gè)驗(yàn)證碼圖片數(shù)據(jù)標(biāo)注GUI程序附源碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對億速云網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

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

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

AI