Last active
June 30, 2023 18:00
-
-
Save tanner-west/00f53950427430eb77109a365b330e41 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RCTVisionModule.swift | |
// VisionNativeModulePOC | |
// | |
// Created by Tanner West on 6/2/23. | |
// | |
import Foundation | |
import CoreImage | |
import Vision | |
@objc(RCTVisionModule) | |
class RCTVisionModule: NSObject { | |
func createCIImage(from imageURL: URL) -> CIImage? { | |
guard let ciImage = CIImage(contentsOf: imageURL) else { | |
// TODO handle error | |
return nil | |
} | |
return ciImage | |
} | |
@objc(detectText:callback:) | |
func detectText(_ imgUrl: String, callback: @escaping RCTResponseSenderBlock) { | |
if let imageURL = URL(string: imgUrl) { | |
if let ciImage = createCIImage(from: imageURL) { | |
let requestHandler = VNImageRequestHandler(ciImage: ciImage) | |
let request = VNRecognizeTextRequest(completionHandler: { request, error in | |
if let error = error { | |
// TODO handle error | |
} else if let results = request.results as? [VNRecognizedTextObservation] { | |
var result: [String] = [] | |
for observation in results { | |
if let recognizedText = observation.topCandidates(1).first { | |
result.append(recognizedText.string) | |
} | |
} | |
callback([result]) | |
} | |
}) | |
do { | |
try requestHandler.perform([request]) | |
} catch { | |
// TODO handle error | |
} | |
} | |
} | |
} | |
@objc(detectFaces:callback:) | |
func detectFaces(_ imgUrl: String, callback: @escaping RCTResponseSenderBlock) { | |
if let imageURL = URL(string: imgUrl) { | |
if let ciImage = createCIImage(from: imageURL) { | |
let requestHandler = VNImageRequestHandler(ciImage: ciImage) | |
let request = VNDetectFaceRectanglesRequest(completionHandler: { request, error in | |
if let error = error { | |
// TODO handle error | |
print(error) | |
} else if let results = request.results as? [VNFaceObservation] { | |
var result: [Dictionary<String, CGFloat>] = [] | |
var resultsLengthString = String(results.count) | |
for observation in results { | |
let box = observation.boundingBox | |
let boxDict = ["width": box.size.width, | |
"height": box.size.height, | |
"x": box.origin.x, | |
"y": box.origin.y] | |
result.append(boxDict) | |
} | |
callback([result]) | |
} | |
}) | |
do { | |
try requestHandler.perform([request]) | |
} catch { | |
// TODO handle error | |
print(error) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment