Have you ever wondered how Instagram or Facebook helps us tag people ? How does it identifies different people? So , this auto tag feature works on computer vision.
In simple words computer vision helps us to recognize objects as we as a human can do!!
But do computers really read An Image?
Actually answer is NO!
For an coloured image there are three channels — RED , GREEN , BLUE. There is a matrix for every colour and image we see is a combination of these channels. In case of Black and white image there is only one channel with a specific length and height.
OpenCV is python library for image and video processing in Python. It is used in many real time applications.
Install OpenCV
pip install opencv
Importing OpenCV
import cv2
Reading an image
img = cv2.imread(‘simon.jpg’)
We can convert from BGR to RGB or to any other colour pattern
new_img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
Now lets see how our image looks like !
plt.imshow(img)
plt.grid(False)
plt.show()
plt.imshow(new_img)
plt.grid(False)
plt.show()
The following code launches camera of two frames one of colored and other of black and white.
When you press ‘q’ your program will stop executing !
import cv2cap = cv2.VideoCapture(0)while(True):
ret,frame = cap.read()
gray_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
if ret==False:
continuecv2.imshow("Video Frame",frame)
cv2.imshow("Black White",gray_frame)
# Wait For user input
key_pressed = cv2.waitKey(1) & 0xFFif(key_pressed == ord('q')):
break
cap.release()
cv2.destroyAllWindows()
The following code uses haarcascade prewritten algorithm.
It Writes a Python Script that captures images from your webcam video stream.
Then Extracts all Faces from the image frame (using haarcascades)
At the end Stores the Face information into numpy arrays.
import cv2
import numpy as np#Init Camera
cap = cv2.VideoCapture(0)# Face Detection
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")skip = 0
face_data = []
dataset_path = './data/'
file_name = input("Enter the name of the person : ")
while True:
ret,frame = cap.read()if ret==False:
continuegray_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)faces = face_cascade.detectMultiScale(frame,1.3,5)
if len(faces)==0:
continuefaces = sorted(faces,key=lambda f:f[2]*f[3])
# Pick the last face (because it is the largest face acc to area(f[2]*f[3]))
for face in faces[-1:]:
x,y,w,h = face
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,255),2)#Extract (Crop out the required face) : Region of Interest
offset = 10
face_section = frame[y-offset:y+h+offset,x-offset:x+w+offset]
face_section = cv2.resize(face_section,(100,100))skip += 1
if skip%10==0:
face_data.append(face_section)
print(len(face_data))cv2.imshow("Frame",frame)
cv2.imshow("Face Section",face_section)key_pressed = cv2.waitKey(1) & 0xFF
if key_pressed == ord('q'):
break# Convert our face list array into a numpy array
face_data = np.asarray(face_data)
face_data = face_data.reshape((face_data.shape[0],-1))
print(face_data.shape)# Save this data into file system
np.save(dataset_path+file_name+'.npy',face_data)
print("Data Successfully save at "+dataset_path+file_name+'.npy')cap.release()
cv2.destroyAllWindows()
1. Read and show video stream, capture images
2. Detect Faces and show bounding box (haarcascade)
3. Flatten the largest face image(gray scale) and save in a numpy array
4. Repeat the above for multiple people to generate training data
Now using this data we can identify the people also using knn algorithm!!
Thanks for reading….