{"id":555,"date":"2014-08-20T14:45:56","date_gmt":"2014-08-20T13:45:56","guid":{"rendered":"http:\/\/ofalcao.pt\/blog\/?p=555"},"modified":"2014-08-20T14:45:56","modified_gmt":"2014-08-20T13:45:56","slug":"computer-vision-com-lego","status":"publish","type":"post","link":"https:\/\/ofalcao.pt\/blog\/2014\/computer-vision-com-lego","title":{"rendered":"Computer Vision com LEGO"},"content":{"rendered":"<p>Com a webcam a funcionar e com tanta coisa em comum com a comunidade Raspberry Pi ocorreu-me experimentar o <a title=\"OpenCV: Open Source Computer Vision\" href=\"http:\/\/opencv.org\/\">OpenCV<\/a> e testar as capacidades do EV3\u00a0 no campo da Computer Vision.<\/p>\n<p>E afinal nem \u00e9 muito dif\u00edcil come\u00e7ar: segundo o artigo &#8216;<a href=\"https:\/\/realpython.com\/blog\/python\/face-recognition-with-python\/\">Face Recognition With Python, in Under 25 Lines of Code<\/a>&#8216; s\u00e3o mesmo suficientes menos de 25 linhas de c\u00f3digo em Python.<\/p>\n<p>Em vez de instalar o opencv todo, instalei apenas o m\u00f3dulo para python:<\/p>\n<pre>root@ev3dev:~# apt-get install python-opencv<\/pre>\n<p>Depois foi s\u00f3 criar o script &#8216;teste-opencv.py&#8217;:<\/p>\n<pre>import numpy as np\nimport cv2\n\nimagePath = \"\/root\/teste-face.jpg\"\ncascPath = \"\/root\/haarcascade_frontalface_default.xml\"\nresultPath =\"\/root\/resultado.jpg\"\n\nfaceCascade = cv2.CascadeClassifier(cascPath)\n\nimage = cv2.imread(imagePath)\ngray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n\nfaces = faceCascade.detectMultiScale(\n\u00a0\u00a0\u00a0 gray,\n\u00a0\u00a0\u00a0 scaleFactor=1.1,\n\u00a0\u00a0\u00a0 minNeighbors=5,\n\u00a0\u00a0\u00a0 minSize=(30, 30),\n\u00a0\u00a0\u00a0 flags = cv2.cv.CV_HAAR_SCALE_IMAGE\n)\n\nprint \"Found {0} faces!\".format(len(faces))\n\n# Draw a rectangle around the faces\nfor (x, y, w, h) in faces:\n\u00a0\u00a0\u00a0 cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)\n\ncv2.imwrite(resultPath,image)<\/pre>\n<p>Como n\u00e3o instalei o opencv n\u00e3o tenho os ficheiros xml com regras de classifica\u00e7\u00e3o. Para este exemplo o autor do artigo referido disponibiliza tamb\u00e9m o ficheiro &#8216;<a href=\"https:\/\/github.com\/shantnu\/FaceDetect\/blob\/master\/haarcascade_frontalface_default.xml\">haarcascade_frontalface_default.xml<\/a>&#8216;.<\/p>\n<p>Para esta foto &#8216;teste-face.jpg&#8217;:<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/151.236.52.166\/blog\/wp-content\/uploads\/2014\/08\/teste-face02.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-556\" src=\"https:\/\/i0.wp.com\/151.236.52.166\/blog\/wp-content\/uploads\/2014\/08\/teste-face02.jpg?resize=640%2C480\" alt=\"teste-face02\" width=\"640\" height=\"480\" srcset=\"https:\/\/i0.wp.com\/ofalcao.pt\/blog\/wp-content\/uploads\/2014\/08\/teste-face02.jpg?w=640&amp;ssl=1 640w, https:\/\/i0.wp.com\/ofalcao.pt\/blog\/wp-content\/uploads\/2014\/08\/teste-face02.jpg?resize=300%2C225&amp;ssl=1 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a>O resultado do script, ao fim de 3 a 5 minutos de CPU a 80% foi este:<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/151.236.52.166\/blog\/wp-content\/uploads\/2014\/08\/resultado02.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-557\" src=\"https:\/\/i0.wp.com\/151.236.52.166\/blog\/wp-content\/uploads\/2014\/08\/resultado02.jpg?resize=640%2C480\" alt=\"resultado02\" width=\"640\" height=\"480\" srcset=\"https:\/\/i0.wp.com\/ofalcao.pt\/blog\/wp-content\/uploads\/2014\/08\/resultado02.jpg?w=640&amp;ssl=1 640w, https:\/\/i0.wp.com\/ofalcao.pt\/blog\/wp-content\/uploads\/2014\/08\/resultado02.jpg?resize=300%2C225&amp;ssl=1 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a>Abre um mundo de possibilidades, n\u00e3o abre?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Com a webcam a funcionar e com tanta coisa em comum com a comunidade Raspberry Pi ocorreu-me experimentar o OpenCV e testar as capacidades do EV3\u00a0 no campo da Computer Vision. E afinal nem \u00e9 muito dif\u00edcil come\u00e7ar: segundo o artigo &#8216;Face Recognition With Python, in Under 25 Lines of Code&#8216; s\u00e3o mesmo suficientes menos &hellip; <a href=\"https:\/\/ofalcao.pt\/blog\/2014\/computer-vision-com-lego\" class=\"more-link\">Continuar a ler<span class=\"screen-reader-text\"> &#8220;Computer Vision com LEGO&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[16,18,19,20],"tags":[47,92,223],"series":[],"class_list":["post-555","post","type-post","status-publish","format-standard","hentry","category-geek","category-lego","category-lego-mindstorms","category-linux","tag-computer-vision","tag-opencv","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2Mhyv-8X","_links":{"self":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/555","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/comments?post=555"}],"version-history":[{"count":0,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/555\/revisions"}],"wp:attachment":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/media?parent=555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/categories?post=555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/tags?post=555"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/series?post=555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}