Changed Linux timestamp behavior. Explicitly using CLOCK_MONOTONIC_RAW and CLOCK_REALTIME

This commit is contained in:
Matt Hill
2015-03-16 22:12:37 -04:00
parent f7ed84d5db
commit 39c4756ce7
20 changed files with 120 additions and 79 deletions

View File

@@ -55,7 +55,7 @@ namespace alpr
{
std::cerr << "file does not exist: " << filepath << std::endl;
AlprResults emptyResults;
emptyResults.epoch_time = getEpochTime();
emptyResults.epoch_time = getEpochTimeMs();
emptyResults.img_width = 0;
emptyResults.img_height = 0;
emptyResults.total_processing_time_ms = 0;

View File

@@ -72,12 +72,12 @@ namespace alpr
AlprFullDetails AlprImpl::recognizeFullDetails(cv::Mat img, std::vector<cv::Rect> regionsOfInterest)
{
timespec startTime;
getTime(&startTime);
getTimeMonotonic(&startTime);
AlprFullDetails response;
response.results.epoch_time = getEpochTime();
response.results.epoch_time = getEpochTimeMs();
response.results.img_width = img.cols;
response.results.img_height = img.rows;
@@ -126,7 +126,7 @@ namespace alpr
PipelineData pipeline_data(img, plateRegion.rect, config);
timespec platestarttime;
getTime(&platestarttime);
getTimeMonotonic(&platestarttime);
LicensePlateCandidate lp(&pipeline_data);
@@ -196,7 +196,7 @@ namespace alpr
}
timespec plateEndTime;
getTime(&plateEndTime);
getTimeMonotonic(&plateEndTime);
plateResult.processing_time_ms = diffclock(platestarttime, plateEndTime);
if (plateResult.topNPlates.size() > 0)
@@ -222,7 +222,7 @@ namespace alpr
}
timespec endTime;
getTime(&endTime);
getTimeMonotonic(&endTime);
response.results.total_processing_time_ms = diffclock(startTime, endTime);
if (config->debugTiming)

View File

@@ -29,7 +29,7 @@ namespace alpr
ColorFilter::ColorFilter(Mat image, Mat characterMask, Config* config)
{
timespec startTime;
getTime(&startTime);
getTimeMonotonic(&startTime);
this->config = config;
@@ -53,7 +53,7 @@ namespace alpr
if (config->debugTiming)
{
timespec endTime;
getTime(&endTime);
getTimeMonotonic(&endTime);
cout << " -- ColorFilter Time: " << diffclock(startTime, endTime) << "ms." << endl;
}
}

View File

@@ -102,7 +102,7 @@ namespace alpr
//-- Detect plates
timespec startTime;
getTime(&startTime);
getTimeMonotonic(&startTime);
float maxWidth = ((float) w) * (config->maxPlateWidthPercent / 100.0f) * this->scale_factor;
float maxHeight = ((float) h) * (config->maxPlateHeightPercent / 100.0f) * this->scale_factor;
@@ -118,7 +118,7 @@ namespace alpr
if (config->debugTiming)
{
timespec endTime;
getTime(&endTime);
getTimeMonotonic(&endTime);
cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl;
}

View File

@@ -98,7 +98,7 @@ namespace alpr
//-- Detect plates
timespec startTime;
getTime(&startTime);
getTimeMonotonic(&startTime);
float maxWidth = ((float) w) * (config->maxPlateWidthPercent / 100.0f) * this->scale_factor;
float maxHeight = ((float) h) * (config->maxPlateHeightPercent / 100.0f) * this->scale_factor;
@@ -121,7 +121,7 @@ namespace alpr
if (config->debugTiming)
{
timespec endTime;
getTime(&endTime);
getTimeMonotonic(&endTime);
cout << "LBP Time: " << diffclock(startTime, endTime) << "ms." << endl;
}

View File

@@ -53,7 +53,7 @@ namespace alpr
cout << "PlateCorners::findPlateCorners" << endl;
timespec startTime;
getTime(&startTime);
getTimeMonotonic(&startTime);
int horizontalLines = this->plateLines->horizontalLines.size();
int verticalLines = this->plateLines->verticalLines.size();
@@ -118,7 +118,7 @@ namespace alpr
if (pipelineData->config->debugTiming)
{
timespec endTime;
getTime(&endTime);
getTimeMonotonic(&endTime);
cout << "Plate Corners Time: " << diffclock(startTime, endTime) << "ms." << endl;
}

View File

