0
0
Computer Visionml~10 mins

Document layout analysis 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 for document layout analysis.

Computer Vision
from PIL import Image
image = Image.open([1])
Drag options to blanks, or click blank then click option'
Aopen('document.jpg')
Bdocument.jpg
C'document.jpg'
DImage.load('document.jpg')
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the filename.
Using Image.load instead of Image.open.
2fill in blank
medium

Complete 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'
A'L'
B'P'
C'CMYK'
D'RGB'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'RGB' instead of 'L' for grayscale conversion.
Using modes like 'CMYK' which are for printing.
3fill in blank
hard

Fix 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'
Acv2.RETR_LIST
Bcv2.CHAIN_APPROX_SIMPLE
Ccv2.CHAIN_APPROX_NONE
Dcv2.RETR_TREE
Attempts:
3 left
💡 Hint
Common Mistakes
Using a retrieval mode where an approximation method is expected.
Using CHAIN_APPROX_NONE which is less efficient.
4fill in blank
hard

Fill 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'
A>
B<
C(0, 255, 0)
D(255, 0, 0)
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' to filter contours.
Using red color instead of green for rectangles.
5fill in blank
hard

Fill 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'
Awidth
Bheight
Clabel
Dcolor
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'color' as a feature or label which is not typical here.
Mixing up features and labels.