Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT ✨] Support landscape mode #62

Open
gwendall opened this issue May 17, 2024 · 9 comments
Open

[FEAT ✨] Support landscape mode #62

gwendall opened this issue May 17, 2024 · 9 comments
Assignees
Labels
enhancement New feature or request Fund

Comments

@gwendall
Copy link

gwendall commented May 17, 2024

When switching to landscape mode the detections appear erratic. Is there something we can do to support them better in the current version?

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@luicfrr
Copy link
Owner

luicfrr commented May 20, 2024

can you add a screenshot or a video showing what you mean with "detections appear erratic"?

@luicfrr luicfrr added the enhancement New feature or request label May 20, 2024
@gwendall
Copy link
Author

There you go. It's probably because the model expects images with faces up. I tried doing frame.rotate(90, 0, 0) in the frame processor before passing the frame to the face detector but it doesn't seem to have any effect.

RPReplay_Final1716295251.mov

@luicfrr
Copy link
Owner

luicfrr commented May 21, 2024

Okay, can you please check if frame.orientation change when you rotate your device?

Currently I’m using frame’s orientation to determine MLKit orientation.

@gwendall
Copy link
Author

gwendall commented May 22, 2024

My app was locked in portrait mode so I just unlocked it. Oddly frame.orientation still never changes. Orientation change does get captured with expo-screen-orientation though (see recording below). So I guess it is a react-native-vision-camera issue? It is possible that expo or the screen orientation lib interferes with frame.orientation @mrousavy?

What would be awesome (and maybe the simpler fix) would be to be able to manually pass the screen orientation to detectFaces. May also enable to lock the UI in portrait mode while still handling frames for vision computing in their actual directions like it's done on instagram, tiktok, etc. What do you think?

bug2.mov

@mrousavy
Copy link
Contributor

Okay I definitely need to update the documentation on frame.orientation as a lot of people seem to misunderstand what it actually means and I get asked about this almost daily.

  1. Orientation is not fully implemented. I'm trying to raise money so I can implement orientation here: ✨ Implement Orientation ($3,000) mrousavy/react-native-vision-camera#1891
  2. The orientation of the Frame is probably not what most people think under orientation. Maybe I can find a different name for it. But it is always the way you need to read the Frame to make sure it is up-right. That's because Camera sensors are often in landscape orientation and that means you need to rotate the Frame by 90deg to display it up-right. This is handled already for preview photo and video, but in Frame Processors you need to do it yourself - if I would always rotate the frame to be 90deg upright position, that would introduce a huge latency and performance bottleneck in the pipeline. So instead, ML models work by internally just reading the Frame rotated. That way I don't have to actually rotate any buffers around and it's much faster.

Hope this explains why it stays the same.

@luicfrr
Copy link
Owner

luicfrr commented May 22, 2024

I'll do some tests and try to implement this feature ASAP. The problem is I'm busy with another project right now so I can't say when I'll have time to do this.

Anyway, if you want to try it yourself: orientation is handled on native side in these lines:

val rotation = getFrameRotation(frame.orientation)
val image = InputImage.fromMediaImage(frame.image, rotation)
val sourceWidth: Double
val sourceHeight: Double
if (rotation == 270 || rotation == 90) {
sourceWidth = image.height.toDouble()
sourceHeight = image.width.toDouble()
} else {
sourceWidth = image.width.toDouble()
sourceHeight = image.height.toDouble()
}

let image = VisionImage(buffer: frame.buffer)
image.orientation = frame.orientation
var scaleX:CGFloat
var scaleY:CGFloat
if autoScale {
scaleX = screenBounds.size.width / CGFloat(frame.width)
scaleY = screenBounds.size.height / CGFloat(frame.height)
} else {
scaleX = CGFloat(1)
scaleY = CGFloat(1)
}

a good start point is to set fixed values on native side and check if it works:

// ios
let image = VisionImage(buffer: frame.buffer)
image.orientation = .up //or .left or .right etc....

// android
val image = InputImage.fromMediaImage(frame.image, 0) // or 90, 180, etc...

maybe we can add the orientation prop on config so when orientation changes it will recreate face detector:

const orientation = someOrientationHook() 
const { detectFaces } = useFaceDetector( {
  ... other props,
  orientation
} )

Or maybe get device's orientation on native side instead?

@luicfrr luicfrr closed this as completed in d889e79 Jul 2, 2024
@luicfrr
Copy link
Owner

luicfrr commented Jul 3, 2024

@gwendall I just released 1.7.0 version.
Can you please check if it really fix this issue?

@dkoh555
Copy link

dkoh555 commented Nov 12, 2024

@gwendall I just released 1.7.0 version. Can you please check if it really fix this issue?

I think the issue still isn't fixed. When I run the face detector in a landscape orientation the detections are erratic, similar to what was described by the OG post.

In @mrousavy's post about frame.orientation (mrousavy/react-native-vision-camera#3077), he mentions that the frame is "... always some fixed orientation (e.g. landscape-left), no matter if the phone is in portrait or landscape".

He then mentions that Frame Processor Plugin authors need to adjust their respective Plugins accordingly so that the Plugins themselves compensate for any rotations that the user may encounter or use.
He states that "If the user holds his phone sideways (aka not in it's native portrait orientation), then you would also need to rotate that Orientation".

This is an option I don't believe is currently available in this pacakge, and would really appreciate it if it got added!
That said, there could be something obvious that I'm missing so please advise in the case that I am!

@luicfrr luicfrr reopened this Nov 12, 2024
@luicfrr
Copy link
Owner

luicfrr commented Nov 12, 2024

@dkoh555 The issue is reopened but I don't have time to work on this package right now.
Anyway you're wellcome to open a PR anytime.

@polar-sh polar-sh bot added the Fund label Nov 19, 2024
@luicfrr luicfrr self-assigned this Nov 19, 2024
@luicfrr luicfrr pinned this issue Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Fund
Projects
None yet
Development

No branches or pull requests

4 participants