Amazing-Python-Scripts

Форк
0
45 строк · 1.4 Кб
1
import cv2
2
import label_image
3

4
size = 4
5

6

7
# We load the xml file
8
classifier = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
9

10
webcam = cv2.VideoCapture(0)  # Using default WebCam connected to the PC.
11

12
while True:
13
    (rval, im) = webcam.read()
14
    im = cv2.flip(im, 1, 0)  # Flip to act as a mirror
15

16
    # Resize the image to speed up detection
17
    mini = cv2.resize(im, (int(im.shape[1]/size), int(im.shape[0]/size)))
18

19
    # detect MultiScale / faces
20
    faces = classifier.detectMultiScale(mini)
21

22
    # Draw rectangles around each face
23
    for f in faces:
24
        (x, y, w, h) = [v * size for v in f]  # Scale the shapesize backup
25
        cv2.rectangle(im, (x, y), (x+w, y+h), (0, 255, 0), 4)
26

27
        # Save just the rectangle faces in SubRecFaces
28
        sub_face = im[y:y+h, x:x+w]
29

30
        # Saving the current image from the webcam for testing.
31
        FaceFileName = "test.jpg"
32
        cv2.imwrite(FaceFileName, sub_face)
33

34
        # Getting the Result from the label_image file, i.e., Classification Result.
35
        text = label_image.main(FaceFileName)
36
        text = text.title()  # Title Case looks Stunning.
37
        font = cv2.FONT_HERSHEY_TRIPLEX
38
        cv2.putText(im, text, (x+w, y), font, 1, (0, 0, 255), 2)
39

40
    # Show the image
41
    cv2.imshow('Capture',   im)
42
    key = cv2.waitKey(10)
43
    # if Esc key is press then break out of the loop
44
    if key == 0:  # The Esc key
45
        break
46

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

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

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

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