今天实作将Barcode Scanner结合在我的Button上
根据昨天的AVFoundation建立出的Camera,再增加Vison套件的识别功能
让图像能够作为识别
头好痛,晚了七秒,我的铁人赛啊
import Vision
需要要素:
流程:
透过func photoOutput去辨认detectRequest
将辨别完成的资料储存在detectRequest
透过AfterClassfication将detectRequest里面的资料取出,作为显示
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
if let imageData = photo.fileDateRepresentation, let image = UIImage(data:imageData){
guard let CiImage = CIImage(image:image)else{
showAlert(withTitle:"Unable to Convert")
}
DispatchQueue.global(qos: .userInteractive).sync{
// 让handler装载要使用的CIImage,orientation要让辨识知道方向是否一样
let handler = VNImageRequestHandler(ciImage:CiImage,orientation:CGImagePropertyOrientation.up, options:[:])
do{
try handler.perform([self.detecterRequest])
}
catch{
self.showAlert(withTitle:"Error")
}
}
}
}
// 宣告一个lazy宣吿
lazy DetectRequest:VNDetectBarcodesRequest = {
return VNDetectBarcodesRequest(completionHandler:{(request,error) in
guard error = nil else{
showAlert(withTitle:"BarCode Error")
return
}
// 让AfterDetect读去资料
self.AfterDetect(for:request)
})
}()
// 将刚刚的DetectBarcodeRequest里面的内容拆解出来
func AfterDetect(for request:VNRequest){
// 将物件转型成Observation(已经辨识後的物件)
if let bestResult = request.results?.first as? VNBarcodeObservation{
if let payload = bestResult.payloadStringValue{
// 使用多执行绪的显示UIAlertController
// 更新UI,只能使用Main thread
DispatchQueue.main.async{
let alertController = UIAlertController()
alertController.addAction(UIAlertAction(title: payload, style: .default, handler:nil))
self.present(alertController, animate: true, completion: nil)
}
}
}else{
self.showAlert(withTitle:"BarCode Error")
}
}
成果展示:
参考连结:
程序码都是参考自
加上了一点自己DIY的理解
完整的GitHub:
heartbeat-tutorials/ViewController.swift at master · rickwierenga/heartbeat-tutorials
iOS 11 - Vision 人脸识别 - SwiftCafe 享受代码的乐趣
<<: Day09 - this&Object Prototypes Ch3 Objects - Contents - [[Put]]
>>: [Day 10] 从 tensorflow.keras 开始的 ResNet 生活
今天内容为房间载入的程序码设定,明天会教大家如何测试。 ...
教材网址 https://coding104.blogspot.com/2021/06/java-5...
while 常见用法如下: //while count = 1 while count<=5:...
假设你聆听的对象,可能知道一些你不知道的事 Assume that the person you ...
开发环境设定 1. 安装 python3 在 Mac 环境中预设是安装 python2 我们可以透过...