Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,20 +15,14 @@ import my_model.utilities.st_config as st_config
|
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
-
class ImageHandler:
|
| 19 |
-
@staticmethod
|
| 20 |
-
def analyze_image(image, model, show_processed_image=False):
|
| 21 |
-
img = copy.deepcopy(image)
|
| 22 |
-
caption = model.get_caption(img)
|
| 23 |
-
image_with_boxes, detected_objects_str = model.detect_objects(img)
|
| 24 |
-
if show_processed_image:
|
| 25 |
-
st.image(image_with_boxes, use_column_width=True)
|
| 26 |
-
return caption, detected_objects_str
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
|
|
@@ -42,23 +36,6 @@ class QuestionAnswering:
|
|
| 42 |
st.write(detected_objects_str)
|
| 43 |
return answer
|
| 44 |
|
| 45 |
-
class UIComponents:
|
| 46 |
-
@staticmethod
|
| 47 |
-
def display_image_selection(sample_images):
|
| 48 |
-
cols = st.columns(len(sample_images))
|
| 49 |
-
for idx, sample_image_path in enumerate(sample_images):
|
| 50 |
-
with cols[idx]:
|
| 51 |
-
image = Image.open(sample_image_path)
|
| 52 |
-
st.image(image, use_column_width=True)
|
| 53 |
-
if st.button(f'Select Sample Image {idx + 1}', key=f'sample_{idx}'):
|
| 54 |
-
st.session_state['current_image'] = image
|
| 55 |
-
st.session_state['qa_history'] = []
|
| 56 |
-
st.session_state['analysis_done'] = False
|
| 57 |
-
st.session_state['answer_in_progress'] = False
|
| 58 |
-
|
| 59 |
-
@staticmethod
|
| 60 |
-
def display_image(img):
|
| 61 |
-
st.image(img, use_column_width=True)
|
| 62 |
|
| 63 |
def load_kbvqa_model(detection_model):
|
| 64 |
"""Load KBVQA Model based on the selected detection model."""
|
|
@@ -86,7 +63,18 @@ def set_model_confidence(detection_model):
|
|
| 86 |
def image_qa_app(kbvqa_model):
|
| 87 |
"""Streamlit app interface for image QA."""
|
| 88 |
sample_images = st_config.SAMPLE_IMAGES
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
uploaded_image = st.file_uploader("Or upload an Image", type=["png", "jpg", "jpeg"])
|
| 92 |
|
|
@@ -98,7 +86,7 @@ def image_qa_app(kbvqa_model):
|
|
| 98 |
|
| 99 |
# Display the image if it's in the session state
|
| 100 |
if 'current_image' in st.session_state:
|
| 101 |
-
|
| 102 |
|
| 103 |
else:
|
| 104 |
st.write("image not uploaded properly")
|
|
@@ -109,7 +97,7 @@ def image_qa_app(kbvqa_model):
|
|
| 109 |
|
| 110 |
if st.session_state.get('current_image') and not st.session_state.get('analysis_done', False):
|
| 111 |
if st.button('Analyze Image'):
|
| 112 |
-
caption, detected_objects_str =
|
| 113 |
st.session_state['caption'] = caption
|
| 114 |
st.session_state['detected_objects_str'] = detected_objects_str
|
| 115 |
st.session_state['analysis_done'] = True
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
def analyze_image(image, model, show_processed_image=False):
|
| 20 |
+
img = copy.deepcopy(image)
|
| 21 |
+
caption = model.get_caption(img)
|
| 22 |
+
image_with_boxes, detected_objects_str = model.detect_objects(img)
|
| 23 |
+
if show_processed_image:
|
| 24 |
+
st.image(image_with_boxes, use_column_width=True)
|
| 25 |
+
return caption, detected_objects_str
|
| 26 |
|
| 27 |
|
| 28 |
|
|
|
|
| 36 |
st.write(detected_objects_str)
|
| 37 |
return answer
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
def load_kbvqa_model(detection_model):
|
| 41 |
"""Load KBVQA Model based on the selected detection model."""
|
|
|
|
| 63 |
def image_qa_app(kbvqa_model):
|
| 64 |
"""Streamlit app interface for image QA."""
|
| 65 |
sample_images = st_config.SAMPLE_IMAGES
|
| 66 |
+
|
| 67 |
+
cols = st.columns(len(sample_images))
|
| 68 |
+
for idx, sample_image_path in enumerate(sample_images):
|
| 69 |
+
with cols[idx]:
|
| 70 |
+
image = Image.open(sample_image_path)
|
| 71 |
+
st.image(image, use_column_width=True)
|
| 72 |
+
if st.button(f'Select Sample Image {idx + 1}', key=f'sample_{idx}'):
|
| 73 |
+
st.session_state['current_image'] = image
|
| 74 |
+
st.session_state['qa_history'] = []
|
| 75 |
+
st.session_state['analysis_done'] = False
|
| 76 |
+
st.session_state['answer_in_progress'] = False
|
| 77 |
+
|
| 78 |
|
| 79 |
uploaded_image = st.file_uploader("Or upload an Image", type=["png", "jpg", "jpeg"])
|
| 80 |
|
|
|
|
| 86 |
|
| 87 |
# Display the image if it's in the session state
|
| 88 |
if 'current_image' in st.session_state:
|
| 89 |
+
st,image(st.session_state['current_image'], use_column_width=True)
|
| 90 |
|
| 91 |
else:
|
| 92 |
st.write("image not uploaded properly")
|
|
|
|
| 97 |
|
| 98 |
if st.session_state.get('current_image') and not st.session_state.get('analysis_done', False):
|
| 99 |
if st.button('Analyze Image'):
|
| 100 |
+
caption, detected_objects_str = analyze_image(st.session_state['current_image'], kbvqa_model)
|
| 101 |
st.session_state['caption'] = caption
|
| 102 |
st.session_state['detected_objects_str'] = detected_objects_str
|
| 103 |
st.session_state['analysis_done'] = True
|