Skip to content

Commit

Permalink
removing scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
luicfrr committed Apr 16, 2024
1 parent 22c87fe commit 67d98f4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 79 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export default function App() {
}
```

OBS: Returned face bounds, landmarks and contours are now natively downscaled to screen size. No extra function needed.
OBS: Returned face bounds are relative to frame coordinates and you need to scale them down to preview coordinates.

See example app for more details.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,21 @@ class VisionCameraFaceDetectorPlugin(
}

private fun processBoundingBox(
boundingBox: Rect,
scaleX: Double,
scaleY: Double
boundingBox: Rect
): Map<String, Any> {
val bounds: MutableMap<String, Any> = HashMap()
val width = boundingBox.width().toDouble() * scaleX
val width = boundingBox.width().toDouble()

bounds["width"] = width
bounds["height"] = boundingBox.height().toDouble() * scaleY
bounds["x"] = windowWidth - (width + (
boundingBox.left.toDouble() * scaleX
))
bounds["y"] = boundingBox.top.toDouble() * scaleY
bounds["height"] = boundingBox.height().toDouble()
bounds["x"] = windowWidth - (width + boundingBox.left.toDouble())
bounds["y"] = boundingBox.top.toDouble()

return bounds
}

private fun processLandmarks(
face: Face,
scaleX: Double,
scaleY: Double
face: Face
): Map<String, Any> {
val faceLandmarksTypes = intArrayOf(
FaceLandmark.LEFT_CHEEK,
Expand Down Expand Up @@ -155,18 +149,16 @@ class VisionCameraFaceDetectorPlugin(
}
val point = landmark.position
val currentPointsMap: MutableMap<String, Double> = HashMap()
currentPointsMap["x"] = point.x.toDouble() * scaleX
currentPointsMap["y"] = point.y.toDouble() * scaleY
currentPointsMap["x"] = point.x.toDouble()
currentPointsMap["y"] = point.y.toDouble()
faceLandmarksTypesMap[landmarkName] = currentPointsMap
}

return faceLandmarksTypesMap
}

private fun processFaceContours(
face: Face,
scaleX: Double,
scaleY: Double
face: Face
): Map<String, Any> {
val faceContoursTypes = intArrayOf(
FaceContour.FACE,
Expand Down Expand Up @@ -221,8 +213,8 @@ class VisionCameraFaceDetectorPlugin(
val pointsMap: MutableMap<String, Map<String, Double>> = HashMap()
for (j in points.indices) {
val currentPointsMap: MutableMap<String, Double> = HashMap()
currentPointsMap["x"] = points[j].x.toDouble() * scaleX
currentPointsMap["y"] = points[j].y.toDouble() * scaleY
currentPointsMap["x"] = points[j].x.toDouble()
currentPointsMap["y"] = points[j].y.toDouble()
pointsMap[j.toString()] = currentPointsMap
}

Expand Down Expand Up @@ -251,16 +243,6 @@ class VisionCameraFaceDetectorPlugin(

val rotation = orientation!!.toDegrees()
val image = InputImage.fromMediaImage(frameImage!!, rotation)
val scaleX: Double
val scaleY: Double
if (rotation == 270 || rotation == 90) {
scaleX = windowWidth.toDouble() / image.height
scaleY = windowHeight.toDouble() / image.width
} else {
scaleX = windowWidth.toDouble() / image.width
scaleY = windowHeight.toDouble() / image.height
}

val task = faceDetector!!.process(image)
val faces = Tasks.await(task)
val facesList = ArrayList<Map<String, Any?>>()
Expand All @@ -269,11 +251,7 @@ class VisionCameraFaceDetectorPlugin(
val map: MutableMap<String, Any?> = HashMap()

if (runLandmarks) {
map["landmarks"] = processLandmarks(
face,
scaleX,
scaleY
)
map["landmarks"] = processLandmarks(face)
}

if (runClassifications) {
Expand All @@ -283,11 +261,7 @@ class VisionCameraFaceDetectorPlugin(
}

if (runContours) {
map["contours"] = processFaceContours(
face,
scaleX,
scaleY
)
map["contours"] = processFaceContours(face)
}

if (trackingEnabled) {
Expand All @@ -297,11 +271,7 @@ class VisionCameraFaceDetectorPlugin(
map["rollAngle"] = face.headEulerAngleZ.toDouble()
map["pitchAngle"] = face.headEulerAngleX.toDouble()
map["yawAngle"] = face.headEulerAngleY.toDouble()
map["bounds"] = processBoundingBox(
face.boundingBox,
scaleX,
scaleY
)
map["bounds"] = processBoundingBox(face.boundingBox)
facesList.add(map)
}

Expand Down
48 changes: 14 additions & 34 deletions ios/VisionCameraFaceDetector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,20 @@ public class VisionCameraFaceDetector: FrameProcessorPlugin {
}

func processBoundingBox(
from face: Face,
scaleX: CGFloat,
scaleY: CGFloat
from face: Face
) -> [String:Any] {
let boundingBox = face.frame

return [
"width": boundingBox.width * scaleX,
"height": boundingBox.height * scaleY,
"x": boundingBox.origin.x * scaleX,
"y": boundingBox.origin.y * scaleY
"width": boundingBox.width,
"height": boundingBox.height,
"x": boundingBox.origin.x,
"y": boundingBox.origin.y
]
}

func processLandmarks(
from face: Face,
scaleX: CGFloat,
scaleY: CGFloat
from face: Face
) -> [String:[String: CGFloat?]] {
let faceLandmarkTypes = [
FaceLandmarkType.leftCheek,
Expand Down Expand Up @@ -141,8 +137,8 @@ public class VisionCameraFaceDetector: FrameProcessorPlugin {
for i in 0..<faceLandmarkTypes.count {
let landmark = face.landmark(ofType: faceLandmarkTypes[i]);
let position = [
"x": landmark?.position.x ?? 0.0 * scaleX,
"y": landmark?.position.y ?? 0.0 * scaleY
"x": landmark?.position.x ?? 0.0,
"y": landmark?.position.y ?? 0.0
]
faceLandMarksTypesMap[faceLandmarksTypesStrings[i]] = position
}
Expand All @@ -151,9 +147,7 @@ public class VisionCameraFaceDetector: FrameProcessorPlugin {
}

func processFaceContours(
from face: Face,
scaleX: CGFloat,
scaleY: CGFloat
from face: Face
) -> [String:[[String:CGFloat]]] {
let faceContoursTypes = [
FaceContourType.face,
Expand Down Expand Up @@ -199,8 +193,8 @@ public class VisionCameraFaceDetector: FrameProcessorPlugin {
if let points = contour?.points {
for point in points {
let currentPointsMap = [
"x": point.x * scaleX,
"y": point.y * scaleY,
"x": point.x,
"y": point.y
]

pointsArray.append(currentPointsMap)
Expand Down Expand Up @@ -242,19 +236,13 @@ public class VisionCameraFaceDetector: FrameProcessorPlugin {
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] = [:]

if runLandmarks {
map["landmarks"] = processLandmarks(
from: face,
scaleX: scaleX,
scaleY: scaleY
)
map["landmarks"] = processLandmarks(from: face)
}

if runClassifications {
Expand All @@ -264,11 +252,7 @@ public class VisionCameraFaceDetector: FrameProcessorPlugin {
}

if runContours {
map["contours"] = processFaceContours(
from: face,
scaleX: scaleX,
scaleY: scaleY
)
map["contours"] = processFaceContours(from: face)
}

if trackingEnabled {
Expand All @@ -278,11 +262,7 @@ public class VisionCameraFaceDetector: FrameProcessorPlugin {
map["rollAngle"] = face.headEulerAngleZ
map["pitchAngle"] = face.headEulerAngleX
map["yawAngle"] = face.headEulerAngleY
map["bounds"] = processBoundingBox(
from: face,
scaleX: scaleX,
scaleY: scaleY
)
map["bounds"] = processBoundingBox(from: face)

faceList.append(map)
}
Expand Down

0 comments on commit 67d98f4

Please sign in to comment.