Artificial intelligenceCodingHighlevel

Up and Running with Python & OpenCV

Introduction:

Introduction to the AI : in this manipulation we will have our first python program with OpenCV libraries.

Installing OpenCV and pip

sudo apt-get update && sudo apt-get upgrade 
sudo apt-get install build-essential cmake pkg-config 
sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev 
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev 
sudo apt-get install libgtk2.0-dev libgtk-3-dev 
sudo apt-get install libatlas-base-dev gfortran 
sudo apt-get install python2.7-dev python3-dev 

wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.3.0.zip
unzip opencv_contrib.zip 

installing pip

wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
sudo python3 get-pip.py

writing our program:

After having installing Python, Python libraries and the pip, make a new file name it : “pythonOpenCV.py”

cd /home/pi/Documents/ 
mkdir pythonOpenCV
cd  pythonOpenCV 
sudo nano  pythonOpenCV.py

Type this bloc of code :

#!/usr/bin/env python
import numpy as np
import cv2

img = cv2.imread('pythonOpenCV.png')
cv2.imshow('My photo', img)
cv2.waitKey(0)
cv2.destroyAllWindows() 

Ctr+X and type Y to exit and save the file;

Execute the code using :

sudo python pythonOpenCV.py

Making the code executable :

In order to make the file executable type :

$ sudo chmod +x pythonOpenCV.py
$ ./pythonOpenCV.py 

Results :

There will appear a window containing the photo that is in ‘pythonOpenCV.py’ in the same folder as our program.

Downloading:

Get the code form gist :

https://gist.github.com/salhina/3e17021f2bb41c12d871b5caf2494745

Get the repository :

One time coding :

cd /home/pi/Documents/ && git clone https://github.com/salhina/PythonOpenCV
cd PythonOpenCV 
sudo chmod +x pythonOpenCV.py && ./pythonOpenCV.py 

Leave a Reply

Your email address will not be published. Required fields are marked *