[ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['POST'], ], ], ]; } public function actionSaveLogs() { if (Yii::$app->request->post()) { $post = Yii::$app->request->bodyParams; $model = new \app\models\FaceLogs(); $model->create($post); Yii::$app->response->format = "json"; return ["stt" => true]; } } public function actionSyncUrl() { if (Yii::$app->request->post()) { $post = Yii::$app->request->bodyParams; $sync = \app\models\SyncUrl::find()->one(); if ($sync) { $sync->data = $post['url']; $sync->save(); } else { $model = new \app\models\SyncUrl(); $model->create($post); } Yii::$app->response->format = "json"; return ["stt" => true]; } } public function actionGetLogs() { if (Yii::$app->request->post()) { $post = Yii::$app->request->bodyParams; $time = date_format(date_create_from_format('Y-m-d H:i:s', $post['time']), 'U'); $key = common::generateRandomString(); $RootFolder = Yii::getAlias('@webroot') . "/data/uploads"; $targetPath = $RootFolder . "/face"; $fileName = "face_" . $key . "_" . $time . ".png"; FileHelper::createDirectory($targetPath, 0777); file_put_contents($targetPath . "/" . $fileName, base64_decode($post['image'])); $totalsLogs = CaptureLogs::find()->count(); if ($totalsLogs >= \Yii::$app->params['maxLogs']) { $lastLogs = CaptureLogs::find()->orderBy(["id" => SORT_ASC])->limit(1)->one(); @unlink("/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $lastLogs->image); $lastLogs->delete(); } $model = new CaptureLogs(); $logs = $model->create([ "Staff" => $post["id"], "Time" => $time, "Image" => $fileName, "Confidence" => json_encode($post["confidence"]) ]); $server_ip = \app\models\SyncUrl::findOne(['key_config' => 'server_api']); $ip = "https://dev-dc.beetai.com"; if ($server_ip) $ip = $server_ip->data; if ($this->is_connected() && $logs) { $device_id = \app\models\SyncUrl::findOne(['key_config' => 'device_id']); $id_camera = 0; if ($device_id) $id_camera = intval($device_id->data); $logsInfo = CaptureLogs::findOne($logs); $staffInfo = ListManagement::findOne($post['id']); $tk = ""; $token = \app\models\SyncUrl::findOne(['key_config' => 'token']); if ($token) $tk = $token->data; $data = []; $data[] = strval($id_camera); //camera_id $data[] = date("Y-m-d H:i:s", $time); //frametime $data[] = $staffInfo ? strval($staffInfo->code) : "0"; //idCard $data[] = $staffInfo ? $staffInfo->staff_id : ""; //idObject $data[] = "123"; //person_id $data[] = "+7"; //timezone $text = implode("|", $data); $res = json_decode(file_get_contents($ip . "/api/box/face_recognition_auth_v2?token=" . $tk, false, stream_context_create([ 'http' => [ 'header' => "Content-Type: application/json", 'method' => "POST", 'content' => json_encode([ 'image' => base64_encode(file_get_contents("/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $fileName)), 'data' => common::rsaEncode($text) ]) ] ])), true); if ($res['status'] == 10000) { $logsInfo->sync_status = 1; $logsInfo->save(); } } Yii::$app->response->format = "json"; return ["status" => "success"]; } } public function actionGetAllFeatures($offset = 0, $limit = 10) { $listManagement = ListManagement::find()->limit($limit)->offset($offset)->all(); $allFeatures = []; foreach ($listManagement as $key => $value) { $features = json_decode($value->image, true); // $f = []; // foreach ($features as $k => $v) { // $f[] = $v['features']; // if (isset($v['features512'])) // if (count($v['features512'])) // $f[] = $v['features512']; // } // $allFeatures[] = [ // "id" => $value->id, // "name" => $this->convert_vi_to_en($value->name), // "features" => $f // ]; $feature1 = $feature2 = []; foreach ($features as $k => $v) { $feature1[] = $v['features']; if (isset($v['features512'])) if (count($v['features512'])) $feature2[] = $v['features512']; } $allFeatures[] = [ "id" => $value->id, "name" => $this->convert_vi_to_en($value->name), "feature1" => $feature1, "feature2" => $feature2 ]; } Yii::$app->response->format = "json"; return $allFeatures; } public function actionData() { if (Yii::$app->request->post()) { Yii::$app->response->format = "json"; return ListManagement::find()->orderBy(['last_modified' => SORT_DESC])->all(); } } public function actionFullData() { if (Yii::$app->request->post()) { Yii::$app->response->format = "json"; $lists = ListManagement::find()->andWhere(["IN", "code", Yii::$app->request->post()])->orderBy(['last_modified' => SORT_DESC])->all(); $res = []; foreach ($lists as $key => $value) { $f = []; $features = json_decode($value->image, true); foreach ($features as $k => $v) { $f[] = ["url" => base64_encode(file_get_contents(Yii::getAlias('@webroot') . "/data/uploads/face/" . $v['url'])), "features" => $v['features']]; } $res[] = [ "id" => $value->id, "code" => $value->code, "type" => $value->type, "name" => $value->name, "gender" => $value->gender, "birthday" => $value->birthday, "telephone" => $value->telephone, "department" => $value->address, "time" => $value->time, "images" => $f ]; } return $res; } } public function actionGetAllImage() { Yii::$app->response->format = "json"; $lists = ListManagement::find()->orderBy(['id' => SORT_DESC])->all(); $res = []; foreach ($lists as $key => $value) { $f = []; $features = json_decode($value->image, true); foreach ($features as $k => $v) { $f[] = Yii::$app->request->hostInfo . "/data/uploads/face/" . $v['url']; } $res[] = [ "id" => $value->id, "code" => $value->code, "type" => $value->type, "name" => $value->name, "gender" => $value->gender, "birthday" => $value->birthday, "telephone" => $value->telephone, "address" => $value->address, "time" => $value->time, "image" => $f ]; } return $res; } public function actionResetData() { // return file_get_contents("https://bischool.beetai.com/view/image/5f7c1b22dfe5aaf427a6dfa1"); \Yii::$app->db->createCommand()->truncateTable('capture_logs')->execute(); \Yii::$app->db->createCommand()->truncateTable('list_management')->execute(); } public function convert_vi_to_en($str) { $str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", "a", $str); $str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", "e", $str); $str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", "i", $str); $str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", "o", $str); $str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", "u", $str); $str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", "y", $str); $str = preg_replace("/(đ)/", "d", $str); $str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", "A", $str); $str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", "E", $str); $str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", "I", $str); $str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", "O", $str); $str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", "U", $str); $str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", "Y", $str); $str = preg_replace("/(Đ)/", "D", $str); //$str = str_replace(" ", "-", str_replace("&*#39;","",$str)); return $str; } public function getCurrentTime() { $time = microtime(); $time = explode(' ', $time); return $time[1] + $time[0]; } public function actionAutoGenFeature() { Yii::$app->response->format = "json"; if (!$this->check512()) return ["status" => false]; $updating = \app\models\SyncUrl::findOne(['key_config' => 'updating']); if (!$updating) { $model = new \app\models\SyncUrl(); $model->create([ 'key_config' => "updating", 'data' => "false" ]); $updating = \app\models\SyncUrl::findOne(['key_config' => 'updating']); } $currentCache = json_decode(@file_get_contents("http://localhost:2305/current-cache"), true); if ($updating->data === "true") { if ($currentCache['n_128'] == $currentCache['n_512']) { $updating->data = "false"; $updating->save(); $txt = "\n" . date("H:i:s d/m/Y") . " " . "success"; // file_put_contents(date('Ymd') . "_logs.txt", $txt, FILE_APPEND); return ["status" => false]; } else { $txt = "\n" . date("H:i:s d/m/Y") . " " . "updating"; // file_put_contents(date('Ymd') . "_logs.txt", $txt, FILE_APPEND); return ["status" => false]; } } if ($currentCache['n_128'] == $currentCache['n_512']) { $txt = "\n" . date("H:i:s d/m/Y") . " " . "success"; // file_put_contents(date('Ymd') . "_logs.txt", $txt, FILE_APPEND); return ["status" => false]; } $updating->data = "true"; $updating->save(); $processTime = []; set_time_limit(0); $lists = ListManagement::find()->all(); foreach ($lists as $key => $value) { $extract = false; $images = json_decode($value->image, true); $newImgs = []; $temp = []; foreach ($images as $k => $v) { if (!isset($v['features512']) || (isset($v['features512']) && count($v['features512']) == 0)) { $start = $this->getCurrentTime(); $features = common::requestToEngine("/get-feature", [ "image_paths" => [ ["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $v['url'], "type" => "raw"] ], "type" => "512" ]); if (!$features['status']) return ["status" => false]; $features = json_decode($features['data'], true); $finish = $this->getCurrentTime(); $temp[] = round(($finish - $start), 4); $v['features512'] = $features['results'][0]['feature512']; $extract = true; } $newImgs[] = $v; } if ($extract) { $processTime['extract'] = $temp; $start = $this->getCurrentTime(); $value->image = json_encode($newImgs); $value->save(); $finish = $this->getCurrentTime(); $processTime["updateDB"] = round(($finish - $start), 4); $txt = "\n" . json_encode($processTime); // file_put_contents(date('Ymd') . ".txt", $txt, FILE_APPEND); } } @file_get_contents("http://localhost:2305/update-feature"); $updating->data = "false"; $updating->save(); return ["status" => true]; } public function actionSync() { Yii::$app->response->format = "json"; if ($this->is_connected()) { $server_ip = \app\models\SyncUrl::findOne(['key_config' => 'server_api']); $ip = "https://dev-dc.beetai.com"; if ($server_ip) $ip = $server_ip->data; $device_id = \app\models\SyncUrl::findOne(['key_config' => 'device_id']); $id_camera = 0; if ($device_id) $id_camera = intval($device_id->data); $ls = CaptureLogs::find()->andWhere(['sync_status' => null])->all(); $tk = ""; $token = \app\models\SyncUrl::findOne(['key_config' => 'token']); if ($token) $tk = $token->data; foreach ($ls as $key => $value) { if (file_exists("/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $value->image)) { $staffInfo = ListManagement::findOne($value->staff_id); $res = json_decode(file_get_contents($ip . "/api/oem/face_recognition?token=" . $tk, false, stream_context_create([ 'http' => [ 'header' => "Content-Type: application/json", 'method' => "POST", 'content' => json_encode([ 'image' => base64_encode(file_get_contents("/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $value->image)), 'camera_id' => strval($id_camera), 'frametime' => date("Y-m-d H:i:s", $value->time), 'idCard' => $staffInfo ? strval($staffInfo->code) : "0", 'idObject' => $staffInfo ? $staffInfo->staff_id : "", "person_id" => "123", "timezone" => "+7" ]) ] ])), true); if ($res['status'] == 10000) { $value->sync_status = 1; $value->save(); } } else { $value->delete(); } } } return ["status" => true]; } function is_connected($ip = "google.com") { $connected = @fsockopen($ip, 80); //website, port (try 80 or 443) if ($connected) { $is_conn = true; //action when connected fclose($connected); } else { $is_conn = false; //action in connection failure } return $is_conn; } public function actionGetNumberPerson() { return ListManagement::find()->count(); } public function actionGetTotalFeature() { if (Yii::$app->request->isAjax) { Yii::$app->response->format = "json"; $start = $this->getCurrentTime(); $statistics = ListManagement::statisticFeatures(); $finish = $this->getCurrentTime(); $processTime = round(($finish - $start), 4); return [ "statistics" => $statistics, "processTime" => $processTime ]; } } public function actionCurrentCache() { return @file_get_contents("http://localhost:2305/current-cache"); } public function actionSaveToken() { if (Yii::$app->request->post()) { $post = Yii::$app->request->bodyParams; $token = \app\models\SyncUrl::findOne(['key_config' => 'token']); if ($token) { $token->data = $post['token']; $token->save(); } else { $model = new \app\models\SyncUrl(); $model->create([ 'key_config' => "token", 'data' => $post['token'] ]); } $version = \app\models\SyncUrl::findOne(['key_config' => 'version']); if ($version) { $version->data = $post['version']; $version->save(); } else { $model = new \app\models\SyncUrl(); $model->create([ 'key_config' => "version", 'data' => $post['version'] ]); } Yii::$app->response->format = "json"; return ["status" => true]; } } public function check512() { $tempConfig = json_decode(file_get_contents("http://localhost:4004/ReadEngineConfig", false, stream_context_create([ 'http' => [ 'header' => "Content-Type: application/json", 'method' => "POST" ] ])), true); if (!$tempConfig['status']) return false; $engineConfig = json_decode($tempConfig['data'], true); if (count($engineConfig['data']['engines']) == 0) return false; $checkConfig = json_decode(file_get_contents("http://localhost:4004/ReadConfig", false, stream_context_create([ 'http' => [ 'header' => "Content-Type: application/json", 'method' => "POST", 'content' => json_encode([ 'path' => $engineConfig['data']['engines'][0]['path'] ]) ] ])), true); if (!$checkConfig['status']) return false; $config_json = ""; if ($checkConfig['status']) { $config_json = json_decode($checkConfig['data'], true); $check512 = $config_json['feature']['type2']['enable']; if ($check512 !== "0") return true; } return false; } public function actionTest() { // $updating = \app\models\SyncUrl::findOne(['key_config' => 'token']); $updating = \app\models\SyncUrl::find()->all(); foreach ($updating as $key => $value) { echo $value->key_config . " " . $value->data . "
"; } exit(); } public function actionReGenFeature() { $updating = \app\models\SyncUrl::findOne(['key_config' => 'updating']); if (!$updating) { $model = new \app\models\SyncUrl(); $model->create([ 'key_config' => "updating", 'data' => "false" ]); } else { if ($this->check512()) { $currentCache = json_decode(file_get_contents("http://localhost:2305/current-cache"), true); if ($currentCache['n_128'] == $currentCache['n_512']) { $updating->data = "true"; } else { $updating->data = "false"; } } else $updating->data = "false"; $updating->save(); } Yii::$app->response->format = "json"; return ["data" => "reset updating flag"]; } public function actionCheckLogs() { set_time_limit(5000); Yii::$app->response->format = "json"; $logProcessStatus = \app\models\SyncUrl::findOne(['key_config' => 'log_process_status']); if (!$logProcessStatus) { $model = new \app\models\SyncUrl(); $model->create(['key_config' => 'log_process_status', 'data' => "true"]); $logProcessStatus = \app\models\SyncUrl::findOne(['key_config' => 'log_process_status']); } else { if ($logProcessStatus->data == "true") { if (Yii::$app->params['autoSyncLog']) file_put_contents("logs.txt", "CHECK LOGS PROCESSING\n", FILE_APPEND); return ["data" => "check logs processing"]; } else { $logProcessStatus->data = "true"; $logProcessStatus->save(); } } $server_ip = \app\models\SyncUrl::findOne(['key_config' => 'server_api']); $ip = "https://dev-dc.beetai.com"; if ($server_ip) $ip = $server_ip->data; $device_id = \app\models\SyncUrl::findOne(['key_config' => 'device_id']); if (!$device_id) return ["data" => "check logs fail"]; $id_camera = intval($device_id->data); $datas = json_decode(file_get_contents($ip . "/api/model/get_log_model", false, stream_context_create([ 'http' => [ 'header' => "Content-Type: application/json", 'method' => "POST", 'content' => json_encode([ "idCamera" => $id_camera ]) ] ])), true); if (Yii::$app->params['autoSyncLog']) file_put_contents("logs.txt", "START CHECK LOGS\n", FILE_APPEND); if ($datas['status'] == 10000) { $logs = $datas['data']; $infomation = []; $fault_information = []; $count = 0; foreach ($logs as $key => $value) { $count++; $start = $this->getCurrentTime(); $result = true; if ($value['action'] == "insert_image") $result = $this->insertImage($value['images'][0], $value['id'], $value['files_name'][0]); if ($value['action'] == "remove_image") $result = $this->removeImage($value['id'], $value['files_name'][0]); if ($value['action'] == "insert_info") $result = $this->insertInfo($value); if ($value['action'] == "remove_info") $result = $this->removeInfo($value); if ($value['action'] == "reset_all") { \Yii::$app->db->createCommand()->truncateTable('capture_logs')->execute(); \Yii::$app->db->createCommand()->truncateTable('list_management')->execute(); array_map('unlink', glob("/var/www/html/BiFace_Server_Lite/web/data/uploads/face/*.*")); } $finish = $this->getCurrentTime(); $processTime = round(($finish - $start), 4); if (Yii::$app->params['autoSyncLog']) file_put_contents("logs.txt", $count . "\t" . $value['action'] . "\t" . $value['files_name'][0] . "\t" . ($result ? "true" : "false") . "\t" . $processTime . "\t" . $value['name'] . "\n", FILE_APPEND); if (in_array($value['action'], ['insert_image', 'remove_image', 'insert_info', 'remove_info', 'reset_all'])) { if ($result) $infomation[] = [ "obj_Log" => $value['obj_Log'], "action" => $value['action'], "idCamera" => $id_camera ]; else $fault_information[] = [ "obj_Log" => $value['obj_Log'], "action" => $value['action'], "idCamera" => $id_camera ]; } } if (count($infomation) > 0) file_get_contents($ip . "/api/model/set_log_model", false, stream_context_create([ 'http' => [ 'header' => "Content-Type: application/json", 'method' => "POST", 'content' => json_encode([ "infomation" => $infomation, "fault_information" => $fault_information ]) ] ])); @file_get_contents("http://localhost:2305/update-feature?total=" . ListManagement::find()->count()); } $logProcessStatus->data = "false"; $logProcessStatus->save(); return ["data" => "check logs"]; } public function insertImage($image, $staff_id, $files_name) { $model = ListManagement::findOne(['staff_id' => $staff_id]); if (!$model) return false; $RootFolder = Yii::getAlias('@webroot') . "/data/uploads"; $targetPath = $RootFolder . "/face"; FileHelper::createDirectory($targetPath, 0777); $ft = []; $currentArr = []; $currentImg = json_decode($model->image, true); if ($currentImg) { foreach ($currentImg as $key => $value) { if (isset($value['serverKey'])) $currentArr[] = $value['serverKey']; } $ft = $currentImg; } if (!in_array($files_name, $currentArr)) { $fileName = "face_" . $staff_id . "_" . common::generateRandomString() . "_" . time() . ".png"; $img = false; try { $img = file_get_contents(str_replace("&", "&", $image)); } catch (\Exception $e) { } if ($img && $img !== "null") { $fileTarget = $targetPath . "/" . $fileName; if (!$this->resizeImg($img, $fileTarget)) { file_put_contents($fileTarget, $img); } $features = common::requestToEngine("/get-feature", [ "image_paths" => [ ["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $fileName, "type" => "raw"] ], "type" => "128" ]); if (!$features['status']) { if (Yii::$app->params['autoSyncLog']) file_put_contents("logs.txt", "[ERROR] extract feature " . json_encode($features) . "\n", FILE_APPEND); return false; } $features = json_decode($features['data'], true); $ft[] = [ "serverKey" => $files_name, "url" => $fileName, "urlOld" => $fileName, "features" => $features['results'][0]['feature'], "features512" => [] ]; $model->image = json_encode($ft); $model->last_modified = time(); return $model->save(); } else { if (Yii::$app->params['autoSyncLog']) file_put_contents("logs.txt", "[ERROR] image error" . $image . "\n", FILE_APPEND); return false; } } else { if (Yii::$app->params['autoSyncLog']) file_put_contents("logs.txt", "[ERROR] file exists" . $files_name . "\n", FILE_APPEND); return false; } } public function removeImage($staff_id, $files_name) { $model = ListManagement::findOne(['staff_id' => $staff_id]); if (!$model) return false; $images = json_decode($model->image, true); $ft = []; foreach ($images as $key => $value) { if (isset($value['serverKey']) && $value['serverKey'] !== $files_name) $ft[] = $value; else @unlink("/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $value['url']); } $model->image = json_encode($ft); $model->last_modified = time(); return $model->save(); } public function insertInfo($data) { $model = ListManagement::findOne(['staff_id' => $data['id']]); if ($model) { $model->name = $data['name']; $model->abbreviated_name = $data['abbreviated_name']; $model->code = $data['code']; $model->address = $data['department']; $model->last_modified = time(); $model->save(); } else { $model = new ListManagement(); $model->create([ 'staff_id' => $data['id'], 'code' => $data['code'], 'type' => 'wl', 'name' => $data['name'], 'image' => json_encode([]), 'gender' => "Male", 'birthday' => "", 'telephone' => "", 'address' => $data['department'], 'abbreviated_name' => $data['abbreviated_name'], ]); } return true; } public function removeInfo($data) { $model = ListManagement::findOne(['staff_id' => $data['id']]); if ($model) { $images = json_decode($model->image, true); foreach ($images as $k => $v) { @unlink("/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $v['url']); } $model->delete(); } return true; } public function resizeImg($img, $fileTarget) { $im = imagecreatefromstring($img); $width = imagesx($im); $height = imagesy($im); $newwidth = 224; $newheight = 224; if ($width > $newwidth && $height > $newheight) { $thumb = imagecreatetruecolor($newwidth, $newheight); imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); imagejpeg($thumb, $fileTarget); imagedestroy($thumb); imagedestroy($im); return true; } return false; } }