Python and cv2

Joined
Mar 11, 2012
Messages
62
Reaction score
0
Points
6
Your Mac's Specs
MacBookPro early 2012running Mavericks,MacMini Core i7 running Mavericks: iPhone 4s, iPad 3rd Gen
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
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
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.
 
OP
T
Joined
Mar 11, 2012
Messages
62
Reaction score
0
Points
6
Your Mac's 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
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
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!
 
OP
T
Joined
Mar 11, 2012
Messages
62
Reaction score
0
Points
6
Your Mac's 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; }
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
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.
 
OP
T
Joined
Mar 11, 2012
Messages
62
Reaction score
0
Points
6
Your Mac's 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
 

vansmith

Senior Member
Joined
Oct 19, 2008
Messages
19,924
Reaction score
559
Points
113
Location
Queensland
Your Mac's Specs
Mini (2014, 2018, 2020), MBA (2020), iPad Pro (2018), iPhone 13 Pro Max, Watch (S6)
import cv2

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.
 
OP
T
Joined
Mar 11, 2012
Messages
62
Reaction score
0
Points
6
Your Mac's Specs
MacBookPro early 2012running Mavericks,MacMini Core i7 running Mavericks: iPhone 4s, iPad 3rd Gen
I think the answer is to stick to linux !!!
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top