$ brew install python3
· Pip3 isinstalled with Python3
Installation
To install virtualenv via pip run:
$ pip3 install virtualenv
Usage
Creation of virtualenv:
$ virtualenv -p python3 <desired-path>
Activate the virtualenv:
$ source <desired-path>/bin/activate
Deactivate the virtualenv:
$ deactivate
Step 2: Install OpenCV 3 for Python on Mac1. Install and update homebrew
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew update
2. Tap homebrew-bio
$ brew tap brewsci/bio
3. Install OpenCV with homebrew including Python 3
$ brew install opencv3 --with-contrib --with-python3
4. Change into the OpenCV directory
Since weinstalled OpenCV with Homebrew, the package was should be listed in homebrew'sCellar (/usr/local/Cellar/
) To see what's in the Brew Cellar, do this:
$ brew list
# or
$ ls /usr/local/Cellar/
Now, let's gointo the opencv
directory.
$ cd /usr/local/Cellar/opencv
$ ls
3.3.0_3 3.3.1_1
In my case, the numbers 3.3.0_3
and 3.3.1_1
are different versions of OpenCV. This is a goodthing. You'll want the highest version number listed here, in my case it's 3.3.1_1
$ cd 3.3.1_1
$ pwd
/usr/local/Cellar/opencv/3.3.1_1
Awesome. Thisis the OpenCV Version we want to use. We're very close to getting the cv2.<version>.so
we need.
5. Find the cv2.<version>.so file in OpenCV
Okay, so wehave the OpenCV version we're going to use and where it's located. Now we needto link it to our version of Python.
· What Python version are you using?
· $ python --version
· Python 2.7.10
· $ python3 --version
· Python 3.6.3
I have Python 3.6
and Python 2.7
installed on my system. If I want both versionsto have OpenCV, I can. I'll just need to add the correct cv2.<version>.so
to your Python's site packages. We'll do thispart in the next step. In the meantime, let's get the cv2.<version>.so
for each version of Python. You can installwhatever version you want.
Get the Python 3.6 cv2.<version>.so file path:
$ cd /usr/local/Cellar/opencv/3.3.1_1
$ ls
INSTALL_RECEIPT.json bin share
LICENSE include
README.md lib
$ cd lib
ls
# /usr/local/Cellar/opencv/3.3.1_1/lib
...
...
python2.7
python3.6
...
...
$ cd python3.6
$ cd site-packages
$ pwd
/usr/local/Cellar/opencv/3.3.1_1/lib/python3.6/site-packages
$ ls
cv2.cpython-36m-darwin.so # this name might be different but... bingo!
OpenCV Python3.6 Path: /usr/local/Cellar/opencv/3.3.1_1/lib/python3.6/site-packages/cv2.cpython-36m-darwin.so
Get the Python 2.7 cv2.<version>.so file path:
$ cd /usr/local/Cellar/opencv/3.3.1_1
$ ls
INSTALL_RECEIPT.json bin share
LICENSE include
README.md lib
$ cd lib
ls
# /usr/local/Cellar/opencv/3.3.1_1/lib
...
...
python2.7
python3.6
...
...
$ cd python2.7
$ cd site-packages
$ pwd
/usr/local/Cellar/opencv/3.3.1_1/lib/python3.6/site-packages
$ ls
cv2.so # this name might be different but... bingo!
OpenCV Python2.7 Path: /usr/local/Cellar/opencv/3.3.1_1/lib/python2.7/site-packages/cv2.so
Add OpenCV to Virtualenvs
All you haveto do is add the cv2.<version>.so
to your virtualenv's site-packages. Example:
$ mkdir ~/Dev
$ cd ~/Dev
$ virtualenv -p python3 newcvtest
$ cd newcvtest
$ source bin/activate
(newcvtest) $ python --version
Python 3.6.5
(newcvtest) $ pip install numpy
(newcvtest) $ cd lib/python3.6/site-packages
(newcvtest) $ ln -S /usr/local/Cellar/opencv/3.3.1_1/lib/python3.6/site-packages/cv2.cpython-36m-darwin.so cv2.so
$ mkdir ~/Dev
$ cd ~/Dev
$ virtualenv -p python2 newcvtestpy2
$ cd newcvtestpy2
$ source bin/activate
(newcvtestpy2) $ python --version
Python 2.7.10
(newcvtestpy2) $ pip install numpy
(newcvtestpy2) $ cd lib/python3.6/site-packages
(newcvtestpy2) $ ln -S /usr/local/Cellar/opencv/3.3.1_1/lib/python2.7/site-packages/cv2.so cv2.so
6. Test OpenCV Installation
$ python3 # or python2
>>> import cv2
>>> print(cv2.__version__)
3.3.1 # your version may be a newer one
Step 3: OpenCV and Python Webcam Quick TestCreate a Python file, say camera-test.py
containing:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',frame)
cv2.imshow('gray',gray)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Now run:
C:\> python camera-test.py
You should see two images from your webcam--one in gray scale, one in color just like the one above, albeit with your face!.
To quit, hit the key q
"on" the video window(s) to stop thecamera.
Intsalling opencv raspberry
https://www.alatortsev.com/2018/04/27/installing-opencv-on-raspberry-pi-3-b/
https://tutorials-raspberrypi.com/installing-opencv-on-the-raspberry-pi/
https://www.pyimagesearch.com/2017/09/04/raspbian-stretch-install-opencv-3-python-on-your-raspberry-pi/
Step 4: Installing ZBar (with Python Bindings) for Barcode DecodingInstalling ZBar for Ubuntu or Raspbian
Installing ZBar for Ubuntu can be accomplished with the following command:
$ sudo apt-get install libzbar0
Installing ZBar for macOS
Installing ZBar for macOSusing brew is equally as easy (assuming you have homebrew installed):
$ brew install zbar
Install pyzbar
Now that I have a Python 3 virtual environment named barcode on my machine, I activated the barcode environment (yours might have a different name) and installed pyzbar:
An OpenCV barcode and QR code scanner with ZBar
Shell
$ workon barcode$ pip install pyzbar
If you are not using a Pythonvirtual environment you can just do:
An OpenCV barcode and QR code scanner with ZBar
Python
$ pip install pyzbar install numpy $pip install numpy
Now match for OpenCV + ZBar + Python barcode scanner project! Press q to quit
Comments