12/16/2015

Opencv Hog+SVM example of pedestrian detection


source code


#include < iostream>    
#include "opencv2\objdetect\objdetect.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\cudaobjdetect.hpp"
#include "opencv2\cudaimgproc.hpp"
#include "opencv2\cudawarping.hpp"


#ifdef _DEBUG               
#pragma comment(lib, "opencv_core300d.lib")       
#pragma comment(lib, "opencv_highgui300d.lib")    
#pragma comment(lib, "opencv_imgcodecs300d.lib")  
#pragma comment(lib, "opencv_objdetect300d.lib")  
#pragma comment(lib, "opencv_imgproc300d.lib")  
#pragma comment(lib, "opencv_videoio300d.lib")    
#pragma comment(lib, "opencv_cudaobjdetect300d.lib")  
#pragma comment(lib, "opencv_cudawarping300d.lib")
#pragma comment(lib, "opencv_cudaimgproc300d.lib")

#else       
#pragma comment(lib, "opencv_core300.lib")       
#pragma comment(lib, "opencv_highgui300.lib")    
#pragma comment(lib, "opencv_imgcodecs300.lib")    
#pragma comment(lib, "opencv_objdetect300.lib")  
#pragma comment(lib, "opencv_imgproc300.lib")  
#pragma comment(lib, "opencv_videoio300.lib")    
#pragma comment(lib, "opencv_cudaobjdetect300.lib")
#pragma comment(lib, "opencv_cudawarping300.lib")
#pragma comment(lib, "opencv_cudaimgproc300.lib")
#endif        

using namespace std;
using namespace cv;


void main()
{


 cv::Ptr< cv::cuda::HOG> d_hog = cv::cuda::HOG::create(Size(48, 96)); //Size(64, 128));// 
 d_hog->setSVMDetector(d_hog->getDefaultPeopleDetector());
 

 //video loading
 Mat img; 
 VideoCapture cap("M:\\____videoSample____\\tracking\\TownCentreXVID.avi");

 //loading test
 cap >> img;
 if (img.empty())
  return;

 //window
 namedWindow("pedestrian", 0);

 //processing time check
 unsigned long AAtime = 0, BBtime = 0;
 
 //resize
 double scale = float(800) / img.cols;
 cuda::GpuMat GpuImg, rGpuImg;
 GpuImg.upload(img);
 cuda::resize(GpuImg, rGpuImg, Size(GpuImg.cols * scale, GpuImg.rows * scale));
 Mat rInimg;
 rGpuImg.download(rInimg);

 while (1)
 {
  //time check
  AAtime = getTickCount();

  //loading
  cap >> img;
  if (img.empty())
   break;

  //resize
  GpuImg.upload(img);
  cuda::resize(GpuImg, rGpuImg, Size(GpuImg.cols * scale, GpuImg.rows * scale));
  rGpuImg.download(rInimg);
  cuda::cvtColor(rGpuImg, rGpuImg, CV_BGR2GRAY);
  
  
  vector< Point> found_locations;
  d_hog->detect(rGpuImg, found_locations);

  std::vector< cv::Rect> found_locations_rect;
  d_hog->detectMultiScale(rGpuImg, found_locations_rect);


  for (int i = 0; i < found_locations_rect.size(); ++i)
  {

   cv::rectangle(rInimg, found_locations_rect[i], CvScalar(0, 0, 255), 1);   
  }


  imshow("pedestrian", rInimg);
  waitKey(10);

  //time check
  BBtime = getTickCount();
  double s_time = (BBtime - AAtime) / getTickFrequency();
  double fps_time = 1/s_time;
  printf("%.2lf sec / %.2lf fps \n", s_time, fps_time); 

 }


}

3 comments:

  1. μ•ˆλ…•ν•˜μ„Έμš” λΈ”λ‘œκ·Έ 항상 잘 보고 μžˆμŠ΅λ‹ˆλ‹€.
    object tracking κ΄€λ ¨ν•˜μ—¬ κ³΅λΆ€ν•˜κ³ μžˆλŠ” ν•™μƒμž…λ‹ˆλ‹€.
    이 μ½”λ“œλ₯Ό ν•œλ²ˆ ν…ŒμŠ€νŠΈν•΄λ³΄κ³  μ‹Άμ–΄μ„œ CUDA λ₯Ό κΉ”κ³  opencvλž‘ 연동을 ν•˜μ˜€λŠ”λ°
    GpuImg, rGpuImg λ“±μ—μ„œ 빨간쀄이 뜨고 μ•„λž˜μ™€ 같은 였λ₯˜κ°€ λœ¨λŠ”λ°
    no suitable user-defined conversion from "cv::cuda::GpuMat" to "const cv::_InputArray" exists
    CUDA 이외에도 더 μ„€μ •ν•΄μ£Όμ–΄μ•Ό ν•  것이 μžˆλ‚˜μš”? λ‹΅λ³€ λΆ€νƒλ“œλ¦½λ‹ˆλ‹€ T.T

    ReplyDelete
    Replies
    1. cuda κ΄€λ ¨ 문제둜 λ³΄μ΄λ„€μš”..
      빈 νŒŒμΌμ—μ„œ cuda::Gpumat μ„ μ–Έ, upload, resize, download 와 같은 기본적인 μ½”λ“œλ‘œ ν…ŒμŠ€νŠΈλ₯Ό ν•΄λ³΄μ„Έμš”.

      Delete
    2. λ„€ κ°μ‚¬ν•©λ‹ˆλ‹€ μ’‹μ€ν•˜λ£¨λ˜μ„Έμš” :)

      Delete