百度大脑:

如下图,百度开放了许多人工智能接口可以使用,先注册一个百度大脑账户

点击创建应用,选择需要的功能,如人脸识别,语音识别等

点击查看文档,可以查看功能对应语言的方法,参数。首先在CMD命令下输入pip install baidu-aip安装百度大脑库

颜值测试接口功能实现:

测试图片

运行结果:年龄21,颜值78.65,性别女性

from aip import AipFace        #百度大脑库中导入脸部分析的库
import base64              #用于图片base64转换 """ 你的 APPID AK SK """
APP_ID = ''           #导入百度创建应用所给的ID,如这篇随笔第二幅图给出
API_KEY = 'nHyzlAVjvcKuwecpHiSGUH4F'
SECRET_KEY = 'pcZC7p53Img09Q2WnGaq6cUlS3QKOHGk' client = AipFace(APP_ID, API_KEY, SECRET_KEY)    #为访问做签名验证 # image = "取决于image_type参数,传入BASE64字符串或URL字符串或FACE_TOKEN字符串" imageType = "BASE64"       option = {'face_field': 'age,gender,beauty'} def get_file_content(file_path):
"""获取文件内容"""
with open(file_path, 'rb') as fr:
content = base64.b64encode(fr.read())    #把图片转换成base64格式,百度要求格式
return content.decode('utf8') # 把二进制编译 # print(get_file_content(r'C:\Users\Administrator\Desktop\12321.jpeg')) def face_score(file_path):
"""脸部识别分数"""
result = client.detect(get_file_content(file_path), imageType, option)    #百度给出的方法,参考百度大脑文档
print(result)              #所得为字典型数据
age = result['result']['face_list'][0]['age']
beauty = result['result']['face_list'][0]['beauty']
gender = result['result']['face_list'][0]['gender']['type'] return age, beauty, gender print(face_score('CX.jpg'))

