|
| 1 | +import cv2 |
| 2 | +from cv2 import data |
| 3 | + |
| 4 | +face_cascade = cv2.CascadeClassifier(data.haarcascades + "haarcascade_frontalface_default.xml") |
| 5 | + |
| 6 | +image_path = input("Enter image path: ") |
| 7 | +image = cv2.imread(image_path) |
| 8 | + |
| 9 | +if image is None: |
| 10 | + print("Image not found") |
| 11 | + exit() |
| 12 | + |
| 13 | +# grayscale for better detection |
| 14 | +gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
| 15 | + |
| 16 | +# lets find faces |
| 17 | +# cascade detector runs on th eimage and returns a rectangle coordinate x,y,w,h |
| 18 | +# scalefactor repatedly scales image by value to detet faces in differnet sizes |
| 19 | +# minneighbour decides how many rectabgles should be overlapped to trigger face detection |
| 20 | +# minsize ignores detected objects smaller tha 30x30 pixels |
| 21 | +faces = face_cascade.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(30,30)) |
| 22 | + |
| 23 | +#looping over detected face coords |
| 24 | +for (x,y,w,h) in faces: |
| 25 | + # extacts the subarray containing detected face |
| 26 | + face = image[y:y+h,x:x+w] |
| 27 | + # numpy doesnt use a copy but views into image |
| 28 | + # lets apply guassian blur |
| 29 | + blurred_face = cv2.GaussianBlur(face,(99,99),30) |
| 30 | + # (99, 99) is the kernel size. stronger blr |
| 31 | + # 30 is sigmaX (standard deviation in the X direction). used to make rexziabke with face sizes |
| 32 | + # we can write blurred_face in original image |
| 33 | + image[y:y+h, x:x+w] = blurred_face |
| 34 | + |
| 35 | +output_path = "blurred_" + image_path.split("/")[-1] |
| 36 | +cv2.imwrite(output_path,image) |
| 37 | +# show image...comment out if not needed |
| 38 | +cv2.imshow("Blurred Image", image) |
| 39 | +cv2.waitKey(0) |
| 40 | +cv2.destroyAllWindows() |
| 41 | + |
0 commit comments