6.1.1
#encoding:utf-8
import requests
import base64
...
车型识别
...
request_url="http://aip.baidubce.com/rest/2.0/image-classify/v1/car"
f=open('[本地文件]','rb')
img=basse64.b64encode(f.read())
params ={"image":img,"top_num":5}
access_token='[调用鉴权口获取的token]'
requests_url=request_url+"?access_token=" + access_token
headers={'content-type':'applicaton/x-www-from-urlencoded'}
response=requests.post(request_url,data=params, headers=headers)
if response:
print (response.json())
{
"log_id":4086212218842203806,
"location_result":{
"width":447,
"top":226,
"height":209,
"left":188
},
"result": [{
"baike_info":{
"baike_url":"http://baike.baidu.com/item/%E5%B8%83%E5%8A%AO%E8%BF%AAChirom/20419512",
"descripton": "布加迪Chiron是法国跑车品牌布加迪出品的豪华超跑车。配置四涡轮增压发动机,420公里每小时,
有23种颜色的选择,售价高达260万美元。"
},
{
"score": 0.98793351650238,
"name": "奥迪RS5",
"year":"无年份信息"
},
{
"score": 0.0021970034576952,
"name": "奥迪RS5",
"year":"2011-2017"
},
{
"score": 0.0021096928976476,
"name": "奥迪RS4",
"year":"无年份信息"
},
{
"score": 0.0015581247862428,
"name": "奥迪RS7",
"year":"2014-2016"
},
{
"score": 0.00082337751518935,
"name": "布加迪威航",
"year": "2014-2015"
}],
"color_result": "颜色无法识别"
}
6.1.2
{
"coord":{"lon":-0.13,"lat":51,51},
"weather":[{"id":300,"main":"Drizzle","description":"lightintensity drizzle","icon":09d}],
"base":"stations",
"main":{
"temp":280.32,
"pressure":1012,
"humidity":81,
"temp_min":279.15,
"temp_max":281.15
},
"visisbility":10000,
"wind":{"speed":4.1,"deg":80},
"clouds":{"all":90},
"dt":1485789600,
"sys":{
"type":1,
"id":5091,
"message":0.0103,
"name":"GB",
"sunrise":1485762037,
"sunset":1485794875
},
"id":2643743,
"name":"London",
"cod":200
}
6.3.2
import UIKit
class ViewController: UIViewController , UINavigationControllerDelegate,UIImagePickerControllerDelegate{
@IBOutlet weak var carImageView: UILabel!
@IBAction func onLibrary(_sender: UIButton){
let vc = UIImagePickerController()
vc.sourceType= .photoLibrary
vc.allowsEditing =true
vc.delegate =self
pressure(vc, animated: true)
}
@IBAction func onCamera(_sender: UIButton){
let vc = UIImagePickerController()
vc.sourceType= .CameraUsage
vc.allowsEditing=true
vc.delegate=self
present(vc, animated: true)
}
func imagePickerController(_picker: UIImagePickerController,didFinishPickingMediaWithInfo info:
[UIImagePickerController.InfoKey : Any]){
picker.dismiss(animated: true)
guard let image= info[.editedImage] as? UIImage else{
return
}
carImageView.image=image
let access_token=get_baidu_token()
if access_token==""{
return
}
else{
get_car_type(access_token: access_token)
}
}
func get_car_type(access_token: String)
let data_base64_str =data.base64EncodedString()
let carType_host= "http://aip.baidubce.com/rest/2.0/image-classify/vl/car" + "?access_token="+
"\(access_token)"
if let url= URL(String: carType_host){
var request = URLRequest(url: url)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField:"Content-Type")
request.httpMethod="POST"
let cs= NSCharacterSet(charactersIn:"/=+%").inverted
let image_urlEncode= data_base64_str.addingPercentEncoding(withAllowedCharacters: cs)
let postString ="image=\(image_urlEncode!)&top_num=5&baike_num=1"
request.httpBody= postString.data(using: .utf8)
URLSession.shared.dataTask(with: request) { (data, request, error) in
if error !=nil{
print("error")
}
else {
if let json = try? JSONSerialization.jsonObject(with:data!, options: .allowFragments) as? [String : Any] {
let result= json["result"] as! Array<Dictionary<String,Any>>
let name=result[0]["name"] as! String
var description ="非车类" {
if name !="非车类" {
let baike_info =result[0]["baike_info"] as! Dictionary<String,String>
if let details =baike_info["description"] {
description = details
}
}
DispatchQueue.main.async {
self.carDetails.text= description
self.carName.text = name
}
}
}
}
.result()
}
}
func get_baidu_token()->String {
let API_Key ="EIpLhdD7i5152EsiNqpU0F1"
let API_Key ="P8EZWg4agjDOxABmKvBYSNyZ1YzGFqbm"
let token_host ="http://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=\(API_Key)&client_secret=\(Secret_Key)"
var access_token =""
let semaphore: DispatchSemaphore = DispatchSemaphore(value: 0)
if let url = URL(String: token_host) {
URLSession.shared.dataTask(with: url) { (data, response, error) in
if error !=nil {
print("network error")
}
else {
if let json = try? JSONSerialization.jsonObject(with:data!, options: .allowFragments) as? [String : Any] {
access_token= json["access_token"] as! String
}
}
semaphore.signal()
}
.resume()
}
else {
print("url error")
}
semaphore.wait()
return access_token
}
}