(365)
(8)
(130)
(155)
(50)
(22)
分类: python/ruby
2021-06-30 17:18:05
#include
#include
#include
#include
#include
#include
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 confidence_threshold = 0.5;
for (int i = 0; i < detectionmat.rows; i ) {
float confidence = detectionmat.at
if (confidence > confidence_threshold) {
size_t objindex = (size_t)(detectionmat.at
float tl_x = detectionmat.at
float tl_y = detectionmat.at
float br_x = detectionmat.at
float br_y = detectionmat.at
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;
}