-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.py
116 lines (83 loc) · 3.86 KB
/
project.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from guizero import App, Box, Text, TextBox, Picture, PushButton
from PIL import Image
import tensorflow as tf
import numpy as np
import wikipedia as wiki
model = tf.keras.applications.VGG16()
### This is the code for the user interface, including updating the text and resizing images ###
# This dictionary stores the dimensions for the displayed image
DISPLAY_IMAGE_SIZE = {
'max_width': 300.0,
'max_height': 300.0
}
# This function resizes an image that is bigger than either the max width or height to fit within them
def generate_display_picture(picture_path, display_image_size):
picture = Image.open(picture_path)
if picture.height > picture.width:
scaled_width = picture.height / display_image_size['max_height']
scaled_width = (scaled_width if picture.height < display_image_size[
'max_height'] else 1 / scaled_width) * picture.width
picture = picture.resize(
(
int(scaled_width),
int(display_image_size['max_height'])
)
)
else:
scaled_height = picture.width / display_image_size['max_width']
scaled_height = (scaled_height if picture.width < display_image_size[
'max_width'] else 1 / scaled_height) * picture.height
picture = picture.resize(
(
int(display_image_size['max_width']),
int(scaled_height)
)
)
return picture
# This function updates the wiki_text text box with contents passed to it in updated_text
def update_text_box(updated_text):
global wiki_text
wiki_text.enable()
wiki_text.clear()
wiki_text.append(updated_text)
wiki_text.disable()
# Create the application
app = App(title='Amazing Image Identifier')
# Create an invisible box to hold the title — useful for setting up complex layouts
title_box = Box(app, width='fill', align='top')
# Create the title and centre it in the application
title = Text(title_box, text="Amazing Image Identifier")
# Create an invisible box to hold the image and article text
content_box = Box(app, width='fill', align='top')
# Create an invisible box to hold the image
picture_box = Box(content_box, align='left')
# Create the image and set it to the start.jpg file included in this directory
display_image = Picture(picture_box, image=generate_display_picture(
'start.jpg', DISPLAY_IMAGE_SIZE))
# Create the text box for article text
wiki_text = TextBox(content_box, multiline=True, enabled=False, scrollbar=True,
text="Please choose an image to identify.", width='fill', height='fill')
# Create a box to hold the button
button_box = Box(app, width='fill', align='bottom')
### End user interface code ###
def update_picture():
pic = app.select_file()
display_image.image = generate_display_picture(pic, DISPLAY_IMAGE_SIZE)
prediction = identify_image(pic)
article = wiki.page(prediction)
title.value = article.title
update_text_box(article.summary)
print(prediction)
IMAGE_SIZE = 224
def identify_image(image_path):
image = tf.keras.preprocessing.image.load_img(image_path, target_size=(IMAGE_SIZE, IMAGE_SIZE))
image = tf.keras.preprocessing.image.img_to_array(image)
image = np.expand_dims(image, axis=0)
prediction_result = model.predict(image, batch_size=1)
best_prediction = tf.keras.applications.imagenet_utils.decode_predictions(prediction_result, top=1)
return best_prediction[0][0][1]
# This line creates a button with the text 'Select picture' and tells it to run the update_picture function when clicked
# It has to come later than the other interface code, as it needs the update_picture function to be created first
button = PushButton(button_box, text='Select picture', command=update_picture)
# Display the whole interface — this has to come after the button, the last piece of the interface, is created
app.display()