Saltar al contenido principal

Integración con React

Aprenda cómo integrar la biblioteca tm-videoface-plus en su proyecto React creando un componente reutilizable para la funcionalidad de OCR, paso a paso.


Implementación Básica

import React, { useEffect, useRef } from 'react';
import { VideoFace, CameraHelper, Envs, ChallengeMoves, ChallengeType } from 'tm-videoface-plus';

const LivenessDetectionComponent: React.FC = () => {
const videoRef = useRef<HTMLVideoElement>(null);
let videoFace: VideoFace | null = null;
let mediaStream: MediaStream | null = null;

useEffect(() => {
const initializeVideoFace = async () => {
const config = [
{ type: ChallengeType.CH_BLINK, enabled: true },
{ type: ChallengeType.CH_SMILE, enabled: true },
{ type: ChallengeType.CH_HEAD_POSE, enabled: true, moves: [ChallengeMoves.MV_HEAD_TURN_LEFT, ChallengeMoves.MV_HEAD_TURN_RIGHT] }
];

videoFace = new VideoFace('YOUR_KEY', Envs.DEV, config);

try {
// Open the camera
mediaStream = await videoFace.openCamera();
if (videoRef.current) {
videoRef.current.srcObject = stream;
}
pauseCamera();

// Subscribe to events
videoFace.events(EventType.CONNECTED).subscribe(() => {
console.log('Connected');
});

videoFace.events(EventType.MOVE).subscribe((move) => {
// Handle move events e.g. ChallengeMoves.MV_HEAD_TURN_LEFT
console.log('Move:', move);
});

videoFace.events(EventType.RESULT).subscribe((result) => {
console.log('Result:', result);
stopCamera();
});

videoFace.events(EventType.ERROR).subscribe((error) => {
console.error('Error:', error);
});

// Start the stream process
await videoFace.startStream();
} catch (error) {
console.error('Error initializing VideoFace:', error);
}
};

initializeVideoFace();

return () => {
if (videoFace) {
videoFace.close();
}
};
}, []);

const pauseCamera = () => {
if (mediaStream) {
mediaStream.getTracks().forEach((track) => (track.enabled = false));
setCameraPaused(true);
}
};

const resumeCamera = () => {
if (mediaStream) {
mediaStream.getTracks().forEach((track) => (track.enabled = true));
setCameraPaused(false);
}
};

const stopCamera = () => {
if (mediaStream) {
mediaStream.getTracks().forEach((track) => track.stop());
mediaStream = null;
}
};

return (
<div>
<video ref={videoRef} autoPlay playsInline style={{ width: '100%' }} />
<div>
<button onClick={resumeCamera} disabled={!cameraPaused}>
Start Camera
</button>
</div>
</div>
);
};

export default LivenessDetectionComponent;

Uso del Componente

Integre el componente LivenessDetectionComponent en su aplicación React.

import React from 'react';
import ReactDOM from 'react-dom';
import LivenessDetectionComponent from './LivenessDetectionComponent';

const App: React.FC = () => (
<div>
<h1>Liveness Integration Example</h1>
<LivenessDetectionComponent />
</div>
);

ReactDOM.render(<App />, document.getElementById('root'));

Soportesoporte.onboarding@finwave.es