{"id":1246,"date":"2014-03-06T00:16:09","date_gmt":"2014-03-05T16:16:09","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1246"},"modified":"2014-03-28T12:48:49","modified_gmt":"2014-03-28T04:48:49","slug":"people-detection-in-processing-with-opencv","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2014\/03\/06\/people-detection-in-processing-with-opencv\/","title":{"rendered":"People Detection in Processing with OpenCV"},"content":{"rendered":"<p>This is the original OpenCV people detection example ported to Processing with the Java library of OpenCV 2.4.8. It can achieve more than 20 frames per second. Here is a sample test snapshot.<br \/>\n&nbsp;<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2014\/03\/peopledetect.jpg\" alt=\"\" title=\"People detection\" width=\"640\" height=\"480\" class=\"alignnone size-full wp-image-1249\" srcset=\"http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2014\/03\/peopledetect.jpg 640w, http:\/\/www.magicandlove.com\/blog\/wp-content\/uploads\/2014\/03\/peopledetect-300x225.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><br \/>\n&nbsp;<br \/>\n<!--more--><\/p>\n<pre lang=\"java\">\r\nimport processing.video.*;\r\n\r\nimport java.util.*;\r\nimport java.nio.*;\r\n\r\nimport org.opencv.core.Core;\r\nimport org.opencv.core.Mat;\r\nimport org.opencv.core.CvType;\r\nimport org.opencv.core.Scalar;\r\nimport org.opencv.objdetect.HOGDescriptor;\r\nimport org.opencv.core.MatOfRect;\r\nimport org.opencv.core.MatOfDouble;\r\nimport org.opencv.core.Rect;\r\nimport org.opencv.core.Size;\r\nimport org.opencv.imgproc.Imgproc;\r\n\r\nCapture cap;\r\nPImage small;\r\nHOGDescriptor hog;\r\n\r\nbyte [] bArray;\r\nint [] iArray;\r\nint pixCnt1, pixCnt2;\r\nint w, h;\r\nfloat ratio;\r\n\r\nvoid setup() {\r\n  size(640, 480);\r\n  ratio = 0.5;\r\n  w = int(width*ratio);\r\n  h = int(height*ratio);\r\n\r\n  background(0);\r\n  \/\/ Define and initialise the default capture device.\r\n  cap = new Capture(this, width, height);\r\n  cap.start();\r\n\r\n  \/\/ Load the OpenCV native library.\r\n  System.loadLibrary(Core.NATIVE_LIBRARY_NAME);\r\n  println(Core.VERSION);\r\n\r\n  \/\/ pixCnt1 is the number of bytes in the pixel buffer.\r\n  \/\/ pixCnt2 is the number of integers in the PImage pixels buffer.\r\n  pixCnt1 = w*h*4;\r\n  pixCnt2 = w*h;\r\n\r\n  \/\/ bArray is the temporary byte array buffer for OpenCV cv::Mat.\r\n  \/\/ iArray is the temporary integer array buffer for PImage pixels.\r\n  bArray = new byte[pixCnt1];\r\n  iArray = new int[pixCnt2];\r\n\r\n  small = createImage(w, h, ARGB);\r\n  hog = new HOGDescriptor();\r\n  hog.setSVMDetector(HOGDescriptor.getDefaultPeopleDetector());\r\n  noFill();\r\n  stroke(255, 255, 0);\r\n}\r\n\r\nvoid draw() {\r\n  if (cap.available()) {\r\n    cap.read();\r\n  } \r\n  else {\r\n    return;\r\n  }\r\n  image(cap, 0, 0);\r\n  \/\/ Resize the video to a smaller PImage.\r\n  small.copy(cap, 0, 0, width, height, 0, 0, w, h);\r\n  \/\/ Copy the webcam image to the temporary integer array iArray.\r\n  arrayCopy(small.pixels, iArray);\r\n\r\n  \/\/ Define the temporary Java byte and integer buffers. \r\n  \/\/ They share the same storage.\r\n  ByteBuffer bBuf = ByteBuffer.allocate(pixCnt1);\r\n  IntBuffer iBuf = bBuf.asIntBuffer();\r\n\r\n  \/\/ Copy the webcam image to the byte buffer iBuf.\r\n  iBuf.put(iArray);\r\n\r\n  \/\/ Copy the webcam image to the byte array bArray.\r\n  bBuf.get(bArray);\r\n\r\n  \/\/ Create the OpenCV cv::Mat.\r\n  Mat m1 = new Mat(h, w, CvType.CV_8UC4);\r\n\r\n  \/\/ Initialise the matrix m1 with content from bArray.\r\n  m1.put(0, 0, bArray);\r\n  \/\/ Prepare the grayscale matrix.\r\n  Mat m3 = new Mat(h, w, CvType.CV_8UC1);\r\n  Imgproc.cvtColor(m1, m3, Imgproc.COLOR_BGRA2GRAY);\r\n\r\n  MatOfRect found = new MatOfRect();\r\n  MatOfDouble weight = new MatOfDouble();\r\n\r\n  hog.detectMultiScale(m3, found, weight, 0, new Size(8, 8), new Size(32, 32), 1.05, 2, false);\r\n\r\n  Rect [] rects = found.toArray();\r\n  if (rects.length > 0) {\r\n    for (int i=0; i<rects.length; i++) {\r\n      rect(rects[i].x\/ratio, rects[i].y\/ratio, rects[i].width\/ratio, rects[i].height\/ratio);\r\n    }\r\n  }\r\n  text(\"Frame Rate: \" + round(frameRate), 500, 50);\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is the original OpenCV people detection example ported to Processing with the Java library of OpenCV 2.4.8. It can achieve more than 20 frames per second. Here is a sample test snapshot. &nbsp; &nbsp;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79,66],"tags":[3,16,62],"class_list":["post-1246","post","type-post","status-publish","format-standard","hentry","category-software-2","category-testing","tag-opencv","tag-people-detection","tag-processing-org"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/comments?post=1246"}],"version-history":[{"count":4,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1246\/revisions"}],"predecessor-version":[{"id":1269,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1246\/revisions\/1269"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1246"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}