@@ -47,7 +47,7 @@ namespace alpr
cout << "PlateLines findLines" << endl;
timespec startTime;
getTime(&startTime);
getTimeMonotonic(&startTime);
// Ignore input images that are pure white or pure black
@@ -129,7 +129,7 @@ namespace alpr
if (pipelineData->config->debugTiming)
{
timespec endTime;
getTime(&endTime);
getTimeMonotonic(&endTime);
cout << "Plate Lines Time: " << diffclock(startTime, endTime) << "ms." << endl;
}

View File

@@ -69,7 +69,7 @@ namespace alpr
{
timespec startTime;
getTime(&startTime);
getTimeMonotonic(&startTime);
Mat originalCrop = pipeline_data->crop_gray;
@@ -113,7 +113,7 @@ namespace alpr
if (config->debugTiming)
{
timespec endTime;
getTime(&endTime);
getTimeMonotonic(&endTime);
cout << "deskew Time: " << diffclock(startTime, endTime) << "ms." << endl;
}

View File

@@ -54,7 +54,7 @@ namespace alpr
void OCR::performOCR(PipelineData* pipeline_data)
{
timespec startTime;
getTime(&startTime);
getTimeMonotonic(&startTime);
postProcessor.clear();
@@ -131,7 +131,7 @@ namespace alpr
if (config->debugTiming)
{
timespec endTime;
getTime(&endTime);
getTimeMonotonic(&endTime);
cout << "OCR Time: " << diffclock(startTime, endTime) << "ms." << endl;
}
}

View File

@@ -152,7 +152,7 @@ namespace alpr
void PostProcess::analyze(string templateregion, int topn)
{
timespec startTime;
getTime(&startTime);
getTimeMonotonic(&startTime);
// Get a list of missing positions
for (int i = letters.size() -1; i >= 0; i--)
@@ -201,7 +201,7 @@ namespace alpr
findAllPermutations(tmp, 0, config->postProcessMaxSubstitutions);
timespec sortStartTime;
getTime(&sortStartTime);
getTimeMonotonic(&sortStartTime);
int numelements = topn;
if (allPossibilities.size() < topn)
@@ -212,7 +212,7 @@ namespace alpr
if (config->debugTiming)
{
timespec sortEndTime;
getTime(&sortEndTime);
getTimeMonotonic(&sortEndTime);
cout << " -- PostProcess Sort Time: " << diffclock(sortStartTime, sortEndTime) << "ms." << endl;
}
@@ -290,7 +290,7 @@ namespace alpr
if (config->debugTiming)
{
timespec endTime;
getTime(&endTime);
getTimeMonotonic(&endTime);
cout << "PostProcess Time: " << diffclock(startTime, endTime) << "ms." << endl;
}

View File

@@ -40,7 +40,7 @@ namespace alpr
//CharacterRegion charRegion(img, debug);
timespec startTime;
getTime(&startTime);
getTimeMonotonic(&startTime);
if (pipeline_data->plate_inverted)
bitwise_not(pipeline_data->crop_gray, pipeline_data->crop_gray);
@@ -110,7 +110,7 @@ namespace alpr
// Do the histogram analysis to figure out char regions
timespec startTime;
getTime(&startTime);
getTimeMonotonic(&startTime);
vector<Mat> allHistograms;
@@ -166,7 +166,7 @@ namespace alpr
if (config->debugTiming)
{
timespec endTime;
getTime(&endTime);
getTimeMonotonic(&endTime);
cout << " -- Character Segmentation Create and Score Histograms Time: " << diffclock(startTime, endTime) << "ms." << endl;
}
@@ -189,7 +189,7 @@ namespace alpr
}
}
getTime(&startTime);
getTimeMonotonic(&startTime);
filterEdgeBoxes(pipeline_data->thresholds, candidateBoxes, medianCharWidth, avgCharHeight);
candidateBoxes = filterMostlyEmptyBoxes(pipeline_data->thresholds, candidateBoxes);
@@ -204,7 +204,7 @@ namespace alpr
if (config->debugTiming)
{
timespec endTime;
getTime(&endTime);
getTimeMonotonic(&endTime);
cout << " -- Character Segmentation Box cleaning/filtering Time: " << diffclock(startTime, endTime) << "ms." << endl;
}
@@ -226,7 +226,7 @@ namespace alpr
if (config->debugTiming)
{
timespec endTime;
getTime(&endTime);
getTimeMonotonic(&endTime);
cout << "Character Segmenter Time: " << diffclock(startTime, endTime) << "ms." << endl;
}
}

View File

