车牌图片识别使用hyperlpr

功能简介:监听8000端口,接受直接上传图片来识别车牌号,也支持根据图片url来识别车牌号。

#coding=UTF-8

import flask, os, sys,time

from flask import request

from gevent import pywsgi

import requests

import numpy as np

from hyperlpr import *

import cv2

app = flask.Flask(__name__, static_folder='static')

@app.route('/upload', methods=['post'])

def upload():

    fname = request.files['file']  #获取上传的文件

    print(fname)

    print(request.files)

    if fname:

        t = time.strftime('%Y%m%d%H%M%S')

        new_fname = r'static/' + t + fname.filename

        fname.save(new_fname)  #保存文件到指定路径

        image = cv2.imread(new_fname)

        res = HyperLPR_plate_recognition(image)

        print(len(res[0]))

        str = " ".join('%s' %id for id in res[0])

        strList = str.split(' ')

        return strList[0].encode('raw_unicode_escape')

    else:

        return '{"msg": "请上传文件!"}'

@app.route('/check', methods=['get'])

def check():

    try:

        fileUrl = request.args.get('fileUrl')  #获取上传的文件url

        print(fileUrl)

        if fileUrl:

            file = requests.get(fileUrl)

            image = cv2.imdecode(np.fromstring(file.content, np.uint8), 1) #file.content 是读取的远程文件的字节流

            res = HyperLPR_plate_recognition(image)

            if len(res) > 0:

                print(len(res[0]))

                str = " ".join('%s' %id for id in res[0])

                strList = str.split(' ')

                return strList[0].encode('raw_unicode_escape')

            else:

                return '' 

        else:

            return '{"msg": "请上传文件url"}',400

    except Exception, e:

        msg = 'str(Exception):\t', str(Exception)

        return msg,500

server = pywsgi.WSGIServer(('0.0.0.0', 8000), app)

server.serve_forever()

你可能感兴趣的:(车牌图片识别使用hyperlpr)