|
@@ -30,18 +30,27 @@ class BxOpencvModule extends BxDolModule
|
|
|
|
|
|
|
|
public function serviceProcessImage($sFilePath, $sExt = 'jpg')
|
|
public function serviceProcessImage($sFilePath, $sExt = 'jpg')
|
|
|
{
|
|
{
|
|
|
|
|
+ $sImgSmilePath = BX_DIRECTORY_PATH_MODULES . 'boonex/opencv/template/images/Emojis-01.png';
|
|
|
|
|
+ $bBlur = 'blur' == getParam('bx_opencv_option_obfuscation_method');
|
|
|
$src = imread($sFilePath);
|
|
$src = imread($sFilePath);
|
|
|
- $blr = null;
|
|
|
|
|
|
|
+ $src2 = null;
|
|
|
|
|
|
|
|
$netDet = CV\FaceDetectorYN::create(BX_DIRECTORY_PATH_MODULES . 'boonex/opencv/models/opencv_zoo/face_detection_yunet_2022mar.onnx', '', $src->size());
|
|
$netDet = CV\FaceDetectorYN::create(BX_DIRECTORY_PATH_MODULES . 'boonex/opencv/models/opencv_zoo/face_detection_yunet_2022mar.onnx', '', $src->size());
|
|
|
|
|
|
|
|
|
|
+ // detect faces
|
|
|
if ($r = $netDet->detect($src)) {
|
|
if ($r = $netDet->detect($src)) {
|
|
|
- $dst = $src->clone();
|
|
|
|
|
- medianBlur($src, $dst, $this->_blurFactor($src->cols));
|
|
|
|
|
|
|
+ // prepare some data
|
|
|
|
|
+ if ($bBlur) {
|
|
|
|
|
+ $dst = $src->clone();
|
|
|
|
|
+ medianBlur($src, $dst, $this->_blurFactor($src->cols));
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ $src2 = BxDolImageResize::getInstance()->getManager()->make($sFilePath);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // obfuscate
|
|
|
$fConfidence = (float)getParam('bx_opencv_option_confidence');
|
|
$fConfidence = (float)getParam('bx_opencv_option_confidence');
|
|
|
-
|
|
|
|
|
$faces = [];
|
|
$faces = [];
|
|
|
$bDetection = false;
|
|
$bDetection = false;
|
|
|
for ($i = 0; $i < $r->rows; $i++) {
|
|
for ($i = 0; $i < $r->rows; $i++) {
|
|
@@ -53,21 +62,36 @@ class BxOpencvModule extends BxDolModule
|
|
|
$w = $r->atIdx([$i,2]);
|
|
$w = $r->atIdx([$i,2]);
|
|
|
$h = $r->atIdx([$i,3]);
|
|
$h = $r->atIdx([$i,3]);
|
|
|
|
|
|
|
|
- $this->_blur($src, $dst, (int)$startX, (int)$startY, (int)$w, (int)$h);
|
|
|
|
|
|
|
+ if ($bBlur) {
|
|
|
|
|
+ $this->_blur($src, $dst, (int)$startX, (int)$startY, (int)$w, (int)$h);
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ $img = BxDolImageResize::getInstance()->getManager()->make($sImgSmilePath);
|
|
|
|
|
+ $x = (int)(1.3 * max($w, $h));
|
|
|
|
|
+ $dx = (int)(($x - max($w, $h)) / 2.0);
|
|
|
|
|
+ $img->resize($x, $x);
|
|
|
|
|
+ $src2->insert($img, 'top-left', (int)$startX - $dx, (int)$startY - $dx);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
$bDetection = true;
|
|
$bDetection = true;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // save resulted image
|
|
|
if ($bDetection) {
|
|
if ($bDetection) {
|
|
|
- $sExtReal = pathinfo($sFilePath, PATHINFO_EXTENSION);
|
|
|
|
|
- if ($sExtReal) {
|
|
|
|
|
- imwrite($sFilePath, $src);
|
|
|
|
|
|
|
+ if ($bBlur) {
|
|
|
|
|
+ $sExtReal = pathinfo($sFilePath, PATHINFO_EXTENSION);
|
|
|
|
|
+ if ($sExtReal) {
|
|
|
|
|
+ imwrite($sFilePath, $src);
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ imwrite($sFilePath . '.' . $sExt, $src);
|
|
|
|
|
+ if (file_exists($sFilePath . '.' . $sExt) && unlink($sFilePath))
|
|
|
|
|
+ rename($sFilePath . '.' . $sExt, $sFilePath);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
else {
|
|
else {
|
|
|
- imwrite($sFilePath . '.' . $sExt, $src);
|
|
|
|
|
- if (file_exists($sFilePath . '.' . $sExt) && unlink($sFilePath))
|
|
|
|
|
- rename($sFilePath . '.' . $sExt, $sFilePath);
|
|
|
|
|
|
|
+ $src2->save($sFilePath);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|