使用Face++ API进行车牌识别

本文介绍了如何使用Face++API接口,请求车牌识别;以及对结果进行解析,并根据Face++返回的坐标值画出车牌所在位置的矩形框。

Face++ AI开放平台车牌识别使用

1. 获取API key 和 API Secret

API key获取地址
在这里插入图片描述

2. 车牌识别

调用Face++ API接口,请求车牌识别。

使用API key 替换key, API Secret 替换 Secret。

# 请求face++车牌识别
def requtest_facePlusPlus_lpr(image):
    http_url = 'https://api-cn.faceplusplus.com/imagepp/v1/licenseplate'
    key = "DbI7qZH0ZB9pm-HxOymU2wrzg5logflJ"
    secret = "SKE9l1-dDwvVOkoEp22ccqb0zxzZTTOg"
    filepath = "./static/images/lpr/1.jpg"

    boundary = '----------%s' % hex(int(time.time() * 1000))
    data = []
    data.append('--%s' % boundary)
    data.append('Content-Disposition: form-data; name="%s"\r\n' % 'api_key')
    data.append(key)
    data.append('--%s' % boundary)
    data.append('Content-Disposition: form-data; name="%s"\r\n' % 'api_secret')
    data.append(secret)
    data.append('--%s' % boundary)

    fr = open(filepath, 'rb')
    data.append('Content-Disposition: form-data; name="%s"; filename=" "' % 'image_file')
    data.append('Content-Type: %s\r\n' % 'application/octet-stream')
    data.append(fr.read())
    fr.close()

    data.append('--%s' % boundary)
    data.append('Content-Disposition: form-data; name="%s"\r\n' % 'return_landmark')
    data.append('1')
    data.append('--%s' % boundary)
    data.append('Content-Disposition: form-data; name="%s"\r\n' % 'return_attributes')
    data.append(
        "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus")
    data.append('--%s--\r\n' % boundary)

    for i, d in enumerate(data):
        if isinstance(d, str):
            data[i] = d.encode('utf-8')

    http_body = b'\r\n'.join(data)

    # build http request
    req = urllib.request.Request(url=http_url, data=http_body)

    # header
    req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)

    # post data to server
    resp = urllib.request.urlopen(req, timeout=5)
    # get response
    qrcont = resp.read()

    # if you want to load as json, you should decode first,
    # for example: json.loads(qrount.decode('utf-8'))
    return json.loads(qrcont.decode('utf-8'))

3. 解析结果

经过第二步得到的是json文件,需要对其进行解析,同时也需要根据Face++返回的坐标值画出矩形框。

# 解析face++识别结果
# 画出矩形框
def parse_face_result(result, image):
    result = result['results']
    ret_lpr = result[0]['license_plate_number']
    print('face++识别结果', ret_lpr)
    pos_left_top = (result[0]['bound']['left_top']['x'] - 1, result[0]['bound']['left_top']['y'] - 12)
    pos_right_bottom = (result[0]['bound']['right_bottom']['x'] + 3, result[0]['bound']['right_bottom']['y'] + 12)


    image = cv2.imread(image)
    image = cv2.rectangle(image, pos_left_top, pos_right_bottom, (0, 0, 255), 3, cv2.LINE_AA)

    t = str(time.time())
    img_url = './static/images/lpr/lpr_result' + t + '.jpg'
    cv2.imwrite(img_url, image)

    return ret_lpr,img_url

上诉步骤中需要使用到库:

import urllib.request
import urllib.error
import time
import json
import cv2
import requests
import base64

参考

  1. Face++ 文档中心 – 调用示例
  2. 使用百度AI开放平台进行车牌识别
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值