Home > other >  Mask_RCNN trained model using object_detection API calls too slow may be what is the reason, how to
Mask_RCNN trained model using object_detection API calls too slow may be what is the reason, how to

Time:09-18

#include
#include
#include
#include
#include
#include
#include
#include


using namespace cv;
Using the namespace within DNN.
using namespace std;

//Initialize the parameters
Float confThreshold=0.5;//the Confidence threshold
Float maskThreshold=0.3;//Mask threshold

Vector Classes;
Vector Colors={Scalar (255, 0, 255), a Scalar (0, 255, 0), Scalar (255, 0, 0)};

//Draw the predicted bounding box
Void drawBox (Mat& Frame, int every, float the conf, the Rect box, Mat& ObjectMask, Mat & amp; Mask);

//Postprocess the neural network 's output for each frame
Void postprocess (Mat& Frame, const vector & Outs, Mat & amp; Mask);

Int main ()
{
//the Load names of classes
String classesFile="G: \ \ sheep_label_map pb model PBTXT";
Ifstream ifs (classesFile c_str ());
The string line;
While (getline (ifs, line) classes. The push_back (line);

////Load the colors
//string colorsFile="./mask_rcnn_inception_v2_coco_2018_01_28/colors. TXT ".
//ifstream colorFptr (colorsFile c_str ());
//while (getline (colorFptr, line))
//{
//char * pEnd;
//double r, g, b.
//r=strtod (line c_str (), & amp; PEnd);
//g=strtod (pEnd, NULL);
//b=strtod (pEnd, NULL);
//Scalar color=Scalar (r, g, b, 255.0);
//colors. The push_back (Scalar (r, g, b, 255.0));
//}

//difference the configuration and weight files for model
String textGraph="G:/pb model/mytest. PBTXT";
String modelWeights="G:/pb model/frozen_inference_graph. Pb";

//Load the network
Clock_t startTime1=clock ();
Net Net=readNetFromTensorflow (modelWeights textGraph);
Net. SetPreferableBackend (DNN_BACKEND_OPENCV);
Net. SetPreferableTarget (DNN_TARGET_CPU);
Clock_t endTime1=clock ();
//Open a video file or an image file or a camera stream.
cout <"Load network:" & lt; String STR, outputFile;
//VideoWriter video;
Mat frame, blob;

//Create a window
The static const string kWinName="Deep learning object detection in OpenCV";
NamedWindow (kWinName, 0);

String path="C: \ \ Users \ \ 13786 \ \ Desktop \ \ PIC \ ";
//clock_t startTime=clock ();
//get the frame from the video
for (int i=1; i <204; I++)
{
Clock_t startTime=clock ();
String path1=path + to_string (I) + ". PNG ";
Frame=imread (path1);
//Stop the program if reached end of video
If (frame. The empty ())
{
cout <"Done processing!!!!!! "& lt; cout <"The Output file is stored as" & lt; WaitKey (3000);
}
//Create a 4 d blob from a frame.
BlobFromImage (frame, blob, 1.0, the Size (512, 512), a Scalar (), true, false);
//blobFromImage (frame, blob);

//Sets the input to the network
Net. SetInput (blob);

//Runs the forward pass to get the output from the output the layers
STD: : vector OutNames (2);
OutNames [0]="detection_out_final";
OutNames [1]="detection_masks";
Vector Outs;
Net. The forward (outs, outNames);
Mat mask;
//Extract the bounding box and mask for each of the detected objects
Postprocess (frame, outs, mask);

//Put efficiency information. The function getPerfProfile returns The overall time for inference (t) and The timings for each of The layers (in layersTimes)
Vector LayersTimes;
Double freq=getTickFrequency ()/1000;
Double t=net. GetPerfProfile (layersTimes)/freq.
String label=format (" Mask - RCNN on 2.5 GHz Intel Core i7 CPU, the Inference time for a frame: ms "% 0.0 f, t);
//putText (frame, label, Point (0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar (0, 0, 0));

//for the frame with the detection boxes
Mat detectedFrame;
Frame. ConvertTo (detectedFrame CV_8U);
Clock_t endTime=clock ();
cout <"Recognition is:" & lt; String path2="G: \ \ \ \ pb model mask \ " + to_string (I) + ". PNG ";
Imwrite (path2, mask);
//imshow (kWinName, frame);
//imshow (" 1 ", mask);
//waitKey (30);
}
system("pause");
return 0;
}

//For each frame, extract the bounding box and mask For each detected object
Void postprocess (Mat& Frame, const vector & Outs, Mat & amp; Mask)
{
Mat outDetections=outs [0];
Mat outMasks=outs [1].

//the Output size of masks is NxCxHxW where
//N - the number of detected boxes
//C - number of classes (excluding background)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related