www.pudn.com > colortracker.rar > mpisearchthread.cpp
/* * mpisearchthread.cpp * * * Created by John Hershey 2002. * Copyright (c) 2003 Machine Perception Laboratory * University of California San Diego. * Please read the disclaimer and notes about redistribution * at the end of this file. * * Authors: John Hershey, Josh Susskind, Bret Fortenberry */ #ifdef WIN32 #pragma warning(disable:4786) #endif #include "mpisearch.h" #include "mpisearchthread.h" #include "iniconfig.h" #include#include #include
#include "Rectangle.h" #include "Images/imageio.h" #include "common.h" #ifdef WIN32 #include #else #include #endif /* for call to feature detector */ const int FRACTIONAL_SCALES=1; /* Alternative to non-blocking or infinite mutex waits */ const int WAIT_EXTERNAL=16; #define MPI_IMAGE_WIDTH 320 // reduce the width & height of pixels for mpisearch for higher performance /* ================================================================ */ THREAD_RETURN_TYPE MPISearchThread::StartThreadedTracker(void *ThisMPISearchThread) { MPISearchThread *MPI = static_cast (ThisMPISearchThread); while (MPI->m_ThreadRunning) MPI->Process(); #ifdef WIN32 _endthread(); #else return 0; #endif } /* ================================================================ */ /* Constructor */ MPISearchThread::MPISearchThread() { // ASSERT( (m_Detector.DataLoaded()) ); m_MyPixels = 0; m_NumPixels = m_NumFaces = m_lastWidth = m_lastHeight = 0; m_ColorMeanX = m_ColorMeanY = m_MPIMeanX = m_MPIMeanY = 0.0; m_ResetTracker = 0; m_llikePix = 0; m_shiftAmount = 1; //sets max pixel value based on max pixel possibility of FFImage m_Detector.setPixelMax(2560); CreateMutex (m_DataMutex); CreateMutex (m_FaceMutex); CreateMutex (m_ImageMutex); CreateMutexEvent(m_GotBufferFillRequest); } /* ================================================================ */ #ifdef USEHUE void MPISearchThread::GetColorModel(MPHistogramHue *lpf, MPHistogramHue *lpb, int *reset, double *llike) #else void MPISearchThread::GetColorModel(MPHistogram *lpf, MPHistogram *lpb, int *reset, double *llike) #endif { MUTEX_LOCK_RETURN_TYPE waitResult = TryLockMutex(m_DataMutex); switch( waitResult ) { case WAIT_OBJECT_0: *lpf = m_LogProbColorGivenFace; *lpb = m_LogProbColorGivenBack; *reset = m_ResetTracker; //m_ResetTracker = 0; *llike = m_llikePix; ReleaseMutex(m_DataMutex); break; default: break; } } /* ================================================================ */ void MPISearchThread::Start() { LockMutex( m_DataMutex ); m_ThreadRunning = TRUE; InitColorModel(); ReleaseMutex( m_DataMutex); #ifdef WIN32 m_ThreadHandle = (HANDLE) (_beginthread( StartThreadedTracker, 0, this )); #ifdef SHOWPROBS SetThreadPriority(m_ThreadHandle, THREAD_PRIORITY_NORMAL); #else SetThreadPriority(m_ThreadHandle, THREAD_PRIORITY_NORMAL-1); #endif // SHOWPROBS #else pthread_create(&m_ThreadHandle, NULL, &StartThreadedTracker, (void *)this); #endif } /* ================================================================ */ void MPISearchThread::PutData(RGBTRIPLE *d, int x, int y, int np, int colorMeanX, int colorMeanY, long curFrame) { MUTEX_LOCK_RETURN_TYPE waitResult = TryLockMutex(m_ImageMutex); switch( waitResult ) { case WAIT_OBJECT_0: if (!m_NumPixels) { m_NumPixels = np; if (!m_MyPixels) m_MyPixels = new RGBTRIPLE[m_NumPixels]; memcpy(m_MyPixels, d, m_NumPixels*sizeof(RGBTRIPLE)); m_Width = x; m_Height = y; m_ColorMeanX = colorMeanX; m_ColorMeanY = colorMeanY; m_curFrame = curFrame; MutexCondSignal(m_GotBufferFillRequest); } ReleaseMutex( m_ImageMutex); break; default: break; } } /* ================================================================ */ void MPISearchThread::Process() { FaceBoxList faces; TIMETYPE timeBeginMPISearchSearch; MutexCondWait(m_GotBufferFillRequest, m_ImageMutex); if (m_NumPixels) { timeBeginMPISearchSearch = getCurTime(); setShift(); // Create FFImage wrapper around pixels #ifdef WIN32 //(pthreads)lock is handled in wait condition LockMutex(m_ImageMutex); #endif FFImage source(m_MyPixels, m_Width, m_Height, IMAGE_FLIPPED); // Create New Grayscale version of the FFImage to be used in mpisearch FFImage pixels(source, m_shiftAmount); ReleaseMutex(m_ImageMutex); Search(pixels, faces); // do not hold mutex during long process m_NumFaces=faces.size(); if (!faces.empty()) { shiftFaces(faces); faces.objects.sort(); faces.objects.reverse(); LockMutex(m_FaceMutex); m_MPIBoxes = faces; m_lastFrame = m_curFrame; ReleaseMutex(m_FaceMutex); UpdateTimeMPISearchBoxes(timeBeginMPISearchSearch); // Added by Javier UpdateColorModel(faces); } m_NumPixels=0; // Tells color tracker that processing is done } //else ReleaseMutex(m_ImageMutex); } /* ================================================================ */ void MPISearchThread::Search(FFImage &pixels, FaceBoxList &faces) { if((pixels.width != m_lastWidth) || (pixels.height != m_lastHeight)){ m_Detector.resetStream(pixels.width, pixels.height); m_lastWidth = pixels.width ; m_lastHeight = pixels.height; } m_Detector.search(pixels, faces); faces.simplify(0.2f); } /* ================================================================ */ void MPISearchThread::InitColorModel() { m_ProbColorGivenFace.InitColorModel(1); m_CurrentProbColorGivenFace.InitColorModel(1); m_ProbColorGivenBack.InitColorModel(0); m_CurrentProbColorGivenBack.InitColorModel(0); UpdateProbabilities(); } /* ================================================================ */ void MPISearchThread::UpdateColorModel(FaceBoxList & faces) { m_CurrentProbColorGivenFace.initialize(); m_CurrentProbColorGivenBack.initialize(); Square mainface; mainface = faces.front(); bool isNonFace; double shrink = 0.15; double shrinky = .8; // We collect stats for the central region of the face box. This central region is // n % of the original box but has been modified so that the offset in the y direction is less. int offsetx = static_cast (shrink*mainface.size), offsety = (int) (((float) offsetx)*shrinky ), startXMPI, startYMPI, endXMPI, endYMPI, startXFace, startYFace, endXFace, endYFace; startXMPI = mainface.x; startYMPI = mainface.y; endXMPI = startXMPI+mainface.size; endYMPI = startYMPI+mainface.size; startXFace = startXMPI + offsetx; startYFace = startYMPI + offsety; endXFace = endXMPI - offsetx; endYFace = endYMPI - offsety; list searchRects; list xPos; list yPos; list ::iterator it = faces.begin(); list ::iterator end = faces.end(); xPos.push_back(0); xPos.push_back(m_Width); yPos.push_back(0); yPos.push_back(m_Height); for (;it != end; it++) { Square face = *(it); //set rectangle for foreground **there is a cushion set smaller then face box MPRectangle foreground(face.x+offsetx, face.x+face.size-offsetx, face.y+offsety, face.y+face.size-offsety, true); searchRects.push_back(foreground); //set positions to create background rectangles **there is a cushion set larger then face box xPos.push_back(max(0,face.x-offsetx)); xPos.push_back(min(m_Width,face.x+face.size+offsetx)); yPos.push_back(max(0,face.y-offsety)); yPos.push_back(min(m_Height,face.y+face.size+offsety)); } xPos.sort(); yPos.sort(); //set rectange for background from positions created previously list ::iterator itX = xPos.begin(); list ::iterator endX = xPos.end(); list ::iterator itY = yPos.begin(); list ::iterator endY = yPos.end(); int leftX, rightX, topY, bottomY; float midX, midY; while (itX != endX) { leftX = *(itX); itX++; if (itX == endX) break; rightX = *(itX); midX = (leftX+rightX)*0.5; //cout << "leftx = " << leftX; //cout << " rightX = " << rightX << endl; itY = yPos.begin(); while (itY != endY) { topY = *(itY); itY++; if (itY == endY) break; bottomY = *(itY); midY = (topY+bottomY)*0.5; isNonFace = true; it = faces.begin(); for(;it != end; it++) { Square face = *(it); if (midX > face.x && midX < (face.x+face.size) && midY > face.y && midY < (face.y+face.size)) isNonFace = false; } if(isNonFace) { MPRectangle background(leftX, rightX, topY, bottomY, false); searchRects.push_back(background); } } } // Create FFImage wrapper around pixels FFImage rgbimage(m_MyPixels, m_Width, m_Height, IMAGE_FLIPPED); RGBTRIPLE temp; // get probs for both face and background list ::iterator it2 = searchRects.begin(); list ::iterator end2 = searchRects.end(); for (;it2 != end2; it2++) { MPRectangle rect = *(it2); for(int y=rect.Y1; y (distanceBtwn) > box.size) m_ResetTracker = 1; } /* ================================================================ */ void MPISearchThread::UpdateProbabilities() { m_ProbColorGivenBack.update_hist (m_CurrentProbColorGivenBack, m_LogProbColorGivenBack, 0.5); m_ProbColorGivenFace.update_hist (m_CurrentProbColorGivenFace, m_LogProbColorGivenFace, 0.5); } // MPISearchThread::UpdateProbabilities /* ================================================================ */ void MPISearchThread::GetHueMinMax( int r, int g, int b, double &h, int &min, int &max ) { /* float delta; if (r >= g) {max = r; min = g;} else {max = g; min = r;} if (b > max) max = b; else if (b < min) min = b; delta = (float)(max - min); if (max == r) h = (g-b)/delta; else if (max == g) h = 2 + (b-r)/delta; else h = 4 + (r-g)/delta; h = (h < 0) ? (float)(h*60.0 + 360.0) : (float)(h*60.0);*/ } /* ================================================================ */ void MPISearchThread::UpdateTimeMPISearchBoxes(TIMETYPE t) { m_TimeMPISearchBoxes = t; } /* ================================================================ */ int MPISearchThread::GetMPIFaceBoxes(FaceBoxList &boxes) { MUTEX_LOCK_RETURN_TYPE waitResult = TryLockMutex(m_FaceMutex); switch( waitResult ) { case WAIT_OBJECT_0: boxes = m_MPIBoxes; ReleaseMutex(m_FaceMutex); break; case WAIT_TIMEOUT: case WAIT_ABANDONED: case WAIT_FAILED: default: break; } return 1; } /* ================================================================ */ TIMETYPE MPISearchThread::getCurTime(){ TIMETYPE curTime; #ifdef WIN32 curTime = timeGetTime(); #else gettimeofday( &curTime, NULL ); #endif return curTime; } /////////////////////////////////////////////////////////////////// #ifndef WIN32 double difftv(TIMETYPE t1, TIMETYPE t0) { double s; double secs = t1.tv_sec - t0.tv_sec; double usecs = ((double)t1.tv_usec) - ((double)t0.tv_usec); usecs *= 1e-6; s = secs + usecs; return s; } #endif /////////////////////////////////////////////////////////////////// double MPISearchThread::getElapsedTime(){ double elapsedTime; #ifdef WIN32 elapsedTime = timeGetTime()-m_TimeMPISearchBoxes; #else TIMETYPE curTime; gettimeofday( &curTime, NULL ); elapsedTime = difftv(curTime, m_TimeMPISearchBoxes) * 1000.0; #endif return elapsedTime; } /* ================================================================ */ void MPISearchThread::setShift() { m_shiftAmount = static_cast (m_Width/MPI_IMAGE_WIDTH); if (m_shiftAmount < 1) m_shiftAmount = 1; } /* ================================================================ */ void MPISearchThread::shiftFaces(FaceBoxList & faces) { if(m_shiftAmount < 1) m_shiftAmount = 1; list ::iterator it = faces.begin(); while (it != faces.end()) { it->size *= m_shiftAmount; it->x *= m_shiftAmount; it->y *= m_shiftAmount; *it++; } } /* ================================================================ */ int MPISearchThread::GetNumFaces() { int nf = 0; MUTEX_LOCK_RETURN_TYPE waitResult = TryLockMutex(m_FaceMutex); switch( waitResult ) { case WAIT_OBJECT_0: nf = m_NumFaces; ReleaseMutex(m_FaceMutex); default: break; } return nf; } /* ================================================================ */ /* destructor */ MPISearchThread::~MPISearchThread() { m_ThreadRunning = FALSE; if (m_MyPixels) delete [] m_MyPixels; } /* * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */