-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize_all.py
More file actions
80 lines (63 loc) · 3.27 KB
/
visualize_all.py
File metadata and controls
80 lines (63 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from PIL import Image, ImageDraw, ImageFont, ImageColor
import os
from tqdm import tqdm
import csv
def calculate_text_size(text, font):
# calculate text size based on font properties
ascent, descent = font.getmetrics()
text_width = font.getmask(text).getbbox()[2]
text_height = ascent + descent
return text_width, text_height
def get_brightness(color):
# Calculate brightness of a color (grayscale value) for the text
r, g, b = ImageColor.getrgb(color)
return (r * 299 + g * 587 + b * 114) / 1000
def visualize_image(filename, csv_filename):
# Open image
image_path = filename
img = Image.open(image_path)
draw = ImageDraw.Draw(img)
# Read bounding box information from CSV file
if os.path.getsize(csv_filename) > 0:
with open(csv_filename, 'r') as csvfile:
csvreader = csv.reader(csvfile)
#next(csvreader) # Skip header row
for row in csvreader:
if row:
xmin, ymin, xmax, ymax = map(int, row[0:4])
class_name = row[4]
# Define colors for different classes
class_colors = {
'danger': 'yellow',
'interdiction': 'purple',
'obligation': 'blue',
'stop': 'magenta',
'ceder': 'cyan',
'frouge': 'red',
'forange': 'orange',
'fvert': 'green'
}
# Define brightness threshold for determining text color
brightness_threshold = 150
# Get bounding box color
box_color = class_colors.get(class_name, 'white') #white is the de
# Determine text color based on brightness of box color
text_color = 'black' if get_brightness(box_color) > brightness_threshold else 'white'
# Draw bounding box
draw.rectangle([(xmin, ymin), (xmax, ymax)], outline=box_color)
# Define font and size
font_size = 30 # Adjust the font size here
font = ImageFont.truetype("arial.ttf", font_size)
# Get text size
text_width, text_height = calculate_text_size(class_name, font)
# Draw filled rectangle as background for class name
draw.rectangle([(xmin, ymin - text_height), (xmin + text_width, ymin)], fill=box_color)
# Draw class name text on top of the filled rectangle
draw.text((xmin, ymin - text_height), class_name, fill=text_color, font=font)
return img
for subdir, dirs, files in os.walk('dataset/train/images'):
for file in tqdm(files):
filepath = subdir + os.sep + file
filepath_csv = 'dataset/train/labels/' + file.replace(".jpg",".csv")
vi = visualize_image(filepath, filepath_csv)
vi.save('visualized/{}'.format(file),'jpeg')