1 Star 1 Fork 2

带刀医生/php-opencv

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
bak_detect_face_by_dnn_ssd.php_bak 4.60 KB
一键复制 编辑 原始数据 按行查看 历史
yangquanliang 提交于 2020-11-23 10:22 . 初始化php-opencv
<?php
use CV\Scalar, CV\Size;
use function CV\{imread, imwrite, rectangle};
$imgname = 'doctor';
$filetype = '.jpg';
$imgfile = $imgname.$filetype;
$src = imread("images/".$imgfile);
$size = $src->size(); // 2000x500
$minSide = min($size->width, $size->height);
$divider = $minSide / 300;
\CV\resize($src, $resized, new Size($size->width / $divider, $size->height / $divider)); // 1200x300
$blob = \CV\DNN\blobFromImage($resized, 1, new Size(), new Scalar(104, 177, 123), true, false);
$net = \CV\DNN\readNetFromCaffe('models/ssd/res10_300x300_ssd_deploy.prototxt', 'models/ssd/res10_300x300_ssd_iter_140000.caffemodel');
$net->setInput($blob, "");
$r = $net->forward();
var_export($r->shape);
$width = 413;
$height = 626;
// $scalar = new Scalar(0, 0, 255);
// $scalar = new Scalar(255, 0, 0);
$scalar = new Scalar(0, 253, 255);
$scalarCut = new Scalar(253, 95, 47);
for ($i = 0; $i < $r->shape[2]; $i++) {
$confidence = $r->atIdx([0,0,$i,2]);
if ($confidence > 0.9) {
var_export($confidence);echo "\n";
$startX = $r->atIdx([0,0,$i,3]) * $src->cols;##宽X
$startY = $r->atIdx([0,0,$i,4]) * $src->rows;##高Y
$endX = $r->atIdx([0,0,$i,5]) * $src->cols;##w
$endY = $r->atIdx([0,0,$i,6]) * $src->rows;##h
##裁切外边框
// $zone = [];
// $a = $startX;//宽
// $b = $startY;//高
// $offSetLeft = $a/4;//x偏移
// $offSetTop = $b*0.5;
// $zone['x'] = $endX-$offSetLeft;
// $zone['y'] = $endY-$offSetTop;
// $zone['w'] = $a/4 *2+$a;
// $zone['h'] = $zone['w']*($height*1.0/$width);
// $cutX = $zone['x'];
// $cutY = $zone['y'];
// $cutEndX = $zone['w'];
// $cutEndY = $zone['h'];
// $cutX = $startX-275;
// $cutY = $startY-200;
// $cutEndX = $endX+413;
// $cutEndY = $endY+626;
$cutX = $startX/1.5;
$cutY = $startY/1.5;
$cutEndX = $endX+413;
$cutEndY = $endY+626;
// Rect zone;
// int a= faceRect.width;//宽
// int b= faceRect.height;//高
// int offSetLeft=a/4;//x偏移
// int offSetTop=b*0.5;
// zone.x=faceRect.x-offSetLeft;
// zone.y=faceRect.y-offSetTop;
// zone.width= a/4 *2+a;
// zone.height=zone.width*(height*1.0/width);
rectangle($src, $startX, $startY, $endX, $endY, $scalar, 3);
rectangle($src, $cutX, $cutY, $cutEndX, $cutEndY, $scalarCut, 5);
// Mat srcMat;//图片源矩阵
// bitmap2Mat(env, bitmap, &srcMat);//图片源矩阵初始化
// auto faces = FaceDetector::detectorFace(srcMat);//识别图片源矩阵,返回矩形集
// Rect faceRect= faces[0];
// rectangle(srcMat, faceRect, Scalar(0, 253, 255), 5);//在srcMat上画矩形
// //识别目标区域区域---------------------------
// Rect zone;
// int a= faceRect.width;//宽
// int b= faceRect.height;//高
// int offSetLeft=a/4;//x偏移
// int offSetTop=b*0.5;
// zone.x=faceRect.x-offSetLeft;
// zone.y=faceRect.y-offSetTop;
// zone.width= a/4 *2+a;
// zone.height=zone.width*(height*1.0/width);
// rectangle(srcMat, zone, Scalar(253, 95, 47), 5);//在srcMat上画矩
}
}
$data = [];
$time = date("Y-mdHis");
$resImg = $imgname.$time.$filetype;
var_export($resImg);echo "\n";
imwrite("results/".$resImg, $src);
// function GetImageRect($IplImage, $orgImage, $CvRect,$rectInImage, $IplImage,$imgRect,$scale)
// {
// //从图像orgImage中提取一块(rectInImage)子图像imgRect
// IplImage *result=imgRect;
// CvRect size;
// size.x=rectInImage.x*scale;
// size.y=rectInImage.y*scale;
// size.width=rectInImage.width*scale;
// size.height=rectInImage.height*scale;
// //result=cvCreateImage( size, orgImage->depth, orgImage->nChannels );
// //从图像中提取子图像
// cvSetImageROI(orgImage,size);
// cvCopy(orgImage,result);
// cvResetImageROI(orgImage);
// }
// void GetImageRect(IplImage* orgImage, CvRect rectInImage, IplImage* imgRect,double scale)
// {
// //从图像orgImage中提取一块(rectInImage)子图像imgRect
// IplImage *result=imgRect;
// CvRect size;
// size.x=rectInImage.x*scale;
// size.y=rectInImage.y*scale;
// size.width=rectInImage.width*scale;
// size.height=rectInImage.height*scale;
// //result=cvCreateImage( size, orgImage->depth, orgImage->nChannels );
// //从图像中提取子图像
// cvSetImageROI(orgImage,size);
// cvCopy(orgImage,result);
// cvResetImageROI(orgImage);
// }
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/mycharm/php-opencv.git
git@gitee.com:mycharm/php-opencv.git
mycharm
php-opencv
php-opencv
master

搜索帮助