Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load an image while respecting privacy by blurring faces.
Computer Vision
import cv2 image = cv2.imread('input.jpg') face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.1, 4) for (x, y, w, h) in faces: face_region = image[y:y+h, x:x+w] blurred_face = cv2.[1](face_region, (99, 99), 30) image[y:y+h, x:x+w] = blurred_face cv2.imwrite('output.jpg', image)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using resize instead of blur will not obscure the face.
Using cvtColor or threshold does not blur the image.
✗ Incorrect
The GaussianBlur function blurs the detected face region to protect privacy by obscuring facial details.
2fill in blank
mediumComplete the code to anonymize faces by pixelating them.
Computer Vision
import cv2 image = cv2.imread('input.jpg') face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.1, 4) for (x, y, w, h) in faces: face_region = image[y:y+h, x:x+w] small = cv2.resize(face_region, ([1], [1]), interpolation=cv2.INTER_LINEAR) pixelated_face = cv2.resize(small, (w, h), interpolation=cv2.INTER_NEAREST) image[y:y+h, x:x+w] = pixelated_face cv2.imwrite('output_pixelated.jpg', image)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a large size like 100 will not pixelate the face.
Choosing 50 or 10 may not anonymize enough.
✗ Incorrect
Using a small size like 5x5 pixels for resizing creates a pixelated effect that anonymizes the face.
3fill in blank
hardFix the error in the code that attempts to remove metadata from an image to protect privacy.
Computer Vision
from PIL import Image image = Image.open('photo.jpg') if 'exif' in image.info: image.info.pop([1]) image.save('photo_no_metadata.jpg')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'metadata' or 'info' as keys will cause a KeyError.
Not removing 'exif' leaves metadata in the saved image.
✗ Incorrect
The key to remove in the image info dictionary to delete metadata is 'exif'.
4fill in blank
hardFill both blanks to create a function that detects faces and replaces them with black rectangles to protect privacy.
Computer Vision
import cv2 def anonymize_faces(image_path): image = cv2.imread(image_path) face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.1, 4) for (x, y, w, h) in faces: cv2.[1](image, (x, y), (x + w, y + h), [2], -1) return image
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'circle' will not cover the entire face properly.
Using white color (255,255,255) may not anonymize well on light backgrounds.
✗ Incorrect
The rectangle function draws black rectangles (color (0,0,0)) over faces to anonymize them.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that filters out sensitive keys from metadata.
Computer Vision
metadata = {'name': 'Alice', 'age': 30, 'ssn': '123-45-6789', 'email': 'alice@example.com'}
safe_metadata = {k: v for k, v in metadata.items() if k not in [1] and v != [2] and k != [3] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keys in the list will not filter sensitive data.
Using wrong value for comparison will not filter correctly.
✗ Incorrect
We exclude keys 'ssn' and 'email' in the list, filter out values equal to None, and exclude key 'age' to protect privacy.