INSTALLATION PYTHON 3.
X
Open Terminal/Command Prompt and type :
~ pip install opencv-python
GETTING STARTED (HOW TO READ IMAGES)
[Link] PyCharm.
[Link] cv2.
[Link] a test image in the directory.
[Link] variable to store image using imread() function.
5. Display the image using imshow() function.
6. Add a delay using a waitkey() function.
import cv2
# LOAD AN IMAGE USING 'IMREAD'
img = [Link]("Resources/[Link]")
# DISPLAY
[Link]("Lena Soderberg”, img)
[Link](0)
PLAYING VIDEO USING VideoCapture() FUNCTION
1. Open PyCharm.
2. Import cv2.
3. Paste a test video in the directory.
4. Create variable to store video using VideoCapture() function.
5. Create an infinite while loop to display each frame of the video continuously.
6. Display the video using imshow() function.
7. Add a delay using a waitkey() function.
import cv2
frameWidth = 640
frameHeight = 480
cap = [Link]("Resources/test_ video.mp4")
while True:
success, img = [Link]()
img = [Link](img, (frameWidth, frameHeight))
[Link]("Result", img)
break
ACCESSING LIVE FEED FROM WEBCAM
1. Open PyCharm.
2. Import cv2.
3. Create variable to store video using VideoCapture() function.
4. Pass parameter 0 in VideoCapture(0) to access webcam.
5. Create an infinite while loop to display each frame of the webcam’s video continuously.
6. Display the live feed using imshow() function.
7. Add a delay of infinity using waitKey(0).
import cv2
Width = 640
Height = 480
cap = [Link](0)
[Link](3, frameWidth)
[Link](4, frameHeight)
[Link](10, 150)
while True:
success, img = [Link]()
[Link]("Result", img)
break
FUNCTIONS OF OPENCV
Converting image to grayscale
1. Open PyCharm.
2. Import cv2.
3. Create variable to store image using imread() function.
4. To convert to grayscale use [Link]() function
5. Pass the parameter image location and COLOR_BGR2GRAY to convert.
import cv2
img = [Link]("Resources/[Link]")
imgGray = [Link](img, cv2.COLOR_BGR2GRAY)
[Link]("Gray Image", imgGray)
[Link](0)
Edge detection
1. Open PyCharm.
2. Import cv2.
3. Create variable to store image using imread() function.
4. To detect edge use [Link]() function
5. Pass the parameter image location and threshold to convert.
import cv2
img = [Link]("Resources/[Link]")
imgCanny = [Link](img, 150, 200)
[Link]("Canny Image”, imgCanny)
[Link](0)
CROPPING IMAGE
1. Import numpy and cv2.
2. Create two variables to store the height and width of the image.
3. Create two numpy arrays to store the coordinates.
4. First array - store the coordinates of the image to be cropped.
5. Second array - store the coordinates of the complete image.
6. Crop the image using getPerspective() and wrapPerspective() function.
image = [Link]("Assets/[Link]")
width, height = 250, 350
point1 = np.float32([[111, 219], [287, 188], [154, 482], [352, 440]])
point2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])
matrix = [Link](point1, point2)
Output = [Link](image, matrix, (width, height))
[Link]("Image”, image)
[Link]("Output”, Output)
[Link](0)
FACE DETECTION
1. Open PyCharm.
2. Import cv2.
[Link] a variable to store cascade classifier (to learn more about cascade classifier click
here.
3. Convert image to greyscale using [Link]() function.
4. Detect face using detectMultiscale() function.
5. Draw a rectangle around the detected face.
import cv2
face_Cascade = [Link]("Resources/haarcascade_frontalface_default.xml")
image = [Link]('Resources/[Link]')
imgGray = [Link](image, cv2.COLOR_BGR2GRAY)
faces = face_Cascade.detectMultiScale(imgGray, 1.1, 4)
for (x, y, w, h) in faces:
[Link](image, (x, y), (x + w, y + h), (255, 0, 0), 2)
[Link]("Result", image)
[Link](0)
# Basic image processing using OpenCV
import cv2
import requests
import numpy as np
import [Link] as plt
from io import BytesIO
# Fetch the image from the URL using requests
url = '[Link]
w=800&auto=format&fit=crop&q=60&ixlib=rb-
4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MXx8YmlyZHxlbnwwfHwwfHx8Mg%3D%3D'
response = [Link](url)
image = [Link](bytearray([Link]), dtype="uint8")
image = [Link](image, cv2.IMREAD_COLOR)
# Convert the image to grayscale
gray_image = [Link](image, cv2.COLOR_BGR2GRAY)
# Apply a Gaussian blur to the image
blurred_image = [Link](gray_image, (5, 5), 0)
# Detect edges using the Canny edge detection algorithm
edges = [Link](blurred_image, 100, 200)
# Display the original and processed images
[Link](figsize=(10, 7))
[Link](2, 2, 1)
[Link]('Original Image')
[Link]([Link](image, cv2.COLOR_BGR2RGB))
[Link]('off')
[Link](2, 2, 2)
[Link]('Grayscale Image')
[Link](gray_image, cmap='gray')
[Link]('off')
[Link](2, 2, 3)
[Link]('Blurred Image')
[Link](blurred_image, cmap='gray')
[Link]('off')
[Link](2, 2, 4)
[Link]('Edge Detection')
[Link](edges, cmap='gray')
[Link]('off')
plt.tight_layout()
[Link]()
Python
num_terms = 10
fibonacci = [0, 1]
while len(fibonacci) < num_terms:
next_term = fibonacci[-1] + fibonacci[-2]
[Link](next_term)
print(fibonacci)
output- [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
-----------------------------------------------------------------------------------------------------------------
import [Link] as plt
[Link]([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])
[Link]()
import pandas as pd
df = pd.read_csv("[Link]
[Link]")
[Link]()
mp cylinder displacemen horsepowe weigh acceleratio model_yea origi nam
g s t r t n r n e
chevrole
t
0 18.0 8 307.0 130.0 3504 12.0 70 usa
chevelle
malibu
buick
1 15.0 8 350.0 165.0 3693 11.5 70 usa skylark
320
plymout
2 18.0 8 318.0 150.0 3436 11.0 70 usa h
satellite
3 16.0 8 304.0 150.0 3433 12.0 70 usa amc
mp cylinder displacemen horsepowe weigh acceleratio model_yea origi nam
g s t r t n r n e
rebel sst
ford
4 17.0 8 302.0 140.0 3449 10.5 70 usa
torino
NUMPY
Usage – NumPy arrays come in two flavors: vectors and matrices.
Vectors are strictly one dimensional arrays.
Matrices are two dimensional arrays but can have only one row or one column.
NumPy Arrays
To create a one dimensional NumPy array from Python objects, like List:
1. Create a Python List: >>> my_list = [ 1, 2, 3 ]
2. Import NumPy: >>> import numpy as np
3. Cast and assign the array >>> arr = [Link]( my_list )
To create 2 dimensional NumPy arrays from Python List, cast list of list.
4. Create a Python List >>> my_mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
5. Cast and assign the array >>> arr = [Link]( my_mat )
Link : [Link]
To create a NumPy array with built in functions.
[ NOTE: NumPy is imported as np for the whole content ]
1
2
3
4
5
6
>>> [Link](start, stop, step)– creates a 1 d array >>>
[Link]() - create numpy array with value as 0. >>>
[Link]()– create numpy array with values as 1. >>>
[Link]()– creates numpy array with evenly spaced points. >>>
[Link]()– creates identity matrix. >>>
[Link]()– creates a matrix with random numbers.
Link: [Link]
Reshaping a NumPy array :
1
2
>>> arr = [Link](0, 25) # creates a 1 d array with[0, 1, 3… .24] >>>
[Link](5, 5) # converts the 1 d array with 25 values to a 5 x 5 matrix
Link : [Link]
Here is the link : [Link] to go through all the NumPy
array methods.
Some NumPy Methods
1
2
3
4
>>> [Link]()– copies the NumPy array >>>
[Link](axis = value)– returns max element(axis = 1[/ 0 for row / [col wise) >>>
[Link](axis = value)– returns min element >>>
[Link]()– returns the sum of all array elements
NumPy Operations
1
2
3
4
5
6
7
8
>>> arr = [Link](0, 11) >>>
arr + arr # Adding arrays >>>
arr– arr # Sub arrays >>>
arr * arr # Multiplication arrays >>>
arr + 100 # Adds 100 to every element >>>
arr ** 2 # Exponent operation >>>
[Link](arr) # Square root of arrays >>>
[Link](arr2) # Matrix Multiplication
Link : [Link]
Indexing and Slicing:
1
2
>>> arr = [Link]([(100, 200, 300), (400, 500, 600)]) >>>
arr[0](Selects the first row)
[ NOTE :
The value at the right side is for the column.
For row selection the value is before comma.
Column is selected by adding “ : ” before index.]
1
2
>>> arr[: , 1](Selects second columns) >>>
arr[1,: 2](Selects second row with 2 values)
Link : [Link]