当前位置:   article > 正文

基于Django的图片OCR识别系统

基于django的图片

功能模块介绍

本系统采用python语言、Django架构、Mysql数据库。

项目Value
语言python
架构Django
数据库MySql

登录、注册模块

系统登录模块采用了check_code,即身份验证+验证码模式:

def image_code(request):
    '''生成图像验证码'''

    # os.chdir('django_yibiao')  # 改变工作目录

    # 调用pillow函数,生成图片
    img,code_string = check_code()
    print(code_string)  # YLCHV 每次都会变

    # 写入到自己的session中(以便于后续获取验证码再进行校验)
    request.session['image_code'] = code_string
    # 给session设置60s超时
    request.session.set_expiry(60)

    stream = BytesIO()
    img.save(stream,'png')
    return HttpResponse(stream.getvalue())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

登录时调用image_code方法:

def login(request):
    req_method = request.method
    if req_method == 'GET':
        form = LoginForm()
        return render(request, 'login.html', {"form": form})
    form = LoginForm(data=request.POST)
    if form.is_valid():
        # 验证成功,获取到的用户名和密码
        # print(form.cleaned_data)   # {'username': 'root', 'password': '4876931d2563f86d7abfa501e3a51554','code': 'YLCHV'}

        # 验证码的校验
        # pop('code')相当于删除cleaned_data字典中的code字段,
        # 删除是因为filter(**form.cleaned_data).first()数据库中没有code字段
        user_input_code = form.cleaned_data.pop('code')
        img_code = request.session.get('image_code',"")
        if user_input_code.upper() != img_code.upper():
            form.add_error("code", "验证码错误")  # form.add_error主动添加一个错误
            return render(request, 'login.html', {"form": form})

        # 去数据库校验用户名和密码是否正确,获取用户对象、没有数据为None
        # admin = models.Admin.objects.filter(username="xxx",password="xxx").first()
        admin = models.Admin.objects.filter(**form.cleaned_data).first()
        if not admin:  # 说明用户名和密码不存在
            # form.add_error("username", "用户名或密码错误")
            form.add_error("password","用户名或密码错误")     # form.add_error主动添加一个错误
            return render(request, 'login.html', {"form": form})

        # 用户名和密码正确
        # 网站生成随机字符串; 写到用户浏览器的cookie中;再写入到session中;
        request.session["info"] = {'id': admin.id, 'username': admin.username, "name": admin.name,'role': admin.get_role_display()}
        # session可以保存7天
        request.session.set_expiry(60 * 60 * 24 * 7)

        # return redirect("/yibiao/list/")
        return redirect("/")
    return render(request, 'login.html', {"form": form})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

系统登录界面预览:
在这里插入图片描述

图片识别模块

先浏览下系统界面。
在这里插入图片描述
该模块具有上传图片识别与我的上传(即上传历史)两个功能。

上传图片识别

该系统目前基于paddlepaddle百度飞浆进行识别(PS前期进行过训练算法改进,但识别精度不够,所以采用了百度飞浆,接口已经保留,可按需进行改进)。
调用百度飞浆:

def ocr_rec(img_name):

    result = ""

    # print(os.getcwd())   # E:\python\django_yibiao
    os.chdir('OCR_rec-main/django_yibiao/yibiao_ocr')  # 改变工作目录

    # client_id 为官网获取的AK, client_secret 为官网获取的SK
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=wgEHks0l6MCpalbs3lPuFX1U&client_secret=XXX'
    response = requests.get(host)
    # if response:
    #     print(response.json()['access_token'])

    '''
    通用文字识别(高精度版)
    '''
    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
    # 二进制方式打开图片文件
    f = open('./'+img_name, 'rb')
    img = base64.b64encode(f.read())
    params = {"image": img}
    # 获取后的Token的调用
    # access_token = 'XXX'
    access_token = response.json()['access_token']
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        # print(response.json())
        img_rec = response.json()
        for i in response.json()['words_result']:
            result = result + i['words'] + "\n"
    return result
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

测试图片:
在这里插入图片描述

识别结果:
在这里插入图片描述

我的上传

该模块可查看上传图片的历史记录,如下:
在这里插入图片描述

用户管理

该模块类似于管理员模块,只有管理员权限用户才能看到此模块。该模块示例如下:
在这里插入图片描述

欢迎大家留言或私信讨论~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/880985
推荐阅读
相关标签
  

闽ICP备14008679号