https://cloud.google.com/vision/docs/detecting-safe-search?hl=zh-tw

原生 http post

yourKey 記得更換你自己的憑證
[“type”:”SAFE_SEARCH_DETECTION”] 設置SAFE_SEARCH_DETECTION

let semaphore = DispatchSemaphore (value: 0)
let testImageData: NSData = UIImage.init(named: "test2")!.jpegData(compressionQuality: 0.5)! as NSData
let base64ImageString = testImageData.base64EncodedString(options: .endLineWithCarriageReturn)

let parameters = ["requests":["image":["content":base64ImageString],"features":[["type":"SAFE_SEARCH_DETECTION"]],"imageContext":["languageHints":["zh","en"]]]] as [String : Any]
let postData = try? JSONSerialization.data(withJSONObject: parameters)
var request = URLRequest(url: URL(string: "https://vision.googleapis.com/v1/images:annotate?key=yourKey")!,timeoutInterval: Double.infinity)
request.addValue("text/plain", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = postData

let task = URLSession.shared.dataTask(with: request) { data, response, error in
    guard let data = data else {
        print(String(describing: error))
        return
    }
    print(String(data: data, encoding: .utf8)!)
    semaphore.signal()
}

task.resume()
semaphore.wait()

安全照片搜尋

[“type”:”SAFE_SEARCH_DETECTION”]

色情照片

色情檢測SafeSearch

色情檢測SafeSearch

血腥照片

色情檢測SafeSearch


Categorized in:

Tagged in:

,