You have a custom object detection dataset with images and annotations in COCO format. What is the number of unique object categories in the dataset?
import json with open('annotations.json') as f: data = json.load(f) categories = data['categories'] num_categories = len(categories) print(num_categories)
Look at the 'categories' key in the COCO annotation file. Count how many category entries it has.
The 'categories' list in COCO format contains all unique object categories. Counting its length gives the number of categories.
Given the following code snippet that extracts bounding boxes from annotations, what is the output?
annotations = [
{'bbox': [10, 20, 30, 40], 'category_id': 1},
{'bbox': [15, 25, 35, 45], 'category_id': 2},
{'bbox': [50, 60, 70, 80], 'category_id': 1}
]
bboxes = [ann['bbox'] for ann in annotations if ann['category_id'] == 1]
print(bboxes)Filter bounding boxes where category_id equals 1.
The list comprehension filters annotations with category_id 1 and collects their bounding boxes.
Which option correctly draws bounding boxes on an image using OpenCV for a given list of boxes?
import cv2 import numpy as np image = np.zeros((100, 100, 3), dtype=np.uint8) bboxes = [[10, 10, 30, 30], [50, 50, 20, 20]] for box in bboxes: x, y, w, h = box cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2) cv2.imwrite('output.png', image)
Bounding boxes are defined by top-left corner (x, y) and width and height (w, h). Use these to find corners.
cv2.rectangle requires top-left and bottom-right points. Bottom-right is (x+w, y+h). Other options misuse the API or draw incorrect shapes.
What error will this code raise when loading a custom object detection dataset?
import json with open('annotations.json') as f: data = json.load(f) images = data['images'] annotations = data['annotations'] image_id = images[0]['id'] image_annotations = [ann for ann in annotations if ann['image_id'] == image_id] print(len(image_annotations))
Check if the JSON file has 'images' and 'annotations' keys and they contain lists.
If the JSON file is correctly formatted in COCO style, the code runs without error and prints the count of annotations for the first image.
Given a dataset with annotations, which option correctly computes the average bounding box area for each category?
annotations = [
{'bbox': [10, 20, 30, 40], 'category_id': 1},
{'bbox': [15, 25, 35, 45], 'category_id': 2},
{'bbox': [50, 60, 70, 80], 'category_id': 1},
{'bbox': [5, 10, 15, 20], 'category_id': 2}
]
from collections import defaultdict
area_sum = defaultdict(float)
count = defaultdict(int)
for ann in annotations:
_, _, w, h = ann['bbox']
area = w * h
area_sum[ann['category_id']] += area
count[ann['category_id']] += 1
avg_area = {cat: area_sum[cat]/count[cat] for cat in area_sum}
print(avg_area)Calculate area as width * height for each bbox, sum by category, then divide by count.
Category 1 areas: 30*40=1200, 70*80=5600; sum=6800, average=3400.0. Category 2 areas: 35*45=1575, 15*20=300; sum=1875, average=937.5.