功能簡介:監(jiān)聽8000端口,接受直接上傳圖片來識別車牌號,也支持根據(jù)圖片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 是讀取的遠(yuǎn)程文件的字節(jié)流
? ? ? ? ? ? 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()