/* * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include #include #include #include #ifdef HEADLESS #define IS_HEADLESS() "headless" // run without display #else #define IS_HEADLESS() (const char*)NULL #endif bool signal_recieved = false; void sig_handler(int signo) { if( signo == SIGINT ) { LogVerbose("received SIGINT\n"); signal_recieved = true; } } int usage() { printf("usage: detectnet [--help] [--network=NETWORK] [--threshold=THRESHOLD] ...\n"); printf(" input_URI [output_URI]\n\n"); printf("Locate objects in a video/image stream using an object detection DNN.\n"); printf("See below for additional arguments that may not be shown above.\n\n"); printf("positional arguments:\n"); printf(" input_URI resource URI of input stream (see videoSource below)\n"); printf(" output_URI resource URI of output stream (see videoOutput below)\n\n"); printf("%s", detectNet::Usage()); printf("%s", videoSource::Usage()); printf("%s", videoOutput::Usage()); printf("%s", Log::Usage()); return 0; } int main( int argc, char** argv ) { /* * parse command line */ commandLine cmdLine(argc, argv, IS_HEADLESS()); if( cmdLine.GetFlag("help") ) return usage(); /* * attach signal handler */ if( signal(SIGINT, sig_handler) == SIG_ERR ) LogError("can't catch SIGINT\n"); /* * create input stream */ videoOptions ioptions; ioptions.width = 1920; ioptions.height = 1080; ioptions.frameRate = 30; ioptions.zeroCopy = true; ioptions.deviceType = videoOptions::DEVICE_CSI; ioptions.ioType = videoOptions::INPUT; ioptions.flipMethod = videoOptions::FLIP_COUNTERCLOCKWISE; ioptions.codec = videoOptions::CODEC_RAW; videoSource* input = videoSource::Create("csi://0", ioptions); if( !input ) { LogError("test: failed to create input stream\n"); return 0; } /* * create output stream */ videoOutput* output = videoOutput::Create("display://0"); if( !output ) LogError("test: failed to create output stream\n"); /* * create detection network */ // detectNet::NetworkType networkType = detectNet::NetworkTypeFromStr("ssd-mobilenet-v2"); // detectNet* net = detectNet::Create(networkType, 0.5); /* if( !net ) { LogError("test: failed to load detectNet model\n"); return 0; } */ // parse overlay flags //const uint32_t overlayFlags = detectNet::OverlayFlagsFromStr(cmdLine.GetString("overlay", "box,labels,conf")); /* * processing loop */ int counter = 0; while( !signal_recieved ) { // capture next image image uchar3* image = NULL; if( !input->Capture(&image, 100) ) { // check for EOS if( !input->IsStreaming() ) break; LogError("detect-test: failed to capture video frame\n"); continue; } /* if (++counter % 30 == 1) { // detect objects in the frame detectNet::Detection* detections = NULL; const int numDetections = net->Detect(image, input->GetWidth(), input->GetHeight(), &detections, detectNet::OVERLAY_NONE); if( numDetections > 0 ) { LogVerbose("%i objects detected\n", numDetections); for( int n=0; n < numDetections; n++ ) { LogVerbose("detected obj %i class #%u (%s) confidence=%f\n", n, detections[n].ClassID, net->GetClassDesc(detections[n].ClassID), detections[n].Confidence); LogVerbose("bounding box %i (%f, %f) (%f, %f) w=%f h=%f\n", n, detections[n].Left, detections[n].Top, detections[n].Right, detections[n].Bottom, detections[n].Width(), detections[n].Height()); } } } */ uchar3* imgOutput = NULL; if( !cudaAllocMapped(&imgOutput, 720, 1280)) { // check for EOS LogError("detect-test: failed to allocate space for resize output\n"); continue; } if( CUDA_FAILED(cudaResize(image, 1080, 1920, imgOutput, 720, 1280))) { LogError("detect-test: failed to resize image\n"); continue; } uchar3* imgOutput2 = NULL; if( !cudaAllocMapped(&imgOutput2, 1280, 720)) { // check for EOS LogError("detect-test: failed to allocate space for flip output\n"); continue; } if( CUDA_FAILED(cudaFlip(imgOutput, imgOutput2, 3, 720, 1280))) { LogError("detect-test: failed to flip image\n"); continue; } // render outputs if( output != NULL ) { output->Render(imgOutput2, 1280, 720); //update the status bar // char str[256]; // sprintf(str, "TensorRT %i.%i.%i | %s | Network %.0f FPS", NV_TENSORRT_MAJOR, NV_TENSORRT_MINOR, NV_TENSORRT_PATCH, precisionTypeToStr(net->GetPrecision()), net->GetNetworkFPS()); // output->SetStatus(str); // check if the user quit if( !output->IsStreaming() ) signal_recieved = true; } // print out timing inf //net->PrintProfilerTimes(); } /* * destroy resources */ LogVerbose("test: shutting down...\n"); SAFE_DELETE(input); SAFE_DELETE(output); // SAFE_DELETE(net); LogVerbose("test: shutdown complete.\n"); return 0; }