Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load an image for document layout analysis.
Computer Vision
from PIL import Image image = Image.open([1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the filename.
Using Image.load instead of Image.open.
✗ Incorrect
The Image.open() function requires the file path as a string, so it must be enclosed in quotes.
2fill in blank
mediumComplete the code to convert the image to grayscale for easier layout analysis.
Computer Vision
gray_image = image.convert([1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'RGB' instead of 'L' for grayscale conversion.
Using modes like 'CMYK' which are for printing.
✗ Incorrect
The mode 'L' converts the image to grayscale (luminance), which simplifies layout analysis.
3fill in blank
hardFix the error in the code to detect contours using OpenCV for layout blocks.
Computer Vision
import cv2 contours, _ = cv2.findContours(gray_image, cv2.RETR_EXTERNAL, [1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a retrieval mode where an approximation method is expected.
Using CHAIN_APPROX_NONE which is less efficient.
✗ Incorrect
CHAIN_APPROX_SIMPLE compresses horizontal, vertical, and diagonal segments and leaves only their end points, which is efficient for layout detection.
4fill in blank
hardFill both blanks to filter contours by area and draw bounding boxes.
Computer Vision
for cnt in contours: area = cv2.contourArea(cnt) if area [1] 1000: x, y, w, h = cv2.boundingRect(cnt) cv2.rectangle(image, (x, y), (x + w, y + h), [2], 2)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' to filter contours.
Using red color instead of green for rectangles.
✗ Incorrect
We want to keep contours with area greater than 1000 to ignore small noise. The rectangle color is green (0,255,0).
5fill in blank
hardFill all three blanks to prepare data for a simple ML model to classify layout blocks.
Computer Vision
features = [] labels = [] for block in layout_blocks: features.append([block.[1], block.[2]]) labels.append(block.[3]) from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, random_state=42)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'color' as a feature or label which is not typical here.
Mixing up features and labels.
✗ Incorrect
Width and height are common features for layout blocks. The label is the target variable for classification.