【Python】垃圾分类,调用阿里云API
生活随笔
收集整理的這篇文章主要介紹了
【Python】垃圾分类,调用阿里云API
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
垃圾分類
# -*- coding: UTF-8 -*- # !/usr/local/bin/python3# 阿里云服務第三方公司服務 # 可用于檢測垃圾的種類,返回結果是金屬的概率,塑料的概率,玻璃的概率,和其他的概率 # 基于對圖片的材料進行分析,以此來判斷垃圾的種類import urllib import urllib.request import time import base64#UUID采用當前程序運行時間,用于防止重放攻擊,開發者可根據自己需求,自定義字符串 UUID = str(time.time()) #API產品路徑 host = 'http://rubbish.market.alicloudapi.com' path = '/ai_market/ai_image_universal/rubbish/v1' #阿里云APPCODE appcode = 'APPCODE' #自己購買服務的AppCode bodys = {} url = host + path#內容數據類型,如:0,則表示BASE64編碼;1,則表示圖像文件URL鏈接#啟用BASE64編碼方式進行識別 #內容數據類型是BASE64編碼 #f = open(r'圖片文件', 'rb') #contents = base64.b64encode(f.read()) #f.close() #bodys['IMAGE'] = contents #bodys['IMAGE_TYPE'] = '0'#啟用URL方式進行識別 #內容數據類型是圖像文件URL鏈接 bodys['IMAGE'] = 'https://images-na.ssl-images-amazon.com/images/I/51RCosrIe7L._SY550_.jpg' #圖片的URL地址 bodys['IMAGE_TYPE'] = '1'post_data = urllib.parse.urlencode(bodys).encode('utf-8') request = urllib.request.Request(url, post_data) request.add_header('Authorization', 'APPCODE ' + appcode) request.add_header('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8') request.add_header('X-Ca-Nonce', UUID) response = urllib.request.urlopen(request) content = response.read() if (content):print(content.decode('utf-8'))Github鏈接
總結
以上是生活随笔為你收集整理的【Python】垃圾分类,调用阿里云API的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 介绍几本专业的书籍,一起学习
- 下一篇: Module 'matplotlib'