{"id":64,"date":"2009-06-02T14:15:08","date_gmt":"2009-06-02T04:15:08","guid":{"rendered":"http:\/\/www.computer-vision-software.com\/blog\/?p=64"},"modified":"2009-11-30T13:12:04","modified_gmt":"2009-11-30T03:12:04","slug":"opencv-haartraining-detect-objects-using-haar-like-features","status":"publish","type":"post","link":"http:\/\/www.computer-vision-software.com\/blog\/2009\/06\/opencv-haartraining-detect-objects-using-haar-like-features\/","title":{"rendered":"OpenCV Haartraining: Detect objects using Haar-like features"},"content":{"rendered":"<p style=\"padding-left: 30px;\">\n<p style=\"padding-left: 30px; text-align: justify;\">OpenCV&#8217;s standart cascades allow to detect faces and eyes.\u00a0 I wanted to create cascade in similar way to detect another objects:\u00a0 pringles or plate for example.\u00a0 I found some material in Net how to use OpenCV training tools, also I investigated training tool&#8217;s source\u00a0 code myself\u00a0 to found out, what training parameters can be tuned.<\/p>\n<h3><span style=\"color: #000000;\"><strong>Prepare images.<\/strong><\/span><\/h3>\n<p style=\"text-align: justify;\">For training, I needed thousands of images, containing my object with different lightning conditions and perspectives. After trying to find required number of pictures with Google ,\u00a0 I understood, that it&#8217;s really difficult task =).\u00a0 So I decided <span style=\"text-decoration: underline;\">to take video with my object<\/span>,\u00a0 then I wrote simple program to crop object from video, frame by frame.\u00a0 In such way,\u00a0 I generated about 3000 positive samples (cropped images\u00a0 with my object).\u00a0 Resolution varied from 50&#215;50 to 100&#215;100.\u00a0 The advantage of this method &#8211; you\u00a0 get many samples with different reflections, illuminations and backgrounds.\u00a0 It&#8217;s very important, that all these images &#8220;features&#8221; are various!<\/p>\n<p style=\"text-align: justify;\"><!--more--><\/p>\n<p style=\"text-align: justify;\">Negative samples I got\u00a0 from Internet public databases&#8230;, there were about 5000 images of different resolution, which did not contain my objects.\u00a0\u00a0 I worked with BMP format, due to compression artefacts in some compression levels of JPEG.<\/p>\n<h3><strong>Sample\/Test data creation.<\/strong><\/h3>\n<p>So I split all my images into 4 sets:<\/p>\n<ul>\n<li>Positive images, containing my object &#8211; about 3000 images, for training.<\/li>\n<li>Negative images &#8211; about 5000 images, for training.<\/li>\n<li>Positive images &#8211; about 100 for testing.<\/li>\n<li>Negative images &#8211; about 100 for testing.<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">Of course, I could use the same images for testing and training, but it&#8217;s not good.\u00a0 Positive images should be be packed to OpenCV vec-file.\u00a0 it can be done with createsamples program from OpenCV.\u00a0 When packed, all images are resized to 20&#215;20 &#8211; it&#8217;s good size for training speed.\u00a0 Also during object detection, object with smaller size will not be found.\u00a0\u00a0 For negative samples,\u00a0 I only created negative.dat(list of negative file names) with win dir \\b command.<\/p>\n<h3><strong>Training.<\/strong><\/h3>\n<p style=\"text-align: justify;\">OpenCV implements Adaboost algorithm in haartraining. exe.\u00a0 This tool\u00a0 generates xml cascade file, which is used in haardetection tool for object detection.\u00a0 There are some main characteristics of cascade:\u00a0 number of stages, type of classifiers on each stage.\u00a0These parameters can be configured when you launch haartraining. I configured it as per my training data (positives and negatives).<\/p>\n<p style=\"padding-left: 30px;\">\n<p style=\"text-align: justify;\">Training is finished, when required false alarm of cascade is achieved.\u00a0 So cascade can contain less stages than nstage &#8211; it&#8217;s ok. It&#8217;s rather worse, when we achieve required number of stage, but required false alarm is not. It&#8217;s signal, that something is wrong either with content or with haartraining parameters.\u00a0 Also maxfalsealarm is false alarm of stage, false alarm of cascade is multiplication of stage&#8217;s alarms.<\/p>\n<p style=\"text-align: justify;\">I used P4 2.7GHZ with 2 GB and it took about !6 days! of permanent working to finally get cascade.\u00a0 If you have Hyper Threading, you can recompile haartraining.exe with OpenMP support, it will speed up process a little bit =).<\/p>\n<h3><strong>Testing.<\/strong><\/h3>\n<p>Finally I got haarcascade.xml file. OpenCV perfomance.exe tool can be used for testing cascade.\u00a0 I tested it on my testing content\u00a0 &#8211; sorry for pun =).<\/p>\n\n\t\t<style type=\"text\/css\">\n\t\t\t#gallery-1 {\n\t\t\t\tmargin: auto;\n\t\t\t}\n\t\t\t#gallery-1 .gallery-item {\n\t\t\t\tfloat: left;\n\t\t\t\tmargin-top: 10px;\n\t\t\t\ttext-align: center;\n\t\t\t\twidth: 25%;\n\t\t\t}\n\t\t\t#gallery-1 img {\n\t\t\t\tborder: 2px solid #cfcfcf;\n\t\t\t}\n\t\t\t#gallery-1 .gallery-caption {\n\t\t\t\tmargin-left: 0;\n\t\t\t}\n\t\t\t\/* see gallery_shortcode() in wp-includes\/media.php *\/\n\t\t<\/style>\n\t\t<div id='gallery-1' class='gallery galleryid-64 gallery-columns-4 gallery-size-thumbnail'><dl class='gallery-item'>\n\t\t\t<dt class='gallery-icon landscape'>\n\t\t\t\t<a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/www.computer-vision-software.com\/blog\/2009\/06\/opencv-haartraining-detect-objects-using-haar-like-features\/picture1\/');\"  href='http:\/\/www.computer-vision-software.com\/blog\/2009\/06\/opencv-haartraining-detect-objects-using-haar-like-features\/picture1\/'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.computer-vision-software.com\/blog\/wp-content\/uploads\/2009\/06\/picture1-150x150.jpg\" class=\"attachment-thumbnail size-thumbnail\" alt=\"\" \/><\/a>\n\t\t\t<\/dt><\/dl><dl class='gallery-item'>\n\t\t\t<dt class='gallery-icon landscape'>\n\t\t\t\t<a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/www.computer-vision-software.com\/blog\/2009\/06\/opencv-haartraining-detect-objects-using-haar-like-features\/attachment\/11\/');\"  href='http:\/\/www.computer-vision-software.com\/blog\/2009\/06\/opencv-haartraining-detect-objects-using-haar-like-features\/attachment\/11\/'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.computer-vision-software.com\/blog\/wp-content\/uploads\/2009\/06\/11-150x150.png\" class=\"attachment-thumbnail size-thumbnail\" alt=\"\" \/><\/a>\n\t\t\t<\/dt><\/dl><dl class='gallery-item'>\n\t\t\t<dt class='gallery-icon landscape'>\n\t\t\t\t<a onclick=\"javascript:pageTracker._trackPageview('\/outgoing\/www.computer-vision-software.com\/blog\/2009\/06\/opencv-haartraining-detect-objects-using-haar-like-features\/attachment\/2\/');\"  href='http:\/\/www.computer-vision-software.com\/blog\/2009\/06\/opencv-haartraining-detect-objects-using-haar-like-features\/attachment\/2\/'><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"150\" src=\"http:\/\/www.computer-vision-software.com\/blog\/wp-content\/uploads\/2009\/06\/2-150x150.png\" class=\"attachment-thumbnail size-thumbnail\" alt=\"\" \/><\/a>\n\t\t\t<\/dt><\/dl>\n\t\t\t<br style='clear: both' \/>\n\t\t<\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>OpenCV&#8217;s standart cascades allow to detect faces and eyes.\u00a0 I wanted to create cascade in similar way to detect another objects:\u00a0 pringles or plate for example.\u00a0 I found some material in Net how to use OpenCV training tools, also I investigated training tool&#8217;s source\u00a0 code myself\u00a0 to found out, what training parameters can be tuned. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[84],"tags":[31,44,36,6,30],"class_list":["post-64","post","type-post","status-publish","format-standard","hentry","category-opencv","tag-haar","tag-object-detection","tag-object-recognition","tag-opencv","tag-viola-jones"],"_links":{"self":[{"href":"http:\/\/www.computer-vision-software.com\/blog\/wp-json\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.computer-vision-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.computer-vision-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.computer-vision-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.computer-vision-software.com\/blog\/wp-json\/wp\/v2\/comments?post=64"}],"version-history":[{"count":0,"href":"http:\/\/www.computer-vision-software.com\/blog\/wp-json\/wp\/v2\/posts\/64\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.computer-vision-software.com\/blog\/wp-json\/wp\/v2\/media?parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.computer-vision-software.com\/blog\/wp-json\/wp\/v2\/categories?post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.computer-vision-software.com\/blog\/wp-json\/wp\/v2\/tags?post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}