Update app.py
Browse files
app.py
CHANGED
@@ -15,44 +15,91 @@ from PIL import Image as PILImage
|
|
15 |
from io import BytesIO
|
16 |
|
17 |
# Streamlit title
|
18 |
-
st.title("Vehicle Information Extraction from Images")
|
19 |
-
|
20 |
-
|
21 |
-
st.
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
#
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
def display_image_grid(image_paths, rows=2, cols=3, figsize=(10, 7)):
|
57 |
fig = plt.figure(figsize=figsize)
|
58 |
max_images = rows * cols
|
@@ -69,41 +116,15 @@ def display_image_grid(image_paths, rows=2, cols=3, figsize=(10, 7)):
|
|
69 |
plt.tight_layout()
|
70 |
st.pyplot(fig)
|
71 |
|
72 |
-
#
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
- License plate number and type (if identifiable, such as personal, commercial, government)
|
79 |
-
- Vehicle make, model, and year (e.g., 2020 Toyota Corolla)
|
80 |
-
- Vehicle color and condition (e.g., Red, well-maintained, damaged)
|
81 |
-
- Any visible brand logos or distinguishing marks (e.g., Tesla logo)
|
82 |
-
- Details of any visible damage (e.g., scratches, dents)
|
83 |
-
- Vehicleβs region or country (based on the license plate or other clues)
|
84 |
-
If some details are unclear or not visible, return `None` for those fields. Do not guess or provide inaccurate information."""),
|
85 |
-
HumanMessage(
|
86 |
-
content=[
|
87 |
-
{"type": "text", "text": "Analyze the vehicle in the image and extract as many details as possible, including type, license plate, make, model, year, condition, damage, etc."},
|
88 |
-
{"type": "text", "text": instructions}, # include any other format instructions here
|
89 |
-
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{inputs['image']}", "detail": "low"}}
|
90 |
-
]
|
91 |
-
)
|
92 |
-
]
|
93 |
-
return prompt
|
94 |
-
|
95 |
-
# Invoke the model for extracting vehicle details
|
96 |
-
@chain
|
97 |
-
def MLLM_response(inputs):
|
98 |
-
model: ChatOpenAI = ChatOpenAI(model="gpt-4o-2024-08-06", temperature=0.0, max_tokens=1024)
|
99 |
-
output = model.invoke(inputs)
|
100 |
-
return output.content
|
101 |
-
|
102 |
-
# The complete pipeline for extracting vehicle details
|
103 |
-
pipeline = image_encoding | prompt | MLLM_response | parser
|
104 |
|
105 |
# Streamlit Interface for uploading images and showing results
|
106 |
-
st.header("Upload Vehicle Images for Information Extraction")
|
107 |
|
108 |
# Option to select either single or batch image upload
|
109 |
upload_option = st.radio("Select Upload Type", ["Single Image Upload", "Batch Images Upload"])
|
@@ -114,23 +135,23 @@ if upload_option == "Single Image Upload":
|
|
114 |
uploaded_image = st.file_uploader("Choose an Image (JPEG, PNG, GIF, BMP, etc.)", type=["jpeg", "png", "gif", "bmp", "jpg"])
|
115 |
|
116 |
if uploaded_image is not None:
|
117 |
-
# Display the uploaded image
|
118 |
image = PILImage.open(uploaded_image)
|
119 |
-
st.image(image, caption="Uploaded Image", use_container_width=True)
|
120 |
|
121 |
# Convert the uploaded image to base64
|
122 |
image_path = "/tmp/uploaded_image" + os.path.splitext(uploaded_image.name)[1]
|
123 |
with open(image_path, "wb") as f:
|
124 |
f.write(uploaded_image.getbuffer())
|
125 |
|
126 |
-
# Add button to trigger information extraction
|
127 |
if st.button("Extract Vehicle Information"):
|
128 |
# Process the image through the pipeline
|
129 |
output = pipeline.invoke({"image_path": image_path})
|
130 |
|
131 |
# Show the results in a user-friendly format
|
132 |
-
st.subheader("Extracted Vehicle Information")
|
133 |
-
st.json(output)
|
134 |
|
135 |
# Batch Images Upload
|
136 |
elif upload_option == "Batch Images Upload":
|
@@ -148,8 +169,12 @@ elif upload_option == "Batch Images Upload":
|
|
148 |
# Process the batch and display the results in a DataFrame
|
149 |
batch_output = pipeline.batch(batch_input)
|
150 |
df = pd.DataFrame(batch_output)
|
151 |
-
st.dataframe(df)
|
152 |
|
153 |
-
#
|
|
|
154 |
image_paths = [f"/tmp/{file.name}" for file in batch_images]
|
155 |
-
|
|
|
|
|
|
|
|
15 |
from io import BytesIO
|
16 |
|
17 |
# Streamlit title
|
18 |
+
st.title("π Vehicle Information Extraction from Images π")
|
19 |
+
|
20 |
+
# Custom CSS Styling for the app
|
21 |
+
st.markdown("""
|
22 |
+
<style>
|
23 |
+
body {
|
24 |
+
background-color: #f0f8ff;
|
25 |
+
font-family: 'Arial', sans-serif;
|
26 |
+
color: #333;
|
27 |
+
}
|
28 |
+
|
29 |
+
.stButton button {
|
30 |
+
background-color: #1E90FF;
|
31 |
+
color: white;
|
32 |
+
border-radius: 5px;
|
33 |
+
font-size: 16px;
|
34 |
+
padding: 10px 20px;
|
35 |
+
transition: transform 0.2s ease-in-out;
|
36 |
+
}
|
37 |
+
|
38 |
+
.stButton button:hover {
|
39 |
+
background-color: #4682b4;
|
40 |
+
transform: scale(1.05);
|
41 |
+
}
|
42 |
+
|
43 |
+
.stTitle {
|
44 |
+
font-size: 36px;
|
45 |
+
font-weight: bold;
|
46 |
+
color: #1E90FF;
|
47 |
+
}
|
48 |
+
|
49 |
+
.stSubheader {
|
50 |
+
color: #4682b4;
|
51 |
+
font-size: 24px;
|
52 |
+
font-weight: 600;
|
53 |
+
text-align: center;
|
54 |
+
}
|
55 |
+
|
56 |
+
.stImage {
|
57 |
+
border-radius: 10px;
|
58 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
59 |
+
}
|
60 |
+
|
61 |
+
.stJson {
|
62 |
+
background-color: #f8f9fa;
|
63 |
+
padding: 15px;
|
64 |
+
border-radius: 5px;
|
65 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
66 |
+
font-size: 16px;
|
67 |
+
}
|
68 |
+
|
69 |
+
.grid-container {
|
70 |
+
display: grid;
|
71 |
+
grid-template-columns: repeat(3, 1fr);
|
72 |
+
gap: 10px;
|
73 |
+
padding: 10px;
|
74 |
+
}
|
75 |
+
|
76 |
+
.grid-container img {
|
77 |
+
border-radius: 10px;
|
78 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
79 |
+
transition: transform 0.2s ease-in-out;
|
80 |
+
}
|
81 |
+
|
82 |
+
.grid-container img:hover {
|
83 |
+
transform: scale(1.05);
|
84 |
+
}
|
85 |
+
|
86 |
+
@keyframes fadeIn {
|
87 |
+
from {
|
88 |
+
opacity: 0;
|
89 |
+
}
|
90 |
+
to {
|
91 |
+
opacity: 1;
|
92 |
+
}
|
93 |
+
}
|
94 |
+
|
95 |
+
.fade-in {
|
96 |
+
animation: fadeIn 1s ease-in-out;
|
97 |
+
}
|
98 |
+
|
99 |
+
</style>
|
100 |
+
""", unsafe_allow_html=True)
|
101 |
+
|
102 |
+
# Image display function with animation
|
103 |
def display_image_grid(image_paths, rows=2, cols=3, figsize=(10, 7)):
|
104 |
fig = plt.figure(figsize=figsize)
|
105 |
max_images = rows * cols
|
|
|
116 |
plt.tight_layout()
|
117 |
st.pyplot(fig)
|
118 |
|
119 |
+
# Image encoding function
|
120 |
+
def image_encoding(inputs):
|
121 |
+
"""Load and convert image to base64 encoding"""
|
122 |
+
with open(inputs["image_path"], "rb") as image_file:
|
123 |
+
image_base64 = base64.b64encode(image_file.read()).decode("utf-8")
|
124 |
+
return {"image": image_base64}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
# Streamlit Interface for uploading images and showing results
|
127 |
+
st.header("π Upload Vehicle Images for Information Extraction π")
|
128 |
|
129 |
# Option to select either single or batch image upload
|
130 |
upload_option = st.radio("Select Upload Type", ["Single Image Upload", "Batch Images Upload"])
|
|
|
135 |
uploaded_image = st.file_uploader("Choose an Image (JPEG, PNG, GIF, BMP, etc.)", type=["jpeg", "png", "gif", "bmp", "jpg"])
|
136 |
|
137 |
if uploaded_image is not None:
|
138 |
+
# Display the uploaded image with animation
|
139 |
image = PILImage.open(uploaded_image)
|
140 |
+
st.image(image, caption="Uploaded Image", use_container_width=True, class_="fade-in")
|
141 |
|
142 |
# Convert the uploaded image to base64
|
143 |
image_path = "/tmp/uploaded_image" + os.path.splitext(uploaded_image.name)[1]
|
144 |
with open(image_path, "wb") as f:
|
145 |
f.write(uploaded_image.getbuffer())
|
146 |
|
147 |
+
# Add button to trigger information extraction with animated hover effect
|
148 |
if st.button("Extract Vehicle Information"):
|
149 |
# Process the image through the pipeline
|
150 |
output = pipeline.invoke({"image_path": image_path})
|
151 |
|
152 |
# Show the results in a user-friendly format
|
153 |
+
st.subheader("Extracted Vehicle Information", class_="fade-in")
|
154 |
+
st.json(output, class_="fade-in")
|
155 |
|
156 |
# Batch Images Upload
|
157 |
elif upload_option == "Batch Images Upload":
|
|
|
169 |
# Process the batch and display the results in a DataFrame
|
170 |
batch_output = pipeline.batch(batch_input)
|
171 |
df = pd.DataFrame(batch_output)
|
172 |
+
st.dataframe(df, use_container_width=True)
|
173 |
|
174 |
+
# Display batch images in a grid with animation
|
175 |
+
st.subheader("Images in Grid", class_="fade-in")
|
176 |
image_paths = [f"/tmp/{file.name}" for file in batch_images]
|
177 |
+
st.markdown('<div class="grid-container">', unsafe_allow_html=True)
|
178 |
+
for image_path in image_paths:
|
179 |
+
st.markdown(f'<img src="data:image/jpeg;base64,{base64.b64encode(open(image_path, "rb").read()).decode()}" class="fade-in"/>', unsafe_allow_html=True)
|
180 |
+
st.markdown('</div>', unsafe_allow_html=True)
|