Просмотр исходного кода

#4625 opencv integration: different obfuscation method

Alex Trofimov 2 лет назад
Родитель
Сommit
d28cc24b76

+ 35 - 11
modules/boonex/opencv/classes/BxOpencvModule.php

@@ -30,18 +30,27 @@ class BxOpencvModule extends BxDolModule
 
     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);
-        $blr = null;
+        $src2 = null;
 
         $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)) {
-            $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');
-
         $faces = [];
         $bDetection = false;
         for ($i = 0; $i < $r->rows; $i++) {
@@ -53,21 +62,36 @@ class BxOpencvModule extends BxDolModule
                 $w = $r->atIdx([$i,2]);
                 $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;
             }
         }
 
+        // save resulted image
         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 {
-                imwrite($sFilePath . '.' . $sExt, $src);
-                if (file_exists($sFilePath . '.' . $sExt) && unlink($sFilePath))
-                    rename($sFilePath . '.' . $sExt, $sFilePath);
+                $src2->save($sFilePath);
             }
         }
     }

+ 1 - 0
modules/boonex/opencv/install/langs/en.xml

@@ -8,6 +8,7 @@
     <string name="_bx_opencv_adm_stg_cpt_category_general"><![CDATA[General]]></string>
     <string name="_bx_opencv_option_confidence"><![CDATA[Detection confidence threshold]]></string>
     <string name="_bx_opencv_option_storages"><![CDATA[Enable for storages]]></string>
+    <string name="_bx_opencv_option_obfuscation_method"><![CDATA[Obfuscation method]]></string>
 
 	<string name="_bx_opencv_information"><![CDATA[Information]]></string>
     <string name="_bx_opencv_information_block"><![CDATA[

+ 2 - 1
modules/boonex/opencv/install/sql/enable.sql

@@ -12,7 +12,8 @@ SET @iCategId = LAST_INSERT_ID();
 
 INSERT INTO `sys_options` (`name`, `value`, `category_id`, `caption`, `type`, `extra`, `check`, `check_error`, `order`) VALUES
 ('bx_opencv_option_confidence', '0.9', @iCategId, '_bx_opencv_option_confidence', 'digit', '', '', '', 10),
-('bx_opencv_option_storages', '', @iCategId, '_bx_opencv_option_storages', 'list', 'a:2:{s:6:"module";s:9:"bx_opencv";s:6:"method";s:12:"get_storages";}', '', '', 20);
+('bx_opencv_option_storages', '', @iCategId, '_bx_opencv_option_storages', 'list', 'a:2:{s:6:"module";s:9:"bx_opencv";s:6:"method";s:12:"get_storages";}', '', '', 20),
+('bx_opencv_option_obfuscation_method', 'emoji', @iCategId, '_bx_opencv_option_obfuscation_method', 'select', 'emoji,blur', '', '', 30);
 
 -- Alerts
 

BIN
modules/boonex/opencv/template/images/Emojis-01.png