update button add feature - control logs site

This commit is contained in:
dongpd 2020-12-24 13:57:24 +07:00
parent 492651570e
commit b26814bb97
6 changed files with 120 additions and 8 deletions

View File

@ -9,6 +9,8 @@ use yii\web\Controller;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter; use yii\filters\VerbFilter;
use app\models\ListManagement; use app\models\ListManagement;
use yii\helpers\FileHelper;
use app\models\common;
/** /**
* CaptureLogsController implements the CRUD actions for CaptureLogs model. * 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.'); 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;
}
} }

View File

@ -585,7 +585,7 @@ class ListManagementController extends Controller {
]) ])
] ]
])), true); ])), true);
return ["status" => true]; return $res; //["status" => true];
} }
} }

View File

@ -34,12 +34,24 @@ class CaptureLogsGrid {
}; };
} }
public static function image() { public static function image($process = false) {
return function($model) { return function($model) use ($process) {
return Html::img("/data/uploads/face/" . $model->image, [ $btn = "";
if ($process)
$btn = Html::button("<i class='fa fa-reply'></i>", [
"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 "<div class='feature-img'>" . Html::img("/data/uploads/face/" . $model->image, [
"class" => "img-thumbnail", "class" => "img-thumbnail",
"style" => "width: 150px;height:150px;" "style" => "width: 150px;height:150px;"
]); ]) . $btn . "</div>";
}; };
} }

View File

@ -14,6 +14,13 @@
.table-striped > tbody > tr:nth-of-type(odd){ .table-striped > tbody > tr:nth-of-type(odd){
background-color: rgb(210, 210, 210); background-color: rgb(210, 210, 210);
} }
.feature-img{
display:inline-block;
position: relative;
}
.feature-img:hover .btn{
visibility: visible !important;
}
</style> </style>
<div class="row"> <div class="row">
<div class="col-md-10" style="max-height: 850px;overflow-y: auto;"> <div class="col-md-10" style="max-height: 850px;overflow-y: auto;">
@ -49,7 +56,7 @@
'format' => "raw", 'format' => "raw",
'contentOptions' => ['class' => 'text-center'], 'contentOptions' => ['class' => 'text-center'],
'headerOptions' => ['class' => 'text-center', 'style' => 'width:15%'], 'headerOptions' => ['class' => 'text-center', 'style' => 'width:15%'],
'value' => \app\helpers\CaptureLogsGrid::image() 'value' => \app\helpers\CaptureLogsGrid::image(true)
], ],
[ [
'attribute' => 'confidence', 'attribute' => 'confidence',

View File

@ -9,3 +9,26 @@ function _search(e) {
location = location + "&gender=" + $("select[name='GenderSearch']").val(); location = location + "&gender=" + $("select[name='GenderSearch']").val();
window.location = location; window.location = location;
} }
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();
}
});
}

View File

@ -525,7 +525,7 @@ function _syncToServer() {
syncFeatureToServer(lists[i]); syncFeatureToServer(lists[i]);
} }
} }
var error = 0;
function syncFeatureToServer(id) { function syncFeatureToServer(id) {
$.ajax({ $.ajax({
url: $("input[name='sync_feature_to_server_url']").val(), url: $("input[name='sync_feature_to_server_url']").val(),
@ -534,6 +534,10 @@ function syncFeatureToServer(id) {
id: id id: id
}, },
success: function (data) { success: function (data) {
if (data.status != 10000) {
error++;
console.log(error);
}
progressToServer++; progressToServer++;
$("#progress-current").html(parseInt($("#progress-current").html()) + 1); $("#progress-current").html(parseInt($("#progress-current").html()) + 1);
var percent = parseInt(progressToServer / totalsToServer * 100); var percent = parseInt(progressToServer / totalsToServer * 100);