Python opencv--background extraction (MOG, KNN), Identification and detection (Haar Cascade)

Source: Internet
Author: User
Tags ord
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 ()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.