- First dowload library opencv and numpy
pip install opencv-python
pip install numpy
- Make the program to connect to the webcam
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
cv2.imshow('just face', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
- Download the model for face, that is haarcascade_frontalface_default.xml
- Write the code to translate the model
face_detect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
- Load the image in grayscale
frame = cv2.imread('', cv2.IMREAD_GRAYSCALE)
By changing to gray, the program will be easier to learn
- Turn the picture to gray color
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- To make a rectangle, use this code
for (x, y, w, h) in faces :
cv2.rectangle(frame,(x,y), (x+w,y+h),(0,255,0),2)