-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcore.py
More file actions
46 lines (40 loc) · 1.56 KB
/
Copy pathcore.py
File metadata and controls
46 lines (40 loc) · 1.56 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
import numpy as np
import PIL
from keras.applications import ResNet50
import io
from keras.preprocessing.image import img_to_array
from keras.applications import imagenet_utils
from PIL import Image
from PIL.ExifTags import TAGS, GPSTAGS
class PhotoProcessor(object):
def __init__(self, image):
self._image = Image.open(io.BytesIO(image))
self.exif = self.get_exif_data(self._image)
def get_exif_data(self, image):
"""Returns a dictionary from the exif data of an PIL Image item. Also converts the GPS Tags"""
exif_data = {}
if image.__getattribute__('_getexif'):
info = image._getexif()
if info:
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
if decoded == "GPSInfo":
gps_data = {}
for t in value:
sub_decoded = GPSTAGS.get(t, t)
gps_data[sub_decoded] = value[t]
exif_data[decoded] = gps_data
else:
exif_data[decoded] = value
return exif_data
def prepare_rgb_data(self, img_size):
rgb_data = None
if self._image.mode != 'RGB':
rgb_data = self._image.convert('RGB ')
else:
rgb_data = self._image
rgb_data = rgb_data.resize(img_size)
rgb_data = img_to_array(rgb_data)
rgb_data = np.expand_dims(rgb_data, axis=0)
rgb_data = imagenet_utils.preprocess_input(rgb_data)
return rgb_data