//==============================================================================
void
face_detector::
train(ft_data &data,
      const string fname,
      const Mat &ref,
      const bool mirror,
      const bool visi,
      const float frac,
      const float scaleFactor,
      const int minNeighbours,
      const Size minSize)
{
  detector.load(fname.c_str()); detector_fname = fname; reference = ref.clone();
  vector<float> xoffset(0),yoffset(0),zoffset(0);
  for(int i = 0; i < data.n_images(); i++){
    Mat im = data.get_image(i,0); if(im.empty())continue;
    vector<Point2f> p = data.get_points(i,false); int n = p.size();
    Mat pt = Mat(p).reshape(1,2*n);
    vector<Rect> faces; Mat eqIm; equalizeHist(im,eqIm);
    detector.detectMultiScale(eqIm,faces,scaleFactor,minNeighbours,0
                  |CV_HAAR_FIND_BIGGEST_OBJECT
                  |CV_HAAR_SCALE_IMAGE,minSize);
    if(faces.size() >= 1){
      if(visi){
    Mat I; cvtColor(im,I,CV_GRAY2RGB);
    for(int i = 0; i < n; i++)circle(I,p[i],1,CV_RGB(0,255,0),2,CV_AA);
    rectangle(I,faces[0].tl(),faces[0].br(),CV_RGB(255,0,0),3);
    imshow("face detector training",I); waitKey(10); 
      }
      //check if enough points are in detected rectangle
      if(this->enough_bounded_points(pt,faces[0],frac)){
    Point2f center = this->center_of_mass(pt); float w = faces[0].width;
    xoffset.push_back((center.x - (faces[0].x+0.5*faces[0].width ))/w);
    yoffset.push_back((center.y - (faces[0].y+0.5*faces[0].height))/w);
    zoffset.push_back(this->calc_scale(pt)/w);
      }
    }
    if(mirror){
      im = data.get_image(i,1); if(im.empty())continue;
      p = data.get_points(i,true);
      pt = Mat(p).reshape(1,2*n);
      equalizeHist(im,eqIm);
      detector.detectMultiScale(eqIm,faces,scaleFactor,minNeighbours,0
                  |CV_HAAR_FIND_BIGGEST_OBJECT
                |CV_HAAR_SCALE_IMAGE,minSize);
      if(faces.size() >= 1){
    if(visi){
      Mat I; cvtColor(im,I,CV_GRAY2RGB);
      for(int i = 0; i < n; i++)circle(I,p[i],1,CV_RGB(0,255,0),2,CV_AA);
      rectangle(I,faces[0].tl(),faces[0].br(),CV_RGB(255,0,0),3);
      imshow("face detector training",I); waitKey(10);
    }
    //check if enough points are in detected rectangle
    if(this->enough_bounded_points(pt,faces[0],frac)){
      Point2f center = this->center_of_mass(pt); float w = faces[0].width;
      xoffset.push_back((center.x - (faces[0].x+0.5*faces[0].width ))/w);
      yoffset.push_back((center.y - (faces[0].y+0.5*faces[0].height))/w);
      zoffset.push_back(this->calc_scale(pt)/w);
    }
      }
    }
  }
  //choose median value
  Mat X = Mat(xoffset),Xsort,Y = Mat(yoffset),Ysort,Z = Mat(zoffset),Zsort;
  cv::sort(X,Xsort,CV_SORT_EVERY_COLUMN|CV_SORT_ASCENDING); int nx = Xsort.rows;
  cv::sort(Y,Ysort,CV_SORT_EVERY_COLUMN|CV_SORT_ASCENDING); int ny = Ysort.rows;
  cv::sort(Z,Zsort,CV_SORT_EVERY_COLUMN|CV_SORT_ASCENDING); int nz = Zsort.rows;
  detector_offset = Vec3f(Xsort.fl(nx/2),Ysort.fl(ny/2),Zsort.fl(nz/2)); return;
}
Beispiel #2
0
//------------------------------------------------------------------------
Vec3 CGunTurret::GetSweepPos(IEntity *pTarget, const Vec3 &shootPos)
{
	pTarget=ResolveTarget(pTarget);

	// sweep on ground
	int nhints = m_fireparams.hints.size();
	float sweepTime = m_turretparams.sweep_time / (float)nhints;
	float timeFiring = max(0.f, GetBurstTime() - sweepTime*(m_fireHint-1));
	float sweepRelTime = min(1.f, timeFiring/sweepTime);

	if(sweepRelTime == 1.f && m_fireHint == nhints)
		return shootPos;

	Vec3 wpos(GetWeaponPos());
	Vec3 dir = shootPos - wpos;
	Vec3 dir2d(dir.x, dir.y, 0.f);
	float dist2d = dir2d.GetLength();

	if(dist2d < 2.f*m_fireparams.hints[0].y)
		return shootPos; // don't sweep when target too close

	dir2d /= dist2d;
	Vec3 right = Vec3(0,0,-1) % dir2d;
	Vec3 zoffset(0,0,0);

	if(IPhysicalEntity *pPE = pTarget->GetPhysics())
	{
		pe_status_pos ppos;

		if(pPE->GetStatus(&ppos))
			zoffset=Vec3(0,0,-0.5f*(ppos.BBox[1].z-ppos.BBox[0].z));
	}

	Vec3 lastHintPos(shootPos);
	const Vec2 &lastHint = m_fireparams.hints[m_fireHint-1];
	lastHintPos += lastHint.y*-dir2d + lastHint.x*right + zoffset;

	Vec3 nextHintPos(shootPos);

	if(m_fireHint < nhints)
	{
		const Vec2 &nextHint = m_fireparams.hints[m_fireHint];
		nextHintPos += nextHint.y*-dir2d + nextHint.x*right + zoffset;
	}

	Vec3 currPos = Vec3::CreateLerp(lastHintPos, nextHintPos, sweepRelTime);

	if(sweepRelTime == 1.f && m_fireHint < nhints)
		++m_fireHint;

	if(g_pGameCVars->i_debug_turrets == eGTD_Sweep)
	{
		IRenderAuxGeom *pGeom = gEnv->pRenderer->GetIRenderAuxGeom();
		pGeom->SetRenderFlags(e_Def3DPublicRenderflags);
		ColorB col(0,255,255,128);
		pGeom->DrawSphere(currPos, 0.3f, col);
		pGeom->DrawSphere(lastHintPos, 0.3f, col);
		pGeom->DrawSphere(nextHintPos, 0.3f, col);
		pGeom->DrawLine(lastHintPos, col, nextHintPos, col);
		gEnv->pRenderer->DrawLabel(currPos, 1.4f, "sweep, hint %i, ratio %.2f)", m_fireHint, sweepRelTime);
	}

	return currPos;
}