Spaces:
Runtime error
Runtime error
feat: add image capture
Browse files- URL image cache using exception handling
- image input using camera
- app.py +6 -5
- src/data/data.py +29 -13
app.py
CHANGED
@@ -34,24 +34,26 @@ content_image, style_image = st.columns(2)
|
|
34 |
with content_image:
|
35 |
ContentColumnTitle = "## πΌοΈ Content Image πΌοΈ"
|
36 |
ContentImageSelectionPrompt = "Pick a Content image"
|
37 |
-
content_image_file
|
38 |
ContentColumnTitle, ContentImageSelectionPrompt, "Content", "content"
|
39 |
)
|
40 |
|
41 |
with style_image:
|
42 |
StyleColumnTitle = "## π¨ Style Image π¨"
|
43 |
StyleImageSelectionPrompt = "Pick a Style image"
|
44 |
-
style_image_file
|
45 |
StyleColumnTitle, StyleImageSelectionPrompt, "Style", "style"
|
46 |
)
|
47 |
|
48 |
if None not in (content_image_file, style_image_file):
|
49 |
-
|
50 |
# CLEAR IMAGES
|
51 |
clear_images = st.button(
|
52 |
-
label="πβ Clear
|
53 |
on_click=remove_source_images(),
|
54 |
)
|
|
|
|
|
55 |
|
56 |
# STYLIZE CONTENT IMAGE
|
57 |
stylize_image = st.button("πΌοΈποΈπ¨ Start Neural Style Transfer πΌοΈποΈπ¨")
|
@@ -65,6 +67,5 @@ if None not in (content_image_file, style_image_file):
|
|
65 |
except:
|
66 |
pass
|
67 |
|
68 |
-
|
69 |
else:
|
70 |
st.write("Please upload content and style image to go to next step.")
|
|
|
34 |
with content_image:
|
35 |
ContentColumnTitle = "## πΌοΈ Content Image πΌοΈ"
|
36 |
ContentImageSelectionPrompt = "Pick a Content image"
|
37 |
+
content_image_file = upload_image(
|
38 |
ContentColumnTitle, ContentImageSelectionPrompt, "Content", "content"
|
39 |
)
|
40 |
|
41 |
with style_image:
|
42 |
StyleColumnTitle = "## π¨ Style Image π¨"
|
43 |
StyleImageSelectionPrompt = "Pick a Style image"
|
44 |
+
style_image_file = upload_image(
|
45 |
StyleColumnTitle, StyleImageSelectionPrompt, "Style", "style"
|
46 |
)
|
47 |
|
48 |
if None not in (content_image_file, style_image_file):
|
49 |
+
try:
|
50 |
# CLEAR IMAGES
|
51 |
clear_images = st.button(
|
52 |
+
label="πβ Clear Image Cache βπ",
|
53 |
on_click=remove_source_images(),
|
54 |
)
|
55 |
+
except:
|
56 |
+
pass
|
57 |
|
58 |
# STYLIZE CONTENT IMAGE
|
59 |
stylize_image = st.button("πΌοΈποΈπ¨ Start Neural Style Transfer πΌοΈποΈπ¨")
|
|
|
67 |
except:
|
68 |
pass
|
69 |
|
|
|
70 |
else:
|
71 |
st.write("Please upload content and style image to go to next step.")
|
src/data/data.py
CHANGED
@@ -5,6 +5,21 @@ import tensorflow as tf
|
|
5 |
from ..image_utils import load_img, imshow, transform_img
|
6 |
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
def upload_image_file(ImageSelectionPrompt, ImageType, image_upload_method):
|
9 |
st.write(f"{ImageSelectionPrompt}: {image_upload_method}")
|
10 |
image_file = st.file_uploader(
|
@@ -17,17 +32,12 @@ def upload_image_file(ImageSelectionPrompt, ImageType, image_upload_method):
|
|
17 |
pass
|
18 |
|
19 |
|
20 |
-
def
|
21 |
st.write(f"{ImageSelectionPrompt}: {image_upload_method}")
|
22 |
-
|
23 |
try:
|
24 |
-
|
25 |
-
|
26 |
-
)
|
27 |
-
except:
|
28 |
-
pass
|
29 |
-
try:
|
30 |
-
return load_img(image_path)
|
31 |
except:
|
32 |
pass
|
33 |
|
@@ -35,19 +45,25 @@ def upload_image_url(ImageSelectionPrompt, ImageType, image_upload_method):
|
|
35 |
def upload_image(ColumnTitle, ImageSelectionPrompt, ImageType, KeyString):
|
36 |
st.write(ColumnTitle)
|
37 |
image_upload_method = st.radio(
|
38 |
-
label="", options=["π File Upload", "
|
39 |
)
|
|
|
|
|
|
|
|
|
|
|
40 |
if image_upload_method == "π File Upload":
|
41 |
image_file = upload_image_file(
|
42 |
ImageSelectionPrompt, ImageType, image_upload_method
|
43 |
)
|
44 |
-
|
45 |
-
|
|
|
46 |
ImageSelectionPrompt, ImageType, image_upload_method
|
47 |
)
|
48 |
try:
|
49 |
st.write(f"{ImageType} Image")
|
50 |
st.image(imshow(image_file))
|
51 |
-
return image_file
|
52 |
except:
|
53 |
pass
|
|
|
5 |
from ..image_utils import load_img, imshow, transform_img
|
6 |
|
7 |
|
8 |
+
def upload_image_url(ImageSelectionPrompt, ImageType, image_upload_method):
|
9 |
+
st.write(f"{ImageSelectionPrompt}: {image_upload_method}")
|
10 |
+
url = st.text_input(f"{ImageType} Image URL")
|
11 |
+
try:
|
12 |
+
image_path = tf.keras.utils.get_file(
|
13 |
+
os.path.join(os.getcwd(), f"{ImageType.lower()}.jpg"), url
|
14 |
+
)
|
15 |
+
except:
|
16 |
+
pass
|
17 |
+
try:
|
18 |
+
return load_img(image_path)
|
19 |
+
except:
|
20 |
+
pass
|
21 |
+
|
22 |
+
|
23 |
def upload_image_file(ImageSelectionPrompt, ImageType, image_upload_method):
|
24 |
st.write(f"{ImageSelectionPrompt}: {image_upload_method}")
|
25 |
image_file = st.file_uploader(
|
|
|
32 |
pass
|
33 |
|
34 |
|
35 |
+
def upload_image_capture(ImageSelectionPrompt, ImageType, image_upload_method):
|
36 |
st.write(f"{ImageSelectionPrompt}: {image_upload_method}")
|
37 |
+
image_file = st.camera_input(f"Capture {ImageType} Image")
|
38 |
try:
|
39 |
+
image_file = image_file.read()
|
40 |
+
return transform_img(image_file)
|
|
|
|
|
|
|
|
|
|
|
41 |
except:
|
42 |
pass
|
43 |
|
|
|
45 |
def upload_image(ColumnTitle, ImageSelectionPrompt, ImageType, KeyString):
|
46 |
st.write(ColumnTitle)
|
47 |
image_upload_method = st.radio(
|
48 |
+
label="", options=["π URL", "π File Upload", "πΈ Capture"], key=KeyString
|
49 |
)
|
50 |
+
if image_upload_method == "π URL":
|
51 |
+
image_file = upload_image_url(
|
52 |
+
ImageSelectionPrompt, ImageType, image_upload_method
|
53 |
+
)
|
54 |
+
|
55 |
if image_upload_method == "π File Upload":
|
56 |
image_file = upload_image_file(
|
57 |
ImageSelectionPrompt, ImageType, image_upload_method
|
58 |
)
|
59 |
+
|
60 |
+
if image_upload_method == "πΈ Capture":
|
61 |
+
image_file = upload_image_capture(
|
62 |
ImageSelectionPrompt, ImageType, image_upload_method
|
63 |
)
|
64 |
try:
|
65 |
st.write(f"{ImageType} Image")
|
66 |
st.image(imshow(image_file))
|
67 |
+
return image_file
|
68 |
except:
|
69 |
pass
|