利用Python识别照片条形码的方法
发布时间: 2020-11-17 14:45:38
来源:亿速云
阅读: 94
本文详细介绍了如何利用Python来识别照片上的条形码。 文章内容质量很高,编辑请参考。 希望你读了这篇文章了解知识。
最近在数独,实现图像识别求解数独,从输入到输出平均需要0.5s。
整体的想法可能是识别图中的数字,生成list并求解。
输入输出demo
数独采用的是微软拥有的Microsoft sudoku软件随意剪切的图像。 下图:
解开程序的结果如下图。
defgetfollow(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
defgetfollowforvar(var,varset,terminalset,first_dic,production_list,follow_dic,done ) ) :
if done[var]==1:
返回
forproductioninproduction _ list :
if var in production.right:
#index这里在一些极端的情况下有错误。 例如,如果var多次出现,索引就是最左侧的
if production.right.index(var )!=len(production.right )- 1:
follow _ DIC [ var ]=first _ DIC [ production.right.index [ var ]1]|follow _ DIC [ var ]
#右边有非终端符号,但没有考虑为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,
丹)
follow _ DIC [ var ]=follow _ DIC [ var ]|follow _ DIC [ production.left [0] ]
done[var]=1
步骤的具体流程
整体过程如下图所示。
读入图像后,求出轮廓信息找出有数字的位置和不包含数字的空白位置,提取数字信息用KNN识别,识别数字; 没有数字信息的在list中放置0; 生成未解决数独list,然后解除数独,在原图中表示信息。
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.appen
d(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"], ["
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", " 关键字"], [1, "lexicalanalysis", " 标识符"], [13, "(", " 左括号"], [14, ")", " 右括号"], [20, "{", " 左大括号"],
[4, "float", " 关键字"], [1, "a", " 标识符"], [15, ";", " 分号"], [5, "int", " 关键字"], [1, "b", " 标识符"],
[15, ";", " 分号"], [1, "a", " 标识符"], [12, "=", " 赋值号"], [3, "1.1", " 浮点数"], [15, ";", " 分号"], [1, "b", " 标识符"],
[12, "=", " 赋值号"], [2, "2", " 整数"], [15, ";", " 分号"], [8, "while", " 关键字"], [13, "(", " 左括号"],
[1, "b", " 标识符"], [17, "
[1, "b", " 标识符"], [12, "=", " 赋值号"], [1, "b", " 标识符"], [9, "+", " 加 号"], [2, "1", " 整数"], [15, ";", " 分号"],
[1, "a", " 标识符"], [12, "=", " 赋值号"], [1, "a", " 标识符"], [9, "+", " 加号"], [2, "3", " 整数"], [15, ";", " 分号"],
[21, "}", " 右大括号"], [15, ";", " 分号"], [6, "if", " 关键字"], [13, "(", " 左括号"], [1, "a", " 标识符"],
[16, ">", " 大于号"], [2, "5", " 整数"], [14, ")", " 右括号"], [20, "{", " 左大括号"], [1, "b", " 标识符"],
[12, "=", " 赋值号"], [1, "b", " 标识符"], [10, "-", " 减号"], [2, "1", " 整数"], [15, ";", " 分号"], [21, "}", " 右大括号"],
[7, "else", " 关键字"], [20, "{", " 左大括号"], [1, "b", " 标识符"], [12, "=", " 赋值号"], [1, "b", " 标识符"],
[9, "+", " 加号"], [2, "1", " 整数"], [15, ";", " 分号"], [21, "}", " 右大括号"], [21, "}", " 右大括号"]]
关于利用Python如何实现识别照片中的条形码就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。