颜值测试窗口功能实现:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
pip install pillow
pip install baidu-aip
pip install tkinter
"""
import PIL
import time
import base64
import tkinter as tk
from PIL import Image
from PIL import ImageTk
from aip import AipFace
from tkinter.filedialog import askopenfilename # 配置百度aip参数
APP_ID = ''
API_KEY = 'nHyzlAVjvcKuwecpHiSGUH4F'
SECRET_KEY = 'pcZC7p53Img09Q2WnGaq6cUlS3QKOHGk'
a_face = AipFace(APP_ID, API_KEY, SECRET_KEY)
image_type = 'BASE64' options = {'face_field': 'age,gender,beauty'} def get_file_content(file_path):
"""获取文件内容"""
with open(file_path, 'rb') as fr:
content = base64.b64encode(fr.read()) return content.decode('utf8') def face_score(file_path):
"""脸部识别分数"""
result = a_face.detect(get_file_content(file_path), image_type, options)
print(result)
age = result['result']['face_list'][0]['age']
beauty = result['result']['face_list'][0]['beauty']
gender = result['result']['face_list'][0]['gender']['type'] return age, beauty, gender class ScoreSystem():
"""打分系统类"""
root = tk.Tk() # 修改程序框的大小
root.geometry('800x500') # 添加程序框标题
root.title('女神颜值打分系统') # 修改背景色
canvas = tk.Canvas(root,
width=800, # 指定Canvas组件的宽度
height=500, # 指定Canvas组件的高度
bg='#E6E6FA') # 指定Canvas组件的背景色
canvas.pack() def start_interface(self):
"""主运行函数"""
self.title()
self.time_component() # 打开本地文件
tk.Button(self.root, text='打开文件', command=self.show_original_pic).place(x=50, y=150)
# 进行颜值评分
tk.Button(self.root, text='运行程序', command=self.open_files2).place(x=50, y=230)
# 显示帮助文档
tk.Button(self.root, text='帮助文档', command=self.show_help).place(x=50, y=310)
# 退出系统
tk.Button(self.root, text='退出软件', command=self.quit).place(x=50, y=390)
# 显示图框标题
tk.Label(self.root, text='原图', font=10).place(x=380, y=120)
# 修改图片大小
self.label_img_original = tk.Label(self.root)
# 设置显示图框背景
self.cv_orinial = tk.Canvas(self.root, bg='white', width=270, height=270)
# 设置显示图框边框
self.cv_orinial.create_rectangle(8, 8, 260, 260, width=1, outline='red')
# 设置位置
self.cv_orinial.place(x=265, y=150)
# 显示图片位置
self.label_img_original.place(x=265, y=150) # 设置评分标签
tk.Label(self.root, text='性别', font=10).place(x=680, y=150)
self.text1 = tk.Text(self.root, width=10, height=2)
tk.Label(self.root, text='年龄', font=10).place(x=680, y=250)
self.text2 = tk.Text(self.root, width=10, height=2)
tk.Label(self.root, text='评分', font=10).place(x=680, y=350)
self.text3 = tk.Text(self.root, width=10, height=2) # 填装文字
self.text1.place(x=680, y=175)
self.text2.place(x=680, y=285)
self.text3.place(x=680, y=385) # 开启循环
self.root.mainloop() def show_original_pic(self):
"""放入文件"""
self.path_ = askopenfilename(title='选择文件')
# 处理文件
img = Image.open(fr'{self.path_}')
img = img.resize((270, 270), PIL.Image.ANTIALIAS) # 调整图片大小至270*270
# 生成tkinter图片对象
img_png_original = ImageTk.PhotoImage(img)
# 设置图片对象
self.label_img_original.config(image=img_png_original)
self.label_img_original.image = img_png_original
self.cv_orinial.create_image(5, 5, anchor='nw', image=img_png_original) def open_files2(self):
# 获取百度API接口获得的年龄、分数、性别
age, score, gender = face_score(self.path_) # 清楚text文本框内容并进行插入
self.text1.delete(1.0, tk.END)
self.text1.tag_config('red', foreground='RED')
self.text1.insert(tk.END, gender, 'red') self.text2.delete(1.0, tk.END)
self.text2.tag_config('red', foreground='RED')
self.text2.insert(tk.END, age, 'red') self.text3.delete(1.0, tk.END)
self.text3.tag_config('red', foreground='RED')
self.text3.insert(tk.END, score, 'red') def show_help(self):
"""显示帮助"""
pass def quit(self):
"""退出"""
self.root.quit() def get_time(self, lb):
"""获取时间"""
time_str = time.strftime("%Y-%m-%d %H:%M:%S") # 获取当前的时间并转化为字符串
lb.configure(text=time_str) # 重新设置标签文本
self.root.after(1000, self.get_time, lb) # 每隔1s调用函数 get_time自身获取时间 def time_component(self):
"""时间组件"""
lb = tk.Label(self.root, text='', fg='blue', font=("黑体", 15))
lb.place(relx=0.75, rely=0.90)
self.get_time(lb) def title(self):
"""标题设计"""
lb = tk.Label(self.root, text='女神颜值打分系统',
bg='#6495ED',
fg='lightpink', font=('华文新魏', 32),
width=20,
height=2,
# relief=tk.SUNKEN
)
lb.place(x=200, y=10) score_system = ScoreSystem()
score_system.start_interface()

语音合成:

合成的语音

from aip import AipSpeech
#from ffmpy3 import FFmpeg """ 你的 APPID AK SK """
APP_ID = ''
API_KEY = 'nHyzlAVjvcKuwecpHiSGUH4F'
SECRET_KEY = 'pcZC7p53Img09Q2WnGaq6cUlS3QKOHGk' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) """语音合成"""
result = client.synthesis('中国,是以华夏文明为源泉、中华文化为基础,并以汉族为主体民族的多民族国家', 'zh', 1, {
'vol': 10,
'per' :0,
}) # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
if not isinstance(result, dict):
with open('auido.mp3', 'wb') as f:
f.write(result)

音频文件转码(百度识音要求格式)

下载ffmpeg,并加入到环境变量中

cmd命令行中输入:ffmpeg -y -i auido.mp3 -acodec pcm_s16le -f s16le -ac 1 -ar 16000 audio.pcm

成功生成audio.pcm的文件

语音识别:

事先准备文件生成记事本及内容

from aip import AipSpeech
#from ffmpy3 import FFmpeg """ 你的 APPID AK SK """
APP_ID = ''
API_KEY = 'nHyzlAVjvcKuwecpHiSGUH4F'
SECRET_KEY = 'pcZC7p53Img09Q2WnGaq6cUlS3QKOHGk' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) """语音识别"""
# 读取文件
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read() # 识别本地文件
res=client.asr(get_file_content('audio.pcm'), 'pcm', 16000, { #识别pcm文件
'dev_pid': 1536,
})
print(res)
print(res['result'][0])         #控制台显示识别出的文字内容 f1 = open('heihei.txt','w')      #把识别内容写入heihei.txt文件
f1.write(res['result'][0])
f1.close()

python预课06 基于百度大脑AI的人工智能,百度颜值检测,语音合成与识别的更多相关文章

  1. 如何快速将百度大脑AI技术内置智能小程序中

    实现效果: 该AI智能小程序目前集成了百度AI开放平台数十个AI服务产品功能,包括人脸识别.文字识别.表格识别.红酒识别.货币识别.地标识别.手势识别.商标识别.果蔬识别.菜品识别等图片识别功能,以及 ...

  2. 综合5项百度大脑AI技术,快速构建智能交通方案

    一.整体方案:思路:整合百度AI功能,通过百度AI解决.优化在公交运行过程中遇到的运营.管理.安全等方面的问题.具体如下: 安全方面:通过驾驶员检测+语音合成,对驾驶员状态进行实时检测,跟踪,告警.  ...

  3. python预课05 爬虫初步学习+jieba分词+词云库+哔哩哔哩弹幕爬取示例(数据分析pandas)

    结巴分词 import jieba """ pip install jieba 1.精确模式 2.全模式 3.搜索引擎模式 """ txt ...

  4. python预课04 列表,元祖,统计值计算示例,py文件转为EXE文件,爬虫初步学习

    列表,元组 #list l1 = [1, 2, 3, '高弟弟'] #定义一个列表 #增 l1.append("DSB") #最后增加"DSB"的元素 #删 l ...

  5. python预课02 time模块,文本进度条示例,数字类型操作,字符串操作

    time模块 概述:time库是Python中处理时间的标准库,包含以下三类函数 时间获取: time(), ctime(), gmtime() 时间格式化: strftime(), strptime ...

  6. python预课03 三元表达式示例,函数定义示例,七段彩码管绘制示例

    三元表达式 s = '不下雨' if s == '下雨': print('带伞') if s == '不下雨': print('不带伞') #等效与以下语句 print('带伞' if s == '下 ...

  7. python预课01 turtle学习

    Turtle命令: import turtle # 导入模块 t = turtle.Pen() # 生成画笔 t.speed() #设置速度0-10:0最快 t.forward() # 前进 t.ba ...

  8. 全面解析百度大脑发布“AI开发者‘战疫’守护计划”

    即日起,百度大脑发布“AI开发者战疫守护计划” 大疫当前,人人有责,携手开发者共同出击抗击疫情 基于百度大脑AI开放平台和飞桨深度学习平台,积极运用算法.算力.软件等“武器”助力抗疫!   谁能参与计 ...

  9. 百度大脑发布“AI开发者‘战疫’守护计划”,AI支援抗疫再升级

    面对新冠肺炎疫情,AI开发者们正在积极运用算法.算力.软件等“武器”助力抗疫.针对开发者们在疫情防控期间的开发与学习需求,2月6日,百度大脑推出“AI开发者‘战疫’守护计划”, 正在进行疫情防控相关应 ...

随机推荐

  1. Maven设置MAVEN_OPTS环境变量

    原文地址:https://blog.csdn.net/porsche_gt3rs/article/details/78787491 一 原因: 运行mvn命令实际是执行java命令,既然是运行java ...

  2. | C语言I作业02

    C语言I博客作业02 标签: 18软件2班 李煦亮 问题 答案 这个作业属于那个课程 C语言程序设计I 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/C ...

  3. 鼠标滚轮滚动慢/拖动Office出现滞后问题处理

    一.说明 我对外设一直不是很了解,买的鼠标今天到了,使用时遇到了两个问题在这里记一下. 二.滚轮滚动慢处理 问题描述:在网页中滚动滚轮每次只能上下移动一点点,感觉很难受. 问题原因:此问题是滚轮滚动一 ...

  4. 螺旋折线-C++

    标题:螺旋折线 如图p1.png所示的螺旋折线经过平面上所有整点恰好一次. 对于整点(X, Y),我们定义它到原点的距离dis(X, Y)是从原点到(X, Y)的螺旋折线段的长度. 例如dis(0, ...

  5. 【c++primer练习】 typedef与指针、常量和类型别名

    # c++primer 61页 typedef char* ptr ; cstr 是一个指向 char 的常量指针, 一种错误的理解是将语句等同于const char* ptr cstr; 但 ptr ...

  6. DDR3(3):写控制

    调取的 DDR3 控制器给用户端预留了接口,用于实现对该 IP 核的控制,本篇介绍一下 DDR3 IP核写.在生成 DDR3 IP 核的界面中,可以找到 User Guide 手册,DDR3 的使用将 ...

  7. Hive sampling 语法之TABLESAMPLE用法理解

    官网关于LanguageManual Sampling的教程,部分截图如下,这里主要分享对TABLESAMPLE(BUCKET 3 OUT OF 16 ON id)子句的理解 ​ 官网中假设创建表时设 ...

  8. 几分钟打造超级好看又好用的zsh command line环境

    source: https://www.pexels.com/photo/office-working-app-computer-97077/ 注:这篇适用于用MAC 开发的developer 身为程 ...

  9. java中的进制与操作符

    直接常量 double: 111d,111D 二进制:前缀为0b 十六进制:前缀为0x或0X,后面最大9位. 八进制:前缀为0,后面最大7位. 按位操作符 与(&): 或(||): 异或(^) ...

  10. 详细的Hadoop的入门教程-伪分布模式Pseudo-Distributed Operation

    一. 伪分布模式Pseudo-Distributed Operation 这里关于VM虚拟机的安装就不再介绍了,详细请看<VMware虚拟机的三种网络管理模式>一章介绍.这章只介绍hado ...