google ml kit를 이용하여 이미지 또는 카메라에서 실시간으로 글자 인식하기
google과 apple 모두 글자 인식 하는 머신러닝 기능이 있지만 처리 방식에 차이가 있었는데
ios에서 글자 인식을 구현하는 경우
apple의 ml kit가 속도가 빠르고 카메라 모드에서 바로 인식된 글자를 넘겨주었고,
google의 경우 카메라 모드에서 넘어온 이미지를 글자 인식 처리하였다.
이번 프로젝트는 특정 영역에서 유효한 글자가 있는지 확인해야 해서 google ml kit를 사용하였다.
텍스트 인식 | ML Kit | Google Developers
이 페이지는 Cloud Translation API를 통해 번역되었습니다. Switch to English 텍스트 인식 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. ML Kit Text Recognition API는 라틴
developers.google.com
//1. podfile 에 google ml kit 추가
pod 'GoogleMLKit/TextRecognition','2.2.0'
//2. pod install
//2-1. intell의 경우
pod install
//2-2. m1의 경우
arch -x86_64 pod install
import Foundation
import MLKitTextRecognition
import MLKitVision
class TextRecognitionManager {
private let textRecognizer = TextRecognizer.textRecognizer()
//CMSampleBuffer로 전달하는 경우 //카메라 모드에서 글자인식
func processText(buffer: CMSampleBuffer){
let inputImage = VisionImage(buffer: buffer)
inputImage.orientation = .up //글자를 읽을 방향 지정해주어야함.
textRecognizer.process(inputImage) {
result, error in
guard error == nil, let result = result else {
print("Text Recognition Failed", error)
return
}
let trimmedText = self.trimText(textToTrim: result.text)
print(trimmedText)
}
}
//uiimage로 전달하는 경우 //이미지에서 글자 인식
func processText(img: UIImage){
let inputImage = VisionImage(image: img)
inputImage.orientation = .up //글자를 읽을 방향 지정해주어야함.
textRecognizer.process(inputImage) {
result, error in
guard error == nil, let result = result else {
print("Text Recognition Failed", error)
return
}
let trimmedText = self.trimText(textToTrim: result.text)
print(trimmedText)
}
}
private func trimText(textToTrim: String) -> String {
//문자열에서 띄어쓰기, \n등을 포함한 모든 공백 제거하고 문자만 남김
return textToTrim.replacingOccurrences(of: "\\s", with: "", options: .regularExpression)
}
}
Google ML Kit에서 글자 인식 방향
글자의 방향을 지정해주어야 글자로 인식이 가능하다.
ex) orientation을 .up으로 지정해주었는데 글자 방향이 정방향 (Text)로 적혀 있으면 글자가 인식 되지 않는다.
** 카메라 모드에서 넘어온 이미지는 눈에 보이는 정방향에서 시계방향으로 270도 회전된 상태로 넘어오므로 이미지의 방향과 글자의 방향 두가지를 잘 설정해주어야 글자를 제대로 인식할 수 있다.
아래의 이미지는 눈에 보이는 글자 방향에 따라 orientation에 지정해주어야 하는 raw value
카메라 모드에서 글자 인식 하려는 경우
extension PreviewView: AVCaptureVideoDataOutputSampleBufferDelegate {
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
textRecognitionManager.processText(buffer: sampleBuffer)
//let iamge = buffer.uiImage!
//textRecognitionManager.processText(img: image)
}
}
[ios - swift] custom camera
UIView의 class를 PreviewView로 지정하여 camera에서 이미지가 넘어올때마다 uiview에 실시간으로 보인다. import UIKit import AVFoundation import Alamofire class CaptureViewController: UIViewController { @IBOutlet weak var preview: P
tok-tok-100.tistory.com
'iOS > Swift' 카테고리의 다른 글
[ios - swift] how to know enter background or foreground (0) | 2022.12.16 |
---|---|
[ios - swift] remove UITableView Top Padding (0) | 2022.12.07 |
[ios - swift] custom camera (0) | 2022.12.06 |
[ios - swift] UIView several corner radius (0) | 2022.12.06 |
[ios - swift] download image from url (0) | 2022.12.05 |