Python識(shí)別處理照片中的條形碼
最近一直在玩數(shù)獨(dú),突發(fā)奇想實(shí)現(xiàn)圖像識(shí)別求解數(shù)獨(dú),輸入到輸出平均需要0.5s。
整體思路大概就是識(shí)別出圖中數(shù)字生成list,然后求解。
輸入輸出demo
數(shù)獨(dú)采用的是微軟自帶的Microsoft sudoku軟件隨便截取的圖像,如下圖所示:

經(jīng)過(guò)程序求解后,得到的結(jié)果如下圖所示:

def getFollow(varset, terminalset, first_dic, production_list):
follow_dic = {}
done = {}
for var in varset:
follow_dic[var] = set()
done[var] = 0
follow_dic["A1"].add("#")
# for var in terminalset:
# follow_dic[var]=set()
# done[var] = 0
for var in follow_dic:
getFollowForVar(var, varset, terminalset, first_dic, production_list, follow_dic, done)
return follow_dic
def getFollowForVar(var, varset, terminalset, first_dic, production_list, follow_dic, done):
if done[var] == 1:
return
for production in production_list:
if var in production.right:
##index這里在某些極端情況下有bug,比如多次出現(xiàn)var,index只會(huì)返回最左側(cè)的
if production.right.index(var) != len(production.right) - 1:
follow_dic[var] = first_dic[production.right[production.right.index(var) + 1]] | follow_dic[var]
# 沒(méi)有考慮右邊有非終結(jié)符但是為null的情況
if production.right[len(production.right) - 1] == var:
if var != production.left[0]:
# print(var, "吸納", production.left[0])
getFollowForVar(production.left[0], varset, terminalset, first_dic, production_list, follow_dic,
done)
follow_dic[var] = follow_dic[var] | follow_dic[production.left[0]]
done[var] = 1
程序具體流程
程序整體流程如下圖所示:

