-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualize.py
66 lines (48 loc) · 1.99 KB
/
visualize.py
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
import json
from PIL import Image, ImageDraw
import os
import pathlib
from matplotlib import font_manager
from PIL import Image, ImageDraw, ImageFont, ExifTags
import numpy as np
from tqdm import tqdm
font = font_manager.FontProperties(family='sans-serif', weight='bold')
file = font_manager.findfont(font)
home = pathlib.Path.home()
with open('./coco_json/cleaned_sote_mammo_emk.json', 'r') as f:
datastore = json.load(f)
id2cat = {_id : cat for cat, _id in datastore['cat2id'].items()}
for img in tqdm(datastore['images']):
image_id = img['id']
bboxes = []
for ann in datastore['annotations']:
if ann['image_id'] == image_id:
bboxes.append((ann['bbox'], id2cat[ann['category_id']]))
from PIL import Image, ImageFont, ImageDraw, ImageEnhance
try:
image=Image.open(img['path'])
for orientation in ExifTags.TAGS.keys():
if ExifTags.TAGS[orientation]=='Orientation':
break
exif = image._getexif()
if exif[orientation] == 3:
image=image.rotate(180, expand=True)
elif exif[orientation] == 6:
image=image.rotate(270, expand=True)
elif exif[orientation] == 8:
image=image.rotate(90, expand=True)
image.save(filepath)
image.close()
except (AttributeError, KeyError, IndexError, TypeError) as e:
# cases: image don't have exif
pass
draw = ImageDraw.Draw(image)
fnt = ImageFont.truetype(file, 32, encoding="unic")
height, width = image.size
for bbox, cat in bboxes:
draw.rectangle(((bbox[0], bbox[1]), (bbox[0] + bbox[2], bbox[1] + bbox[3])), outline=255, width=8)
draw.text((bbox[0], bbox[1]), cat, font=fnt, fill=(255), stroke_width=1)
crop_mean = np.array(image.crop((bbox[0], bbox[1], bbox[0] + bbox[2], bbox[1] + bbox[3]))).mean()
image.save('./validation_pictures/%s' % pathlib.Path(img['path'].split('/')[-1]).stem + f'_{int(crop_mean)}.png', "PNG")
del draw
del image