Skip to content

Commit

Permalink
drop base64 frame conversion support
Browse files Browse the repository at this point in the history
  • Loading branch information
luicfrr committed Apr 19, 2024
1 parent 23a9138 commit 337d131
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 154 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ If you like this package please give it a ⭐ on [GitHub](https://github.com/non
- Real-time face detection using front camera
- Integration with Vision Camera library
- Adjustable face visualization with customizable styles
- Base64 frame convertion

## 🧰 Installation

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class VisionCameraFaceDetectorPlugin(
private var runClassifications = false
private var runContours = false
private var trackingEnabled = false
private var returnOriginal = false
private var convertFrame = false

init {
// initializes faceDetector on creation
Expand Down Expand Up @@ -85,10 +83,6 @@ class VisionCameraFaceDetectorPlugin(
faceDetector = FaceDetection.getClient(
optionsBuilder.build()
)

// also check about returing frame settings
returnOriginal = options?.get("returnOriginal").toString() == "true"
convertFrame = options?.get("convertFrame").toString() == "true"
}

private fun processBoundingBox(
Expand Down Expand Up @@ -234,8 +228,8 @@ class VisionCameraFaceDetectorPlugin(
override fun callback(
frame: Frame,
params: Map<String, Any>?
): Map<String, Any> {
val resultMap: MutableMap<String, Any> = HashMap()
): ArrayList<Map<String, Any>> {
val facesList = ArrayList<Map<String, Any>>()

try {
val frameImage = frame.image
Expand Down Expand Up @@ -304,26 +298,12 @@ class VisionCameraFaceDetectorPlugin(
)
facesList.add(map)
}

val frameMap: MutableMap<String, Any> = HashMap()
if (returnOriginal) {
frameMap["original"] = frame
}

if (convertFrame) {
frameMap["converted"] = BitmapUtils.convertYuvToRgba(frameImage)
}

resultMap["faces"] = facesList
if(returnOriginal || convertFrame) {
resultMap["frame"] = frameMap
}
} catch (e: Exception) {
Log.e(TAG, "Error processing face detection: ", e)
} catch (e: FrameInvalidError) {
Log.e(TAG, "Frame invalid error: ", e)
}

return resultMap
return facesList
}
}
48 changes: 3 additions & 45 deletions ios/VisionCameraFaceDetector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public class VisionCameraFaceDetector: FrameProcessorPlugin {
private var runClassifications = false
private var runContours = false
private var trackingEnabled = false
private var returnOriginal = false
private var convertFrame = false

public override init(
proxy: VisionCameraProxyHolder,
Expand Down Expand Up @@ -69,10 +67,6 @@ public class VisionCameraFaceDetector: FrameProcessorPlugin {
}

faceDetector = FaceDetector.faceDetector(options: optionsBuilder)

// also check about returing frame settings
returnOriginal = config?["returnOriginal"] as? Bool == true
convertFrame = config?["convertFrame"] as? Bool == true
}

func getConfig(
Expand Down Expand Up @@ -213,38 +207,18 @@ public class VisionCameraFaceDetector: FrameProcessorPlugin {
return faceContoursTypesMap
}

func convertFrameToBase64(
_ frame: Frame
) -> Any! {
guard let imageBuffer = CMSampleBufferGetImageBuffer(frame.buffer) else {
print("Failed to get CVPixelBuffer!")
return nil
}
let ciImage = CIImage(cvPixelBuffer: imageBuffer)

guard let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else {
print("Failed to create CGImage!")
return nil
}
let image = UIImage(cgImage: cgImage)
let imageData = image.jpegData(compressionQuality: 100)

return imageData?.base64EncodedString() ?? ""
}

public override func callback(
_ frame: Frame,
withArguments arguments: [AnyHashable: Any]?
) -> Any? {
var result: [String: Any] = [:]
var facesList: [Any] = []

do {
let image = VisionImage(buffer: frame.buffer)
image.orientation = .up

let scaleX = screenBounds.size.width / CGFloat(frame.width)
let scaleY = screenBounds.size.height / CGFloat(frame.height)
var faceList: [Any] = []
let faces: [Face] = try faceDetector!.results(in: image)
for face in faces {
var map: [String: Any] = [:]
Expand Down Expand Up @@ -284,28 +258,12 @@ public class VisionCameraFaceDetector: FrameProcessorPlugin {
scaleY: scaleY
)

faceList.append(map)
facesList.append(map)
}

var frameMap: [String: Any] = [:]
if returnOriginal {
frameMap["original"] = frame
}

if convertFrame {
frameMap["converted"] = convertFrameToBase64(frame)
}

result = returnOriginal || convertFrame ? [
"faces": faceList,
"frame": frameMap
] : [
"faces": faceList
]
} catch let error {
print("Error processing face detection: \(error)")
}

return result
return facesList
}
}
31 changes: 6 additions & 25 deletions src/FaceDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type CallbackType = (

export interface DetectionResult {
faces: Face[]
frame: FrameData
frame: Frame
}

export interface Face {
Expand All @@ -46,11 +46,6 @@ export interface Face {
landmarks: Landmarks
}

export interface FrameData {
converted?: string
original?: Frame
}

export interface Bounds {
width: number
height: number
Expand Down Expand Up @@ -133,24 +128,6 @@ export interface FaceDetectionOptions {
* @default false
*/
trackingEnabled?: boolean

/**
* Should return converted frame as base64?
*
* Note that no frame data will be returned if disabled.
*
* @default false
*/
convertFrame?: boolean

/**
* Should return original frame data?
*
* WARNING: On my tests this freeze frame processor pipeline on IOS.
*
* @default false
*/
returnOriginal?: boolean
}

/**
Expand All @@ -177,7 +154,11 @@ function createFaceDetectorPlugin(
}: DetectFacesType ): DetectionResult => {
'worklet'
// @ts-ignore
const result: DetectionResult = plugin.call( frame )
const faces: Face[] = plugin.call( frame )
const result: DetectionResult = {
faces,
frame
}
callback?.( result )
return result
}
Expand Down

0 comments on commit 337d131

Please sign in to comment.