Python识别处理照片中的条形码


Posted in Python onNovember 16, 2020

最近一直在玩数独,突发奇想实现图像识别求解数独,输入到输出平均需要0.5s。

整体思路大概就是识别出图中数字生成list,然后求解。

输入输出demo

数独采用的是微软自带的Microsoft sudoku软件随便截取的图像,如下图所示:

Python识别处理照片中的条形码

经过程序求解后,得到的结果如下图所示:

Python识别处理照片中的条形码

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,比如多次出现var,index只会返回最左侧的
            if production.right.index(var) != len(production.right) - 1:
                follow_dic[var] = first_dic[production.right[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,
                                    done)
                    follow_dic[var] = follow_dic[var] | follow_dic[production.left[0]]
  
    done[var] = 1

程序具体流程

程序整体流程如下图所示:

Python识别处理照片中的条形码

读入图像后,根据求解轮廓信息找到数字所在位置,以及不包含数字的空白位置,提取数字信息通过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.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", " 关键字"], [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, "<", " 小于号"], [2, "100", " 整数"], [14, ")", " 右括号"], [20, "{", " 左大括号"],
          [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识别处理照片中的条形码的详细内容,更多关于python 识别条形码的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
Python类的多重继承问题深入分析
Nov 09 Python
Python 实现文件的全备份和差异备份详解
Dec 27 Python
python实现的正则表达式功能入门教程【经典】
Jun 05 Python
详解Golang 与python中的字符串反转
Jul 21 Python
Python 多线程的实例详解
Sep 07 Python
Python 查看list中是否含有某元素的方法
Jun 27 Python
Python嵌套列表转一维的方法(压平嵌套列表)
Jul 03 Python
python使用scrapy发送post请求的坑
Sep 04 Python
python 美化输出信息的实例
Oct 15 Python
Django外键(ForeignKey)操作以及related_name的作用详解
Jul 29 Python
python使用requests库爬取拉勾网招聘信息的实现
Nov 20 Python
Python可视化学习之seaborn绘制矩阵图详解
Feb 24 Python
Python将list元素转存为CSV文件的实现
Nov 16 #Python
python list等分并从等分的子集中随机选取一个数
Nov 16 #Python
Python大批量搜索引擎图像爬虫工具详解
Nov 16 #Python
详解Python中list[::-1]的几种用法
Nov 16 #Python
使用Pytorch搭建模型的步骤
Nov 16 #Python
Python图像读写方法对比
Nov 16 #Python
python3中编码获取网页的实例方法
Nov 16 #Python
You might like
php 执行系统命令的方法
2009/07/07 PHP
php+html5基于websocket实现聊天室的方法
2015/07/17 PHP
php进程间通讯实例分析
2016/07/11 PHP
php 常用的系统函数
2017/02/07 PHP
理清PHP在Linxu下执行时的文件权限方法
2017/06/07 PHP
JavaScript 动态将数字金额转化为中文大写金额
2009/05/14 Javascript
js 获取Listbox选择的值的代码
2010/04/15 Javascript
js格式化货币数据实现代码
2013/09/04 Javascript
jQuery操作Select的Option上下移动及移除添加等等
2013/11/18 Javascript
jquery使用append(content)方法注意事项分享
2014/01/06 Javascript
jQuery实现鼠标划过添加和删除class的方法
2015/06/26 Javascript
JS实现在状态栏显示打字效果完整实例
2015/11/02 Javascript
FullCalendar日历插件应用之数据展现(一)
2015/12/23 Javascript
浅谈如何实现easyui的datebox格式化
2016/06/12 Javascript
canvas实现环形进度条效果
2017/03/23 Javascript
微信小程序 实例开发总结
2017/04/26 Javascript
浅谈javascript中的prototype和__proto__的理解
2019/04/07 Javascript
微信小程序scroll-view锚点链接滚动跳转功能
2019/12/12 Javascript
Vue项目页面跳转时浏览器窗口上方显示进度条功能
2020/03/26 Javascript
用Python给文本创立向量空间模型的教程
2015/04/23 Python
python打开url并按指定块读取网页内容的方法
2015/04/29 Python
Anaconda多环境多版本python配置操作方法
2017/09/12 Python
Python线程创建和终止实例代码
2018/01/20 Python
Ubuntu下使用Python实现游戏制作中的切分图片功能
2018/03/30 Python
Python基于TCP实现会聊天的小机器人功能示例
2018/04/09 Python
python logging日志模块原理及操作解析
2019/10/12 Python
python 实现方阵的对角线遍历示例
2019/11/29 Python
Python爬虫入门有哪些基础知识点
2020/06/02 Python
我为自己代言广告词
2014/03/18 职场文书
《故乡》教学反思
2014/04/10 职场文书
2015学生会文艺部工作总结
2015/04/03 职场文书
本科毕业答辩开场白
2015/05/27 职场文书
演讲稿:​快乐,从不抱怨开始!
2019/04/02 职场文书
pytorch中Schedule与warmup_steps的用法说明
2021/05/24 Python
代码复现python目标检测yolo3详解预测
2022/05/06 Python
关于windows server 2012 DC 环境 重启后蓝屏代码:0xc00002e2的问题
2022/05/25 Servers