基于opencv接入百度ai实现银行卡卡号识别

话不多说直接上代码
import requests
import base64
import urllib,sys
import ssl
import urllib.request
import cv2
这些都是需要包含的库文件
APP_ID = ‘####’
API_KEY = ####’
SECRET_KEY = ####
#号的部分是需要你们自己填写的,后边的照片保存地址以及打开地址也需要你们自己设置,后边就不一一赘述了。

使用cv2库进行图像扫描
cap = cv2.VideoCapture(0)
flag = 1
num = 1
while(cap.isOpened()):
ret_flag,Vshow =cap.read()
cv2.imshow(“dx”,Vshow)
k = cv2.waitKey(1) & 0xff
if k ==ord(‘s’):
cv2.imwrite(“C:\Users\dx\Desktop\opencvphoto\”+str(num)+".jpg",Vshow)
print(“success to save " + str(num) + “.jpg”)
print(”-------------------------")
num += 1
elif k == ord(‘q’):
break
cap.release()
cv2.destroyAllWindows()

api接入
host = ‘https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=’+API_KEY+’&client_secret=’+SECRET_KEY
request = urllib.request.Request(host)
request.add_header(‘Content-Type’, ‘application/json; charset=UTF-8’)
response = urllib.request.urlopen(request)
content = response.read()
if (content):
print(content)
x=str(content)
y=x.split(’:’,5)
y1=y[4].split(",",2)
#强制将byte转换str用split方法将access_token分割出来
request_url = “https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard”

imgnum=1
while(imgnum < num):
f = open(“C:/Users/dx/Desktop/opencvphoto/”+str(imgnum)+’.jpg’, ‘rb’)
imgnum+=1
img = base64.b64encode(f.read())
params = {“image”: img}
access_token = y1[0]
request_url = request_url + “?access_token=” + access_token
headers = {‘content-type’: ‘application/x-www-form-urlencoded’}
response1 = requests.post(request_url, data=params, headers=headers)
if response1:
print(response1.json())

在这里插入代码片# encoding:utf-8

import requests
import base64
import urllib,sys
import ssl
import urllib.request
import cv2

APP_ID = '#'
API_KEY = '#'
SECRET_KEY = '#'

#使用cv2库进行图像扫描
cap = cv2.VideoCapture(0)
flag = 1
num = 1
while(cap.isOpened()):
    ret_flag,Vshow =cap.read()
    cv2.imshow("dx",Vshow)
    k = cv2.waitKey(1) & 0xff
    if k ==ord('s'):
        cv2.imwrite("C:\\Users\\dx\\Desktop\\opencvphoto\\"+str(num)+".jpg",Vshow)
        print("success to save  " + str(num) + ".jpg")
        print("-------------------------")
        num += 1
    elif k == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()


# api接入
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+API_KEY+'&client_secret='+SECRET_KEY
request = urllib.request.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
content = response.read()
if (content):
    print(content)
    x=str(content)
    y=x.split(':',5)
    y1=y[4].split(",",2)
    #强制将byte转换str用split方法将access_token分割出来
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard"
# 二进制方式打开图片文件
imgnum=1
while(imgnum < num):
    f = open("C:/Users/dx/Desktop/opencvphoto/"+str(imgnum)+'.jpg', 'rb')
    imgnum+=1
    img = base64.b64encode(f.read())
    params = {"image": img}
    access_token = y1[0]
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response1 = requests.post(request_url, data=params, headers=headers)
    if response1:
        print(response1.json())

大概就是这些了,需要源代码全部文件的可以在链接里下载[添加链接描述](https://download.csdn.net/download/qq_41510116/12191255)

你可能感兴趣的:(基于opencv接入百度ai实现银行卡卡号识别)