Skip to content

Commit

Permalink
👍 implement scan logic
Browse files Browse the repository at this point in the history
  • Loading branch information
touyou committed Jul 1, 2022
1 parent cf6755d commit f8899cf
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 7 deletions.
126 changes: 126 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
receipt_images
### Generated by gibo (https://github.com/simonwhitaker/gibo)
### https://raw.github.com/github/gitignore/b0012e4930d0a8c350254a3caeedf7441ea286a3/Swift.gitignore

# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## User settings
xcuserdata/

## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint
*.xccheckout

## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
build/
DerivedData/
*.moved-aside
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3

## Obj-C/Swift specific
*.hmap

## App packaging
*.ipa
*.dSYM.zip
*.dSYM

## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
# *.xcodeproj
#
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
# .swiftpm

.build/

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# Pods/
#
# Add this line if you want to avoid checking in source code from the Xcode workspace
# *.xcworkspace

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build/

# Accio dependency management
Dependencies/
.accio/

# fastlane
#
# It is recommended to not store the screenshots in the git repo.
# Instead, use fastlane to re-generate the screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output

# Code Injection
#
# After new code Injection tools there's a generated folder /iOSInjectionProject
# https://github.com/johnno1962/injectionforxcode

iOSInjectionProject/


### https://raw.github.com/github/gitignore/b0012e4930d0a8c350254a3caeedf7441ea286a3/Global/Xcode.gitignore

# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## User settings
xcuserdata/

## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint
*.xccheckout

## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
build/
DerivedData/
*.moved-aside
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3

## Gcc Patch
/*.gcno


56 changes: 49 additions & 7 deletions VisionReceipt/ScannerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

import SwiftUI
import PhotosUI
import Vision

struct ScannerView: View {
@State private var pickerItems: [PhotosPickerItem] = []
@State private var uiImage: UIImage?
@State private var isPresented: Bool = false
@State private var loading: Bool = false
@State private var imageLoading: Bool = false
@State private var processLoading: Bool = false

var body: some View {
VStack(spacing: 24) {
Expand All @@ -22,7 +24,7 @@ struct ScannerView: View {
.resizable()
.scaledToFit()
.frame(maxHeight: 300)
} else if loading {
} else if imageLoading {
ProgressView()
.progressViewStyle(.circular)
} else {
Expand All @@ -37,11 +39,18 @@ struct ScannerView: View {
Image(systemName: "photo.fill")
.font(.title)
})
.disabled(processLoading)
Button(action: {
print("proceed vision detect")
processLoading = true
executeTextRecognizer()
}, label: {
Image(systemName: "brain")
.font(.title)
if processLoading {
ProgressView()
.progressViewStyle(.circular)
} else {
Image(systemName: "brain")
.font(.title)
}
})
.disabled(uiImage == nil)
}
Expand All @@ -56,11 +65,11 @@ struct ScannerView: View {
)
.onChange(of: pickerItems) { newValue in
if let value = newValue.first {
loading = true
imageLoading = true
Task {
try await loadTransferable(from: value)
await MainActor.run {
loading = false
imageLoading = false
}
}
}
Expand All @@ -85,6 +94,39 @@ struct ScannerView: View {
print("\(#function) | error: \(error)")
}
}

/// 文字認識を走らせる
/// ref: https://developer.apple.com/documentation/vision/recognizing_text_in_images/
private func executeTextRecognizer() {
guard let cgImage = uiImage?.cgImage else {
processLoading = false
return
}
let requestHandler = VNImageRequestHandler(cgImage: cgImage)
let request = VNRecognizeTextRequest(completionHandler: recognizeTextHandler)
request.revision = VNRecognizeTextRequestRevision3
request.recognitionLanguages = ["ja", "en"]
do {
try requestHandler.perform([request])
} catch {
processLoading = false
print("Unable to perform the requests: \(error)")
}
}

private func recognizeTextHandler(request: VNRequest, error: Error?) {
guard let observations = request.results as? [VNRecognizedTextObservation] else {
processLoading = false
return
}
print("observations: \(observations)")
let recognizedStrings = observations.compactMap { observation in
return observation.topCandidates(1).first?.string
}

processLoading = false
print("result \(recognizedStrings)")
}
}

#if DEBUG
Expand Down

0 comments on commit f8899cf

Please sign in to comment.