shukdevdatta123 commited on
Commit
8f9de0f
·
verified ·
1 Parent(s): 9d8fac7

Create abc.txt

Browse files
Files changed (1) hide show
  1. abc.txt +148 -0
abc.txt ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import glob
4
+ import base64
5
+ import json
6
+ import pandas as pd
7
+ import matplotlib.pyplot as plt
8
+ import matplotlib.image as mpimg
9
+ from langchain_openai import ChatOpenAI
10
+ from langchain_core.pydantic_v1 import BaseModel, Field
11
+ from langchain_core.messages import HumanMessage, SystemMessage
12
+ from langchain_core.output_parsers import JsonOutputParser
13
+ from langchain_core.runnables import chain
14
+ 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
+ # Prompt user for OpenAI API key
21
+ openai_api_key = st.text_input("Enter your OpenAI API Key:", type="password")
22
+
23
+ # Set the OpenAI API key if provided
24
+ if openai_api_key:
25
+ os.environ["OPENAI_API_KEY"] = openai_api_key
26
+
27
+ # Vehicle class (same as in the original code)
28
+ class Vehicle(BaseModel):
29
+ Type: str = Field(..., examples=["Car", "Truck", "Motorcycle", 'Bus', 'Van'], description="The type of the vehicle.")
30
+ License: str = Field(..., description="The license plate number of the vehicle.")
31
+ Make: str = Field(..., examples=["Toyota", "Honda", "Ford", "Suzuki"], description="The Make of the vehicle.")
32
+ Model: str = Field(..., examples=["Corolla", "Civic", "F-150"], description="The Model of the vehicle.")
33
+ Color: str = Field(..., example=["Red", "Blue", "Black", "White"], description="Return the color of the vehicle.")
34
+ Year: str = Field(None, description="The year of the vehicle.")
35
+ Condition: str = Field(None, description="The condition of the vehicle.")
36
+ Logo: str = Field(None, description="The visible logo of the vehicle, if applicable.")
37
+ Damage: str = Field(None, description="Any visible damage or wear and tear on the vehicle.")
38
+ Region: str = Field(None, description="Region or country based on the license plate or clues from the image.")
39
+ PlateType: str = Field(None, description="Type of license plate, e.g., government, personal.")
40
+
41
+ # Parser for vehicle details
42
+ parser = JsonOutputParser(pydantic_object=Vehicle)
43
+ instructions = parser.get_format_instructions()
44
+
45
+ # Image encoding function (for base64 encoding)
46
+ def image_encoding(inputs):
47
+ """Load and convert image to base64 encoding"""
48
+ with open(inputs["image_path"], "rb") as image_file:
49
+ image_base64 = base64.b64encode(image_file.read()).decode("utf-8")
50
+ return {"image": image_base64}
51
+
52
+ # Image display in grid (for multiple images)
53
+ def display_image_grid(image_paths, rows=2, cols=3, figsize=(10, 7)):
54
+ fig = plt.figure(figsize=figsize)
55
+ max_images = rows * cols
56
+ image_paths = image_paths[:max_images]
57
+
58
+ for idx, path in enumerate(image_paths):
59
+ ax = fig.add_subplot(rows, cols, idx + 1)
60
+ img = mpimg.imread(path)
61
+ ax.imshow(img)
62
+ ax.axis('off')
63
+ filename = path.split('/')[-1]
64
+ ax.set_title(filename)
65
+
66
+ plt.tight_layout()
67
+ st.pyplot(fig)
68
+
69
+ # Create the prompt for the AI model
70
+ @chain
71
+ def prompt(inputs):
72
+ prompt = [
73
+ SystemMessage(content="""You are an AI assistant tasked with extracting detailed information from a vehicle image. Please extract the following details:
74
+ - Vehicle type (e.g., Car, Truck, Bus)
75
+ - License plate number and type (if identifiable, such as personal, commercial, government)
76
+ - Vehicle make, model, and year (e.g., 2020 Toyota Corolla)
77
+ - Vehicle color and condition (e.g., Red, well-maintained, damaged)
78
+ - Any visible brand logos or distinguishing marks (e.g., Tesla logo)
79
+ - Details of any visible damage (e.g., scratches, dents)
80
+ - Vehicle’s region or country (based on the license plate or other clues)
81
+ If some details are unclear or not visible, return None for those fields. Do not guess or provide inaccurate information."""
82
+ ),
83
+ HumanMessage(
84
+ content=[
85
+ {"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."},
86
+ {"type": "text", "text": instructions}, # include any other format instructions here
87
+ {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{inputs['image']}", "detail": "low"}}
88
+ ]
89
+ )
90
+ ]
91
+ return prompt
92
+
93
+ # Invoke the model for extracting vehicle details
94
+ @chain
95
+ def MLLM_response(inputs):
96
+ model: ChatOpenAI = ChatOpenAI(model="gpt-4o-2024-08-06", temperature=0.0, max_tokens=1024)
97
+ output = model.invoke(inputs)
98
+ return output.content
99
+
100
+ # The complete pipeline for extracting vehicle details
101
+ pipeline = image_encoding | prompt | MLLM_response | parser
102
+
103
+ # Streamlit Interface for uploading images and showing results
104
+ st.header("Upload a Vehicle Image for Information Extraction")
105
+
106
+ uploaded_image = st.file_uploader("Choose a JPEG image", type="jpeg")
107
+
108
+ if uploaded_image is not None:
109
+ # Display the uploaded image
110
+ image = PILImage.open(uploaded_image)
111
+ st.image(image, caption="Uploaded Image", use_column_width=True)
112
+
113
+ # Convert the uploaded image to base64
114
+ image_path = "/tmp/uploaded_image.jpeg"
115
+ with open(image_path, "wb") as f:
116
+ f.write(uploaded_image.getbuffer())
117
+
118
+ # Process the image through the pipeline
119
+ output = pipeline.invoke({"image_path": image_path})
120
+
121
+ # Show the results in a user-friendly format
122
+ st.subheader("Extracted Vehicle Information")
123
+ st.json(output)
124
+
125
+ # Optionally, display more vehicle images from the folder
126
+ img_dir = "/content/images"
127
+ image_paths = glob.glob(os.path.join(img_dir, "*.jpeg"))
128
+ display_image_grid(image_paths)
129
+
130
+ # You can also allow users to upload and process a batch of images
131
+ st.sidebar.header("Batch Image Upload")
132
+
133
+ batch_images = st.sidebar.file_uploader("Upload Images", type="jpeg", accept_multiple_files=True)
134
+
135
+ if batch_images:
136
+ batch_input = [{"image_path": f"/tmp/{file.name}"} for file in batch_images]
137
+ for file in batch_images:
138
+ with open(f"/tmp/{file.name}", "wb") as f:
139
+ f.write(file.getbuffer())
140
+
141
+ # Process the batch and display the results in a DataFrame
142
+ batch_output = pipeline.batch(batch_input)
143
+ df = pd.DataFrame(batch_output)
144
+ st.dataframe(df)
145
+
146
+ # Show images in a grid
147
+ image_paths = [f"/tmp/{file.name}" for file in batch_images]
148
+ display_image_grid(image_paths)