### Installation and Initialization Source: https://context7.com/baidu-aip/python-sdk/llms.txt Instructions on how to install the Baidu AIP SDK and initialize clients for various AI services using common authentication parameters. ```APIDOC ## Installation and Initialization SDK 提供统一的客户端初始化方式,所有服务类都继承自 AipBase 基类,通过相同的认证参数进行实例化。 ```python # 安装 SDK # pip install baidu-aip # 导入所需的服务类 from aip import AipOcr, AipFace, AipNlp, AipSpeech from aip import AipImageClassify, AipImageCensor, AipImageSearch from aip import AipKg, AipBodyAnalysis # 初始化配置(所有服务通用) APP_ID = 'your_app_id' API_KEY = 'your_api_key' SECRET_KEY = 'your_secret_key' # 创建 OCR 客户端示例 ocr_client = AipOcr(APP_ID, API_KEY, SECRET_KEY) # 可选:设置超时时间(毫秒) ocr_client.setConnectionTimeoutInMillis(5000) ocr_client.setSocketTimeoutInMillis(60000) # 可选:设置代理 ocr_client.setProxies({ 'http': 'http://proxy.example.com:8080', 'https': 'https://proxy.example.com:8080' }) ``` ``` -------------------------------- ### Install Baidu AIP Python SDK Source: https://github.com/baidu-aip/python-sdk/blob/master/README.md Install the SDK from GitHub using pip. Ensure you have Python 2.7+ or 3+ installed. ```bash pip install git+https://github.com/Baidu-AIP/python-sdk.git@master ``` -------------------------------- ### Install and Initialize Baidu AIP SDK Source: https://context7.com/baidu-aip/python-sdk/llms.txt Install the SDK using pip and initialize clients for various AI services with your App ID, API Key, and Secret Key. Optional configurations for timeouts and proxies are also shown. ```python # 安装 SDK # pip install baidu-aip # 导入所需的服务类 from aip import AipOcr, AipFace, AipNlp, AipSpeech from aip import AipImageClassify, AipImageCensor, AipImageSearch from aip import AipKg, AipBodyAnalysis # 初始化配置(所有服务通用) APP_ID = 'your_app_id' API_KEY = 'your_api_key' SECRET_KEY = 'your_secret_key' # 创建 OCR 客户端示例 ocr_client = AipOcr(APP_ID, API_KEY, SECRET_KEY) # 可选:设置超时时间(毫秒) ocr_client.setConnectionTimeoutInMillis(5000) ocr_client.setSocketTimeoutInMillis(60000) # 可选:设置代理 ocr_client.setProxies({ 'http': 'http://proxy.example.com:8080', 'https': 'https://proxy.example.com:8080' }) ``` -------------------------------- ### Import SDK Service Source: https://github.com/baidu-aip/python-sdk/blob/master/README.md Import the required service module from the installed SDK. Replace '对应服务' with the specific service you need, e.g., 'Speech', 'Face', 'Ocr'. ```python from aip import 对应服务 ``` -------------------------------- ### Knowledge Graph Task Management with Baidu AIP SDK Source: https://context7.com/baidu-aip/python-sdk/llms.txt Provides functions to create, query, update, start, and check the status of knowledge graph tasks. Requires specifying input/output files and URL patterns. ```python from aip import AipKg client = AipKg('APP_ID', 'API_KEY', 'SECRET_KEY') # 创建任务 result = client.createTask( name='my_task', template_content='{"template": "..."}', input_mapping_file='bos://bucket/input.json', output_file='bos://bucket/output', url_pattern='https://example.com/{url}' ) # 返回: {"id": 12345} # 查询任务详情 result = client.getTaskInfo(12345) # 查询所有任务 result = client.getUserTasks({'page_no': 1, 'page_size': 10}) # 更新任务 result = client.updateTask(12345, { 'name': 'updated_task_name', 'template_content': '{"updated": "template"}' }) # 启动任务 result = client.startTask(12345) # 查询任务状态 result = client.getTaskStatus(12345) # 返回: {"task_status": "running", "progress": 50} ``` -------------------------------- ### Knowledge Graph API (AipKg) Source: https://context7.com/baidu-aip/python-sdk/llms.txt APIs for managing knowledge graph tasks, including creation, querying, updating, starting, and status checking. ```APIDOC ## Knowledge Graph (AipKg) ### Task Management Create and manage knowledge graph tasks. ```python from aip import AipKg client = AipKg('APP_ID', 'API_KEY', 'SECRET_KEY') # Create Task result = client.createTask( name='my_task', template_content='{"template": "..."}', input_mapping_file='bos://bucket/input.json', output_file='bos://bucket/output', url_pattern='https://example.com/{url}' ) # Returns: {"id": 12345} # Query Task Details result = client.getTaskInfo(12345) # Query All Tasks result = client.getUserTasks({'page_no': 1, 'page_size': 10}) # Update Task result = client.updateTask(12345, { 'name': 'updated_task_name', 'template_content': '{"updated": "template"}' }) # Start Task result = client.startTask(12345) # Query Task Status result = client.getTaskStatus(12345) # Returns: {"task_status": "running", "progress": 50} ``` ``` -------------------------------- ### Human Flow Statistics with Baidu AIP SDK Source: https://context7.com/baidu-aip/python-sdk/llms.txt Counts the number of people in an image. Also includes an example for dynamic flow statistics with tracking capabilities. ```python from aip import AipBodyAnalysis client = AipBodyAnalysis('APP_ID', 'API_KEY', 'SECRET_KEY') with open('crowd.jpg', 'rb') as f: image = f.read() result = client.bodyNum(image) # 返回: {"person_num": 15} # 动态版人流量统计(支持追踪) result = client.bodyTracking(image, 'true', {'case_id': 1, 'case_init': 'true'}) ``` -------------------------------- ### Similar Image Search with Baidu AIP SDK Source: https://context7.com/baidu-aip/python-sdk/llms.txt Demonstrates how to add, search, update, and delete images for visual similarity retrieval. Ensure the image is read in binary mode. ```python from aip import AipImageSearch client = AipImageSearch('APP_ID', 'API_KEY', 'SECRET_KEY') with open('image.jpg', 'rb') as f: image = f.read() # 入库 result = client.similarAdd(image, {'brief': '{"category": "风景"}'}) # 检索 result = client.similarSearch(image, {'pn': 0, 'rn': 100}) # 返回: {"result": [{"brief": "{...}", "score": 0.95, "cont_sign": "..."}]} # 更新 result = client.similarUpdate(image, {'brief': '{"category": "新分类"}'}) # 删除 result = client.similarDeleteByImage(image) result = client.similarDeleteByUrl('https://example.com/image.jpg') result = client.similarDeleteBySign('cont_sign_value') ``` -------------------------------- ### Product Image Search with Baidu AIP SDK Source: https://context7.com/baidu-aip/python-sdk/llms.txt Enables searching for products based on images. Includes adding, searching, updating, and deleting product information. Specify class IDs for better categorization. ```python from aip import AipImageSearch client = AipImageSearch('APP_ID', 'API_KEY', 'SECRET_KEY') with open('product.jpg', 'rb') as f: image = f.read() # 商品入库 result = client.productAdd(image, { 'brief': '{"product_id": "SKU001", "name": "红色连衣裙", "price": 299}', 'class_id1': 1, # 服饰类 'class_id2': 101 # 连衣裙 }) # 商品检索 result = client.productSearch(image, { 'class_id1': 1, 'pn': 0, 'rn': 50 }) # 返回: {"result": [{"brief": "{...}", "score": 0.98, "cont_sign": "..."}]} # 更新商品信息 result = client.productUpdate(image, {'brief': '{"price": 199}'}) # 删除商品 result = client.productDeleteByImage(image) result = client.productDeleteBySign('cont_sign_value') ``` -------------------------------- ### Landmark Recognition with Python SDK Source: https://context7.com/baidu-aip/python-sdk/llms.txt Identifies famous landmark buildings in an image. Requires an image file. ```python from aip import AipImageClassify client = AipImageClassify('APP_ID', 'API_KEY', 'SECRET_KEY') with open('landmark.jpg', 'rb') as f: image = f.read() result = client.landmark(image) # 返回: {"result": {"landmark": "天安门"}} ``` -------------------------------- ### Other Image Recognition Functions with Python SDK Source: https://context7.com/baidu-aip/python-sdk/llms.txt Provides functions for flower, ingredient, red wine, currency recognition, and object detection. Requires an image file and optional parameters for top results and Baidu Baike information. ```python from aip import AipImageClassify client = AipImageClassify('APP_ID', 'API_KEY', 'SECRET_KEY') with open('image.jpg', 'rb') as f: image = f.read() # 花卉识别 result = client.flower(image, {'top_num': 3, 'baike_num': 1}) # 食材识别 result = client.ingredient(image, {'top_num': 3}) # 红酒识别 result = client.redwine(image) # 货币识别 result = client.currency(image) # 图像主体检测 result = client.objectDetect(image, {'with_face': 0}) ``` -------------------------------- ### Pornography and Terror Detection with Python SDK Source: https://context7.com/baidu-aip/python-sdk/llms.txt Provides separate interfaces for pornography detection and terror detection. Supports image files and GIF files for pornography detection. ```python from aip import AipImageCensor client = AipImageCensor('APP_ID', 'API_KEY', 'SECRET_KEY') with open('image.jpg', 'rb') as f: image = f.read() # 色情检测 result = client.antiPorn(image) # 返回: {"result": [{"probability": 0.01, "class_name": "非色情"}]} # GIF 色情检测 with open('image.gif', 'rb') as f: gif = f.read() result = client.antiPornGif(gif) # 暴恐识别 result = client.antiTerror(image) # 返回: {"result": [{"probability": 0.01, "class_name": "非暴恐"}]} ``` -------------------------------- ### Car Detection with Python SDK Source: https://context7.com/baidu-aip/python-sdk/llms.txt Identifies car brand, model, and year from an image. Requires an image file and configuration for top results and Baidu Baike information. ```python from aip import AipImageClassify client = AipImageClassify('APP_ID', 'API_KEY', 'SECRET_KEY') with open('car.jpg', 'rb') as f: image = f.read() result = client.carDetect(image, {'top_num': 3, 'baike_num': 1}) # 返回: {"result": [{"name": "奥迪A6L", "score": 0.96, "year": "2019"}, ...]}} ``` -------------------------------- ### Dish Recognition with Python SDK Source: https://context7.com/baidu-aip/python-sdk/llms.txt Recognizes dish names and calorie information from an image. Requires an image file and parameters for top results and a filter threshold. ```python from aip import AipImageClassify client = AipImageClassify('APP_ID', 'API_KEY', 'SECRET_KEY') with open('food.jpg', 'rb') as f: image = f.read() result = client.dishDetect(image, {'top_num': 3, 'filter_threshold': 0.7}) # 返回: {"result": [{"name": "宫保鸡丁", "calorie": "197", "probability": "0.95"}, ...]}} ``` -------------------------------- ### Add User with Face Image Source: https://context7.com/baidu-aip/python-sdk/llms.txt Registers a new user with their face image into a specified group. Includes options for user info, quality, and liveness control. ```python from aip import AipFace import base64 client = AipFace('APP_ID', 'API_KEY', 'SECRET_KEY') with open('user_face.jpg', 'rb') as f: image = base64.b64encode(f.read()).decode() result = client.addUser(image, 'BASE64', 'employees', 'user001', { 'user_info': '{"name": "张三", "department": "技术部"}', 'quality_control': 'NORMAL', 'liveness_control': 'LOW' }) # 返回: {"result": {"face_token": "face_token_string", "location": {...}}} ``` -------------------------------- ### Same Image Retrieval with Python SDK Source: https://context7.com/baidu-aip/python-sdk/llms.txt Finds identical or similar images. Supports adding, searching, updating, and deleting images from the database using image files or URLs. ```python from aip import AipImageSearch client = AipImageSearch('APP_ID', 'API_KEY', 'SECRET_KEY') with open('image.jpg', 'rb') as f: image = f.read() # 入库 result = client.sameHqAdd(image, {'brief': '{"name": "商品A", "id": "001"}'}) # 或使用 URL 入库 result = client.sameHqAddUrl('https://example.com/image.jpg', {'brief': '{"name": "商品A"}'}) # 检索 result = client.sameHqSearch(image) # 返回: {"result": [{"brief": "{...}", "score": 1.0, "cont_sign": "..."}]} # 更新 result = client.sameHqUpdate(image, {'brief': '{"name": "更新后的信息"}'}) # 删除 result = client.sameHqDeleteByImage(image) result = client.sameHqDeleteBySign('cont_sign_value') ``` -------------------------------- ### Logo Search and Management with Python SDK Source: https://context7.com/baidu-aip/python-sdk/llms.txt Identifies brand logos in an image. Supports searching, adding custom logos, and deleting custom logos by sign. ```python from aip import AipImageClassify client = AipImageClassify('APP_ID', 'API_KEY', 'SECRET_KEY') with open('logo.jpg', 'rb') as f: image = f.read() result = client.logoSearch(image, {'custom_lib': 'false'}) # 返回: {"result": [{"name": "百度", "probability": 0.99, "location": {...}}, ...]}] # 添加自定义 Logo result = client.logoAdd(image, '{"name": "我的品牌"}') # 删除自定义 Logo result = client.logoDeleteBySign('cont_sign_value') ``` -------------------------------- ### Body Keypoint Detection with Baidu AIP SDK Source: https://context7.com/baidu-aip/python-sdk/llms.txt Identifies 14 major human body keypoints in an image. The image must be read in binary mode. ```python from aip import AipBodyAnalysis client = AipBodyAnalysis('APP_ID', 'API_KEY', 'SECRET_KEY') with open('person.jpg', 'rb') as f: image = f.read() result = client.bodyAnalysis(image) # 返回: {"person_num": 1, "person_info": [{"body_parts": {"head": {"x": 100, "y": 50}, ...}}]} ``` -------------------------------- ### News Summarization Source: https://context7.com/baidu-aip/python-sdk/llms.txt Automatically generates a summary for a news article. Requires initialization of the AipNlp client. ```python from aip import AipNlp client = AipNlp('APP_ID', 'API_KEY', 'SECRET_KEY') content = '长篇新闻内容...' result = client.newsSummary(content, 200, {'title': '新闻标题'}) ``` -------------------------------- ### Gesture Recognition with Baidu AIP SDK Source: https://context7.com/baidu-aip/python-sdk/llms.txt Identifies different types of hand gestures in an image. The SDK supports recognizing various gestures like fists, OK, prayer, etc. ```python from aip import AipBodyAnalysis client = AipBodyAnalysis('APP_ID', 'API_KEY', 'SECRET_KEY') with open('hand.jpg', 'rb') as f: image = f.read() result = client.gesture(image) # 返回: {"result": [{"classname": "OK", "probability": 0.98, "location": {...}}]} # 支持识别:拳头、OK、祈祷、作揖、点赞、Aloha、比心等手势 ``` -------------------------------- ### Face Detection with Image File Source: https://context7.com/baidu-aip/python-sdk/llms.txt Detects faces in an image file and returns attributes like age, gender, and expression. The image is base64 encoded. ```python from aip import AipFace import base64 client = AipFace('APP_ID', 'API_KEY', 'SECRET_KEY') # 使用 base64 编码的图片 with open('face.jpg', 'rb') as f: image = base64.b64encode(f.read()).decode() result = client.detect(image, 'BASE64', { 'face_field': 'age,gender,beauty,expression,face_shape,glasses,emotion', 'max_face_num': 10, 'face_type': 'LIVE' # LIVE/IDCARD/WATERMARK/CERT }) # 返回: {"result": {"face_num": 1, "face_list": [{"age": 25, "gender": {"type": "male"}, ...}]}} ``` -------------------------------- ### NLP - newsSummary - 新闻摘要 Source: https://context7.com/baidu-aip/python-sdk/llms.txt Automatically generates a summary for a news article. ```APIDOC ## newsSummary - 新闻摘要 ### Description Automatically generates a summary for a news article. ### Method POST (assumed) ### Endpoint /api/newsSummary (assumed) ### Parameters #### Query Parameters - **content** (string) - Required - The full content of the news article. - **max_length** (integer) - Required - The maximum length of the summary. - **options** (object) - Optional - Additional options. - **title** (string) - Optional - The title of the news article. ### Request Example ```python from aip import AipNlp client = AipNlp('APP_ID', 'API_KEY', 'SECRET_KEY') content = '长篇新闻内容...' result = client.newsSummary(content, 200, {'title': '新闻标题'}) ``` ### Response #### Success Response (200) - **summary** (string) - The generated summary of the news article. #### Response Example ```json { "summary": "自动生成的摘要内容..." } ``` ``` -------------------------------- ### Face Registration API Source: https://context7.com/baidu-aip/python-sdk/llms.txt Adds a user and their face information to a face library. ```APIDOC ## POST /api/face/addUser ### Description Adds a user and their face information to a face library. ### Method POST ### Endpoint /api/face/addUser ### Parameters #### Path Parameters - **group_id** (string) - Required - The group ID to add the user to. - **user_id** (string) - Required - The ID of the user to add. #### Query Parameters - **user_info** (string) - Optional - User-defined information, JSON string format. - **quality_control** (string) - Optional - Quality control level. Options: 'NORMAL', 'LOW', 'HIGH'. Defaults to 'NORMAL'. - **liveness_control** (string) - Optional - Liveness control level. Options: 'LOW', 'NORMAL', 'HIGH'. Defaults to 'LOW'. ### Request Body - **image** (string) - Required - The image data of the user's face. Can be base64 encoded string. - **image_type** (string) - Required - Type of the image. Options: 'BASE64'. ### Request Example ```python from aip import AipFace import base64 client = AipFace('APP_ID', 'API_KEY', 'SECRET_KEY') with open('user_face.jpg', 'rb') as f: image = base64.b64encode(f.read()).decode() result = client.addUser(image, 'BASE64', 'employees', 'user001', { 'user_info': '{"name": "张三", "department": "技术部"}', 'quality_control': 'NORMAL', 'liveness_control': 'LOW' }) ``` ### Response #### Success Response (200) - **result** (object) - Contains registration results. - **face_token** (string) - The face token of the registered face. - **location** (object) - The bounding box of the detected face. #### Response Example ```json { "result": { "face_token": "face_token_string", "location": {"left": 10, "top": 20, "width": 50, "height": 60} } } ``` ``` -------------------------------- ### NLP - simnet - 短文本相似度 Source: https://context7.com/baidu-aip/python-sdk/llms.txt Calculates the semantic similarity between two short texts. ```APIDOC ## simnet - 短文本相似度 ### Description Calculates the semantic similarity between two short texts. ### Method POST (assumed) ### Endpoint /api/simnet (assumed) ### Parameters #### Query Parameters - **text1** (string) - Required - The first text. - **text2** (string) - Required - The second text. - **options** (object) - Optional - Additional options. - **model** (string) - Optional - The model to use (e.g., 'CNN'). ### Request Example ```python from aip import AipNlp client = AipNlp('APP_ID', 'API_KEY', 'SECRET_KEY') result = client.simnet('今天天气怎么样', '今天天气好吗', {'model': 'CNN'}) ``` ### Response #### Success Response (200) - **score** (float) - The similarity score between the two texts. - **texts** (object) - Additional text information (structure not detailed). #### Response Example ```json { "score": 0.8567, "texts": {} } ``` ``` -------------------------------- ### Driver Behavior Analysis with Baidu AIP SDK Source: https://context7.com/baidu-aip/python-sdk/llms.txt Analyzes driver behavior to detect unsafe actions like smoking, phone use, or eye closure. Specify the behaviors to detect using the 'type' parameter. ```python from aip import AipBodyAnalysis client = AipBodyAnalysis('APP_ID', 'API_KEY', 'SECRET_KEY') with open('driver.jpg', 'rb') as f: image = f.read() result = client.driverBehavior(image, {'type': 'smoke,bindhand,bindphone,eyes_closed'}) # 返回: {"person_info": [{"attributes": {"smoke": {"score": 0.01}, "bindphone": {"score": 0.95}, ...}}]} ``` -------------------------------- ### Custom Lexer Analysis Source: https://context7.com/baidu-aip/python-sdk/llms.txt Performs custom lexical analysis on text. Requires initialization of the AipNlp client. ```python from aip import AipNlp client = AipNlp('APP_ID', 'API_KEY', 'SECRET_KEY') text = '今天天气很好' result = client.lexerCustom(text) ``` -------------------------------- ### Speech - asr - 语音识别 Source: https://context7.com/baidu-aip/python-sdk/llms.txt Converts speech audio files into text. ```APIDOC ## asr - 语音识别 ### Description Converts speech audio files into text. ### Method POST (assumed) ### Endpoint /api/asr (assumed) ### Parameters #### Request Body - **audio** (binary) - Required - The audio data (e.g., PCM, WAV, AMR). - **format** (string) - Required - The audio format (e.g., 'pcm'). - **rate** (integer) - Required - The audio sample rate (e.g., 16000). - **options** (object) - Optional - Additional options. - **dev_pid** (integer) - Optional - Device ID for language model (e.g., 1537 for Mandarin). ### Request Example ```python from aip import AipSpeech client = AipSpeech('APP_ID', 'API_KEY', 'SECRET_KEY') # Read local audio file with open('audio.pcm', 'rb') as f: audio = f.read() # Speech recognition result = client.asr(audio, 'pcm', 16000, { 'dev_pid': 1537 # Mandarin + Input Method model }) ``` ### Response #### Success Response (200) - **result** (array) - An array containing the recognized text. - **err_no** (integer) - Error code (0 indicates success). #### Response Example ```json { "result": ["识别的文字内容"], "err_no": 0 } ``` ### Common `dev_pid` values: - 1537 - Mandarin (Input Method model) - 1737 - English - 1637 - Cantonese - 1837 - Sichuanese ``` -------------------------------- ### Dependency Parsing Source: https://context7.com/baidu-aip/python-sdk/llms.txt Analyzes the dependency relationships between words in a sentence. Requires initialization of the AipNlp client. ```python from aip import AipNlp client = AipNlp('APP_ID', 'API_KEY', 'SECRET_KEY') text = '今天天气很好' result = client.depParser(text, {'mode': 1}) ``` -------------------------------- ### OCR: Table Recognition (Sync and Async) Source: https://context7.com/baidu-aip/python-sdk/llms.txt Recognize table content from images. Supports both synchronous (blocking) and asynchronous (submit and query) methods. The async method requires polling for results using a request ID. ```python from aip import AipOcr client = AipOcr('APP_ID', 'API_KEY', 'SECRET_KEY') with open('table.jpg', 'rb') as f: image = f.read() # 同步识别(等待结果返回,默认超时10秒) result = client.tableRecognition(image, timeout=15000) # 返回: {"result": {"result_data": "...", "ret_code": 3}} # 异步方式:先提交任务 task_result = client.tableRecognitionAsync(image) request_id = task_result['result'][0]['request_id'] # 稍后查询结果 import time time.sleep(5) result = client.getTableRecognitionResult(request_id) ``` -------------------------------- ### NLP - wordSimEmbedding - 词义相似度 Source: https://context7.com/baidu-aip/python-sdk/llms.txt Calculates the semantic similarity between two words. ```APIDOC ## wordSimEmbedding - 词义相似度 ### Description Calculates the semantic similarity between two words. ### Method POST (assumed) ### Endpoint /api/wordSimEmbedding (assumed) ### Parameters #### Query Parameters - **word1** (string) - Required - The first word. - **word2** (string) - Required - The second word. ### Request Example ```python from aip import AipNlp client = AipNlp('APP_ID', 'API_KEY', 'SECRET_KEY') result = client.wordSimEmbedding('苹果', '橘子') ``` ### Response #### Success Response (200) - **score** (float) - The similarity score between the two words. - **words** (object) - Additional word information (structure not detailed). #### Response Example ```json { "score": 0.7234, "words": {} } ``` ``` -------------------------------- ### Speech Synthesis Source: https://context7.com/baidu-aip/python-sdk/llms.txt Converts text to speech files. Requires initialization of the AipSpeech client. ```python from aip import AipSpeech client = AipSpeech('APP_ID', 'API_KEY', 'SECRET_KEY') # 文本转语音 result = client.synthesis('你好,欢迎使用百度语音', 'zh', 1, { 'vol': 5, # 音量 0-15 'spd': 5, # 语速 0-15 'pit': 5, # 音调 0-15 'per': 0, # 发音人:0-普通女声,1-普通男声,3-情感合成-度逍遥,4-情感合成-度丫丫 'aue': 3 # 音频格式:3-mp3,4-pcm,5-pcm-16k,6-wav }) # 返回音频二进制数据,非JSON时表示成功 if not isinstance(result, dict): with open('output.mp3', 'wb') as f: f.write(result) else: print('合成失败:', result) ``` -------------------------------- ### Combined Image Moderation with Python SDK Source: https://context7.com/baidu-aip/python-sdk/llms.txt Combines multiple moderation strategies for image auditing. Supports local images and network URLs, and allows specifying moderation types. ```python from aip import AipImageCensor client = AipImageCensor('APP_ID', 'API_KEY', 'SECRET_KEY') with open('image.jpg', 'rb') as f: image = f.read() result = client.imageCensorComb(image, ['antiporn', 'antiterror', 'politician']) # 或使用 URL result = client.imageCensorComb('https://example.com/image.jpg', 'antiporn,antiterror') ``` -------------------------------- ### Product Search API Source: https://context7.com/baidu-aip/python-sdk/llms.txt Specialized image search service for products, allowing for product addition, search, update, and deletion. ```APIDOC ## Product Search Specialized retrieval service for product images. ### Add Product Image to Index ```python from aip import AipImageSearch client = AipImageSearch('APP_ID', 'API_KEY', 'SECRET_KEY') with open('product.jpg', 'rb') as f: image = f.read() result = client.productAdd(image, { 'brief': '{"product_id": "SKU001", "name": "红色连衣裙", "price": 299}', 'class_id1': 1, # Apparel category 'class_id2': 101 # Dress }) ``` ### Search Product Images ```python result = client.productSearch(image, { 'class_id1': 1, 'pn': 0, 'rn': 50 }) # Returns: {"result": [{"brief": "{...}", "score": 0.98, "cont_sign": "..."}]} ``` ### Update Product Information ```python result = client.productUpdate(image, {'brief': '{"price": 199}'}) ``` ### Delete Product from Index ```python result = client.productDeleteByImage(image) result = client.productDeleteBySign('cont_sign_value') ``` ``` -------------------------------- ### Speech Recognition (ASR) Source: https://context7.com/baidu-aip/python-sdk/llms.txt Converts speech files to text. Requires initialization of the AipSpeech client and reading audio file content. ```python from aip import AipSpeech client = AipSpeech('APP_ID', 'API_KEY', 'SECRET_KEY') # 读取本地音频文件(支持 pcm, wav, amr 等格式) with open('audio.pcm', 'rb') as f: audio = f.read() # 语音识别 result = client.asr(audio, 'pcm', 16000, { 'dev_pid': 1537 # 普通话+输入法模型 }) ``` -------------------------------- ### Face Verification (Liveness Detection) API Source: https://context7.com/baidu-aip/python-sdk/llms.txt Verifies if a face in an image is live, preventing photo attacks. ```APIDOC ## POST /api/face/faceverify ### Description Verifies if the face in an image is live, preventing attacks using photos. ### Method POST ### Endpoint /api/face/faceverify ### Parameters #### Query Parameters - **face_field** (string) - Optional - Specifies the face attributes to return (e.g., 'age,gender'). ### Request Body - **images** (array) - Required - A list containing one or more image objects for verification. - **image** (string) - Required - Image data (base64 encoded). - **image_type** (string) - Required - Type of the image. Options: 'BASE64'. - **face_field** (string) - Optional - Specifies the face attributes to return. ### Request Example ```python from aip import AipFace import base64 client = AipFace('APP_ID', 'API_KEY', 'SECRET_KEY') with open('live_face.jpg', 'rb') as f: image = base64.b64encode(f.read()).decode() result = client.faceverify([ {'image': image, 'image_type': 'BASE64', 'face_field': 'age,gender'} ]) ``` ### Response #### Success Response (200) - **result** (object) - Contains verification results. - **face_liveness** (float) - The liveness score (closer to 1 indicates higher confidence of being live). - **face_list** (array) - Information about the detected faces. #### Response Example ```json { "result": { "face_liveness": 0.9999, "face_list": [...] } } ``` ``` -------------------------------- ### Face Detection with Image URL Source: https://context7.com/baidu-aip/python-sdk/llms.txt Detects faces in an image specified by a URL and returns basic attributes. Requires 'face_field' parameter. ```python # 使用图片 URL result = client.detect('https://example.com/face.jpg', 'URL', { 'face_field': 'age,gender' }) ``` -------------------------------- ### Keyword Extraction and Topic Classification Source: https://context7.com/baidu-aip/python-sdk/llms.txt Extracts keywords and classifies articles. Requires initialization of the AipNlp client. ```python from aip import AipNlp client = AipNlp('APP_ID', 'API_KEY', 'SECRET_KEY') title = '人工智能改变生活' content = '近年来,人工智能技术飞速发展...' # 提取关键词标签 result = client.keyword(title, content) # 文章分类 result = client.topic(title, content) ``` -------------------------------- ### Online Liveness Detection Source: https://context7.com/baidu-aip/python-sdk/llms.txt Performs real-time liveness detection on a face image to prevent spoofing attacks. Can return face attributes. ```python from aip import AipFace import base64 client = AipFace('APP_ID', 'API_KEY', 'SECRET_KEY') with open('live_face.jpg', 'rb') as f: image = base64.b64encode(f.read()).decode() result = client.faceverify([ {'image': image, 'image_type': 'BASE64', 'face_field': 'age,gender'} ]) # 返回: {"result": {"face_liveness": 0.9999, "face_list": [...]}} ``` -------------------------------- ### NLP - emotion - 对话情绪识别 Source: https://context7.com/baidu-aip/python-sdk/llms.txt Recognizes the emotion type in conversational text. ```APIDOC ## emotion - 对话情绪识别 ### Description Recognizes the emotion type in conversational text. ### Method POST (assumed) ### Endpoint /api/emotion (assumed) ### Parameters #### Query Parameters - **text** (string) - Required - The conversational text. - **options** (object) - Optional - Additional options. - **scene** (string) - Optional - The scene of the conversation (e.g., 'talk'). ### Request Example ```python from aip import AipNlp client = AipNlp('APP_ID', 'API_KEY', 'SECRET_KEY') text = '今天心情真好,中奖了!' result = client.emotion(text, {'scene': 'talk'}) ``` ### Response #### Success Response (200) - **items** (array) - An array of detected emotions. - **label** (string) - The emotion label (e.g., 'happy'). - **prob** (float) - The probability of the emotion. #### Response Example ```json { "items": [ {"label": "happy", "prob": 0.95} ] } ``` ``` -------------------------------- ### Body Attribute Recognition with Baidu AIP SDK Source: https://context7.com/baidu-aip/python-sdk/llms.txt Recognizes human attributes such as gender, age, and clothing. Specify the desired attributes in the 'type' parameter. ```python from aip import AipBodyAnalysis client = AipBodyAnalysis('APP_ID', 'API_KEY', 'SECRET_KEY') with open('person.jpg', 'rb') as f: image = f.read() result = client.bodyAttr(image, {'type': 'gender,age,upper_wear,lower_wear'}) # 返回: {"person_num": 1, "person_info": [{"attributes": {"gender": {"name": "男性", "score": 0.98}, ...}}]} ``` -------------------------------- ### Image Recognition - animalDetect / plantDetect - 动植物识别 Source: https://context7.com/baidu-aip/python-sdk/llms.txt Identifies animal or plant species in an image. ```APIDOC ## animalDetect / plantDetect - 动植物识别 ### Description Identifies animal or plant species in an image. ### Method POST (assumed) ### Endpoint /api/animalDetect, /api/plantDetect (assumed) ### Parameters #### Request Body - **image** (binary) - Required - The image file to analyze. - **options** (object) - Optional - Additional options. - **top_num** (integer) - Optional - Number of top results to return (for animalDetect). - **baike_num** (integer) - Optional - Number of Baidu Baike entries to return. ### Request Example ```python from aip import AipImageClassify client = AipImageClassify('APP_ID', 'API_KEY', 'SECRET_KEY') # Animal detection with open('animal.jpg', 'rb') as f: image = f.read() result = client.animalDetect(image, {'top_num': 3, 'baike_num': 1}) # Plant detection with open('plant.jpg', 'rb') as f: image = f.read() result = client.plantDetect(image, {'baike_num': 1}) ``` ### Response #### Success Response (200) - Animal Detection - **result** (array) - An array of detected animals. - **name** (string) - The name of the animal. - **score** (string) - The confidence score. - **baike_info** (object) - Baidu Baike information (structure not detailed). #### Response Example - Animal Detection ```json { "result": [ {"name": "金毛犬", "score": "0.987", "baike_info": {}}, ... ] } ``` #### Success Response (200) - Plant Detection - **result** (array) - An array of detected plants. - **name** (string) - The name of the plant. - **score** (float) - The confidence score. - **baike_info** (object) - Baidu Baike information (structure not detailed). #### Response Example - Plant Detection ```json { "result": [ {"name": "玫瑰", "score": 0.95, "baike_info": {}}, ... ] } ``` ``` -------------------------------- ### Body Analysis API (AipBodyAnalysis) Source: https://context7.com/baidu-aip/python-sdk/llms.txt APIs for analyzing human bodies in images, including keypoint recognition, attribute recognition, crowd counting, gesture recognition, segmentation, and driver behavior analysis. ```APIDOC ## Body Analysis (AipBodyAnalysis) ### bodyAnalysis - Human Keypoint Recognition Recognizes the positions of 14 main keypoints of the human body in an image. ```python from aip import AipBodyAnalysis client = AipBodyAnalysis('APP_ID', 'API_KEY', 'SECRET_KEY') with open('person.jpg', 'rb') as f: image = f.read() result = client.bodyAnalysis(image) # Returns: {"person_num": 1, "person_info": [{"body_parts": {"head": {"x": 100, "y": 50}, ...}}]} ``` ### bodyAttr - Human Attribute Recognition Recognizes human attributes, including gender, age, clothing, etc. ```python result = client.bodyAttr(image, {'type': 'gender,age,upper_wear,lower_wear'}) # Returns: {"person_num": 1, "person_info": [{"attributes": {"gender": {"name": "男性", "score": 0.98}, ...}}]} ``` ### bodyNum - Crowd Counting Counts the number of people in an image. ```python result = client.bodyNum(image) # Returns: {"person_num": 15} # Dynamic crowd counting (supports tracking) result = client.bodyTracking(image, 'true', {'case_id': 1, 'case_init': 'true'}) ``` ### gesture - Gesture Recognition Recognizes the type of gesture in an image. ```python result = client.gesture(image) # Returns: {"result": [{"classname": "OK", "probability": 0.98, "location": {...}}]} # Supports recognition of: fist, OK, prayer, salute, thumbs up, Aloha, heart gesture, etc. ``` ### bodySeg - Human Segmentation Segments human figures from the background in an image. ```python import base64 result = client.bodySeg(image, {'type': 'labelmap'}) # Returns base64 encoded segmentation result image if 'labelmap' in result: mask_data = base64.b64decode(result['labelmap']) with open('mask.png', 'wb') as f: f.write(mask_data) ``` ### driverBehavior - Driver Behavior Analysis Analyzes driver behavior to detect unsafe actions. ```python result = client.driverBehavior(image, {'type': 'smoke,bindhand,bindphone,eyes_closed'}) # Returns: {"person_info": [{"attributes": {"smoke": {"score": 0.01}, "bindphone": {"score": 0.95}, ...}}]} ``` ``` -------------------------------- ### Speech - synthesis - 语音合成 Source: https://context7.com/baidu-aip/python-sdk/llms.txt Converts text into speech audio files. ```APIDOC ## synthesis - 语音合成 ### Description Converts text into speech audio files. ### Method POST (assumed) ### Endpoint /api/synthesis (assumed) ### Parameters #### Query Parameters - **text** (string) - Required - The text to synthesize. - **language** (string) - Required - The language of the text (e.g., 'zh'). - **voice_type** (integer) - Required - Voice type (e.g., 1 for standard male voice). - **options** (object) - Optional - Synthesis options. - **vol** (integer) - Optional - Volume (0-15). - **spd** (integer) - Optional - Speech speed (0-15). - **pit** (integer) - Optional - Pitch (0-15). - **per** (integer) - Optional - Speaker: 0 (standard female), 1 (standard male), 3 (emotional - Du Xiaoyao), 4 (emotional - Du Yaya). - **aue** (integer) - Optional - Audio format: 3 (mp3), 4 (pcm), 5 (pcm-16k), 6 (wav). ### Request Example ```python from aip import AipSpeech client = AipSpeech('APP_ID', 'API_KEY', 'SECRET_KEY') # Text to speech result = client.synthesis('你好,欢迎使用百度语音', 'zh', 1, { 'vol': 5, # Volume 0-15 'spd': 5, # Speed 0-15 'pit': 5, # Pitch 0-15 'per': 0, # Speaker: 0-standard female, 1-standard male, 3-emotional Du Xiaoyao, 4-emotional Du Yaya 'aue': 3 # Audio format: 3-mp3, 4-pcm, 5-pcm-16k, 6-wav }) ``` ### Response #### Success Response (200) - **audio data** (binary) - The synthesized audio data if successful. - **error details** (object) - Error details if synthesis fails. #### Response Example (Success) (Binary audio data, e.g., MP3 file content) #### Response Example (Failure) ```json { "error_code": "...", "error_msg": "..." } ``` ``` -------------------------------- ### Anti-Spam Text Detection with Python SDK Source: https://context7.com/baidu-aip/python-sdk/llms.txt Detects if text contains spam or advertising content. Requires the text content to be checked. ```python from aip import AipImageCensor client = AipImageCensor('APP_ID', 'API_KEY', 'SECRET_KEY') content = '这是需要检测的文本内容' result = client.antiSpam(content) # 返回: {"result": {"spam": 0}} # 0-正常,1-垃圾 ``` -------------------------------- ### Landmark Recognition API Source: https://context7.com/baidu-aip/python-sdk/llms.txt Identifies famous landmark buildings in an image. ```APIDOC ## POST /api/landmark ### Description Identifies famous landmark buildings in an image. ### Method POST ### Endpoint /api/landmark ### Request Example ```python from aip import AipImageClassify client = AipImageClassify('APP_ID', 'API_KEY', 'SECRET_KEY') with open('landmark.jpg', 'rb') as f: image = f.read() result = client.landmark(image) ``` ### Response #### Success Response (200) - **result** (object) - An object containing the detected landmark name. #### Response Example ```json { "result": { "landmark": "天安门" } } ``` ```