Results 1 to 9 of 9

Thread: Python and cv2

  1. #1
    Python and cv2

    Member Since
    Mar 11, 2012
    Posts
    62
    Specs:
    MacBookPro early 2012running Mavericks,MacMini Core i7 running Mavericks: iPhone 4s, iPad 3rd Gen
    Python and cv2
    I am taking my first steps in python 2.7 and am using Mountain Lion on a MacBook Pro
    I cannot see what I am doing wrong re cv2

    After installing Xcode and command tools, then Macports, I then used the following in a terminal

    sudo port selfupdate
    sudo port install py27-numpy
    sudo port install opencv +python27

    All went through without errors.
    However when I enter "import cv2" in python it returns "no module named cv2"

    Any help most appreciated

  2. #2
    Python and cv2
    vansmith's Avatar
    Member Since
    Oct 19, 2008
    Location
    Toronto
    Posts
    19,390
    Specs:
    2012 13" MBP (2.5 i5, 8GB)
    The module isn't called "cv2" which would explain the import problem. As you can see here, opencv is broken up into a variety of different modules.
    Important Links: Community Guidelines : Use the reputation system if you've been helped.
    M-F Blog :: Write for the blog
    Writing a Quality Post

  3. #3
    Python and cv2

    Member Since
    Mar 11, 2012
    Posts
    62
    Specs:
    MacBookPro early 2012running Mavericks,MacMini Core i7 running Mavericks: iPhone 4s, iPad 3rd Gen
    Thanks for that
    I had assumed that it was just like linux where it is called by import cv2
    I have spent hours looking in the wrong direction. ie paths and the like
    Much appreciated

  4. #4
    Python and cv2
    vansmith's Avatar
    Member Since
    Oct 19, 2008
    Location
    Toronto
    Posts
    19,390
    Specs:
    2012 13" MBP (2.5 i5, 8GB)
    I'm not sure why the Linux build would be any different but the package maintainers for your distro of choice may have patched opencv. I'm not sure why they would since the opencv devs are explicit with their naming conventions. But, whatever. Glad you go it working!
    Important Links: Community Guidelines : Use the reputation system if you've been helped.
    M-F Blog :: Write for the blog
    Writing a Quality Post

  5. #5
    Python and cv2

    Member Since
    Mar 11, 2012
    Posts
    62
    Specs:
    MacBookPro early 2012running Mavericks,MacMini Core i7 running Mavericks: iPhone 4s, iPad 3rd Gen
    Unfortunately I am stuck again !!
    Reading through the opencv2 reference manual I tried the following code but it still throws an error on line using namespace cv;
    Can you advise ?

    #!/usr/bin/python
    #include "opencv2/core/core.hpp"
    #include "cv.h"
    #include "highgui.h"
    using namespace cv;
    int main(int, char**) {
    VideoCapture cap(0); if(!cap.isOpened()) return -1;
    Mat frame, edges; namedWindow("edges",1); for(;
    {
    cap >> frame;
    cvtColor(frame, edges, CV_BGR2GRAY); GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5); Canny(edges, edges, 0, 30, 3);
    imshow("edges", edges);
    if(waitKey(30) >= 0) break;
    }
    return 0; }

  6. #6
    Python and cv2
    vansmith's Avatar
    Member Since
    Oct 19, 2008
    Location
    Toronto
    Posts
    19,390
    Specs:
    2012 13" MBP (2.5 i5, 8GB)
    Hold on here, why are you mixing Python code and C? You initially tried to import opencv as a Python module and now you're trying to import opencv headers in a C application.
    Important Links: Community Guidelines : Use the reputation system if you've been helped.
    M-F Blog :: Write for the blog
    Writing a Quality Post

  7. #7
    Python and cv2

    Member Since
    Mar 11, 2012
    Posts
    62
    Specs:
    MacBookPro early 2012running Mavericks,MacMini Core i7 running Mavericks: iPhone 4s, iPad 3rd Gen
    Sorry about that, I had assumed the reference manual was all about python
    I had blindly copied and pasted !!

    So I want to capture from the web cam
    #!/usr/bin/python
    from here what do I need

    this is what I use in windows python
    import cv2
    import numpy as np
    c = cv2.VideoCapture(0)

    while(1):
    _,f = c.read()
    cv2.imshow('e2',f)
    if cv2.waitKey(5)==27:
    break
    cv2.destroyAllWindows()

    I am well aware I have much reading to do, but was hoping to use the webcam as something to build code round as I progress

  8. #8
    Python and cv2
    vansmith's Avatar
    Member Since
    Oct 19, 2008
    Location
    Toronto
    Posts
    19,390
    Specs:
    2012 13" MBP (2.5 i5, 8GB)
    Quote Originally Posted by TamPepper View Post
    import cv2
    Quote Originally Posted by vansmith View Post
    The module isn't called "cv2" which would explain the import problem. As you can see here, opencv is broken up into a variety of different modules.
    I think I found your problem.

    Execute the following at the command line:
    Code:
    python -c "help('modules')"
    I think I might be able to find the right one in that list.
    Important Links: Community Guidelines : Use the reputation system if you've been helped.
    M-F Blog :: Write for the blog
    Writing a Quality Post

  9. #9
    Python and cv2

    Member Since
    Mar 11, 2012
    Posts
    62
    Specs:
    MacBookPro early 2012running Mavericks,MacMini Core i7 running Mavericks: iPhone 4s, iPad 3rd Gen
    I think the answer is to stick to linux !!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Using Python
    By AllanGreen in forum OS X - Apps and Games
    Replies: 8
    Last Post: 07-07-2012, 10:11 PM
  2. Using TextMate with Python
    By xxViennaxx in forum OS X - Development and Darwin
    Replies: 0
    Last Post: 04-06-2012, 01:40 PM
  3. Python
    By NickBird in forum Switcher Hangout
    Replies: 0
    Last Post: 11-14-2008, 01:54 AM
  4. Replies: 4
    Last Post: 03-20-2005, 05:17 PM
  5. Python Problems
    By dr_springfield in forum OS X - Development and Darwin
    Replies: 3
    Last Post: 05-12-2004, 01:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •