Note that the axis of the OPENCV, the x-axis to the right, and the width corresponding to the Y axis downward, and the height of the corresponding;
1. MOG2 and KNN
Mog:mixture of Gaussian
Import cv2
cap = Cv2. Videocapture ('./data/video/768x576.avi ')
knn_sub = CV2.CREATEBACKGROUNDSUBTRACTORKNN ()
mog2_sub = CV2.CREATEBACKGROUNDSUBTRACTORMOG2 ()
while True:
ret, frame = Cap.read ()
if not ret:
break
Mog_ Sub_mask = mog2_sub.apply (frame)
Knn_sub_mask = knn_sub.apply (frame)
cv2.imshow (' original ', frame)
Cv2.imshow (' MOG2 ', mog_sub_mask)
cv2.imshow (' KNN ', knn_sub_mask)
key = Cv2.waitkey & 0xFF
if Key = = or key = = Ord (' q '): Break
cap.release ()
cv2.destroyallwindows ()
2. Identification and detection
Haar Cascade
Import CV2 Import NumPy as NP # Haar Cascade classifier, OpenCV the Data folder in the source file will have a # https://github.com/opencv/opencv/tree/master/data/h Aarcascades Face_cascade = Cv2. Cascadeclassifier ('./data/classifiers/haarcascade_frontalface_default.xml ') Eye_cascade = Cv2. Cascadeclassifier ('./data/classifiers/haarcascade_eye.xml ') cap = Cv2. Videocapture (0) while True:ret, img = cap.read () if not ret:break Gray = Cv2.cvtcolor (img, Cv2. Color_bgr2gray) faces = Face_cascade.detectmultiscale (gray, 1.3, 5) for (x, Y, W, h) in Faces:cv2.rectang Le (IMG, (x, y), (x+w, Y+h), color= (255, 0, 0), thickness=2) Roi_gray = Gray[y:y+h, x:x+w] Roi_color = img[
Y:y+h, x:x+w] eyes = Eye_cascade.detectmultiscale (Roi_gray) Try:ex, EY, ew, eh = eyes[0] Cv2.rectangle (Roi_color, (ex, EY), (Ex+ew, Ey+eh), color= (0, 255, 0), thickness=2) E X1, Ey1, ew1, EH1 = eyes[1] Cv2.rectangle (Roi_color, Ex1, Ey1), (Ex1+ew1, EY1+EH1), color= (0, 255, 0), thickness=2) except Indexerror:print (
' Cv2.imshow (' Me ', img) key = Cv2.waitkey if key = =/Key = = Ord (' q '): Break Cap.release () Cv2.destroyallwindows ()