Amazing-Python-Scripts

Форк
0
35 строк · 1.3 Кб
1
import cv2
2
cap = cv2.VideoCapture(0)
3
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
4

5
while True:
6
    # capture camera frame, and ret store true and false
7
    r, frame = cap.read()
8
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
9

10
    if r == False:
11
        continue
12
    faces = face_cascade.detectMultiScale(gray_frame, 1.3, 5)
13

14
    # """ The first argument is the image, the second is the
15
    # scalefactor (how much the image size will be reduced at each image scale),
16
    # and the third is the minNeighbors (how many neighbors each rectangle should have)"""
17

18
    for (x, y, w, h) in faces:
19
        cv2.rectangle(frame, (x, y), (x+w, y+h),
20
                      (255, 125, 0), 2)  # color and width
21
        cv2.putText(frame, "DETECTED", (x, y-10),
22
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (100, 125, 255), 1, cv2.LINE_AA)
23
    cv2.imshow("video frame", frame)
24
    key_pressed = cv2.waitKey(1) & 0xFF
25

26
    # """cv2.waitKey() returns a 32 Bit integer value (might be dependent on the platform).
27
    # The key input is in ASCII which is an 8 Bit integer value. So you only care
28
    # about these 8 bits and want all other bits to be 0. This you can achieve with:0xFF"""
29

30
    # ord converts characters in unicode
31
    if key_pressed == ord('n'):
32
        break
33

34
cap.release()
35
cv2.destroyAllWindows()
36

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.