ydshieh commited on
Commit
2a5dc71
1 Parent(s): 144ec50

update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -2
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  import requests
 
3
 
4
 
5
  # Designing the interface
@@ -32,16 +33,27 @@ if st.sidebar.button("Random COCO 2017 (val) images"):
32
  random_image_id = get_random_image_id()
33
  sample_image_id = "None"
34
 
 
 
 
 
 
 
 
 
 
35
  image_id = random_image_id
36
  if sample_image_id != "None":
37
  assert type(sample_image_id) == int
38
  image_id = sample_image_id
39
 
40
-
41
  sample_name = f"COCO_val2017_{str(image_id).zfill(12)}.jpg"
42
  sample_path = os.path.join(sample_dir, sample_name)
43
 
44
- if os.path.isfile(sample_path):
 
 
 
45
  image = Image.open(sample_path)
46
  else:
47
  url = f"http://images.cocodataset.org/val2017/{str(image_id).zfill(12)}.jpg"
 
1
  import streamlit as st
2
  import requests
3
+ import io
4
 
5
 
6
  # Designing the interface
 
33
  random_image_id = get_random_image_id()
34
  sample_image_id = "None"
35
 
36
+ bytes_data = None
37
+ with st.sidebar.form("file-uploader-form", clear_on_submit=True):
38
+ uploaded_file = st.file_uploader("Choose a file")
39
+ submitted = st.form_submit_button("Upload")
40
+ if submitted and uploaded_file is not None:
41
+ bytes_data = io.BytesIO(uploaded_file.getvalue())
42
+ uploaded_file = None
43
+ submitted = None
44
+
45
  image_id = random_image_id
46
  if sample_image_id != "None":
47
  assert type(sample_image_id) == int
48
  image_id = sample_image_id
49
 
 
50
  sample_name = f"COCO_val2017_{str(image_id).zfill(12)}.jpg"
51
  sample_path = os.path.join(sample_dir, sample_name)
52
 
53
+ if bytes_data is not None:
54
+ image = Image.open(bytes_data)
55
+ bytes_data = None
56
+ elif os.path.isfile(sample_path):
57
  image = Image.open(sample_path)
58
  else:
59
  url = f"http://images.cocodataset.org/val2017/{str(image_id).zfill(12)}.jpg"