1.前言
版本:Python3.6.1 + PyQt5 + SQL Server 2012
以前一直覺得,機器學習、手寫體識別這種程序都是很高大上很難的,直到偶然看到了這個視頻,聽了老師講的思路后,瞬間覺得原來這個并不是那么的難,原來我還是有可能做到的。
于是我開始順著思路打算用Python、PyQt、SQLServer做一個出來,看看能不能行。然而中間遇到了太多的問題,數據庫方面的問題有十幾個,PyQt方面的問題有接近一百個,還有數十個Python基礎語法的問題。但好在,通過不斷的Google,終于湊出了這么一個成品來。
最終還是把都湊在一個函數里的代碼重構了一下,改寫成了4個模塊:
main.py、Learning.py、LearningDB.py、LearningUI.py
其中LearningDB實現python與數據庫的交互,LearningUI實現界面的交互,Learning繼承LearningUI類添加上了與LearningDB數據庫類的交互,最后通過main主函數模塊運行程序。
其中涉及數據庫的知識可參考之前的文章:Python3操作SQL Server數據庫(實例講解),
涉及PyQt的知識可參考:Python3使用PyQt5制作簡單的畫板/手寫板
手寫體識別的主要思路是將手寫的字,用一個列表記錄其所經過的點,劃分為一個九宮格,然后數每個格子中點的數目,將數目轉化為所占總點數的百分比。然后兩兩保存的九維數,求他們之間的距離,距離越近代表越接近。
2.通過pymssql與數據庫的交互
因為使用程序之前先需要建表,建表我就直接使用SQL語句執行了:
create database PyLearningDBdrop table table0create table table0(dim0 int not null,dim1 int not null,dim2 int not null,dim3 int not null,dim4 int not null,dim5 int not null,dim6 int not null,dim7 int not null,dim8 int not null)drop table table1create table table1(dim0 int not null,dim1 int not null,dim2 int not null,dim3 int not null,dim4 int not null,dim5 int not null,dim6 int not null,dim7 int not null,dim8 int not null)drop table table2create table table2(dim0 int not null,dim1 int not null,dim2 int not null,dim3 int not null,dim4 int not null,dim5 int not null,dim6 int not null,dim7 int not null,dim8 int not null)drop table table3create table table3(dim0 int not null,dim1 int not null,dim2 int not null,dim3 int not null,dim4 int not null,dim5 int not null,dim6 int not null,dim7 int not null,dim8 int not null)drop table table4create table table4(dim0 int not null,dim1 int not null,dim2 int not null,dim3 int not null,dim4 int not null,dim5 int not null,dim6 int not null,dim7 int not null,dim8 int not null)drop table table5create table table5(dim0 int not null,dim1 int not null,dim2 int not null,dim3 int not null,dim4 int not null,dim5 int not null,dim6 int not null,dim7 int not null,dim8 int not null)drop table table6create table table6(dim0 int not null,dim1 int not null,dim2 int not null,dim3 int not null,dim4 int not null,dim5 int not null,dim6 int not null,dim7 int not null,dim8 int not null)drop table table7create table table7(dim0 int not null,dim1 int not null,dim2 int not null,dim3 int not null,dim4 int not null,dim5 int not null,dim6 int not null,dim7 int not null,dim8 int not null)drop table table8create table table8(dim0 int not null,dim1 int not null,dim2 int not null,dim3 int not null,dim4 int not null,dim5 int not null,dim6 int not null,dim7 int not null,dim8 int not null)drop table table9create table table9(dim0 int not null,dim1 int not null,dim2 int not null,dim3 int not null,dim4 int not null,dim5 int not null,dim6 int not null,dim7 int not null,dim8 int not null)
新聞熱點
疑難解答