# f1bf339ee0ee47178c6bb1b884921c34
# https://du.zsyflive.com/mingpiam.jpg
import base64
from urllib import parse
import requests
import json
import xlwt # 导入xlwt 库
import datetime
import cv2
import os
import requests
import oss2
import socket
from oss2.credentials import EnvironmentVariableCredentialsProvider
imgurl="https://du.zsyflive.com/mingpiam.jpg";
appcode = 'f1bf339ee0ee47178c6bb1b884921c34'
aliUrl="https://bizcard.market.alicloudapi.com/rest/160601/ocr/ocr_business_card.json";
ossUrl='https://qlyjc.oss-cn-hangzhou.aliyuncs.com/m1/'
imgossurlList=[]
userInfos=[];
userName="";
listInfos=[];
# 确保上传的文件类型为图片
supported_extensions = ('.jpg', '.jpeg', '.png', '.gif')
def newTable():
book = xlwt.Workbook(encoding="utf-8") # 创建工作簿
sheet = book.add_sheet("sheet1") # 创建工作表格
def uploadimg(yourLocalFile,filename):
# -*- coding: utf-8 -*-
# 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
auth = oss2.Auth('LTAI5tR********QW5mku2', 'uO**********zu9')
# 填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
# yourBucketName填写存储空间名称。
bucket = oss2.Bucket(auth, 'https://*******ncs.com', 'qlyjc');
# 上传文件到OSS
# yourObjectName由包含文件后缀,不包含Bucket名称组成的Object完整路径,例如abc/efg/123.jpg。
# yourLocalFile由本地文件路径加文件名包括后缀组成,例如/users/local/myfile.txt。
bucket.put_object_from_file('m1/'+filename, yourLocalFile);
ossImgUrl=ossUrl+filename;
imgossurlList.append(ossImgUrl)
def getFiles(images_folder,book,sheet):
# 遍历文件夹内的所有文件
for filename in os.listdir(images_folder):
# 检查是否是图片文件
if filename.endswith(supported_extensions):
file_path = os.path.join(images_folder, filename)
uploadimg(file_path,filename);
addExcel(book,sheet);
def newExcel(index,info,sheet):
print(info)
sheet.write(index, 0, info[0]) # 把python写到第一行、第一列
sheet.write(index, 1, info[1]) # 把love写到第2行、第2列
sheet.write(index, 2, info[2]) # 把love写到第2行、第2列
sheet.write(index, 3, info[3]) # 把love写到第2行、第2列
sheet.write(index, 4, info[4]) # 把love写到第2行、第2列
sheet.write(index, 5, info[5]) # 把love写到第2行、第2列
sheet.write(index, 6, info[6]) # 把python写到第一行、第一列
sheet.write(index, 7, info[7]) # 把love写到第2行、第2列
sheet.write(index, 8, info[8]) # 把love写到第2行、第2列
sheet.write(index, 9, info[9]) # 把love写到第2行、第2列
sheet.write(index, 10, info[10]) # 把love写到第2行、第2列
sheet.write(index, 11, info[11]) # 把love写到第2行、第2列
sheet.write(index, 12, info[12]) # 把python写到第一行、第一列
sheet.write(index, 13, info[13]) # 把love写到第2行、第2列
sheet.write(index, 14, info[14]) # 把love写到第2行、第2列
sheet.write(index, 15, info[15]) # 把love写到第2行、第2列
sheet.write(index, 16, info[16]) # 把love写到第2行、第2列
sheet.write(index, 17, info[17]) # 把love写到第2行、第2列
sheet.write(index, 18, info[18]) # 把python写到第一行、第一列
sheet.write(index, 19, info[19]) # 把love写到第2行、第2列
sheet.write(index, 20, info[20]) # 把love写到第2行、第2列
sheet.write(index, 21, info[21]) # 把love写到第2行、第2列
def getNowTime():
# 获取当前的日期和时间
current_datetime = datetime.datetime.now()
# 解构datetime对象以获取各个部分
year = current_datetime.year
month = current_datetime.month
day = current_datetime.day
hour = current_datetime.hour
minute = current_datetime.minute
second = current_datetime.second
return str(year)+"_"+str(month)+"_"+str(day)+"_"+str(hour)+"_"+str(minute)+"_"+str(second)
def queryApp(urlImg):
url =aliUrl;
querystring = {"image":urlImg}
headers = {
'Authorization':"APPCODE " + appcode,
"Content-Type":"application/json; charset=UTF-8"
}
response = requests.post( url, data=json.dumps(querystring), headers=headers)
info=response.json()
print("名片识别结果",info);
# txtx= ["姓名","单位","Email","地址","电话","传真","邮编","单位主页","部门","移动电话",
# "多个手机号","职务","人员备注","称谓","卡号","公司备注",
# "标签一","产品类别","所属行业","所属展会","需求","注意事项",]
# info['tel_work'].extend(info['tel_work']);
# info['tel_work'].sort(reverse=False)
# info['tel_work'].join(",")
# obgg = [info['name'], info['company'], "Email", info['addr'],','.join(info['tel_work']), "传真", info['email'], "单位主页", "部门",
# ','.join(info['tel_cell']),
# "多个手机号", info['title'], "人员备注", "称谓", "卡号", "公司备注",
# "标签一", "产品类别", "所属行业", "所属展会", "需求", "注意事项" ]
obgg = [info['name'], info['company'], "", info['addr'], ','.join(info['tel_work']), "", info['email'],
"", "",
','.join(info['tel_cell']),
"", info['title'], "", "", "", "",
"", "", "", "", "", ""]
listInfos.append(obgg);
# print("姓名",info['name'],"公司名",info['company'],'公司地址',info['addr'],'职位',info['title'],'电话1',info['tel_work'],'电话2',info['tel_cell'],)
# queryApp()
# newExcel();
def getIp():
# 获取本机计算机名称
hostname = socket.gethostname()
# 获取本机ip
ip = socket.gethostbyname(hostname)
return ip;
def addExcel(book,sheet):
for item in imgossurlList:
queryApp(item)
for index,item in enumerate(listInfos):
newExcel(index+1,item,sheet)
newExcel(0,
["姓名",
"单位",
"Email",
"地址",
"电话",
"传真",
"邮编",
"单位主页",
"部门",
"移动电话",
"多个手机号",
"职务",
"人员备注",
"称谓",
"卡号",
"公司备注",
"标签一",
"产品类别",
"所属行业",
"所属展会",
"需求",
"注意事项",
],sheet
);
timeTxt= getNowTime();
newFileName=input("请输入生成的文件名:");
if '哈哈' in userName:
filename=userName+"-"+newFileName+"名片集.xls"
else :
filename = userName + "(" + timeTxt + "--" + getIp() + ")" + "名片集.xls"
book.save(filename) # 保存到test表格中
print("-------------------------------"+filename+"文件生成完成----------------------------------------")
start1()
def start1():
book = xlwt.Workbook(encoding="utf-8") # 创建工作簿
sheet = book.add_sheet("sheet1") # 创建工作表格
fileAdr=input("请输入名片所在文件夹:");
getFiles(fileAdr,book,sheet)
if __name__ == '__main__':
userName = input("请输入您的姓名:");
if '哈哈' in userName:
userName="哈哈哈"
start1();
python 阿里云 OCR名片识别
于 2024-05-29 14:09:14 首次发布