ClearCode

Face Detector

Apr 1st, 2022 (edited)
1,782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. import PySimpleGUI as sg
  2. import cv2
  3.  
  4. layout = [
  5.     [sg.Image(key = '-IMAGE-')],
  6.     [sg.Text('People in picture: 0', key = '-TEXT-', expand_x = True, justification = 'c')]
  7. ]
  8.  
  9. window = sg.Window('Face Detector', layout)
  10.  
  11. # get video
  12. video = cv2.VideoCapture(0)
  13. face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  14.  
  15. while True:
  16.     event, values = window.read(timeout = 0)
  17.     if event == sg.WIN_CLOSED:
  18.         break
  19.  
  20.     _, frame = video.read()
  21.     gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
  22.     faces = face_cascade.detectMultiScale(
  23.         gray,
  24.         scaleFactor = 1.3,
  25.         minNeighbors = 7,
  26.         minSize =(30,30))
  27.  
  28.     # draw the rectangles
  29.     for (x, y, w, h) in faces:
  30.         cv2.rectangle(frame,(x,y),(x + w, y + h),(0,255,0),2)
  31.  
  32.     # update the image
  33.     imgbytes = cv2.imencode('.png',frame)[1].tobytes()
  34.     window['-IMAGE-'].update(data = imgbytes)
  35.  
  36.     # update the text
  37.     window['-TEXT-'].update(f'People in picture: {len(faces)}')
  38.  
  39. window.close()
Add Comment
Please, Sign In to add comment