2024年1月14日发(作者:)
TEMPLATE = appCONFIG += console c++11CONFIG -= app_bundleCONFIG -= qtINCLUDEPATH += /usr/local/include usr/local/include/opencv4 usr/local/include/opencv4/opencv2LIBS += /usr/local/lib/libopencv_ /usr/local/lib/libopencv_ /usr/local/lib/libopencv_ /usr/local/lib/libopencv_ /usr/local/lib/libopencv_ /usr/local/lib/libopencv_ /usr/local/lib/libopencv_ /usr/local/lib/libopencv_ /usr/local/lib/libopencv_ /usr/local/lib/libopencv_ /usr/local/lib/libopencv_ /usr/local/lib/libopencv_ /usr/local/lib/libopencv_ /usr/local/lib/libopencv_CES += DERS += #include
std::vector
VideoCapture cap; if (("input")) (
{ float confidence = data[i + 2]; if (confidence > confThreshold) { int left = (int)data[i + 3]; int top = (int)data[i + 4]; int right = (int)data[i + 5]; int bottom = (int)data[i + 6]; int width = right - left + 1; int height = bottom - top + 1; if (width * height <= 1) { left = (int)(data[i + 3] * ); top = (int)(data[i + 4] * ); right = (int)(data[i + 5] * ); bottom = (int)(data[i + 6] * ); width = right - left + 1; height = bottom - top + 1; } _back((int)(data[i + 1]) - 1); // Skip 0th background class id. _back(Rect(left, top, width, height)); _back(confidence); } } } } else if (outLayerType == "Region") { for (size_t i = 0; i < (); ++i) { // Network produces output blob with a shape NxC where N is a number of // detected objects and C is a number of classes + 4 where the first 4 // numbers are [center_x, center_y, width, height] float* data = (float*)outs[i].data; for (int j = 0; j < outs[i].rows; ++j, data += outs[i].cols) { Mat scores = outs[i].row(j).colRange(5, outs[i].cols); Point classIdPoint; double confidence; minMaxLoc(scores, 0, &confidence, 0, &classIdPoint); if (confidence > confThreshold) { int centerX = (int)(data[0] * ); int centerY = (int)(data[1] * ); int width = (int)(data[2] * ); int height = (int)(data[3] * ); int left = centerX - width / 2; int top = centerY - height / 2; _back(classIdPoint.x); _back((float)confidence); _back(Rect(left, top, width, height)); } } } } else CV_Error(Error::StsNotImplemented, "Unknown output layer type: " + outLayerType); std::vector
drawPred(classIds[idx], confidences[idx], box.x, box.y, box.x + , box.y + , frame); }}void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame){ rectangle(frame, Point(left, top), Point(right, bottom), Scalar(0, 255, 0)); std::string label = format("%.2f", conf); if (!()) { CV_Assert(classId < (int)()); label = classes[classId] + ": " + label; } int baseLine; Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine); top = max(top, ); rectangle(frame, Point(left, top - ), Point(left + , top + baseLine), Scalar::all(255), FILLED); putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.5, Scalar());}void callback(int pos, void*){ confThreshold = pos * 0.01f;}的内容ocv_install_example_src(dnn *.cpp *.hpp )set(OPENCV_DNN_SAMPLES_REQUIRED_DEPS opencv_core opencv_imgproc opencv_dnn opencv_imgcodecs opencv_videoio opencv_highgui)ocv_check_dependencies(${OPENCV_DNN_SAMPLES_REQUIRED_DEPS})if(NOT BUILD_EXAMPLES OR NOT OCV_DEPENDENCIES_FOUND) return()endif()project(dnn_samples)ocv_include_modules_recurse(${OPENCV_DNN_SAMPLES_REQUIRED_DEPS})file(GLOB_RECURSE dnn_samples RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp)foreach(sample_filename ${dnn_samples}) ocv_define_sample(tgt ${sample_filename} dnn) ocv_target_link_libraries(${tgt} ${OPENCV_LINKER_LIBS} ${OPENCV_DNN_SAMPLES_REQUIRED_DEPS})endforeach()// This code is written at BigVision LLC. It is based on the OpenCV project. It is subject to the license terms in the LICENSE file found in this distribution and at /// Usage example: ./object_detection_ --video=4// ./object_detection_ --image=#include
#include
// Give the configuration and weight files for the model String modelConfiguration = ""; String modelWeights = "s"; // Load the network Net net = readNetFromDarknet(modelConfiguration, modelWeights); ferableBackend(DNN_BACKEND_OPENCV); ferableTarget(DNN_TARGET_CPU);
// Open a video file or an image file or a camera stream. string str, outputFile; VideoCapture cap; VideoWriter video; Mat frame, blob;
try {
outputFile = "yolo_out_"; if (("image")) { // Open the image file str =
ifstream ifile(str); if (!ifile) throw("error"); (str); e(()-4, (), "_yolo_out_"); outputFile = str; } else if (("video")) { // Open the video file str =
} catch(...) { cout << "Could not open the input image/video stream" << endl; return 0; }
// Get the video writer initialized to save the output video if (!("image")) { (outputFile, VideoWriter::fourcc('M','J','P','G'), 28, Size((CAP_PROP_FRAME_WIDTH), (CAP_PROP_FRAME_HEIGHT))); }
// Create a window static const string kWinName = "Deep learning object detection in OpenCV"; namedWindow(kWinName, WINDOW_NORMAL); // Process frames. while (waitKey(1) < 0) { // get frame from the video cap >> frame; // Stop the program if reached end of video if (()) { cout << "Done processing " << endl; cout << "Output file is stored as " << outputFile << endl; waitKey(3000); break; } // Create a 4D blob from a frame. blobFromImage(frame, blob, 1/255.0, cvSize(inpWidth, inpHeight), Scalar(0,0,0), true, false);
//Sets the input to the network ut(blob);
// Runs the forward pass to get output of the output layers vector
// Remove the bounding boxes with low confidence postprocess(frame, outs);
// Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes) vector
putText(frame, label, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255));
// Write the frame with the detection boxes Mat detectedFrame; tTo(detectedFrame, CV_8U); if (("image")) imwrite(outputFile, detectedFrame); else (detectedFrame);
imshow(kWinName, frame);
}
e(); if (!("image")) e(); return 0;}// Remove the bounding boxes with low confidence using non-maxima suppressionvoid postprocess(Mat& frame, const vector
for (size_t i = 0; i < (); ++i) { // Scan through all the bounding boxes output from the network and keep only the // ones with high confidence scores. Assign the box's class label as the class // with the highest score for the box. float* data = (float*)outs[i].data; for (int j = 0; j < outs[i].rows; ++j, data += outs[i].cols) { Mat scores = outs[i].row(j).colRange(5, outs[i].cols); Point classIdPoint; double confidence; // Get the value and location of the maximum score minMaxLoc(scores, 0, &confidence, 0, &classIdPoint); if (confidence > confThreshold) { int centerX = (int)(data[0] * ); int centerY = (int)(data[1] * ); int width = (int)(data[2] * ); int height = (int)(data[3] * ); int left = centerX - width / 2; int top = centerY - height / 2;
_back(classIdPoint.x); _back((float)confidence); _back(Rect(left, top, width, height)); } } }
// Perform non maximum suppression to eliminate redundant overlapping boxes with // lower confidences vector
}// Draw the predicted bounding boxvoid drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame){ //Draw a rectangle displaying the bounding box rectangle(frame, Point(left, top), Point(right, bottom), Scalar(255, 178, 50), 3);
//Get the label for the class name and its confidence string label = format("%.2f", conf); if (!()) { CV_Assert(classId < (int)()); label = classes[classId] + ":" + label; }
//Display the label at the top of the bounding box int baseLine; Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine); top = max(top, ); rectangle(frame, Point(left, top - round(1.5*)), Point(left + round(1.5*), top + baseLine), Scalar(255, 255, 255), FILLED); putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0,0,0),1);}// Get the names of the output layersvector
//get the names of all the layers in the network vector
// Get the names of the output layers in names (()); for (size_t i = 0; i < (); ++i) names[i] = layersNames[outLayers[i] - 1]; } return names;}


发布评论