public ResponseMessage Post() { float thSimilarity = 0.8f; if (!GlobalVar.bInitialized) { string curPath = HttpContext.Current.Server.MapPath("..") + "\\fr.dat"; if (FaceEngine.InitialEngine((float)15.0, (float)100000.0, curPath)) { GlobalVar.bInitialized = true; } } //photo processing String userMainPhoto = HttpContext.Current.Request.Params["MainPhoto"]; string strReturnUrl = HttpContext.Current.Request.Params["ReturnUrl"]; string strFrom = HttpContext.Current.Request.Params["From"]; userMainPhoto = RemoveEXIFRotation(userMainPhoto); userMainPhoto = userMainPhoto.Replace("data:image/jpg;base64,", ""); byte[] bufferMainPhoto = Convert.FromBase64String(userMainPhoto); MemoryStream memStream = new MemoryStream(); memStream.Write(bufferMainPhoto, 0, bufferMainPhoto.Length); memStream.Flush(); memStream.Seek(0, SeekOrigin.Begin); BitmapEx bmpMainPhoto = new BitmapEx(memStream); if (bmpMainPhoto.GetWidth() == 0) { return(makeRedirectResponse(new ResponseMessage(false, "Invalid Photo Error!"), strReturnUrl, strFrom)); } // detect face Int32 nFaceCount = FaceEngine.GetFaceCount(bmpMainPhoto.GetBuffer(), bmpMainPhoto.GetWidth(), bmpMainPhoto.GetHeight()); if (nFaceCount <= 0) { FaceEngine.RemoveAllTemplate(); return(makeRedirectResponse(new ResponseMessage(false, "Face Detection Error! Could not find any face."), strReturnUrl, strFrom)); } else if (nFaceCount > 1) { FaceEngine.RemoveAllTemplate(); return(makeRedirectResponse(new ResponseMessage(false, "Too many faces! Please make sure that there's only 1 face."), strReturnUrl, strFrom)); } //get face region FaceRectInfo faceRect = new FaceRectInfo(); FaceEngine.GetDetectFaces(0, ref faceRect); //detect landmarks SFaceLandmark landmarkPoints = new SFaceLandmark(); FaceEngine.GetDetectPoints(0, ref landmarkPoints); FaceEngine.RemoveAllTemplate(); //extract facial feature float[] pFeature = new float[2048]; FaceEngine.GetFeatures(bmpMainPhoto.GetBuffer(), bmpMainPhoto.GetWidth(), bmpMainPhoto.GetHeight(), 3, ref landmarkPoints, pFeature); //comparing with enrolled faces bool bFaceExisting = false; string sql = "SELECT FeatureData FROM UserData"; SqlConnection dbConn = new SqlConnection(GlobalVar.sqlDatabaseAccessString); SqlCommand dbComm = new SqlCommand(sql, dbConn); try { dbConn.Open(); SqlDataReader dbRead = dbComm.ExecuteReader(); while (dbRead.Read()) { string strJointEnrolledFeatureData = dbRead.GetString(0); string[] strEnrolledFeatureDatas = strJointEnrolledFeatureData.Split('|'); //compare with main feature first float[] pEnrolledMainFeature = Array.ConvertAll(strEnrolledFeatureDatas[0].Split(','), float.Parse); float similarity = FaceEngine.GetSimilarity(pFeature, pEnrolledMainFeature); if (similarity < thSimilarity) { //compare with all features float overallSimilarity = similarity; for (int i = 0; i < strEnrolledFeatureDatas.Length - 1; i++) { float[] pEnrolledSubFeature = Array.ConvertAll(strEnrolledFeatureDatas[i + 1].Split(','), float.Parse); overallSimilarity += FaceEngine.GetSimilarity(pFeature, pEnrolledSubFeature); } overallSimilarity /= strEnrolledFeatureDatas.Length; if (overallSimilarity > thSimilarity) { bFaceExisting = true; break; } } else { bFaceExisting = true; break; } } dbRead.Close(); } catch (Exception e) { return(makeRedirectResponse(new ResponseMessage(false, "DB error occured while comparing with enrolled faces!"), strReturnUrl, strFrom)); } finally { dbConn.Close(); } if (bFaceExisting) { return(makeRedirectResponse(new ResponseMessage(false, "There's already the same face existing!"), strReturnUrl, strFrom)); } //signing up String strFirstName = HttpContext.Current.Request.Params["FirstName"]; String userEmail = HttpContext.Current.Request.Params["UserEmail"]; String strFeatureData = string.Join(",", pFeature); byte[] bufferUserBlob = null; if (HttpContext.Current.Request.Files.AllKeys.Any()) { for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++) { long length = HttpContext.Current.Request.Files[i].InputStream.Length; bufferUserBlob = new byte[length]; HttpContext.Current.Request.Files[i].InputStream.Read(bufferUserBlob, 0, (int)length); if (HttpContext.Current.Request.Files[i].ContentType == "image/jpeg") { //calc features from subPhotos & concatenate them BitmapEx bmpSubPhoto = new BitmapEx(HttpContext.Current.Request.Files[i].InputStream); if (bmpSubPhoto.GetWidth() == 0) { continue; } nFaceCount = FaceEngine.GetFaceCount(bmpSubPhoto.GetBuffer(), bmpSubPhoto.GetWidth(), bmpSubPhoto.GetHeight()); if (nFaceCount == 1) { FaceEngine.GetDetectFaces(0, ref faceRect); FaceEngine.GetDetectPoints(0, ref landmarkPoints); FaceEngine.GetFeatures(bmpSubPhoto.GetBuffer(), bmpSubPhoto.GetWidth(), bmpSubPhoto.GetHeight(), 3, ref landmarkPoints, pFeature); String strSubFeatureData = string.Join(",", pFeature); strFeatureData = strFeatureData + "|" + strSubFeatureData; } FaceEngine.RemoveAllTemplate(); } } } string strQuery = "INSERT INTO UserData (UserName, UserEmail, UserPhoto, UserVideo, FeatureData) "; strQuery += "VALUES (@UserName, @UserEmail, @UserPhoto, @UserVideo, @FeatureData)"; SqlCommand cmd = new SqlCommand(strQuery); cmd.Parameters.Add("@UserName", SqlDbType.NChar).Value = strFirstName; cmd.Parameters.Add("@UserEmail", SqlDbType.NVarChar).Value = userEmail; cmd.Parameters.Add("@UserPhoto", SqlDbType.Image).Value = bmpMainPhoto.GetBuffer(); cmd.Parameters.Add("@UserVideo", SqlDbType.Image).Value = bufferUserBlob; cmd.Parameters.Add("@FeatureData", SqlDbType.Text).Value = strFeatureData; SqlConnection sqlDbConnection = new SqlConnection(GlobalVar.sqlDatabaseAccessString); cmd.CommandType = CommandType.Text; cmd.Connection = sqlDbConnection; try { sqlDbConnection.Open(); cmd.ExecuteNonQuery(); } catch (Exception ex) { var strMsg = ex.Message; // Check whether it is a email if (ex is SqlException) { SqlException exSql = (SqlException)ex; if (exSql.Class == 14 && exSql.Number == 2627) { strMsg = "Email has already taken"; } } return(makeRedirectResponse(new ResponseMessage(false, strMsg), strReturnUrl, strFrom)); } finally { sqlDbConnection.Close(); sqlDbConnection.Dispose(); } // Succeeded return(makeShopifyLoginResponse(userEmail, strReturnUrl, strFrom)); }
public ResponseMessage Post() { float thSimilarity = 0.8f; if (!GlobalVar.bInitialized) { string curPath = HttpContext.Current.Server.MapPath("..") + "\\fr.dat"; if (FaceEngine.InitialEngine((float)15.0, (float)100000.0, curPath)) { GlobalVar.bInitialized = true; } } //user verification(name & face verification) //name verification String userName = HttpContext.Current.Request.Params["UserName"]; string strReturnUrl = HttpContext.Current.Request.Params["ReturnUrl"]; string strFrom = HttpContext.Current.Request.Params["From"]; string sqlQuery = "SELECT COUNT(*) FROM UserData WHERE UserName = '******'"; SqlConnection dbConn = new SqlConnection(GlobalVar.sqlDatabaseAccessString); SqlCommand sqlCmd = new SqlCommand(sqlQuery, dbConn); try { dbConn.Open(); Int32 count = Convert.ToInt32(sqlCmd.ExecuteScalar()); if (count == 0) { return(makeRedirectResponse(new ResponseMessage(false, "There's no user having this name!"), strReturnUrl, strFrom)); } } catch (Exception) { return(makeRedirectResponse(new ResponseMessage(false, "DB error occured while verification is being held!"), strReturnUrl, strFrom)); } finally { dbConn.Close(); } //face verfication String userPhoto = HttpContext.Current.Request.Params["UserPhoto"]; userPhoto = RemoveEXIFRotation(userPhoto); userPhoto = userPhoto.Replace("data:image/jpg;base64,", ""); byte[] img = Convert.FromBase64String(userPhoto); MemoryStream memStream = new MemoryStream(); memStream.Write(img, 0, img.Length); memStream.Flush(); memStream.Seek(0, SeekOrigin.Begin); BitmapEx bmp = new BitmapEx(memStream); if (bmp.GetWidth() == 0) { return(makeRedirectResponse(new ResponseMessage(false, "Invalid Photo Error!"), strReturnUrl, strFrom)); } // detect face Int32 nFaceCount = FaceEngine.GetFaceCount(bmp.GetBuffer(), bmp.GetWidth(), bmp.GetHeight()); if (nFaceCount <= 0) { FaceEngine.RemoveAllTemplate(); return(makeRedirectResponse(new ResponseMessage(false, "Face Detection Error! Could not find any face."), strReturnUrl, strFrom)); } else if (nFaceCount > 1) { FaceEngine.RemoveAllTemplate(); return(makeRedirectResponse(new ResponseMessage(false, "Too many faces! Please make sure that there's only 1 face."), strReturnUrl, strFrom)); } //get face region FaceRectInfo faceRect = new FaceRectInfo(); FaceEngine.GetDetectFaces(0, ref faceRect); //detect landmarks SFaceLandmark landmarkPoints = new SFaceLandmark(); FaceEngine.GetDetectPoints(0, ref landmarkPoints); FaceEngine.RemoveAllTemplate(); //extract facial feature float[] pFeature = new float[2048]; FaceEngine.GetFeatures(bmp.GetBuffer(), bmp.GetWidth(), bmp.GetHeight(), 3, ref landmarkPoints, pFeature); //comparing with enrolled faces sqlCmd.Dispose(); dbConn.Dispose(); bool bFaceVerified = false; string strUserEmail = ""; sqlQuery = "SELECT UserEmail, FeatureData FROM UserData WHERE UserName = '******'"; dbConn = new SqlConnection(GlobalVar.sqlDatabaseAccessString); sqlCmd = new SqlCommand(sqlQuery, dbConn); try { dbConn.Open(); SqlDataReader dbRead = sqlCmd.ExecuteReader(); while (dbRead.Read()) { strUserEmail = dbRead.GetString(0); string strJointEnrolledFeatureData = dbRead.GetString(1); string[] strEnrolledFeatureDatas = strJointEnrolledFeatureData.Split('|'); float maxSimilarity = 0.0f; for (int i = 0; i < strEnrolledFeatureDatas.Length; i++) { float[] pEnrolledSubFeature = Array.ConvertAll(strEnrolledFeatureDatas[i].Split(','), float.Parse); float similarity = FaceEngine.GetSimilarity(pFeature, pEnrolledSubFeature); if (maxSimilarity < similarity) { maxSimilarity = similarity; } } if (maxSimilarity > thSimilarity) { bFaceVerified = true; break; } } dbRead.Close(); } catch (Exception) { return(makeRedirectResponse(new ResponseMessage(false, "Error occured while verification is being held!"), strReturnUrl, strFrom)); } finally { dbConn.Close(); } if (!bFaceVerified) { return(makeRedirectResponse(new ResponseMessage(false, "Sorry, your face doesn't match with enrolled one!"), strReturnUrl, strFrom)); } // Succeeded return(makeShopifyLoginResponse(strUserEmail, strReturnUrl, strFrom)); }