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中的多线程实例教程
Aug 27 Python
在Linux下调试Python代码的各种方法
Apr 17 Python
python的文件操作方法汇总
Nov 10 Python
Django使用Mysql数据库已经存在的数据表方法
May 27 Python
Python做智能家居温湿度报警系统
Sep 25 Python
Python面向对象程序设计中类的定义、实例化、封装及私有变量/方法详解
Feb 28 Python
python aiohttp的使用详解
Jun 20 Python
django做form表单的数据验证过程详解
Jul 26 Python
解决Python二维数组赋值问题
Nov 28 Python
Python实现点云投影到平面显示
Jan 18 Python
使用Django搭建网站实现商品分页功能
May 22 Python
Python常用GUI框架原理解析汇总
Dec 07 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数据结构 算法(PHP描述) 简单选择排序 simple selection sort
2011/08/09 PHP
php利用新浪接口查询ip获取地理位置示例
2014/01/20 PHP
深入理解PHP之OpCode原理详解
2016/06/01 PHP
js Html结构转字符串形式显示代码
2011/11/15 Javascript
js的alert弹出框出现乱码解决方案
2013/09/02 Javascript
jquery获取复选框被选中的值
2014/03/22 Javascript
js利用prototype调用Array的slice方法示例
2014/06/09 Javascript
Ext修改GridPanel数据和字体颜色、css属性等
2014/06/13 Javascript
Js实现手机发送验证码时按钮延迟操作
2014/06/20 Javascript
Node.js中使用事件发射器模式实现事件绑定详解
2014/08/15 Javascript
JavaScript对象反射用法实例
2015/04/17 Javascript
JavaScript包装对象使用详解
2015/07/09 Javascript
每天一篇javascript学习小结(基础知识)
2015/11/10 Javascript
JavaScript创建对象的方式小结(4种方式)
2015/12/17 Javascript
xmlplus组件设计系列之列表(4)
2017/04/26 Javascript
微信小程序module.exports模块化操作实例浅析
2018/12/20 Javascript
浅谈express.js框架中间件(middleware)
2019/04/07 Javascript
jQuery实现轮播图源码
2019/10/23 jQuery
浅谈Vue static 静态资源路径 和 style问题
2020/11/07 Javascript
[57:47]Fnatic vs Winstrike 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
Python代理抓取并验证使用多线程实现
2013/05/03 Python
Python中属性和描述符的正确使用
2016/08/23 Python
python使用magic模块进行文件类型识别方法
2018/12/08 Python
python tkinter图形界面代码统计工具(更新)
2019/09/18 Python
vscode写python时的代码错误提醒和自动格式化的方法
2020/05/07 Python
python中def是做什么的
2020/06/10 Python
Python 日期与时间转换的方法
2020/08/01 Python
HTML5之SVG 2D入门12—SVG DOM及DOM操作介绍
2013/01/30 HTML / CSS
英国花园药房: The Garden Pharmacy
2017/12/28 全球购物
英国户外玩具儿童游乐设备网站:TP Toys(蹦床、攀爬框架、秋千、滑梯和游戏屋)
2018/04/09 全球购物
如果Session Bean得Remove方法一直都不被调用会怎么样
2012/07/14 面试题
土木工程实习生自我鉴定
2013/09/19 职场文书
酒店管理专业自荐信
2014/05/23 职场文书
春节超市活动方案
2014/08/14 职场文书
助学金申请书该怎么写?
2019/07/16 职场文书
ORACLE查看当前账号的相关信息
2021/06/18 Oracle