pkiage commited on
Commit
75ed2e1
β€’
1 Parent(s): 0740ae4

feat: add image capture

Browse files

- URL image cache using exception handling
- image input using camera

Files changed (2) hide show
  1. app.py +6 -5
  2. 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, content_image_upload_method = 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, style_image_upload_method = upload_image(
45
  StyleColumnTitle, StyleImageSelectionPrompt, "Style", "style"
46
  )
47
 
48
  if None not in (content_image_file, style_image_file):
49
- if "πŸ”— URL" in (content_image_upload_method, style_image_upload_method):
50
  # CLEAR IMAGES
51
  clear_images = st.button(
52
- label="πŸ”„βŒ Clear URL Image Cache βŒπŸ”„",
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 upload_image_url(ImageSelectionPrompt, ImageType, image_upload_method):
21
  st.write(f"{ImageSelectionPrompt}: {image_upload_method}")
22
- url = st.text_input(f"{ImageType} Image URL")
23
  try:
24
- image_path = tf.keras.utils.get_file(
25
- os.path.join(os.getcwd(), f"{ImageType.lower()}.jpg"), url
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", "πŸ”— URL"], key=KeyString
39
  )
 
 
 
 
 
40
  if image_upload_method == "πŸ“ File Upload":
41
  image_file = upload_image_file(
42
  ImageSelectionPrompt, ImageType, image_upload_method
43
  )
44
- if image_upload_method == "πŸ”— URL":
45
- image_file = upload_image_url(
 
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, image_upload_method
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