1
0

BxOpencvModule.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup OpenCV OpenCV integration
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. use CV\Scalar;
  12. use function CV\{imread, imwrite, rectangle, medianBlur, addWeighted};
  13. class BxOpencvModule extends BxDolModule
  14. {
  15. function __construct(&$aModule)
  16. {
  17. parent::__construct($aModule);
  18. }
  19. public function serviceGetStorages()
  20. {
  21. $aStorageObjects = BxDolStorageQuery::getStorageObjects();
  22. $a = [];
  23. foreach ($aStorageObjects as $r)
  24. $a[$r['object']] = $r['object'];
  25. return $a;
  26. }
  27. public function serviceProcessImage($sFilePath, $sExt = 'jpg')
  28. {
  29. $src = imread($sFilePath);
  30. $blr = null;
  31. $netDet = CV\FaceDetectorYN::create(BX_DIRECTORY_PATH_MODULES . 'boonex/opencv/models/opencv_zoo/face_detection_yunet_2022mar.onnx', '', $src->size());
  32. if ($r = $netDet->detect($src)) {
  33. $dst = $src->clone();
  34. medianBlur($src, $dst, $this->_blurFactor($src->cols));
  35. }
  36. $fConfidence = (float)getParam('bx_opencv_option_confidence');
  37. $faces = [];
  38. $bDetection = false;
  39. for ($i = 0; $i < $r->rows; $i++) {
  40. $confidence = $r->atIdx([$i,14]);
  41. if ($confidence > $fConfidence) {
  42. $startX = $r->atIdx([$i,0]);
  43. $startY = $r->atIdx([$i,1]);
  44. $w = $r->atIdx([$i,2]);
  45. $h = $r->atIdx([$i,3]);
  46. $this->_blur($src, $dst, (int)$startX, (int)$startY, (int)$w, (int)$h);
  47. $bDetection = true;
  48. }
  49. }
  50. if ($bDetection) {
  51. $sExtReal = pathinfo($sFilePath, PATHINFO_EXTENSION);
  52. if ($sExtReal) {
  53. imwrite($sFilePath, $src);
  54. }
  55. else {
  56. imwrite($sFilePath . '.' . $sExt, $src);
  57. if (file_exists($sFilePath . '.' . $sExt) && unlink($sFilePath))
  58. rename($sFilePath . '.' . $sExt, $sFilePath);
  59. }
  60. }
  61. }
  62. private function _blur(&$src, $dst, $x, $y, $w, $h)
  63. {
  64. $iChannels = $src->channels();
  65. for ($j = 1; $j < $src->rows - 1; ++$j) {
  66. for ($i = 1; $i < $src->cols - 1; ++$i) {
  67. for ($ch = 0; $ch < $iChannels; $ch++) {
  68. if ($i >= $x && $i <= $x + $w && $j >= $y && $j <= $y + $h) {
  69. $val = $dst->at($j, $i, $ch);
  70. $src->at($j, $i, $ch, $val);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. private function _blurFactor($inputValue) {
  77. // Ensure the input is within a reasonable range
  78. $inputValue = max(1, $inputValue);
  79. // Scale the logarithmic value to cover the desired output range
  80. $scaledValue = log($inputValue/1000) / log(5000);
  81. // Map the scaled value to the desired output range
  82. $outputRange = 50 - 19;
  83. $scaledOutput = round($scaledValue * $outputRange);
  84. // Adjust the output to be odd
  85. $outputValue = 19 + ($scaledOutput % 2 === 0 ? 1 : 0) + $scaledOutput;
  86. return 1 + 2*$outputValue;
  87. }
  88. }
  89. /** @} */