The use of Baidu Smart Cloud API (python)

1. Installation

Enter Baidu Smart Cloud and log in to your account. For example, if we want to do image enhancement, first create an application:

人工智能-->图像效果增强

After the creation is successful, there will be an application, the AppID API Key Secret Key inside will be used (in the code)

 For example, we call it image repair, click this image repair to enter the download SDK page

Download SDK, enter the download page

Choose Python to download directly here to get the installation package

 

Unzip, enter the unzip folder

Right click + shift to open the command line

enter

python setup.py install

display


E:\vscode\aip-python-sdk-2.2.18>python setup.py install
running install
running bdist_egg
running egg_info
creating baidu_aip.egg-info
writing baidu_aip.egg-info\PKG-INFO
writing dependency_links to baidu_aip.egg-info\dependency_links.txt
writing requirements to baidu_aip.egg-info\requires.txt
writing top-level names to baidu_aip.egg-info\top_level.txt
writing manifest file 'baidu_aip.egg-info\SOURCES.txt'
reading manifest file 'baidu_aip.egg-info\SOURCES.txt'
writing manifest file 'baidu_aip.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
creating build
creating build\lib
creating build\lib\aip
copying aip\base.py -> build\lib\aip
copying aip\bodyanalysis.py -> build\lib\aip
copying aip\easydl.py -> build\lib\aip
copying aip\face.py -> build\lib\aip
copying aip\imagecensor.py -> build\lib\aip
copying aip\imageclassify.py -> build\lib\aip
copying aip\imageprocess.py -> build\lib\aip
copying aip\imagesearch.py -> build\lib\aip
copying aip\kg.py -> build\lib\aip
copying aip\nlp.py -> build\lib\aip
copying aip\ocr.py -> build\lib\aip
copying aip\speech.py -> build\lib\aip
copying aip\__init__.py -> build\lib\aip
creating build\bdist.win-amd64
creating build\bdist.win-amd64\egg
creating build\bdist.win-amd64\egg\aip
copying build\lib\aip\base.py -> build\bdist.win-amd64\egg\aip
copying build\lib\aip\bodyanalysis.py -> build\bdist.win-amd64\egg\aip
copying build\lib\aip\easydl.py -> build\bdist.win-amd64\egg\aip
copying build\lib\aip\face.py -> build\bdist.win-amd64\egg\aip
copying build\lib\aip\imagecensor.py -> build\bdist.win-amd64\egg\aip
copying build\lib\aip\imageclassify.py -> build\bdist.win-amd64\egg\aip
copying build\lib\aip\imageprocess.py -> build\bdist.win-amd64\egg\aip
copying build\lib\aip\imagesearch.py -> build\bdist.win-amd64\egg\aip
copying build\lib\aip\kg.py -> build\bdist.win-amd64\egg\aip
copying build\lib\aip\nlp.py -> build\bdist.win-amd64\egg\aip
copying build\lib\aip\ocr.py -> build\bdist.win-amd64\egg\aip
copying build\lib\aip\speech.py -> build\bdist.win-amd64\egg\aip
copying build\lib\aip\__init__.py -> build\bdist.win-amd64\egg\aip
byte-compiling build\bdist.win-amd64\egg\aip\base.py to base.cpython-36.pyc
byte-compiling build\bdist.win-amd64\egg\aip\bodyanalysis.py to bodyanalysis.cpython-36.py
byte-compiling build\bdist.win-amd64\egg\aip\easydl.py to easydl.cpython-36.pyc
byte-compiling build\bdist.win-amd64\egg\aip\face.py to face.cpython-36.pyc
byte-compiling build\bdist.win-amd64\egg\aip\imagecensor.py to imagecensor.cpython-36.pyc
byte-compiling build\bdist.win-amd64\egg\aip\imageclassify.py to imageclassify.cpython-36.
byte-compiling build\bdist.win-amd64\egg\aip\imageprocess.py to imageprocess.cpython-36.py
byte-compiling build\bdist.win-amd64\egg\aip\imagesearch.py to imagesearch.cpython-36.pyc
byte-compiling build\bdist.win-amd64\egg\aip\kg.py to kg.cpython-36.pyc
byte-compiling build\bdist.win-amd64\egg\aip\nlp.py to nlp.cpython-36.pyc
byte-compiling build\bdist.win-amd64\egg\aip\ocr.py to ocr.cpython-36.pyc
byte-compiling build\bdist.win-amd64\egg\aip\speech.py to speech.cpython-36.pyc
byte-compiling build\bdist.win-amd64\egg\aip\__init__.py to __init__.cpython-36.pyc
creating build\bdist.win-amd64\egg\EGG-INFO
copying baidu_aip.egg-info\PKG-INFO -> build\bdist.win-amd64\egg\EGG-INFO
copying baidu_aip.egg-info\SOURCES.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying baidu_aip.egg-info\dependency_links.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying baidu_aip.egg-info\requires.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying baidu_aip.egg-info\top_level.txt -> build\bdist.win-amd64\egg\EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist\baidu_aip-2.2.18.0-py3.6.egg' and adding 'build\bdist.win-amd64\egg' to it
removing 'build\bdist.win-amd64\egg' (and everything under it)
Processing baidu_aip-2.2.18.0-py3.6.egg
Copying baidu_aip-2.2.18.0-py3.6.egg to d:\anaconda3\lib\site-packages
Adding baidu-aip 2.2.18.0 to easy-install.pth file

Installed d:\anaconda3\lib\site-packages\baidu_aip-2.2.18.0-py3.6.egg
Processing dependencies for baidu-aip==2.2.18.0
Searching for requests==2.14.2
Best match: requests 2.14.2
Adding requests 2.14.2 to easy-install.pth file

Using d:\anaconda3\lib\site-packages
Finished processing dependencies for baidu-aip==2.2.18.0

Installation is successful, start calling

2. Call

For example, I want to repair this picture (example.jpg)

We write this script

from aip import AipImageProcess

""" 你的 APPID AK SK """
APP_ID = '你的 APP_ID'
API_KEY = '你的 API_KEY'
SECRET_KEY = '你的 SECRET_KEY'

client = AipImageProcess(APP_ID, API_KEY, SECRET_KEY)
import base64
""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

image = get_file_content('example.jpg')
# print(image)
""" 调用图像无损放大 """
strs=client.imageQualityEnhance(image)
img = base64.b64decode(strs['image'])
file = open('test.jpg','wb') 
file.write(img)  
file.close()

Get processed picture

The effect is still good! Other functions are similar, there are official documents, it is easy to get started!

Guess you like

Origin blog.csdn.net/baidu_36669549/article/details/106568158