Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- import os
- import time
- from PIL import ImageGrab
- recognizer = cv2.face.LBPHFaceRecognizer_create()
- recognizer.read('trainer/trainer.yml')
- cascadePath = "haarcascade_frontalface_default.xml"
- faceCascade = cv2.CascadeClassifier(cascadePath);
- font = cv2.FONT_HERSHEY_SIMPLEX #Шрифт
- #инициировать счетчик идентификаторов
- id = 0
- # имена, связанные с идентификаторами: пример ==> Александр: id=1 и т. д
- names = ['None', 'Alexandr', 'Sergey']
- human_found = False
- while True:
- img = ImageGrab.grab()
- img_np = np.array(img)
- frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
- faces = faceCascade.detectMultiScale(
- frame,
- scaleFactor = 1.2,
- minNeighbors = 5,
- minSize = (30, 30),
- )
- persons = dict()
- for x, y, w, h in faces:
- cv2.rectangle(img_np, (x, y), (x+w, y+h), (0, 255, 0), 2)
- id, confidence = recognizer.predict(frame[y:y+h,x:x+w]) #Функция recognizer.predict () Принимает в качестве параметра захваченную часть лица, подлежащую анализу, и
- #возвращает своего вероятного владельца, указывая его идентификатор и степень уверенности распознавателя в связи с этим совпадением.
- if id not in persons:
- persons[id] = 1
- else:
- persons[id] += 1
- if id and human_found == False:
- before_image_time = time.time()
- print ("before_image_time", before_image_time)
- human_found = True
- elif id and human_found == True:
- after_image_time = time.time()
- round (after_image_time)
- print ("after_image_time" , after_image_time)
- if confidence < 100:
- id = names[id]
- confidence = " {0}%".format(round(100 - confidence))
- else:
- id = "unknown"
- confidence = " {0}%".format(round(100 - confidence))
- cv2.putText(img_np, str(id), (x+5,y-5), font, 1, (255,255,255), 2) #Вывод имени идентификатора
- cv2.putText(img_np, str(confidence), (x+5,y+h-5), font, 1, (255,255,0), 1) #Вывод достоверности
- if len(faces) == 0 and human_found == True:
- total_time = after_image_time - before_image_time
- if not id:
- print(f'{names[id]}: {persons[id]} frames')
- print ("total_time names[id]", total_time, names[id])
- human_found = False
- cv2.imshow('Screen', img_np) #imshow отображает изображение в указанном окне.Если окно не было создано,то создается новое.«camera» — имя окна, img—массив картинки.
- k = cv2.waitKey(10) & 0xff # Нажмите 'ESC' для выхода из видео
- if k == 27:
- break
- print("\n [INFO] Выход из программы и очистка материала")
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement