• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

关于python:Opencv-C-调用-tensorflow-模型caffe模型

python 搞代码 3年前 (2022-02-20) 27次浏览 已收录 0个评论

include<opencv2\opencv.hpp>

include<opencv2\dnn.hpp>

include <iostream>

include<map>

include<string>

include<time.h>

using namespace std;
using namespace cv;
const char* classNames[]= {“background”, “person”, “bicycle”, “car”, “motorcycle”, “airplane”, “bus”, “train”, “truck”, “boat”, “traffic light”,
“fire hydrant”, “background”, “stop sign”, “parking meter”, “bench”, “bird”, “cat”, “dog”, “horse”, “sheep”, “cow”, “elephant”, “bear”, “zebra”, “giraffe”, “background”, “backpack”,
“umbrella”, “background”, “background”, “handbag”, “tie”, “suitcase”, “frisbee”,”skis”, “snowboard”, “sports ball”, “kite”, “baseball bat”,”baseball glove”, “skateboard”, “surfboard”, “tennis racket”,
“bottle”, “background”, “wine glass”, “cup”, “fork”, “knife”, “spoon”,”bowl”, “banana”, “apple”, “sandwich”, “orange”,”broccoli”, “carrot”, “hot dog”, “pizza”, “donut”,
“cake”, “chair”, “couch”, “potted plant”, “bed”, “background”, “dining table”, “background”, “background”, “toilet”, “background”,”tv”, “laptop”, “mouse”, “remote”, “keyboard”,
“cell phone”, “microwave”, “oven”, “toaster”, “sink”, “refrigerator”, “background”,”book”, “clock”, “vase”, “scissors”,”teddy bear”, “hair drier”, “toothbrush”};
int main()
{

String weights = "models/frozen_inference_graph.pb";
String prototxt = "models/ssd_mobilenet_v1_coco.pbtxt";
const size_t width = 300;
const size_t height = 300;
VideoCapture capture;
capture.open(0);
namedWindow("input", CV_WINDOW_AUTOSIZE);
int w = capture.get(CAP_PROP_FRAME_WIDTH);
int h = capture.get(CAP_PROP_FRAME_HEIGHT);
printf("frame width : %d, frame height : %d", w, h);
// set up net
dnn::Net net = cv::dnn::readNetFromTensorflow(weights, prototxt);
Mat frame;
/*
while (1)    // 模式1:测试单张图像
{
    frame = imread("models/car.jpg");
    imshow("input", frame);
*/
while (capture.read(frame))        // 模式2:调用摄像头
{
    //预测
    cv::Mat inputblob = cv::dnn::blobFromImage(frame, 1. / 255, Size(width, height));
    net.setInput(inputblob);
    Mat output = net.forward();
    //检测
    Mat detectionMat(output.size[2], output.size[3], CV_32F, output.ptr<float>());
    float confidence_threshold = 0.5;
    for (int i = 0; i < detectionMat.rows; i++) {
        float confidence = [Skrill下载](https://www.gendan5.com/wallet/Skrill.html)detectionMat.at<float>(i, 2);
        if (confidence > confidence_threshold) {
            size_t objIndex = (size_t)(detectionMat.at<float>(i, 1));
            float tl_x = detectionMat.at<float>(i, 3) * frame.cols;
            float tl_y = detectionMat.at<float>(i, 4) * frame.rows;
            float br_x = detectionMat.at<float>(i, 5) * frame.cols;
            float br_y = detectionMat.at<float>(i, 6) * frame.rows;
            Rect object_box((int)tl_x, (int)tl_y, (int)(br_x - tl_x), (int)(br_y - tl_y));
            rectangle(frame, object_box, Scalar(0, 255, 0), 2, 8, 0);
            putText(frame, format("%s", classNames[objIndex]), Point(tl_x, tl_y), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 0), 2);
        }
    }
    imshow("ssd-video-demo", frame);
    char c = waitKey(5);
    if (c == 27) 
    { // ESC退出
        break;
    }
}
capture.release();
waitKey(0);
return 0;

}


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于python:Opencv-C-调用-tensorflow-模型caffe模型
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址