` 本帖最后由 moonyuan 于 2019-9-3 00:48 编辑
在很多场合, 例如节能建筑 智能会议室 需要人走关灯
在传统的PIR红外识别,误触发/不触发概率高,使用摄像头识别比传统方式可靠
那么接下来实现利用opencv 识别当前可视范围的是否存在人类
首先加载caffe网络模型
- net = dnn::readNetFromCaffe("./MobileNetSSD_deploy.prototxt.txt", "./MobileNetSSD_deploy.caffemodel");
复制代码
读取图片 进行灰度转换,因为模型训练时 使用300*300,所以图片需要重新resize
- image = imread("messi.jpg");
- Mat gray;
- cvtColor(image,gray,CV_BGR2GRAY);
- resize(gray, gray, Size(300, 300));
复制代码
将blob输入到网络,并向前推进,
- Mat blob;
- blob = blobFromImage(result,0.007843, Size(300, 300), 127.5);
- net.setinput(blob);
- Mat detections;
- detections = net.forward();
- Mat detectionMat(detections.size[2], detections.size[3], CV_32F, detections.ptr());
- for (int i = 0; i < detectionMat.rows; i++){
- float confidence = detectionMat.at(i, 2);
- if (confidence > 0.1){
- size_t objectClass = (size_t)(detectionMat.at(i, 1));
- printf("n current image classification : %s, possible : %.2f
- ", CLASSES[objectClass].c_str(), confidence);
-
- if((confidence <= 0.1) || strcmp("person",CLASSES[objectClass].c_str())){
-
- continue;
- }
- int xLeftBottom = static_cast(detectionMat.at(i, 3) * gray.cols);
- int yLeftBottom = static_cast(detectionMat.at(i, 4) * gray.rows);
- int xRightTop = static_cast(detectionMat.at(i, 5) * gray.cols);
- int yRightTop = static_cast(detectionMat.at(i, 6) * gray.rows);
-
- ostringstream ss;
- ss << confidence;
- String conf(ss.str());
- Rect object((int)xLeftBottom, (int)yLeftBottom,
- (int)(xRightTop - xLeftBottom),
- (int)(yRightTop - yLeftBottom));
- rectangle(result, object, Scalar(0, 255, 0));
- String label = String(CLASSES[objectClass]) + ": " + conf;
- int baseLine = 0;
- Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
- rectangle(result, Rect(Point(xLeftBottom, yLeftBottom - labelSize.height),
- Size(labelSize.width, labelSize.height + baseLine)),
- Scalar(255, 255, 255), CV_FILLED);
- putText(result, label, Point(xLeftBottom, yLeftBottom),
- FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));
- }
- }
复制代码
最后把图像输出
我们看到图像的认为可以没独立识别出来
再识别一下其他的图
输出结果
结果都是能够识别到 94%以上的相似率,
后续经过修改定可以放到指定的场景稳定准确的工作
`
0
|
|
|
|