Php code for face rating interface
Valid ();} else {$ wechatObj-> responseMsg ();} class wechatCallbackapiTest {public function valid () {$ echoStr = $ _ GET ["echostr"]; // valid signature, option if ($ this-> checkSignature () {echo $ echoStr; exit ;}} public function responseMsg () {// get post data, may be due to the different environments $ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; // extract post data if (! Empty ($ postStr) {/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */libxml_disable_entity_loader (true ); $ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ fromUsername = $ postObj-> FromUserName; $ toUsername = $ postObj-> ToUserName; $ keyword = trim ($ postObj-> Content); $ imgUrl = $ postObj-> PicUrl; $ Event = $ postObj-> Event; $ EventKey = $ postObj-> EventKey; $ MsgType = $ postObj-> MsgType; $ time = time (); $ itemTpl ="
<! [CDATA [% s]>
%s
%s
%s
"; If ($ MsgType =" image ") {$ item_str = sprintf ($ itemTpl," face Value Report ", face ($ imgUrl), $ imgUrl, MESSAGE_URL ); $ xmlTpl ="
%s
%s
% S
news
% S$ Item_str
"; $ ResultStr = sprintf ($ xmlTpl, $ fromUsername, $ toUsername, $ time, 1); echo $ resultStr ;}} else {echo" "; exit ;}} private function checkSignature () {// you must define TOKEN by yourself if (! Defined ("TOKEN") {throw new Exception ('token is not defined! ');} $ Signature = $ _ GET ["signature"]; $ timestamp = $ _ GET ["timestamp"]; $ nonce = $ _ GET ["nonce"]; $ token = TOKEN; $ tmpArr = array ($ token, $ timestamp, $ nonce); // use SORT_STRING rule sort ($ tmpArr, SORT_STRING ); $ tmpStr = implode ($ tmpArr); $ tmpStr = sha1 ($ tmpStr); if ($ tmpStr ==$ signature) {return true;} else {return false ;}}} // call the facial recognition API to return the recognition result function face ($ imgUrl) {// face ++ link $ jsonStr = c Url_get_contents (DETECT_URL.API_KEY.API_SECRET. "& url = ". $ imgUrl. ATTRIBUTE); $ replyDic = json_decode ($ jsonStr, true); $ faceArray = $ replyDic ['face']; $ resultStr = ""; for ($ I = 0; $ I <count ($ faceArray); $ I ++) {$ resultStr. = "<---- no ". ($ I + 1 ). "face ----> \ n"; $ tempFace = $ faceArray [$ I]; $ faceId = $ tempFace ['face _ id']; $ tempAttr = $ tempFace ['attribute']; // Age: The value containing the age analysis result // value is a non-negative integer representing the estimated age, and range representing the positive and negative regions of the estimated age $ TempAge = $ tempAttr ['age']; // gender: the value containing the gender analysis result // value is Male/Female, confidence indicates confidence level $ tempGenger = $ tempAttr ['gender']; // race: contains the result of the racial analysis. // The value of the value is Asian/White/Black, confidence indicates confidence level $ tempRace = $ tempAttr ['Race ']; // Smile: contains a real number with a value of 0-in the smile degree analysis result, A larger value indicates a higher degree of Smile. $ tempSmiling = $ tempAttr ['smiling']; // returns the gender $ sex = $ tempGenger ['value']. if ($ sex = "Male") {$ resultStr. = "Gender: Male \ n";} else if ($ sex = "Fe Male ") {$ resultStr. = "Gender: Female \ n" ;}// return Age $ maxAge = $ tempAge ['value'] + ($ tempAge ['range'])/2; $ age = ceil ($ maxAge); $ resultStr. = "age :". $ age. "About Age ~ \ N "; // returns the race if ($ tempRace ['value'] =" Asian ") {$ resultStr. =" skin color: healthy ~ \ N ";} else if ($ tempRace ['value'] =" White ") {$ resultStr. =" skin color: skin White! ^ 3 ^ \ n ";} else if ($ tempRace ['value'] =" Black ") {$ resultStr. =" skin color: You are a little Black ?!!! \ N ";}// return smile degree $ smiling = intval ($ tempSmiling ['value']); $ smile = round ($ tempSmiling ['value'], 3 ); $ resultStr. = "smile :". $ smile. "% \ n"; if ($ count <3) {// calculate the face color value $ yanzhi = getYanZhi ($ faceId, $ smiling); $ resultStr. = "appearance Association expert rating :". $ yanzhi. "Minute \ n"; $ resultStr. = "\ xe2 \ x9c \ xa8: \ n"; switch ($ yanzhi) {case $ yanzhi> 94: $ resultStr. = "this appearance value, burst the table! \ N "; break; case $ yanzhi> 87: $ resultStr. =" why are you so nice! \ N "; break; case $ yanzhi> 82: $ resultStr. =" continue! \ N "; break; case $ yanzhi> 72: $ resultStr. =" Okay, you can check it! \ N "; break; case $ yanzhi> 67: $ resultStr. =" Ah, it's ugly! \ N "; break; case $ yanzhi> 62: $ resultStr. =" if you have money, you can complete it! \ N "; break; default: $ resultStr. =" Let me be quiet. Is there a mirror in your house? \ N ";}}// calculate the similarity if (count ($ faceArray) = 2) {// Obtain face_id $ tempFace1 = $ faceArray [0]; $ tempId1 = $ tempFace1 ['face _ id']; $ tempFace2 = $ faceArray [1]; $ tempId2 = $ tempFace2 ['face _ id']; // face ++ link $ jsonStr1 = curl_get_contents (COMPARE_URL.API_KEY.API_SECRET. "& face_id2 = ". $ tempId2. "& face_id1 = ". $ tempId1); $ replyDic1 = json_decode ($ jsonStr1, true); // Retrieve similarity $ tempResult = $ replyDic1 ['similarity ']; $ TempSimilarity = $ replyDic1 ['component _ similarity ']; $ tempEye = $ tempSimilarity ['eye']; $ tempEyebrow = $ tempSimilarity ['eyebrow']; $ tempMouth = $ tempSimilarity ['mouth ']; $ tempNose = $ tempSimilarity ['nose']; $ resultStr. = "<---- similarity analysis ----> \ n"; $ resultStr. = "eyes :". round ($ tempEye, 3 ). "% \ n"; $ resultStr. = "eyebrows :". round ($ tempEyebrow, 3 ). "% \ n"; $ resultStr. = "mouth :". round ($ tempMouth, 3 ). "% \ n"; $ resultStr. =" Nose :". round ($ tempNose, 3 ). "% \ n"; $ resultStr. = "\ n <---- matching result ----> \ n similarity :". round ($ tempResult, 3 ). "% \ n"; if ($ tempResult> 70) {$ resultStr. = "Wow! An absolute husband and wife! \ N ";}elseif ($ tempResult> 50) {$ resultStr. =", it looks quite like! Hurry up! \ N ";}else {$ resultStr. =" 0.0 is not the same. \ N ";}}// if the face is not detected if ($ resultStr =" ") {$ resultStr =" Sorry, I did not recognize it, please try another face photo =. = ";} return $ resultStr;} // function getYanZhi ($ faceId, $ smiling) {$ t1 = microtime (1); $ jsonStr = curl_get_contents (response. "& face_id = ". $ faceId. TYPE); $ t2 = microtime (1); if ($ t2-$ t1)> 1.5) {return 75.632;} if ($ jsonStr! = False) {$ replyDic = json_decode ($ jsonStr, true); $ result = $ replyDic ['result']; $ landmarkArry = $ result [0]; $ landmark = $ landmarkArry ['landmark']; $ scheme = $ landmark ['right _ eyebrow_left_corner ']; $ scheme = $ landmark ['left _ eyebrow_right_corner']; $ left_eye_left_corner = $ landmark ['left _ eye_left_corner ']; $ left_eye_right_corner = $ landmark ['left _ eye_right_corner']; $ Marker = $ landmark ['mouth _ left_corner ']; $ mouth_right_corner = $ landmark ['mouth _ right_corner']; $ nose_left = $ landmark ['nose _ left']; $ nose_right = $ landmark ['nose _ right']; $ scheme = $ landmark ['nose _ lower_middle ']; $ scheme = $ landmark ['right _ eye_left_corner']; $ right_eye_right_corner = $ landmark ['right _ eye_right_corner ']; $ pai_left1 = $ landmark ['con Tour_left1 ']; $ export _right1 = $ landmark ['tour _ right1']; $ export _chin = $ landmark ['tour _ chin']; $ export _left6 = $ landmark ['tour _ left6']; $ export _right6 = $ landmark ['tour _ right6']; // calculate the distance between two brows. $ c1 = distance ($ left_eyebrow_right_corner ['x'], $ blank ['Y'], $ right_eyebrow_left_corner ['x'], $ right_eyebrow_left_corner ['Y']); // The midpoint coordinate between eyebrows; $ c1_x = ($ right_eyebrow_left_corner ['x']-$ Pipeline ['x'])/2 + $ left_eyebrow_right_corner ['x']; $ c1_y = ($ Pipeline ['Y']-$ left_eyebrow_right_corner ['Y']) /2 + $ left_eyebrow_right_corner ['Y']; // distance from the center of the eyebrows to the lowest position in the nose $ c2 = distance ($ nose_lower_middle ['x'], $ nose_contour_lower_middle ['Y'], $ c1_x, $ c1_y); // distance between the corners of the eye $ c3 = distance ($ left_eye_right_corner ['x'], $ left_eye_right_corner ['Y'], $ right_eye_left_corner ['x'], $ right_e Ye_left_corner ['Y']); // The width of the nose $ c4 = distance ($ nose_left ['x'], $ nose_left ['Y'], $ nose_right ['x'], $ nose_right ['Y']); // The width of the face $ c5 = distance ($ pai_left1 ['x'], $ pai_left1 ['Y'], $ pai_right1 ['x'], $ pai_right1 ['Y']); // The height from the chin to the bottom of the nose $ c6 = distance ($ pai_chin ['x'], $ pai_chin ['Y'], $ nose_pai_lower_middle ['x'], $ nose_pai_lower_middle ['Y']); // eye size $ c7_left = distance ($ left_eye_left_corner ['X'], $ left_eye_left_corner ['Y'], $ left_eye_right_corner ['x'], $ left_eye_right_corner ['Y']); $ c7_right = distance ($ region ['x'], $ region ['Y'], $ right_eye_right_corner ['x'], $ right_eye_right_corner ['Y']); // mouth size $ c8 = distance ($ mouth_left_corner ['x'], $ mouth_left_corner ['Y'], $ mouth_right_corner ['x'], $ mouth_right_corner ['Y']); // face size at the mouth $ c9 = distance ($ pai_left6 ['x'], $__Left6 ['Y'], $ pai_right6 ['x'], $ pai_right6 ['Y']);/* start the calculation step */$ yourmark = 100; $ mustm = 0; // The corner of the eye is 1/5 of the face width, $ mustm + = abs ($ c3/$ c5) * 100-25 ); // the width of the nose is 1/5 $ mustm + = abs ($ c4/$ c5) * 100-25); // The width of the eye, should be of the same horizontal face width! /5 $ eyepj = ($ c7_left + $ c7_right)/2; $ mustm + = abs ($ eyepj/$ c5 * 100-25 ); // The ideal mouth width should be 1/2 $ mustm + = abs ($ c8/$ c9) * 100-50) of the same face width ); // height between the chin and the bottom of the nose = distance from the center of the eyebrows to the lowest position of the nose $ mustm + = abs ($ c6-$ c2 ); return round ($ yourmark-$ mustm + $ smiling/10, 3);} else {return 60 ;}// function distance ($ px1, $ py1, $ px2, $ py2) {return sqrt (abs (pow ($ px2-$ px1, 2) + abs (pow ($ py2-$ py1, 2 )));} function curl_get_contents ($ url) {$ ch = Curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_TIMEOUT, 1 ); curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 1.5); $ result = curl_exec ($ ch); return $ result ;}?>