|
| 1 | +/* Copyright 2020 The MathWorks, Inc. */ |
| 2 | + |
| 3 | +/* Headers */ |
| 4 | +#include <stdio.h> |
| 5 | +#include <cuda.h> |
| 6 | +#include <sys/timeb.h> |
| 7 | +/* Opencv includes*/ |
| 8 | +#include "opencv2/opencv.hpp" |
| 9 | + |
| 10 | +using namespace cv; |
| 11 | + |
| 12 | +/* targetFunction source headers*/ |
| 13 | +#include "targetFunction.h" |
| 14 | + |
| 15 | +int main(int argc, const char * const argv[]) |
| 16 | +{ |
| 17 | + |
| 18 | + unsigned int wi = 320; |
| 19 | + unsigned int he = 240; |
| 20 | + //unsigned int ch = 3; |
| 21 | + //unsigned int size = wi*he*ch; |
| 22 | + |
| 23 | + int sw; |
| 24 | + sw = atoi(argv[1]); |
| 25 | + |
| 26 | + /* Input arguments */ |
| 27 | + VideoCapture cap; |
| 28 | + if ( sw < 3 ) { |
| 29 | + if ( sw < 2 ) { |
| 30 | + //VideoCapture cap(atoi(argv[2])); //use device number for camera. |
| 31 | + cap.open(atoi(argv[2])); //use device number for camera. |
| 32 | + if (!cap.isOpened()) { |
| 33 | + printf("Could not open the video capture device.\n"); |
| 34 | + return -1; |
| 35 | + } |
| 36 | + cap.set(CAP_PROP_FRAME_WIDTH, wi); |
| 37 | + cap.set(CAP_PROP_FRAME_HEIGHT, he); |
| 38 | + //cap.set(CAP_PROP_AUTOFOCUS, 0); |
| 39 | + //cap.set(28, 70); |
| 40 | + } else { |
| 41 | + //VideoCapture cap(argv[2]); |
| 42 | + cap.open(argv[2]); |
| 43 | + if (!cap.isOpened()) { |
| 44 | + printf("Could not open the video capture device.\n"); |
| 45 | + return -1; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + namedWindow("Defect Detection Demo",CV_WINDOW_NORMAL); |
| 51 | + resizeWindow("Defect Detection Demo", 300,300); |
| 52 | + |
| 53 | + cudaEvent_t start, stop; |
| 54 | + float fps=0; |
| 55 | + Mat inImg; |
| 56 | + //Mat im; |
| 57 | + Mat outImg; |
| 58 | + cudaEventCreate(&start); |
| 59 | + cudaEventCreate(&stop); |
| 60 | + unsigned char output[230400]; |
| 61 | + |
| 62 | + /* Start reading frames */ |
| 63 | + for(;;) |
| 64 | + { |
| 65 | + //Mat inImg; |
| 66 | + |
| 67 | + cudaEventRecord(start); |
| 68 | + |
| 69 | + if ( sw < 3 ) { |
| 70 | + cap >> inImg; |
| 71 | + } else { |
| 72 | + inImg = imread(argv[2], 1); |
| 73 | + } |
| 74 | + |
| 75 | + if (inImg.empty()) { |
| 76 | + if ( sw == 2 ) { |
| 77 | + cap.set(CAP_PROP_POS_FRAMES, 0); |
| 78 | + cap >> inImg; |
| 79 | + } else { |
| 80 | + printf("Could not open the video capture device.\n"); |
| 81 | + break; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + //Size size(wi,he); |
| 86 | + //resize(inImg,im,size); |
| 87 | + |
| 88 | + targetFunction(inImg.data, output); |
| 89 | + cudaEventRecord(stop); |
| 90 | + cudaEventSynchronize(stop); |
| 91 | + |
| 92 | + Mat outImg(inImg.size(),inImg.type(),output); |
| 93 | + |
| 94 | + char strbuf[50]; |
| 95 | + float milliseconds = -1.0; |
| 96 | + cudaEventElapsedTime(&milliseconds, start, stop); |
| 97 | + fps = fps*.9+1000.0/milliseconds*.1; |
| 98 | + |
| 99 | + if( waitKey(50)%256 == 27 ) break; // stop capturing by pressing ESC |
| 100 | + |
| 101 | + if ( sw == 3 ) { |
| 102 | + imwrite("output.png", outImg); |
| 103 | + break; |
| 104 | + } |
| 105 | + |
| 106 | + sprintf (strbuf, "%.2f FPS", fps); |
| 107 | + putText(outImg, strbuf, cvPoint(120,30), CV_FONT_HERSHEY_DUPLEX, 0.5, CV_RGB(0,0,0), 1); |
| 108 | + |
| 109 | + imshow("Defect Detection Demo", outImg); |
| 110 | + } |
| 111 | + |
| 112 | + destroyWindow("Defect Detection Demo"); |
| 113 | + |
| 114 | + return 0; |
| 115 | +} |
| 116 | + |
0 commit comments