@@ -52,7 +52,7 @@ namespace alpr
bool StateIdentifier::recognize(PipelineData* pipeline_data)
{
timespec startTime;
getTime(&startTime);
getTimeMonotonic(&startTime);
Mat plateImg = Mat(pipeline_data->grayImg, pipeline_data->regionOfInterest);
@@ -75,7 +75,7 @@ namespace alpr
if (config->debugTiming)
{
timespec endTime;
getTime(&endTime);
getTimeMonotonic(&endTime);
cout << "State Identification Time: " << diffclock(startTime, endTime) << "ms." << endl;
}

View File

@@ -71,11 +71,24 @@ namespace alpr
return (0);
}
void getTime(timespec* time)
void getTimeMonotonic(timespec* time)
{
clock_gettime(0, time);
}
long getTimeMonotonicMs()
{
timespec time;
getTimeMonotonic(&time);
timespec time_start;
time_start.tv_sec = 0;
time_start.tv_nsec = 0;
return diffclock(time_start, time);
}
double diffclock(timespec time1,timespec time2)
{
timespec delta = diff(time1,time2);
@@ -101,26 +114,48 @@ namespace alpr
}
long getEpochTime()
long getEpochTimeMs()
{
return std::time(0) * 1000;
}
#else
void getTime(timespec* time)
void _getTime(bool realtime, timespec* time)
{
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
time->tv_sec = mts.tv_sec;
time->tv_nsec = mts.tv_nsec;
#else
clock_gettime(CLOCK_MONOTONIC, time);
#endif
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
time->tv_sec = mts.tv_sec;
time->tv_nsec = mts.tv_nsec;
#else
if (realtime)
clock_gettime(CLOCK_REALTIME, time);
else
clock_gettime(CLOCK_MONOTONIC, time);
#endif
}
// Returns a monotonic clock time unaffected by time changes (e.g., NTP)
// Useful for interval comparisons
void getTimeMonotonic(timespec* time)
{
_getTime(false, time);
}
long getTimeMonotonicMs()
{
timespec time;
getTimeMonotonic(&time);
timespec time_start;
time_start.tv_sec = 0;
time_start.tv_nsec = 0;
return diffclock(time_start, time);
}
double diffclock(timespec time1,timespec time2)
@@ -148,13 +183,18 @@ namespace alpr
}
long getEpochTime()
// Returns wall clock time since Unix epoch (Jan 1, 1970)
long getEpochTimeMs()
{
struct timeval tp;
gettimeofday(&tp, NULL);
long ms = tp.tv_sec * 1000 + tp.tv_usec / 1000;
timespec time;
_getTime(true, &time);
timespec epoch_start;
epoch_start.tv_sec = 0;
epoch_start.tv_nsec = 0;
return diffclock(epoch_start, time);
return ms;
}
#endif

View File

@@ -26,10 +26,12 @@
namespace alpr
{
void getTime(timespec* time);
void getTimeMonotonic(timespec* time);
long getTimeMonotonicMs();
double diffclock(timespec time1,timespec time2);
long getEpochTime();
long getEpochTimeMs();
}

View File

@@ -56,7 +56,7 @@ namespace alpr
timespec startTime;
getTime(&startTime);
getTimeMonotonic(&startTime);
pipeline_data->textLines.clear();
@@ -70,12 +70,12 @@ namespace alpr
if (config->debugTiming)
{
timespec endTime;
getTime(&endTime);
getTimeMonotonic(&endTime);
cout << " -- Character Analysis Find Contours Time: " << diffclock(startTime, endTime) << "ms." << endl;
}
//Mat img_equalized = equalizeBrightness(img_gray);
getTime(&startTime);
getTimeMonotonic(&startTime);
for (unsigned int i = 0; i < pipeline_data->thresholds.size(); i++)
{
@@ -88,7 +88,7 @@ namespace alpr
if (config->debugTiming)
{
timespec endTime;
getTime(&endTime);
getTimeMonotonic(&endTime);
cout << " -- Character Analysis Filter Time: " << diffclock(startTime, endTime) << "ms." << endl;
}
@@ -206,7 +206,7 @@ namespace alpr
if (config->debugTiming)
{
timespec endTime;
getTime(&endTime);
getTimeMonotonic(&endTime);
cout << "Character Analysis Time: " << diffclock(startTime, endTime) << "ms." << endl;
}

View File

@@ -121,7 +121,7 @@ namespace alpr
//Mat img_equalized = equalizeBrightness(img_gray);
timespec startTime;
getTime(&startTime);
getTimeMonotonic(&startTime);
vector<Mat> thresholds;
@@ -160,7 +160,7 @@ namespace alpr
if (config->debugTiming)
{
timespec endTime;
getTime(&endTime);
getTimeMonotonic(&endTime);
cout << " -- Produce Threshold Time: " << diffclock(startTime, endTime) << "ms." << endl;
}