-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathimage_rotation_data.py
46 lines (35 loc) · 1.32 KB
/
image_rotation_data.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
import os
import io
import csv
import tqdm
from PIL import Image
from google.cloud import vision
from google.cloud.vision import types
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'gcp_secret.json'
phi_step = 1
image_path = "duckorrabbit.png"
output_path = "/Volumes/Extreme 510/Data/optillusion-animation/rotated_images"
def get_rotated_image_labels(client, image, bg, phi):
# https://stackoverflow.com/a/5253554
rot = image.rotate(-phi) # clockwise
image_tf = Image.composite(rot, bg, rot)
filename = str(phi) + '.png'
image_tf.convert('RGB').save(os.path.join(output_path, filename))
# https://stackoverflow.com/a/33117447
imgByteArr = io.BytesIO()
image_tf.save(imgByteArr, format='PNG')
imgByteArr = imgByteArr.getvalue()
image = types.Image(content=imgByteArr)
response = client.label_detection(image=image)
return response
image = Image.open(image_path).convert('RGBA')
bg = Image.new('RGBA', image.size, (255,) * 4)
client = vision.ImageAnnotatorClient()
t = tqdm.tqdm(range(0, 360, phi_step))
with open('image_rot_results.csv', 'w') as f:
w = csv.writer(f)
w.writerow(['phi', 'label', 'score'])
for phi in t:
r = get_rotated_image_labels(client, image, bg, phi)
for ann in r.label_annotations:
w.writerow([phi, ann.description, ann.score])