forked from wkosenkow/monet_fe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
142 lines (91 loc) · 4.82 KB
/
app.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import streamlit as st
from PIL import Image
from google.cloud import storage
import requests
def file_selector(folder_path='.'):
filenames = os.listdir(folder_path)
selected_filename = st.selectbox('Select a file', filenames)
return os.path.join(folder_path, selected_filename)
@st.cache
def load_image(image_file):
img = Image.open(image_file)
return img
if __name__ == '__main__':
# Select a file
st.markdown("""# Transform any image into a monet-painting
##Upload an image
""")
current_directory = os.getcwd()
final_directory = os.path.join(current_directory, r'tempDir')
if not os.path.exists(final_directory):
os.makedirs(final_directory)
uploaded_file = st.file_uploader("Upload Files",type=['png','jpg','jpeg'])
if uploaded_file is not None:
file_details = {"FileName":uploaded_file.name,"FileType":uploaded_file.type,"FileSize":uploaded_file.size}
filename = file_details['FileName']
st.write(file_details)
img = load_image(uploaded_file)
st.image(img, caption = "Uploaded Image")
with open(os.path.join("tempDir",uploaded_file.name),"wb") as f:
f.write(uploaded_file.getbuffer())
st.success("Saved File")
filepath = (os.path.join("tempDir",uploaded_file.name))
st.write(filepath)
# DISPLAYING THE UPLOADED IMAGE
#st.write(os.path.basename(filepath))
# def find_files(filename, search_path):
# result = []
# # Walking top-down from the root
# for root, dir, files in os.walk(search_path):
# if filename in files:
# result.append(os.path.join(root, filename))
# return result
# st.write("tes tes tes")
# st.write(find_files(filepath,"C:"))
# if st.checkbox('Select a file in current directory'):
# folder_path = '.'
# if st.checkbox('Change directory'):
# folder_path = st.text_input('Enter folder path', '.')
# filepath = file_selector(folder_path=folder_path)
# #st.write('You selected `%s`' % filepath)
# if filepath.endswith("jpg") or filepath.endswith("png"):
# image = Image.open(filepath)
# st.image(image, caption=st.markdown('''#Uploaded File'''), use_column_width=False)]]
############# GOOGLE CLOUD PLATFORM #########################################
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'google-credentials.json'
storage_client = storage.Client()
bucket_name = 'bucket-monet-gan'
#Accessing the bucket
my_bucket = storage_client.get_bucket(bucket_name)
#Uploading files
if filepath.endswith("jpg") or filepath.endswith("jpeg") or filepath.endswith("png"):
def upload_to_bucket(blob_name, file_path, bucket_name):
try:
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(blob_name)
blob.upload_from_filename(file_path)
return True
except Exception as e:
print(e)
return False
filename_upload = f'frontend_upload_images./{filepath}'
#calling the upload function as a condition to then downloading file from bucket
if upload_to_bucket(filename_upload,filepath,bucket_name):
def download_from_bucket(blob_name, file_path, bucket_name):
try:
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(blob_name)
with open(file_path, 'wb') as f:
storage_client.download_blob_to_file(blob, f)
return True
except Exception as e:
print(e)
return False
#calling the download function
folder_name = 'frontend_download_images'
filename_download = filepath
download_from_bucket(f'{folder_name}/{filepath}',os.getcwd(),bucket_name)
downloaded_image_path = uploaded_file.name
image2 = Image.open(downloaded_image_path)
st.image(image2, caption=st.markdown('''#Downloaded Image'''), use_column_width=False)