Bird
Raised Fist0
Computer Visionml~10 mins

Privacy considerations in Computer Vision - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main reason to blur faces in images used for computer vision projects?
easy
A. To make the images look artistic
B. To improve the image quality for better model training
C. To reduce the file size of the images
D. To protect people's privacy by hiding their identity

Solution

  1. Step 1: Understand privacy protection in images

    Blurring faces hides personal identity, which protects privacy.
  2. Step 2: Compare other options

    Improving quality, reducing size, or artistic effects do not relate to privacy.
  3. Final Answer:

    To protect people's privacy by hiding their identity -> Option D
  4. Quick Check:

    Blurring faces = privacy protection [OK]
Hint: Blurring hides identity to protect privacy [OK]
Common Mistakes:
  • Thinking blurring improves image quality
  • Confusing file size reduction with privacy
  • Assuming artistic effects protect privacy
2. Which of the following is the correct way to remove metadata from an image file in Python?
easy
A. Use PIL's Image.save() with 'exif' parameter set to None
B. Use cv2.imread() and cv2.imwrite() without extra steps
C. Rename the image file extension to .txt
D. Open the image in a text editor and delete random lines

Solution

  1. Step 1: Identify proper metadata removal method

    PIL's Image.save() with 'exif=None' removes metadata correctly.
  2. Step 2: Evaluate other options

    cv2.imread/write does not remove metadata; renaming or editing text is invalid.
  3. Final Answer:

    Use PIL's Image.save() with 'exif' parameter set to None -> Option A
  4. Quick Check:

    Remove metadata = PIL save with exif=None [OK]
Hint: Use PIL save with exif=None to remove metadata [OK]
Common Mistakes:
  • Assuming cv2.imwrite removes metadata
  • Renaming file extensions changes nothing
  • Editing image as text corrupts the file
3. Consider this Python code snippet that blurs faces in an image using OpenCV:
import cv2
image = cv2.imread('group_photo.jpg')
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5)
for (x, y, w, h) in faces:
    face_region = image[y:y+h, x:x+w]
    blurred_face = cv2.GaussianBlur(face_region, (99, 99), 30)
    image[y:y+h, x:x+w] = blurred_face
cv2.imwrite('blurred_photo.jpg', image)
What will be the result of running this code?
medium
A. The output image will have all detected faces blurred to protect privacy
B. The output image will be unchanged because GaussianBlur is not applied correctly
C. The code will raise an error because detectMultiScale requires a grayscale image
D. The code will blur the entire image instead of just faces

Solution

  1. Step 1: Trace the code execution

    cv2.imread loads a color image. However, detectMultiScale requires a grayscale image input, so passing a color image will cause an error or incorrect detection.
  2. Step 2: Correct usage

    The image should be converted to grayscale before calling detectMultiScale, e.g., gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY).
  3. Final Answer:

    The code will raise an error because detectMultiScale requires a grayscale image -> Option C
  4. Quick Check:

    detectMultiScale requires grayscale input [OK]
Hint: detectMultiScale needs grayscale image [OK]
Common Mistakes:
  • Thinking detectMultiScale works directly on color images
  • Assuming no error on color input
  • Believing blur applies to whole image
4. You have a dataset of images with faces but forgot to get consent from people. Which fix below best respects privacy and legal rules?
medium
A. Blur all faces in the dataset before using it for training
B. Use the images as is because they are publicly available
C. Remove all images with faces and keep only background images
D. Add random noise to images without blurring faces

Solution

  1. Step 1: Identify privacy and legal requirements

    Consent is needed; without it, faces must be anonymized.
  2. Step 2: Evaluate options for compliance

    Blurring faces anonymizes identities; using images as is or adding noise does not protect privacy properly.
  3. Final Answer:

    Blur all faces in the dataset before using it for training -> Option A
  4. Quick Check:

    No consent = anonymize faces by blurring [OK]
Hint: No consent? Blur faces to protect privacy [OK]
Common Mistakes:
  • Assuming public availability means consent
  • Thinking noise addition protects identity
  • Removing images may lose valuable data unnecessarily
5. You want to build a face recognition system but must comply with privacy laws. Which combined approach best balances functionality and privacy?
hard
A. Train on unblurred public images and delete them after training
B. Collect images only with explicit consent and blur faces in public datasets
C. Use any available images without consent but encrypt the dataset
D. Avoid face recognition and use only object detection instead

Solution

  1. Step 1: Understand privacy law requirements

    Explicit consent is required to use personal images legally.
  2. Step 2: Combine consent and anonymization

    Blurring faces in public datasets protects privacy while allowing training.
  3. Step 3: Evaluate other options

    Using images without consent or deleting after training does not ensure compliance; avoiding face recognition limits functionality.
  4. Final Answer:

    Collect images only with explicit consent and blur faces in public datasets -> Option B
  5. Quick Check:

    Consent + blur = privacy compliance and functionality [OK]
Hint: Consent plus blurring balances privacy and use [OK]
Common Mistakes:
  • Thinking encryption replaces consent
  • Assuming deleting data after training is enough
  • Avoiding face recognition is not always necessary