import React, { Component } from 'react' ;
import * as faceapi from 'face-api.js' ;
import * as webcamjs from 'webcamjs' ;
import config from '@/services/config.js' ;
import { Button } from 'antd' ;
class TakePicture extends Component {
_initCamera = async ( ) => {
await faceapi. nets. ssdMobilenetv1. loadFromUri (
'http://localhost:8004/faceapi-weights/' ,
) ;
await faceapi. nets. faceLandmark68Net. loadFromUri (
'http://localhost:8004/faceapi-weights/' ,
) ;
await faceapi. nets. faceRecognitionNet. loadFromUri (
'http://localhost:8004/faceapi-weights/' ,
) ;
this . openCamara ( )
} ;
componentDidMount ( ) {
this . _initCamera ( )
}
componentWillUnmount ( ) {
webcamjs. reset ( )
}
openCamara = ( ) => {
webcamjs. attach ( '#camera' ) ;
webcamjs. set ( 'upload_name' , 'file' ) ;
webcamjs. set ( {
width: 246 ,
height: 185 ,
image_format: 'jpeg' ,
jpeg_quality: 90
} ) ;
webcamjs. on ( 'error' , ( err ) => {
message. error ( '摄像头调起失败!' )
} ) ;
} ;
uploadPhoto = ( imgUrl ) => {
webcamjs. upload ( imgUrl, config. uploadUrl, ( code, text ) => {
console. log ( JSON . parse ( text) ) ;
} ) ;
}
takePicture = ( ) => {
webcamjs. snap ( ( imgUrl ) => {
this . uploadPhoto ( imgUrl)
} ) ;
} ;
render ( ) {
return (
< >
< h1> 人脸拍照< / h1>
< div id= "camera" style= { { width: 638 , height: 359 , background : '#000' } } > < / div>
< Button type= "primary" onClick= { this . takePicture} style= { { margin: '0 auto' , width : 120 } } > 采集人脸< / Button>
< / >
) ;
}
}
export default TakePicture;