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正则表达式修复网站文章字体不统一的解决方法
Feb 21 Python
Python中使用urllib2防止302跳转的代码例子
Jul 07 Python
在Django的视图中使用form对象的方法
Jul 18 Python
python3编码问题汇总
Sep 06 Python
Python使用matplotlib实现的图像读取、切割裁剪功能示例
Apr 28 Python
对python 多线程中的守护线程与join的用法详解
Feb 18 Python
python 监测内存和cpu的使用率实例
Nov 28 Python
完美解决pyinstaller打包报错找不到依赖pypiwin32或pywin32-ctypes的错误
Apr 01 Python
Python内置函数locals和globals对比
Apr 28 Python
python 牛顿法实现逻辑回归(Logistic Regression)
Oct 15 Python
python实现代码审查自动回复消息
Feb 01 Python
深入浅析Django MTV模式
Sep 04 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和ACCESS写聊天室(六)
2006/10/09 PHP
初学CAKEPHP 基础教程
2009/11/02 PHP
php调用c接口无错版介绍
2014/03/11 PHP
php中文字符串截取方法实例总结
2014/09/30 PHP
Zend Framework+smarty用法实例详解
2016/03/19 PHP
PHP下载文件的函数实例代码
2016/05/18 PHP
利用php抓取蜘蛛爬虫痕迹的示例代码
2016/09/30 PHP
php可变长参数处理函数详解
2017/02/22 PHP
jquery()函数的三种语法介绍
2013/10/09 Javascript
js读取配置文件自写
2014/02/11 Javascript
js实现带关闭按钮始终显示在网页最底部工具条的方法
2015/03/02 Javascript
JavaScript实现DIV层拖动及动态增加新层的方法
2015/05/12 Javascript
快速搭建React的环境步骤详解
2017/11/06 Javascript
微信小程序表单弹窗实例
2018/07/19 Javascript
js实现图片局部放大效果详解
2019/03/18 Javascript
微信小程序使用Vant Weapp组件库的方法步骤
2019/08/01 Javascript
vue 实现LED数字时钟效果(开箱即用)
2019/12/08 Javascript
JavaScript实现10秒后再次获取验证码
2020/12/02 Javascript
利用numpy实现一、二维数组的拼接简单代码示例
2017/12/15 Python
使用python进行拆分大文件的方法
2018/12/10 Python
关于Numpy中的行向量和列向量详解
2019/11/30 Python
python super用法及原理详解
2020/01/20 Python
Python %r和%s区别代码实例解析
2020/04/03 Python
如何查看Django ORM执行的SQL语句的实现
2020/04/20 Python
python中def是做什么的
2020/06/10 Python
Tensorflow与Keras自适应使用显存方式
2020/06/22 Python
4款Python 类型检查工具,你选择哪个呢?
2020/10/30 Python
Python创建简单的神经网络实例讲解
2021/01/04 Python
Pandora德国官网:购买潘多拉手链、戒指、项链和耳环
2020/02/20 全球购物
说出你对remoting 和webservice的理解和应用
2014/06/08 面试题
JAVA软件工程师测试题
2014/07/25 面试题
奥巴马连任演讲稿
2014/05/15 职场文书
倡议书格式
2014/08/30 职场文书
投诉信回复范文
2015/07/03 职场文书
励志语录:你若不勇敢,谁替你坚强
2019/11/08 职场文书
详解Go语言Slice作为函数参数的使用
2021/07/02 Golang