def build_detected_dict(results, detection_thresh, labels):
	"""
	Build detected boxes dictionary from results file data : detected_objects[label] = {'images': [], 'scores': [], 'bboxes': []}.
	Only the bounding boxes whose confidence score is over 'detection_thresh' are used. 
	The valid labels are listed in 'labels'.
	"""
	detected_objects = {}   
	for line in results:
		# Extract each item from the line data
		result_data = line.split(' ')
		# Check usage
		if len(result_data) != 7:
			print("Wrong usage : Results data must follow the template 'image_name class_label score x_top_left y_top_left width height'")
			exit(-1)

		# Get data
		image_name, label, confidence, bbox_xmin, bbox_ymin, bbox_xmax, bbox_ymax = result_data
		# Define bounding box
		bbox = [float(bbox_xmin), float(bbox_ymin), float(bbox_xmax), float(bbox_ymax)]
		# Filter relevant bounding boxes
		if float(confidence) < detection_thresh:
			print('yo')
			continue
		# Check whether the label is valid according to ground truth labels
		if label not in labels:
			print('Invalid label detected.')
			continue
		# Check whether the label is not in the dictionary yet
		if label not in detected_objects.keys():
			detected_objects[label] = {'images': [], 'scores': [], 'bboxes': []}
		# Append results to dictionary
		detected_objects[label]['images'].append(image_name)
		detected_objects[label]['bboxes'].append(bbox)
		detected_objects[label]['scores'].append(confidence)		

	return detected_objects