shukdevdatta123 commited on
Commit
2c9969e
·
verified ·
1 Parent(s): 1d7d460

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -104
app.py CHANGED
@@ -15,91 +15,44 @@ 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
- # 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,15 +69,41 @@ def display_image_grid(image_paths, rows=2, cols=3, figsize=(10, 7)):
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,23 +114,23 @@ if upload_option == "Single Image 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,12 +148,8 @@ 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)
 
15
  from io import BytesIO
16
 
17
  # Streamlit title
18
+ st.title("Vehicle Information Extraction from Images")
19
+
20
+ translateimg = PILImage.open("car.JPG") # Ensure the file is in the correct directory
21
+ st.image(translateimg, use_container_width=True) # Adjust the size as per preference
22
+
23
+ # Prompt user for OpenAI API key
24
+ openai_api_key = st.text_input("Enter your OpenAI API Key:", type="password")
25
+
26
+ # Set the OpenAI API key if provided
27
+ if openai_api_key:
28
+ os.environ["OPENAI_API_KEY"] = openai_api_key
29
+
30
+ # Vehicle class (same as in the original code)
31
+ class Vehicle(BaseModel):
32
+ Type: str = Field(..., examples=["Car", "Truck", "Motorcycle", 'Bus', 'Van'], description="The type of the vehicle.")
33
+ License: str = Field(..., description="The license plate number of the vehicle.")
34
+ Make: str = Field(..., examples=["Toyota", "Honda", "Ford", "Suzuki"], description="The Make of the vehicle.")
35
+ Model: str = Field(..., examples=["Corolla", "Civic", "F-150"], description="The Model of the vehicle.")
36
+ Color: str = Field(..., example=["Red", "Blue", "Black", "White"], description="Return the color of the vehicle.")
37
+ Year: str = Field(None, description="The year of the vehicle.")
38
+ Condition: str = Field(None, description="The condition of the vehicle.")
39
+ Logo: str = Field(None, description="The visible logo of the vehicle, if applicable.")
40
+ Damage: str = Field(None, description="Any visible damage or wear and tear on the vehicle.")
41
+ Region: str = Field(None, description="Region or country based on the license plate or clues from the image.")
42
+ PlateType: str = Field(None, description="Type of license plate, e.g., government, personal.")
43
+
44
+ # Parser for vehicle details
45
+ parser = JsonOutputParser(pydantic_object=Vehicle)
46
+ instructions = parser.get_format_instructions()
47
+
48
+ # Image encoding function (for base64 encoding)
49
+ def image_encoding(inputs):
50
+ """Load and convert image to base64 encoding"""
51
+ with open(inputs["image_path"], "rb") as image_file:
52
+ image_base64 = base64.b64encode(image_file.read()).decode("utf-8")
53
+ return {"image": image_base64}
54
+
55
+ # Image display in grid (for multiple images)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  plt.tight_layout()
70
  st.pyplot(fig)
71
 
72
+ # Create the prompt for the AI model
73
+ @chain
74
+ def prompt(inputs):
75
+ prompt = [
76
+ SystemMessage(content="""You are an AI assistant tasked with extracting detailed information from a vehicle image. Please extract the following details:
77
+ - Vehicle type (e.g., Car, Truck, Bus)
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
  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
  # 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
+ # Show images in a grid
 
154
  image_paths = [f"/tmp/{file.name}" for file in batch_images]
155
+ display_image_grid(image_paths)