C:\> Rostislav Persion's Projects

.:: PYTHON FACE DETECTION SECURITY ::.
Python program that detects faces via webcam




This is a Python program that detects faces via the webcam and saves the image to a file when a face is detected.

In order to get this program to run, you will need to run a command line statement to install the necessary OpenCV module...

py -m pip install opencv-python

You will also need to download this file, which is a "Haar Cascade" definition of what a face should look like (if you want, you can use other Harr Cascade definitions)...

haarcascade_frontalface_default.xml

I want to credit "Shantnu Tiwari" for his sample code. I would have kept it the same, but one of the lines of code only works in an older version of Python, which is why I felt the need to post an updated version.

https://realpython.com/face-detection-in-python-using-a-webcam/

import cv2import sysimport timefrom datetime import datetime#SET PATH TO HAAR FILEcascPath = 'C:\\Users\\seepl\\OneDrive\\Desktop\\python_code\\haarcascade_frontalface_default.xml'faceCascade = cv2.CascadeClassifier(cascPath)#INITIALIZE CAMERAvideo_capture = cv2.VideoCapture(0)#WELCOME TO PROGRAMprint("")print("ROSTISLAV PERSION - WEBCAM FACE DETECTION SECURITY")print("")while True:    #READ IMAGE FROM WEBCAM    ret, frame = video_capture.read()        #ALTERNATE DEBUG IMAGE    #frame = cv2.imread('C:\\Users\\Yuri\\Desktop\\TZ_001.png')          gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)        #PROCESS IMAGE    faces = faceCascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)    #DRAW RECTANGLE FOR EACH FACE    dims = []    face_count = 0    for (x, y, w, h) in faces:        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)        dims.append(int((-0.311111 * w) + 132.444444))        face_count += 1    #IF MORE THAN ZERO FACES, GENERATE A FILE FOR OUTPUT    if face_count > 0:        dateTimeObj = datetime.now()        yr = dateTimeObj.year        mo = dateTimeObj.month        day = dateTimeObj.day        hr = dateTimeObj.hour        mi = dateTimeObj.minute        sec = dateTimeObj.second        usec = dateTimeObj.microsecond        time_str = str(yr).zfill(2) + "-" + str(mo).zfill(2) + "-" + str(day).zfill(2) + "___" + str(hr).zfill(2) + "hrs-" + str(mi).zfill(2) + "minutes-" + str(sec).zfill(2) + "seconds___" + str(usec) + "__NumOfFaces" + str(face_count).zfill(3)         time_str2 = str(yr).zfill(2) + "-" + str(mo).zfill(2) + "-" + str(day).zfill(2) + " " + str(hr).zfill(2) + ":" + str(mi).zfill(2) + ":" + str(sec).zfill(2) + " FACES #" + str(face_count).zfill(3)                 #FONT         font = cv2.FONT_HERSHEY_SIMPLEX         #COORDS        org = (10, 30)         # fontScale         fontScale = 0.5        #TEXT COLOR         color = (255, 0, 0)         #THICKNESS         thickness = 1        #DRAW TEXT         image = cv2.putText(frame, time_str2, org, font, fontScale, color, thickness, cv2.LINE_AA)                 cv2.imwrite("C:\\Users\\seepl\\OneDrive\\Desktop\\python_code\\ftp\\SLAVA___" + str(time_str) + ".jpg", frame)        #SHOW TEXT STATUS        print(time_str2)        for d in dims:            print("FACE DISTANCE: " + str(d) + " IN")        print()    #SHOW VISUAL OUTPUT    cv2.imshow('PERSION SECURITY SYSTEM v1.00', frame)        if cv2.waitKey(1) & 0xFF == ord('x'):        break    #SET PACE FOR PROGRAM    time.sleep(5)#DESTRUCTORvideo_capture.release()cv2.destroyAllWindows()