讀入圖像后,根據(jù)求解輪廓信息找到數(shù)字所在位置,以及不包含數(shù)字的空白位置,提取數(shù)字信息通過(guò)KNN識(shí)別,識(shí)別出數(shù)字;無(wú)數(shù)字信息的在list中置0;生成未求解數(shù)獨(dú)list,之后求解數(shù)獨(dú),將信息在原圖中顯示出來(lái)。
def initProduction():
production_list = []
production = Production(["A1"], ["A"], 0)
production_list.append(production)
production = Production(["A"], ["E", "I", "(", ")", "{", "D", "}"], 1)
production_list.append(production)
production = Production(["E"], ["int"], 2)
production_list.append(production)
production = Production(["E"], ["float"], 3)
production_list.append(production)
production = Production(["D"], ["D", ";", "B"], 4)
production_list.append(production)
production = Production(["B"], ["F"], 5)
production_list.append(production)
production = Production(["B"], ["G"], 6)
production_list.append(production)
production = Production(["B"], ["M"], 7)
production_list.append(production)
production = Production(["F"], ["E", "I"], 8)
production_list.append(production)
production = Production(["G"], ["I", "=", "P"], 9)
production_list.append(production)
production = Production(["P"], ["K"], 10)
production_list.append(production)
production = Production(["P"], ["K", "+", "P"], 11)
production_list.append(production)
production = Production(["P"], ["K", "-", "P"], 12)
production_list.append(production)
production = Production(["I"], ["id"], 13)
production_list.append(production)
production = Production(["K"], ["I"], 14)
production_list.append(production)
production = Production(["K"], ["number"], 15)
production_list.append(production)
production = Production(["K"], ["floating"], 16)
production_list.append(production)
production = Production(["M"], ["while", "(", "T", ")", "{", "D", ";", "}"], 18)
production_list.append(production)
production = Production(["N"], ["if", "(", "T", ")", "{", "D",";", "}", "else", "{", "D", ";","}"], 19)
production_list.append(production)
production = Production(["T"], ["K", "L", "K"], 20)
production_list.append(production)
production = Production(["L"], [">"], 21)
production_list.append(production)
production = Production(["L"], ["<"], 22)
production_list.append(production)
production = Production(["L"], [">="], 23)
production_list.append(production)
production = Production(["L"], ["<="], 24)
production_list.append(production)
production = Production(["L"], ["=="], 25)
production_list.append(production)
production = Production(["D"], ["B"], 26)
production_list.append(production)
production = Production(["B"], ["N"], 27)
production_list.append(production)
return production_list
source = [[5, "int", " 關(guān)鍵字"], [1, "lexicalanalysis", " 標(biāo)識(shí)符"], [13, "(", " 左括號(hào)"], [14, ")", " 右括號(hào)"], [20, "{", " 左大括號(hào)"],
[4, "float", " 關(guān)鍵字"], [1, "a", " 標(biāo)識(shí)符"], [15, ";", " 分號(hào)"], [5, "int", " 關(guān)鍵字"], [1, "b", " 標(biāo)識(shí)符"],
[15, ";", " 分號(hào)"], [1, "a", " 標(biāo)識(shí)符"], [12, "=", " 賦值號(hào)"], [3, "1.1", " 浮點(diǎn)數(shù)"], [15, ";", " 分號(hào)"], [1, "b", " 標(biāo)識(shí)符"],
[12, "=", " 賦值號(hào)"], [2, "2", " 整數(shù)"], [15, ";", " 分號(hào)"], [8, "while", " 關(guān)鍵字"], [13, "(", " 左括號(hào)"],
[1, "b", " 標(biāo)識(shí)符"], [17, "<", " 小于號(hào)"], [2, "100", " 整數(shù)"], [14, ")", " 右括號(hào)"], [20, "{", " 左大括號(hào)"],
[1, "b", " 標(biāo)識(shí)符"], [12, "=", " 賦值號(hào)"], [1, "b", " 標(biāo)識(shí)符"], [9, "+", " 加 號(hào)"], [2, "1", " 整數(shù)"], [15, ";", " 分號(hào)"],
[1, "a", " 標(biāo)識(shí)符"], [12, "=", " 賦值號(hào)"], [1, "a", " 標(biāo)識(shí)符"], [9, "+", " 加號(hào)"], [2, "3", " 整數(shù)"], [15, ";", " 分號(hào)"],
[21, "}", " 右大括號(hào)"], [15, ";", " 分號(hào)"], [6, "if", " 關(guān)鍵字"], [13, "(", " 左括號(hào)"], [1, "a", " 標(biāo)識(shí)符"],
[16, ">", " 大于號(hào)"], [2, "5", " 整數(shù)"], [14, ")", " 右括號(hào)"], [20, "{", " 左大括號(hào)"], [1, "b", " 標(biāo)識(shí)符"],
[12, "=", " 賦值號(hào)"], [1, "b", " 標(biāo)識(shí)符"], [10, "-", " 減號(hào)"], [2, "1", " 整數(shù)"], [15, ";", " 分號(hào)"], [21, "}", " 右大括號(hào)"],
[7, "else", " 關(guān)鍵字"], [20, "{", " 左大括號(hào)"], [1, "b", " 標(biāo)識(shí)符"], [12, "=", " 賦值號(hào)"], [1, "b", " 標(biāo)識(shí)符"],
[9, "+", " 加號(hào)"], [2, "1", " 整數(shù)"], [15, ";", " 分號(hào)"], [21, "}", " 右大括號(hào)"], [21, "}", " 右大括號(hào)"]]
以上就是Python識(shí)別處理照片中的條形碼的詳細(xì)內(nèi)容,更多關(guān)于python 識(shí)別條形碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- python好玩的項(xiàng)目—色情圖片識(shí)別代碼分享
- 如何使用Python進(jìn)行PDF圖片識(shí)別OCR
- python 自動(dòng)識(shí)別并連接串口的實(shí)現(xiàn)
- OpenCV+Python3.5 簡(jiǎn)易手勢(shì)識(shí)別的實(shí)現(xiàn)
- python 實(shí)現(xiàn)表情識(shí)別
- python 實(shí)現(xiàn)性別識(shí)別
- python實(shí)現(xiàn)圖片,視頻人臉識(shí)別(dlib版)
- python實(shí)現(xiàn)圖片,視頻人臉識(shí)別(opencv版)
- python調(diào)用百度API實(shí)現(xiàn)人臉識(shí)別
- 詳解利用python識(shí)別圖片中的條碼(pyzbar)及條碼圖片矯正和增強(qiáng)
- python 實(shí)現(xiàn)的車牌識(shí)別項(xiàng)目
相關(guān)文章
pycharm社區(qū)版安裝node.js插件運(yùn)行js代碼方法
PyCharm可以說(shuō)是當(dāng)今最流行的一款Python IDE了,下面這篇文章主要給大家介紹了關(guān)于pycharm社區(qū)版安裝node.js插件運(yùn)行js代碼的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
python串口如何讀取byte類型數(shù)據(jù)并訪問(wèn)
這篇文章主要介紹了python串口如何讀取byte類型數(shù)據(jù)并訪問(wèn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
一文帶你深入了解Python中的GeneratorExit異常處理
GeneratorExit是Python內(nèi)置的異常,當(dāng)生成器或協(xié)程被強(qiáng)制關(guān)閉時(shí),Python解釋器會(huì)向其發(fā)送這個(gè)異常,下面我們來(lái)看看如何處理這一異常吧2025-03-03
如何解決mmcv無(wú)法安裝或安裝之后報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了如何解決mmcv無(wú)法安裝或安裝之后報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
Python如何定義有默認(rèn)參數(shù)的函數(shù)
這篇文章主要介紹了Python如何定義有默認(rèn)參數(shù)的函數(shù),幫助大家更好的理解和學(xué)習(xí)Python,感興趣的朋友可以了解下2020-08-08
python實(shí)現(xiàn)同一局域網(wǎng)下傳輸圖片
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)同一局域網(wǎng)下傳輸圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03

