在文章Keras入門(四)之利用CNN模型輕鬆破解網站驗證碼中,其中的驗證碼圖片標記是採用tornado實現的網頁實現的。本文將會講述如何利用tornado來實現圖片標記。
咱們的示例圖片以下:html
咱們實現用tornado來實現一個網站,可以很好地幫助咱們完成圖片標記,也就是咱們只須要輸入圖片中的數字,那麼保存後的圖片名稱就是輸入的數字。
下面,讓咱們來一塊兒看一下怎麼這個功能?python
項目的名稱爲captcha_tagging,項目的完整結構以下:git
其中,images文件夾中的圖片是咱們須要標記的圖片,標記完後的圖片存放在new_images文件夾,網頁模板文件爲index.html,控制程序的腳本爲server.py。github
網頁模板文件index.html的完整代碼以下:web
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>圖片瀏覽</title> </head> <body> <div align="center"> <br><br> <img src="{{static_url('images/%s' % img_src)}}" style="width:100;height:44"/> <form action='/index' method='post'> value: <input type="text" name="rename" /><br> imgName: <input type="text" name="imgname" value="{{imgname}}"/><br> <button type="submit">提交</button> </form> </div> </body> </html>
控制程序的腳本server.py的完整代碼以下:算法
# -*- coding: utf-8 -*- import cv2 import random import os.path import tornado.web import tornado.ioloop import tornado.options import tornado.httpserver from tornado.options import define, options # 保存圖片 def tag_picture(imagepath, name): image = cv2.imread(imagepath, 1) cv2.imwrite('%s/new_images/%s.png' % (os.path.dirname(os.path.abspath(__file__)), name), image) #定義端口爲9100 define("port", default=9100, type=int) # 隨機獲取目錄下的一張圖片 def get_image(dir): files = os.listdir(dir) return random.choice(files) class ImageHandler(tornado.web.RequestHandler): # get函數 def get(self): dir = './static/images' img_src = get_image(dir) self.render('index.html', img_src=img_src, imgname=img_src) # post函數 def post(self): filename = self.get_argument('rename') imgname = self.get_argument('imgname') imagepath = os.path.dirname(__file__)+'/static/images/%s' % imgname tag_picture(imagepath, filename) # 保存新圖片 os.system('rm -rf %s' % imagepath) # 刪除原圖片 print(len(os.listdir('./static/images'))) # 剩餘圖片數量 dir = './static/images' img_src = get_image(dir) self.render('index.html', img_src=img_src, imgname=img_src) # 主函數 def main(): # 開啓tornado服務 tornado.options.parse_command_line() # 定義app app = tornado.web.Application( handlers=[(r'/index', ImageHandler) ], # 網頁路徑控制 template_path=os.path.join(os.path.dirname(__file__), "templates"), # 模板路徑 static_path=os.path.join(os.path.dirname(__file__), "static"), # 靜態文件路徑 ) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start() main()
運行server.py,在瀏覽器中輸入localhost:9100/index,界面以下:瀏覽器
在value文本框中輸入驗證碼中的數字8513,而後點提交便可,咱們就會發如今new_images文件夾下已保存好了咱們剛纔標記的圖片,並且頁面會自動跳轉至下一張須要標記的圖片。微信
利用這個程序,咱們只須要標記看到的圖片,後臺會幫你保存標記的結果,並刪除原來的圖片,提示你還剩多少張圖片須要標記,若是顯示剩餘數量爲0,則標記完畢。筆者在將近一小時的時間裏標記了1000張驗證碼。網絡
想要用神經網絡模型去訓練數據,那麼首先就須要標記好的數據。若是須要本身標記數據的話,這將是很是麻煩且耗時的過程,筆者提供了一種圖片標記的思路,但願能給讀者一些啓發~
本項目已放至Github,地址爲:https://github.com/percent4/CAPTCHA-Recognizition 。app
注意:本人現已開通微信公衆號: Python爬蟲與算法(微信號爲:easy_web_scrape), 歡迎你們關注哦~~