formatting

This commit is contained in:
franksim
2025-11-07 11:20:08 +01:00
parent 8fc3559d6c
commit b159d76517
8 changed files with 119 additions and 82 deletions

View File

@@ -28,17 +28,23 @@ class AnnotationRect:
def read_groundtruth_file(path: str) -> List[AnnotationRect]:
"""Exercise 3.1b"""
annotationRects = []
with open(path, 'r') as file:
with open(path, "r") as file:
for line in file:
if line.strip():
values = line.strip().split()
annotationRects.append(AnnotationRect(float(values[0]), float(
values[1]), float(values[2]), float(values[3])))
annotationRects.append(
AnnotationRect(
float(values[0]),
float(values[1]),
float(values[2]),
float(values[3]),
)
)
return annotationRects
def get_image_with_max_annotations(dir_path: str) -> str:
img_pattern = re.compile(r'^(\d+)\.jpg$')
img_pattern = re.compile(r"^(\d+)\.jpg$")
files = set(os.listdir(dir_path))
max_file = None
max_annotations = 0
@@ -47,32 +53,41 @@ def get_image_with_max_annotations(dir_path: str) -> str:
match = img_pattern.match(fname)
if match:
img_file = os.path.join(dir_path, fname)
annotations_number = len(read_groundtruth_file(os.path.join(
dir_path, f"{match.group(1)}.gt_data.txt")))
if (annotations_number > max_annotations):
annotations_number = len(
read_groundtruth_file(
os.path.join(dir_path, f"{match.group(1)}.gt_data.txt")
)
)
if annotations_number > max_annotations:
max_file = img_file
max_annotations = annotations_number
return max_file
def visualize_image(image_path: str, output_path='output.jpg', rect_color=(255, 0, 0), width=2):
img_pattern = re.compile(r'(.*)(\.jpg)')
def visualize_image(
image_path: str, output_path="output.jpg", rect_color=(255, 0, 0), width=2
):
img_pattern = re.compile(r"(.*)(\.jpg)")
match = img_pattern.match(image_path)
annotations = read_groundtruth_file(f"{match.group(1)}.gt_data.txt")
img = Image.open(image_path).convert('RGB')
img = Image.open(image_path).convert("RGB")
draw = ImageDraw.Draw(img)
for annotation in annotations:
draw.rectangle([annotation.x1, annotation.y1, annotation.x2, annotation.y2],
outline=rect_color, width=width)
draw.rectangle(
[annotation.x1, annotation.y1, annotation.x2, annotation.y2],
outline=rect_color,
width=width,
)
img.save(output_path)
def main():
image_file = get_image_with_max_annotations(
"/home/ubuntu/mmp_wise2526_franksim/.data/mmp-public-3.2/train")
"/home/ubuntu/mmp_wise2526_franksim/.data/mmp-public-3.2/train"
)
visualize_image(image_file)