{"id":1625,"date":"2016-06-21T12:09:17","date_gmt":"2016-06-21T12:09:17","guid":{"rendered":"https:\/\/www.gravityjack.com\/?p=1625"},"modified":"2023-11-29T12:09:38","modified_gmt":"2023-11-29T12:09:38","slug":"opencv-python-3-homebrew","status":"publish","type":"post","link":"https:\/\/www.gravityjack.com\/news\/opencv-python-3-homebrew\/","title":{"rendered":"OpenCV, Python 3, and Homebrew Walkthrough"},"content":{"rendered":"\n<p><em>Post by Gravity Jack research team member, Marc Rollins.<\/em><\/p>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"toc_1\">Introduction<\/h5>\n\n\n\n<p>Do you want to use OpenCV, but can&#8217;t get it to work with Python 3? You&#8217;ve come to the right place! Here at Gravity Jack we tackle computer vision problems using <a href=\"http:\/\/opencv.org\">OpenCV<\/a> as one of the tools in our arsenal. It\u2019s very useful for implementing computer vision algorithms, but getting the library installed properly isn\u2019t always easy. Since our research team all develops on OS X, we also make use of the <a href=\"http:\/\/brew.sh\/\">Homebrew<\/a> package manager. Usually Homebrew makes dependency management a breeze, but OpenCV is a curious exception to this rule. This seems especially true when trying to use the Python bindings that OpenCV provides. Even though OpenCV includes the Python 2 bindings by default, the same is not true of Python 3. What follows are the instructions to correctly install.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"toc_2\">Installation<\/h5>\n\n\n\n<p>I\u2019ll assume that Homebrew is up-to-date and <code>brew doctor<\/code> doesn&#8217;t bring up anything serious before installing OpenCV. There are two steps to correctly install Python support. First install the <code>opencv3<\/code> package with Homebrew, and then tell Python 3 where to look for the OpenCV binary:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ brew install opencv3 --with-python3 --c++11 --with-contrib\n$ brew link --force opencv3<\/code><\/pre>\n\n\n\n<p>Then fire up Python and import the OpenCV module. Despite OpenCV 3 being installed, the Python package is still named <code>cv2<\/code>. Everything should go off without a hitch:<\/p>\n\n\n\n<p><a id=\"Additional-step-for-20virtualenv\"><\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python3\nPython 3.5.1 (default, May 20 2016, 12:52:19)\n&#91;GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n&gt;&gt;&gt; import cv2\n&gt;&gt;&gt; print(cv2.__version__)\n3.1.0\n&gt;&gt;&gt; # It worked!<\/code><\/pre>\n\n\n\n<h6 class=\"wp-block-heading\" id=\"toc_3\">Additional step for virtualenv users<\/h6>\n\n\n\n<p>So far I&#8217;ve assumed you&#8217;re working with the standard Python installation. If you&#8217;re using a virtual environment, there is one more step to get Python to recognize the OpenCV package. Open a command prompt and <code>cd<\/code> to the directory where your virtualenv is installed. Here I assume that the virtual environment exists in the <code>venv<\/code> directory. Then run this command:<br><code><br>echo \/usr\/local\/opt\/opencv3\/lib\/python3.5\/site-packages &gt;&gt; venv\/lib\/python3.5\/site-packages\/opencv3.pth<br><\/code><br>And you should now be able to <code>import cv2<\/code> from within your virtual environment! If you have a different Python version than <code>3.5<\/code>, you will need to change the major version numbers in the command above. (i.e. If you are using Python 3.4.3, replace <code>python3.5<\/code> above with <code>python3.4<\/code>)<\/p>\n\n\n\n<p>If something went wrong, check out the <a href=\"#troubleshooting\">Troubleshooting<\/a> section below.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"toc_4\">Explanation<\/h5>\n\n\n\n<p>Let\u2019s break down the commands and options a little bit:<\/p>\n\n\n\n<p><code>brew install opencv3<\/code> &#8212; This specifies the package to install. Note that Homebrew has both an <code>opencv<\/code> and <code>opencv3<\/code> package. The <code>opencv<\/code> formula will install the latest version of OpenCV 2.<\/p>\n\n\n\n<p><code>--with-python3<\/code> &#8212; Tells Homebrew to include Python 3 support for OpenCV.<\/p>\n\n\n\n<p><code>--c++11<\/code> &#8212; Tells Homebrew to build with C++11 support. By now you&#8217;re hopefully using a compiler that supports modern C++!<\/p>\n\n\n\n<p><a id=\"troubleshooting\"><\/a><\/p>\n\n\n\n<p><code>--with-contrib<\/code> &#8212; This is optional, but recommended. The <code>contrib<\/code> module of OpenCV contains some experimental features, including a number of useful feature descriptors like SIFT, SURF, and FREAK.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"toc_5\">Troubleshooting<\/h5>\n\n\n\n<h6 class=\"wp-block-heading\" id=\"toc_6\">Import error<\/h6>\n\n\n\n<p>After opening Python and importing OpenCV, you may run into an <code>ImportError: No module named 'cv2'<\/code>. This error can occur for one of three reasons:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>You forgot to install using the <code>--with-python3<\/code> option.<\/strong> Don&#8217;t worry, it happens to the best of us! Simply <code>brew reinstall opencv3 --with-python3 --c++11 --with-contrib<\/code> to reinstall with Python 3 support. If that doesn&#8217;t work, go to the next step.<\/li>\n\n\n\n<li><strong>OpenCV was not linked correctly.<\/strong> Run the command <code>brew unlink opencv3 &amp;&amp; brew link --force opencv3<\/code>. That should successfully re-link the python libraries.<\/li>\n\n\n\n<li><strong>You&#8217;re inside a virtualenv.<\/strong> If you&#8217;re using a virtual environment, you may have forgotten the final installation step. Follow the instruction <a href=\"#Additional-step-for-20virtualenv\">above<\/a>.<\/li>\n<\/ol>\n\n\n\n<h6 class=\"wp-block-heading\" id=\"toc_7\">Undefined symbols error<\/h6>\n\n\n\n<p>During installation with <code>brew<\/code>, you may come across an undefined symbols error that looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Undefined symbols for architecture x86_64:\n  \"Imf_2_2::Chromaticities::Chromaticities(Imath_2_1::Vec2&lt;float&gt; const&amp;, Imath_2_1::Vec2&lt;float&gt; const&amp;, Imath_2_1::Vec2&lt;float&gt; const&amp;, Imath_2_1::Vec2&lt;float&gt; const&amp;)\", referenced from:\n      cv::ExrDecoder::ExrDecoder() in grfmt_exr.cpp.o\n  \"Imf_2_2::Header::Header(int, int, float, Imath_2_1::Vec2&lt;float&gt; const&amp;, float, Imf_2_2::LineOrder, Imf_2_2::Compression)\", referenced from:\n      cv::ExrEncoder::write(cv::Mat const&amp;, std::__1::vector&lt;int, std::__1::allocator&lt;int&gt; &gt; const&amp;) in grfmt_exr.cpp.o\nld: symbol(s) not found for architecture x86_64\nclang: error: linker command failed with exit code 1 (use -v to see invocation)\nmake&#91;2]: *** &#91;lib\/libopencv_imgcodecs.3.1.0.dylib] Error 1\nmake&#91;1]: *** &#91;modules\/imgcodecs\/CMakeFiles\/opencv_imgcodecs.dir\/all] Error 2\nmake: *** &#91;all] Error 2<\/code><\/pre>\n\n\n\n<p>If you happen to see this, the workaround is simple: add the <code>--without-openexr<\/code> option to your installation command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>install opencv3 --without-openexr --with-python3 --c++11 --with-contrib<\/code><\/pre>\n\n\n\n<p>This error has something to do with the <code>Imf_2_2<\/code> class, which is part of OpenEXR. Unless you specifically need OpenEXR, you can safely install OpenCV without it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Post by Gravity Jack research team member, Marc Rollins. Introduction Do you want to use OpenCV, but can&#8217;t get it to work with Python 3? You&#8217;ve come to the right place! Here at Gravity Jack we tackle computer vision problems using OpenCV as one of the tools in our arsenal. It\u2019s very useful for implementing <a href=\"https:\/\/www.gravityjack.com\/news\/opencv-python-3-homebrew\/\" class=\"more-link\">&#8230;<span class=\"screen-reader-text\">  OpenCV, Python 3, and Homebrew Walkthrough<\/span><\/a><\/p>\n","protected":false},"author":9,"featured_media":1626,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1625","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>OpenCV and Python Tutorial with Homebrew | Developer Walkthrough<\/title>\n<meta name=\"description\" content=\"Need to use OpenCV with Python 3 but are having issues? This developer tutorial helps walk through the installation process, including help for VirtualEnv.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.gravityjack.com\/news\/opencv-python-3-homebrew\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"OpenCV and Python Tutorial with Homebrew | Developer Walkthrough\" \/>\n<meta property=\"og:description\" content=\"Need to use OpenCV with Python 3 but are having issues? This developer tutorial helps walk through the installation process, including help for VirtualEnv.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.gravityjack.com\/news\/opencv-python-3-homebrew\/\" \/>\n<meta property=\"og:site_name\" content=\"Gravity Jack\" \/>\n<meta property=\"article:published_time\" content=\"2016-06-21T12:09:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-29T12:09:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.gravityjack.com\/wp-content\/uploads\/2023\/11\/gj_social_media_graphics_051216_facebook_shared_link_1024-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"344\" \/>\n\t<meta property=\"og:image:height\" content=\"180\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Gravity Admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Gravity Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.gravityjack.com\/news\/opencv-python-3-homebrew\/\",\"url\":\"https:\/\/www.gravityjack.com\/news\/opencv-python-3-homebrew\/\",\"name\":\"OpenCV and Python Tutorial with Homebrew | Developer Walkthrough\",\"isPartOf\":{\"@id\":\"https:\/\/www.gravityjack.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.gravityjack.com\/news\/opencv-python-3-homebrew\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.gravityjack.com\/news\/opencv-python-3-homebrew\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.gravityjack.com\/wp-content\/uploads\/2023\/11\/gj_social_media_graphics_051216_facebook_shared_link_1024-1.jpg\",\"datePublished\":\"2016-06-21T12:09:17+00:00\",\"dateModified\":\"2023-11-29T12:09:38+00:00\",\"author\":{\"@id\":\"https:\/\/www.gravityjack.com\/#\/schema\/person\/63c79a7e30c630a69e6e003ef7476fcc\"},\"description\":\"Need to use OpenCV with Python 3 but are having issues? This developer tutorial helps walk through the installation process, including help for VirtualEnv.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.gravityjack.com\/news\/opencv-python-3-homebrew\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.gravityjack.com\/news\/opencv-python-3-homebrew\/#primaryimage\",\"url\":\"https:\/\/www.gravityjack.com\/wp-content\/uploads\/2023\/11\/gj_social_media_graphics_051216_facebook_shared_link_1024-1.jpg\",\"contentUrl\":\"https:\/\/www.gravityjack.com\/wp-content\/uploads\/2023\/11\/gj_social_media_graphics_051216_facebook_shared_link_1024-1.jpg\",\"width\":344,\"height\":180},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.gravityjack.com\/#website\",\"url\":\"https:\/\/www.gravityjack.com\/\",\"name\":\"Gravity Jack\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.gravityjack.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.gravityjack.com\/#\/schema\/person\/63c79a7e30c630a69e6e003ef7476fcc\",\"name\":\"Gravity Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.gravityjack.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/aa3d42e00068a5cdefa39bab3155196c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/aa3d42e00068a5cdefa39bab3155196c?s=96&d=mm&r=g\",\"caption\":\"Gravity Admin\"},\"url\":\"https:\/\/www.gravityjack.com\/author\/gjmagravityjack-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"OpenCV and Python Tutorial with Homebrew | Developer Walkthrough","description":"Need to use OpenCV with Python 3 but are having issues? This developer tutorial helps walk through the installation process, including help for VirtualEnv.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.gravityjack.com\/news\/opencv-python-3-homebrew\/","og_locale":"en_US","og_type":"article","og_title":"OpenCV and Python Tutorial with Homebrew | Developer Walkthrough","og_description":"Need to use OpenCV with Python 3 but are having issues? This developer tutorial helps walk through the installation process, including help for VirtualEnv.","og_url":"https:\/\/www.gravityjack.com\/news\/opencv-python-3-homebrew\/","og_site_name":"Gravity Jack","article_published_time":"2016-06-21T12:09:17+00:00","article_modified_time":"2023-11-29T12:09:38+00:00","og_image":[{"width":344,"height":180,"url":"https:\/\/www.gravityjack.com\/wp-content\/uploads\/2023\/11\/gj_social_media_graphics_051216_facebook_shared_link_1024-1.jpg","type":"image\/jpeg"}],"author":"Gravity Admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Gravity Admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.gravityjack.com\/news\/opencv-python-3-homebrew\/","url":"https:\/\/www.gravityjack.com\/news\/opencv-python-3-homebrew\/","name":"OpenCV and Python Tutorial with Homebrew | Developer Walkthrough","isPartOf":{"@id":"https:\/\/www.gravityjack.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.gravityjack.com\/news\/opencv-python-3-homebrew\/#primaryimage"},"image":{"@id":"https:\/\/www.gravityjack.com\/news\/opencv-python-3-homebrew\/#primaryimage"},"thumbnailUrl":"https:\/\/www.gravityjack.com\/wp-content\/uploads\/2023\/11\/gj_social_media_graphics_051216_facebook_shared_link_1024-1.jpg","datePublished":"2016-06-21T12:09:17+00:00","dateModified":"2023-11-29T12:09:38+00:00","author":{"@id":"https:\/\/www.gravityjack.com\/#\/schema\/person\/63c79a7e30c630a69e6e003ef7476fcc"},"description":"Need to use OpenCV with Python 3 but are having issues? This developer tutorial helps walk through the installation process, including help for VirtualEnv.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.gravityjack.com\/news\/opencv-python-3-homebrew\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.gravityjack.com\/news\/opencv-python-3-homebrew\/#primaryimage","url":"https:\/\/www.gravityjack.com\/wp-content\/uploads\/2023\/11\/gj_social_media_graphics_051216_facebook_shared_link_1024-1.jpg","contentUrl":"https:\/\/www.gravityjack.com\/wp-content\/uploads\/2023\/11\/gj_social_media_graphics_051216_facebook_shared_link_1024-1.jpg","width":344,"height":180},{"@type":"WebSite","@id":"https:\/\/www.gravityjack.com\/#website","url":"https:\/\/www.gravityjack.com\/","name":"Gravity Jack","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.gravityjack.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.gravityjack.com\/#\/schema\/person\/63c79a7e30c630a69e6e003ef7476fcc","name":"Gravity Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.gravityjack.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/aa3d42e00068a5cdefa39bab3155196c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/aa3d42e00068a5cdefa39bab3155196c?s=96&d=mm&r=g","caption":"Gravity Admin"},"url":"https:\/\/www.gravityjack.com\/author\/gjmagravityjack-com\/"}]}},"_links":{"self":[{"href":"https:\/\/www.gravityjack.com\/wp-json\/wp\/v2\/posts\/1625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gravityjack.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gravityjack.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gravityjack.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gravityjack.com\/wp-json\/wp\/v2\/comments?post=1625"}],"version-history":[{"count":0,"href":"https:\/\/www.gravityjack.com\/wp-json\/wp\/v2\/posts\/1625\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gravityjack.com\/wp-json\/wp\/v2\/media\/1626"}],"wp:attachment":[{"href":"https:\/\/www.gravityjack.com\/wp-json\/wp\/v2\/media?parent=1625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gravityjack.com\/wp-json\/wp\/v2\/categories?post=1625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gravityjack.com\/wp-json\/wp\/v2\/tags?post=1625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}