0
0
Computer Visionml~10 mins

Privacy considerations in Computer Vision - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Athreshold
Bresize
CcvtColor
DGaussianBlur
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.
2fill in blank
medium

Complete 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'
A10
B5
C50
D100
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.
3fill in blank
hard

Fix 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'
A'info'
B'metadata'
C'exif'
D'data'
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.
4fill in blank
hard

Fill 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'
Arectangle
B(0, 0, 0)
C(255, 255, 255)
Dcircle
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.
5fill in blank
hard

Fill 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'
A['ssn', 'email']
BNone
C'age'
D['password', 'token']
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.