This article through the example code to explain the implementation of the small program based on PHP facial recognition brush face login function, interested friends follow the script of the little one to learn together
First we confirm that our Baidu Cloud Human Face Library has uploaded our personal information photos
And then we write the face in the background login interface login We want to store photos taken to the server
Public Function Login () {//upload file path $dir = "./uploads/temp/"; if (!file_exists ($dir)) {mkdir ($dir, 0777,true); } $upload = new \think\upload (); $upload->maxsize = 2048000;//Set attachment upload size $upload->exts = array (' jpg ', ' gif ', ' PNG ', ' jpeg ');//Set attachment upload type $upload ->savepath = "; $upload->autosub = false; $upload->rootpath = $dir; Set attachments upload root directory//upload individual file $info = $upload->uploadone ($_files[' file '); if (! $info) {//Upload error error message echo json_encode (' Error ' =>true, ' msg ' + $upload->geterror ()), json_unescaped _UNICODE); }else{//Upload successfully uploaded file information $file = $dir. $info [' Savepath ']. $info [' Savename ']; $image = Base64_encode (file_get_contents ($file)); $client = $this->init_face (); $options [' liveness_control '] = ' NORMAL '; $options [' max_user_num '] = ' 1 '; $ret = $client->search ($image, ' BASE64 ', ' student ', $options); Echo Json_encode ($ret, Json_unescaped_unicode); Exit if ($ret [' Error_code ']==0) { $user = $ret [' result '] [' user_list '][0]; $no = $user [' user_id ']; $score = $user [' Score ']; if ($score >=95) {$data = M (' student ')->where ("no = ' {$no} '")->find (); $data [' score '] = $score; $data [' name '] = Json_decode ($data [' name '],true); $data [' sex '] = Json_decode ($data [' Sex '],true); Echo ' Recognition succeeded '. Json_encode ($data, Json_unescaped_unicode); }else{Echo ' recognition failed '. $data [' Score ']; } } } }
Then the front desk design
<camera device-position= "{device? ' Back ': ' Front '}} "flash=" Off "binderror=" error "style=" width:100%; height:300px; " ></camera> <view class= "Weui-cells__title" > Switches </view> <view class= "Weui-cells Weui-cells_after-title "> <view class=" Weui-cell weui-cell_switch "> <view class=" weui-cell__bd "> Toggle camera </view> <view class=" weui-cell__ft "> <switch bindtap=" deviceposition "/> </view> </view> </view> <button type= "PRIMARY" bindtap= "Takephoto" > Brush face Login </ Button>
We can also control the front and rear lens of the camera
Deviceposition () {this.setdata ({ device:!this.data.device,}) Console.log ("Current camera camera is:", This.data.device? ") Rear ":" Front "); Camera () {Let {ctx, type, startrecord} = This.data;}, data: { src:null,},
Invoking the interface in JS
Takephoto () { Const CTX = Wx.createcameracontext () Ctx.takephoto ({ quality: ' High ', success: (res) = > { this.setdata ({ src:res.tempImagePath }) Console.log (res) wx.uploadfile ({ URL: ', For example only, the non-real interface address filePath:this.data.src, name: ' file ', formData: { }, success:function ( RES) { //var data = Res.data //var JSON = json.parse (data) Console.log (res) Wx.showmodal ({ Title: "Hint", content:res.data, showcancel:false, confirmtext: "OK" }) } ) } }) },
Brush your face and log on.
Summarize
The above is a small part of the introduction of the PHP implementation of the small program face recognition brush face login, I hope we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for the support of the Scripting House website!
Articles you may be interested in:
PHP implementation prevents form repeat submission function "Token-based verification"
TP Framework (thinkphp) realizes three login password error after locking account function example
PHP implementation Generate data Dictionary function example