.:: 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/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | import cv2 import sys import time from datetime import datetime #SET PATH TO HAAR FILE cascPath = 'C:\\Users\\seepl\\OneDrive\\Desktop\\python_code\\haarcascade_frontalface_default.xml' faceCascade = cv2.CascadeClassifier(cascPath) #INITIALIZE CAMERA video_capture = cv2.VideoCapture(0) #WELCOME TO PROGRAM print("") 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\\seepl\\OneDrive\\Desktop\\group.jpg') 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) #FONT_SCALE 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) #DESTRUCTOR video_capture.release() cv2.destroyAllWindows() |