Update app.py
Browse files
app.py
CHANGED
@@ -10,22 +10,7 @@ from my_model.captioner.image_captioning import get_caption
|
|
10 |
from my_model.utilities import free_gpu_resources
|
11 |
|
12 |
|
13 |
-
|
14 |
-
"""
|
15 |
-
Perform object detection on the given image using the specified model and threshold.
|
16 |
-
Args:
|
17 |
-
image (PIL.Image): The image on which to perform object detection.
|
18 |
-
model_name (str): The name of the object detection model to use.
|
19 |
-
threshold (float): The threshold for object detection.
|
20 |
-
Returns:
|
21 |
-
PIL.Image, str: The image with drawn bounding boxes and a string of detected objects.
|
22 |
-
"""
|
23 |
-
|
24 |
-
processed_image, detected_objects = detect_and_draw_objects(image, model_name, threshold)
|
25 |
-
|
26 |
-
return processed_image, detected_objects
|
27 |
-
|
28 |
-
|
29 |
# Placeholder for undefined functions
|
30 |
def load_caption_model():
|
31 |
st.write("Placeholder for load_caption_model function")
|
@@ -34,9 +19,6 @@ def load_caption_model():
|
|
34 |
def answer_question(image, question, model, processor):
|
35 |
return "Placeholder answer for the question"
|
36 |
|
37 |
-
def detect_and_draw_objects(image, model_name, threshold):
|
38 |
-
perform_object_detection()
|
39 |
-
|
40 |
def get_caption(image):
|
41 |
return "Generated caption for the image"
|
42 |
|
@@ -44,50 +26,12 @@ def free_gpu_resources():
|
|
44 |
pass
|
45 |
|
46 |
# Sample images (assuming these are paths to your sample images)
|
47 |
-
sample_images = ["Files/sample1.jpg", "Files/sample2.jpg", "Files/sample3.jpg",
|
48 |
-
|
49 |
-
|
50 |
-
def main():
|
51 |
-
st.sidebar.title("Navigation")
|
52 |
-
selection = st.sidebar.radio("Go to", ["Home", "Dataset Analysis", "Evaluation Results", "Run Inference", "Dissertation Report", "Object Detection"])
|
53 |
-
|
54 |
-
if selection == "Home":
|
55 |
-
st.title("MultiModal Learning for Knowledg-Based Visual Question Answering")
|
56 |
-
st.write("Home page content goes here...")
|
57 |
-
|
58 |
-
elif selection == "Dissertation Report":
|
59 |
-
st.title("Dissertation Report")
|
60 |
-
st.write("Click the link below to view the PDF.")
|
61 |
-
# Example to display a link to a PDF
|
62 |
-
st.download_button(
|
63 |
-
label="Download PDF",
|
64 |
-
data=open("Files/Dissertation Report.pdf", "rb"),
|
65 |
-
file_name="example.pdf",
|
66 |
-
mime="application/octet-stream"
|
67 |
-
)
|
68 |
-
|
69 |
-
|
70 |
-
elif selection == "Evaluation Results":
|
71 |
-
st.title("Evaluation Results")
|
72 |
-
st.write("This is a Place Holder until the contents are uploaded.")
|
73 |
-
|
74 |
-
|
75 |
-
elif selection == "Dataset Analysis":
|
76 |
-
st.title("OK-VQA Dataset Analysis")
|
77 |
-
st.write("This is a Place Holder until the contents are uploaded.")
|
78 |
-
|
79 |
-
|
80 |
-
elif selection == "Run Inference":
|
81 |
-
run_inference()
|
82 |
-
|
83 |
-
elif selection == "Object Detection":
|
84 |
-
run_object_detection()
|
85 |
-
|
86 |
-
# Other display functions...
|
87 |
|
88 |
def run_inference():
|
89 |
st.title("Run Inference")
|
90 |
-
# Image-based Q&A and Object Detection functionality
|
91 |
image_qa_and_object_detection()
|
92 |
|
93 |
def image_qa_and_object_detection():
|
@@ -109,12 +53,8 @@ def image_qa_app():
|
|
109 |
st.session_state['images_qa_history'] = []
|
110 |
st.experimental_rerun()
|
111 |
|
112 |
-
|
113 |
# Image uploader
|
114 |
uploaded_image = st.file_uploader("Upload an Image", type=["png", "jpg", "jpeg"])
|
115 |
-
if uploaded_image is not None:
|
116 |
-
image = Image.open(uploaded_image)
|
117 |
-
process_uploaded_image(image)
|
118 |
|
119 |
# Display sample images
|
120 |
st.write("Or choose from sample images:")
|
@@ -123,16 +63,48 @@ def image_qa_app():
|
|
123 |
uploaded_image = Image.open(sample_image_path)
|
124 |
process_uploaded_image(uploaded_image)
|
125 |
|
|
|
|
|
|
|
|
|
126 |
def process_uploaded_image(image):
|
127 |
current_image_key = image.filename # Use image filename as a unique key
|
128 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
# Object Detection App
|
131 |
def object_detection_app():
|
132 |
# ... Implement your code for object detection ...
|
133 |
pass
|
134 |
|
135 |
-
#
|
136 |
|
137 |
if __name__ == "__main__":
|
138 |
main()
|
|
|
10 |
from my_model.utilities import free_gpu_resources
|
11 |
|
12 |
|
13 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
# Placeholder for undefined functions
|
15 |
def load_caption_model():
|
16 |
st.write("Placeholder for load_caption_model function")
|
|
|
19 |
def answer_question(image, question, model, processor):
|
20 |
return "Placeholder answer for the question"
|
21 |
|
|
|
|
|
|
|
22 |
def get_caption(image):
|
23 |
return "Generated caption for the image"
|
24 |
|
|
|
26 |
pass
|
27 |
|
28 |
# Sample images (assuming these are paths to your sample images)
|
29 |
+
sample_images = ["Files/sample1.jpg", "Files/sample2.jpg", "Files/sample3.jpg",
|
30 |
+
"Files/sample4.jpg", "Files/sample5.jpg", "Files/sample6.jpg",
|
31 |
+
"Files/sample7.jpg"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
def run_inference():
|
34 |
st.title("Run Inference")
|
|
|
35 |
image_qa_and_object_detection()
|
36 |
|
37 |
def image_qa_and_object_detection():
|
|
|
53 |
st.session_state['images_qa_history'] = []
|
54 |
st.experimental_rerun()
|
55 |
|
|
|
56 |
# Image uploader
|
57 |
uploaded_image = st.file_uploader("Upload an Image", type=["png", "jpg", "jpeg"])
|
|
|
|
|
|
|
58 |
|
59 |
# Display sample images
|
60 |
st.write("Or choose from sample images:")
|
|
|
63 |
uploaded_image = Image.open(sample_image_path)
|
64 |
process_uploaded_image(uploaded_image)
|
65 |
|
66 |
+
if uploaded_image is not None:
|
67 |
+
image = Image.open(uploaded_image)
|
68 |
+
process_uploaded_image(image)
|
69 |
+
|
70 |
def process_uploaded_image(image):
|
71 |
current_image_key = image.filename # Use image filename as a unique key
|
72 |
+
# Check if the image is already in the history
|
73 |
+
if not any(info['image_key'] == current_image_key for info in st.session_state['images_qa_history']):
|
74 |
+
st.session_state['images_qa_history'].append({
|
75 |
+
'image_key': current_image_key,
|
76 |
+
'image': image,
|
77 |
+
'qa_history': []
|
78 |
+
})
|
79 |
+
|
80 |
+
# Display all images and their Q&A histories
|
81 |
+
for image_info in st.session_state['images_qa_history']:
|
82 |
+
st.image(image_info['image'], caption='Uploaded Image.', use_column_width=True)
|
83 |
+
for q, a in image_info['qa_history']:
|
84 |
+
st.text(f"Q: {q}\nA: {a}\n")
|
85 |
+
|
86 |
+
# If the current image is being processed
|
87 |
+
if image_info['image_key'] == current_image_key:
|
88 |
+
# Unique keys for each widget
|
89 |
+
question_key = f"question_{current_image_key}"
|
90 |
+
button_key = f"button_{current_image_key}"
|
91 |
+
|
92 |
+
# Question input for the current image
|
93 |
+
question = st.text_input("Ask a question about this image:", key=question_key)
|
94 |
+
|
95 |
+
# Get Answer button for the current image
|
96 |
+
if st.button('Get Answer', key=button_key):
|
97 |
+
# Process the image and question
|
98 |
+
answer = answer_question(image_info['image'], question, None, None) # Implement this function
|
99 |
+
image_info['qa_history'].append((question, answer))
|
100 |
+
st.experimental_rerun() # Rerun to update the display
|
101 |
|
102 |
# Object Detection App
|
103 |
def object_detection_app():
|
104 |
# ... Implement your code for object detection ...
|
105 |
pass
|
106 |
|
107 |
+
# Main function and other display functions...
|
108 |
|
109 |
if __name__ == "__main__":
|
110 |
main()
|