当前位置: 首页 > news >正文

iOS Swift 拍照识别数字(Recognizing Text in Images)

可以用腾讯云

OCR的iOS demo - 腾讯云

苹果官方的解决方案(识别度太低)

Recognizing Text in Images - apple developer
Extracting phone numbers from text in images(Sample Code) - apple developer

import UIKit
import Visionclass ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {@IBOutlet weak var resLabel: UILabel!@IBAction func recognizeAssetsImage(_ sender: Any) {recognizeText(in: #imageLiteral(resourceName: "image_sample"))}// 按钮点击,打开相册@IBAction func selectImage(_ sender: UIButton) {let imagePickerController = UIImagePickerController()imagePickerController.delegate = selfpresent(imagePickerController, animated: true, completion: nil)}// 选择图片完成后的回调func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {picker.dismiss(animated: true, completion: nil)// 获取选择的图片guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {return}let resImage = binarize(image: selectedImage)!// 识别图片中的文本recognizeText(in: resImage)}func binarize(image: UIImage, threshold: CGFloat = 0.5) -> UIImage? {guard let cgImage = image.cgImage else { return nil }let width = cgImage.widthlet height = cgImage.heightlet colorSpace = CGColorSpaceCreateDeviceGray()var pixels = [UInt8](repeating: 0, count: width * height)guard let context = CGContext(data: &pixels, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width, space: colorSpace, bitmapInfo: CGImageAlphaInfo.none.rawValue) else { return nil }context.draw(cgImage, in: CGRect(x: 0, y: 0, width: width, height: height))for i in 0 ..< pixels.count {pixels[i] = pixels[i] < UInt8(threshold * 255) ? 0 : 255}guard let binarizedCgImage = context.makeImage() else { return nil }return UIImage(cgImage: binarizedCgImage)}func recognizeText(in image: UIImage) {// 将 UIImage 转换为 CGImageguard let cgImage = image.cgImage else { return }// 创建处理请求的处理器let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])// 创建 VNRecognizeTextRequestlet request = VNRecognizeTextRequest { (request, error) inif let error = error {print("Text recognition error: \(error)")return}// 处理识别结果guard let observations = request.results as? [VNRecognizedTextObservation] else { return }for observation in observations {// 获取识别到的文本guard let topCandidate = observation.topCandidates(1).first else { continue }print("Recognized text: \(topCandidate.string)")self.resLabel.text = topCandidate.string}}// 设置识别级别request.recognitionLevel = .accurate// 执行请求do {try handler.perform([request])} catch {print("Failed to perform text recognition: \(error)")}}
}

前期尝试过的解决方案(没有试成功过)
TesseractOCRiOS - github(最后一次提交2020.2)
SwiftyTesseract - github(最后一次提交2022.4)

http://www.lryc.cn/news/176449.html

相关文章:

  • 数学建模:智能优化算法及其python实现
  • monkeyrunner环境搭建和初步用法
  • 2024华为校招面试真题汇总及其解答(一)
  • css调整字体间距 以及让倾斜字体
  • 工具篇 | Gradle入门与使用指南 - 附Github仓库地址
  • 使用 Python 函数callable和isinstance的意义
  • Netty场景及其原理
  • Java接口和接口继承
  • 2023 年解锁网络安全即服务
  • python基于轻量级卷积神经网络模型GhostNet开发构建养殖场景下生猪行为识别系统
  • Selenium自动化测试 —— 通过cookie绕过验证码的操作!
  • 链表(单链表、双链表)
  • 面试题08.05.递归算法
  • 分布式IT监控系统
  • Redis 是什么?
  • 本地源制作
  • 树莓派(Linux系统通用)交叉编译(环境搭建、简单使用)
  • uniapp - 微信小程序实现腾讯地图位置标点展示,将指定地点进行标记选点并以一个图片图标展示出来(详细示例源码,一键复制开箱即用)
  • 网络安全--IDS--入侵检测
  • js实现数组去重方式(12种方法)
  • AI智能语音机器人的优势
  • BERT: 面向语言理解的深度双向Transformer预训练
  • 5-1.(OOP)初步分析MCV架构模式
  • 如何利用React和Flutter构建跨平台移动应用
  • npm install / webdriver-manager update报错 unable to get local issuer certificate
  • 电商项目高级篇-02 elasticsearch-下
  • 计算机竞赛 深度学习人体跌倒检测 -yolo 机器视觉 opencv python
  • CloseableHttpClient详解
  • 从mysql 5.7 升级到 8.0 的一些注意事项
  • 喜迎中秋国庆双节,华为云Astro Canvas之我的中秋节设计大屏