In this exercise we render an image on top of the detected face
Create subfolder src/Glasses. In that folder, create file index.js.
src/Glasses/index.js
import React from 'react'
import { Image, View } from 'react-native';
const Mask = ({
face: {
bounds: {
size: { width: faceWidth, height: faceHeight }
},
leftEyePosition,
rightEyePosition
}
}) => {
const glassesWidth = faceWidth
const glassesHeight = faceHeight / 3
const transformAngle = (
angleRad = Math.atan(
(rightEyePosition.y - leftEyePosition.y) /
(rightEyePosition.x - leftEyePosition.x)
)
) => angleRad * 180 / Math.PI
return (
<View style={{
position: 'absolute',
left: leftEyePosition.x - glassesWidth * 0.35,
top: leftEyePosition.y - glassesHeight * 0.4
}}>
<Image
source={require('../../assets/glasses.png')}
style={{
width: glassesWidth,
height: glassesHeight,
resizeMode: 'contain',
transform: [{ rotate: `${transformAngle()}deg`}]
}}
/>
</View>
);
};
export default Mask
In your app Main view (src/index.js), replace use of Mask-component with newly created Glasses-component. Check exercise 3 for details how to do this.
Test the implementation.
Embed alternative image to app. You can find graphics for round glasses in assets-folder.
Think of ways how to improve UX and implementation.