From b26814bb97a2039bb12fd86586bd3582f07c9f80 Mon Sep 17 00:00:00 2001 From: dongpd Date: Thu, 24 Dec 2020 13:57:24 +0700 Subject: [PATCH] update button add feature - control logs site --- controllers/ControlLogsController.php | 66 ++++++++++++++++++++++++ controllers/ListManagementController.php | 2 +- helpers/CaptureLogsGrid.php | 20 +++++-- views/control-logs/index.tpl | 9 +++- web/js/control-logs.js | 25 ++++++++- web/js/list-management.js | 6 ++- 6 files changed, 120 insertions(+), 8 deletions(-) diff --git a/controllers/ControlLogsController.php b/controllers/ControlLogsController.php index 1a8bebc3..1973583a 100644 --- a/controllers/ControlLogsController.php +++ b/controllers/ControlLogsController.php @@ -9,6 +9,8 @@ use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use app\models\ListManagement; +use yii\helpers\FileHelper; +use app\models\common; /** * CaptureLogsController implements the CRUD actions for CaptureLogs model. @@ -68,4 +70,68 @@ class ControlLogsController extends Controller { throw new NotFoundHttpException('The requested page does not exist.'); } + public function actionUseFeature($id) { + if (Yii::$app->request->isAjax) { + Yii::$app->response->format = "json"; + $model = $this->findModel($id); + $listManagement = ListManagement::findOne($model->staff_id); + if ($listManagement) { + $url = $model->image; + $images = json_decode($listManagement->image, true); + if (count($images) >= \Yii::$app->params['maxPicture']) + return ["status" => false, "text" => "Mỗi đối tượng chỉ nhận tối đa " . \Yii::$app->params['maxPicture'] . " hình ảnh mẫu"]; + $add = true; + foreach ($images as $key => $value) { + if ($value['urlOld'] === $url) + $add = false; + } + if ($add) { + $RootFolder = Yii::getAlias('@webroot') . "/data/uploads"; + $targetPath = $RootFolder . "/face"; + FileHelper::createDirectory($targetPath, 0777); + $fileName = "face_" . common::generateRandomString() . "_" . time() . ".png"; + $img = file_get_contents("http://localhost/data/uploads/face/" . $url); + $fileTarget = $targetPath . "/" . $fileName; + if (!$this->resizeImg($img, $fileTarget)) + $fileName = $url; + + $features = json_decode(common::requestToEngine("/get-feature", [ + "image_paths" => [ + ["url" => "/var/www/html/BiFace_Server_Lite/web/data/uploads/face/" . $fileName, "type" => "raw"] + ] + ]), true); + $images[] = [ + "url" => $fileName, + "urlOld" => $url, + "features" => $features['results'][0]['feature'], + "features512" => isset($features['results'][0]['feature512']) ? $features['results'][0]['feature512'] : [] + ]; + $listManagement->image = json_encode($images); + $listManagement->save(); + file_get_contents("http://localhost:2305/update-feature"); + return ["status" => true]; + } + return ["status" => false, "text" => "Hình ảnh này đã được chọn làm mẫu cho đối tượng này!"]; + } + return ["status" => false, "text" => "Đối tượng không tồn tại!"]; + } + } + + 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; + } + } diff --git a/controllers/ListManagementController.php b/controllers/ListManagementController.php index db647734..f25ca372 100644 --- a/controllers/ListManagementController.php +++ b/controllers/ListManagementController.php @@ -585,7 +585,7 @@ class ListManagementController extends Controller { ]) ] ])), true); - return ["status" => true]; + return $res; //["status" => true]; } } diff --git a/helpers/CaptureLogsGrid.php b/helpers/CaptureLogsGrid.php index 9a03840f..f564b9f7 100644 --- a/helpers/CaptureLogsGrid.php +++ b/helpers/CaptureLogsGrid.php @@ -34,12 +34,24 @@ class CaptureLogsGrid { }; } - public static function image() { - return function($model) { - return Html::img("/data/uploads/face/" . $model->image, [ + public static function image($process = false) { + return function($model) use ($process) { + $btn = ""; + if ($process) + $btn = Html::button("", [ + "class" => "btn btn-info btn-xs", + "style" => "position:absolute;top:0;right:0;visibility: hidden;", + "data" => [ + "toggle" => "tooltip", + "href" => Url::to(["/control-logs/use-feature", "id" => $model->id]) + ], + "title" => "Chuyển làm ảnh mẫu", + "onclick" => "_useFeature(this);" + ]); + return "
" . Html::img("/data/uploads/face/" . $model->image, [ "class" => "img-thumbnail", "style" => "width: 150px;height:150px;" - ]); + ]) . $btn . "
"; }; } diff --git a/views/control-logs/index.tpl b/views/control-logs/index.tpl index 9b8e2b55..bcbc0a02 100644 --- a/views/control-logs/index.tpl +++ b/views/control-logs/index.tpl @@ -14,6 +14,13 @@ .table-striped > tbody > tr:nth-of-type(odd){ background-color: rgb(210, 210, 210); } + .feature-img{ + display:inline-block; + position: relative; + } + .feature-img:hover .btn{ + visibility: visible !important; + }
@@ -49,7 +56,7 @@ 'format' => "raw", 'contentOptions' => ['class' => 'text-center'], 'headerOptions' => ['class' => 'text-center', 'style' => 'width:15%'], - 'value' => \app\helpers\CaptureLogsGrid::image() + 'value' => \app\helpers\CaptureLogsGrid::image(true) ], [ 'attribute' => 'confidence', diff --git a/web/js/control-logs.js b/web/js/control-logs.js index 637cbb57..9713f1db 100644 --- a/web/js/control-logs.js +++ b/web/js/control-logs.js @@ -8,4 +8,27 @@ function _search(e) { location = location + "&type=" + $("select[name='TypeSearch']").val(); location = location + "&gender=" + $("select[name='GenderSearch']").val(); window.location = location; -} \ No newline at end of file +} + +function _useFeature(e) { + common.modalBlock(true); + $.ajax({ + url: $(e).attr("data-href"), + type: 'POST', + data: { + id: $(e).val() + }, + success: function (data) { + common.modalBlock(false); + if (data.status) { + alert("Đã thêm hình ảnh mẫu!"); + } else { + alert(data.text); + } + }, + error: function (jqXHR, textStatus, errorThrown) { + common.modalBlock(false); + common.ajaxError(); + } + }); +} diff --git a/web/js/list-management.js b/web/js/list-management.js index b18cd22d..dafc3d37 100644 --- a/web/js/list-management.js +++ b/web/js/list-management.js @@ -525,7 +525,7 @@ function _syncToServer() { syncFeatureToServer(lists[i]); } } - +var error = 0; function syncFeatureToServer(id) { $.ajax({ url: $("input[name='sync_feature_to_server_url']").val(), @@ -534,6 +534,10 @@ function syncFeatureToServer(id) { id: id }, success: function (data) { + if (data.status != 10000) { + error++; + console.log(error); + } progressToServer++; $("#progress-current").html(parseInt($("#progress-current").html()) + 1); var percent = parseInt(progressToServer / totalsToServer * 100);