Pushpanjali commited on
Commit
e15a1db
·
1 Parent(s): 3b88be5

adding files

Browse files
Files changed (4) hide show
  1. app_seg_llm.py +203 -0
  2. best.pt +3 -0
  3. packages.txt +1 -0
  4. requirements.txt +8 -0
app_seg_llm.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import os
4
+ import anthropic
5
+ import base64
6
+ import numpy as np
7
+ from dotenv import load_dotenv
8
+ import cv2
9
+ import tempfile
10
+ import easyocr
11
+ import pytesseract
12
+ load_dotenv()
13
+
14
+
15
+ from yolo_functions import segment_large_image_with_tiles , usable_data , plot_differences_on_image1 , system_prompt_4 , blueprint_analyzer
16
+ from ultralytics import YOLO
17
+ from openai import OpenAI
18
+ import os
19
+
20
+
21
+
22
+ client = anthropic.Anthropic(
23
+ # api_key="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",
24
+ api_key = os.getenv('ANTHROPIC_API_KEY')
25
+ )
26
+
27
+ openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
28
+
29
+
30
+ def encode_image(image_path):
31
+ # with open(image_path, "rb") as image_file:
32
+ # return base64.b64encode(image_file.read()).decode("utf-8")
33
+ return base64.b64encode(image_path.getvalue()).decode("utf-8")
34
+
35
+ def chat_claude(prompt , image1 , image2 ) :
36
+ # print("image 1" , image1)
37
+ image1_data = encode_image(image1)
38
+ # print("image 1 data" , image1_data)
39
+ image2_data = encode_image(image2)
40
+ message = client.messages.create(
41
+ model="claude-3-opus-20240229",
42
+ max_tokens = 4096,
43
+ temperature=0,
44
+ messages=[
45
+ {
46
+ "role": "user",
47
+ "content": [
48
+ {
49
+ "type": "text",
50
+ "text": "Image 1:"
51
+ },
52
+ {
53
+ "type": "image",
54
+ "source": {
55
+ "type": "base64",
56
+ "media_type": "image/jpeg",
57
+ "data": image1_data,
58
+ },
59
+ },
60
+ {
61
+ "type": "text",
62
+ "text": "Image 2:"
63
+ },
64
+ {
65
+ "type": "image",
66
+ "source": {
67
+ "type": "base64",
68
+ "media_type": "image/jpeg",
69
+ "data": image2_data,
70
+ },
71
+ },
72
+ {
73
+ "type": "text",
74
+ "text": f"{prompt}"
75
+ }
76
+ ],
77
+ }
78
+ ],
79
+ )
80
+ return message.content[0].text
81
+
82
+
83
+ prompt = """Given 2 construction blueprints your task is to analyze carefully both blueprints and point out difference for following categories -
84
+ 1. Strcutural grid.
85
+ 2. Layout Areas - rooms , balcony , porch , staircase , elevator etc.
86
+ 3. Interior changes or optimization.
87
+ Summarize all the difference in paragraph concisely.
88
+ """
89
+ st.set_page_config(layout = "wide")
90
+ uploaded_files = st.file_uploader("Upload 2 image to compare", accept_multiple_files=True)
91
+ # import pdb; pdb.set_trace()
92
+ # print("upladed file length" , len(uploaded_files))
93
+ if len(uploaded_files) !=0 :
94
+ temp_dir = tempfile.TemporaryDirectory()
95
+ # print(temp_dir)
96
+ i = 0
97
+ for one_file in uploaded_files :
98
+ if i == 0 :
99
+ img1 = Image.open(one_file)
100
+ sv_path_1 = temp_dir.name + "/img1.jpg"
101
+ img1.save(sv_path_1)
102
+ # print("uploaded file" , one_file)
103
+ # print("img1" , img1)
104
+
105
+ tmp_img1 = one_file
106
+ # print("tmp_img1" , tmp_img1)
107
+ st.image(img1)
108
+ i = i + 1
109
+ if i == 1 :
110
+ img2 = Image.open(one_file)
111
+ sv_path_2 = temp_dir.name + "/img2.jpg"
112
+ img2.save(sv_path_2)
113
+ tmp_img2 = one_file
114
+ st.image(img2)
115
+ i = i + 1
116
+
117
+ col1 , col2 = st.columns(2)
118
+ col1.header("LLM")
119
+ col2.header("Seg-LLM !")
120
+ # import pdb; pdb.set_trace()
121
+ llm_ans = chat_claude(prompt , tmp_img1 , tmp_img2)
122
+ print(llm_ans)
123
+ col1.write(llm_ans)
124
+ #################### yolo segment from here ################
125
+ model = YOLO("best.pt")
126
+
127
+ final_output_1, class_mask_dict_1 = segment_large_image_with_tiles(
128
+ model,
129
+ # large_image_path=img_1_path,
130
+ # large_image_path= tmp_img1 ,
131
+ large_image_path= sv_path_1 ,
132
+ tile_size=1080,
133
+ overlap=120,
134
+ alpha=0.4,
135
+ display=True
136
+ )
137
+ final_output_2, class_mask_dict_2= segment_large_image_with_tiles(
138
+ model,
139
+ large_image_path=sv_path_2,
140
+ tile_size=1080,
141
+ alpha=0.4,
142
+ display=True
143
+ )
144
+ label_dict = {0: 'EMP', 1: 'balcony_area', 2: 'bathroom', 3: 'brick_wall', 4: 'concrete_wall', 5: 'corridor', 6: 'dining_area', 7: 'door', 8: 'double_window', 9: 'dressing_room', 10: 'elevator', 11: 'elevator_hall', 12: 'emergency_exit', 13: 'empty_area', 14: 'lobby', 15: 'pantry', 16: 'porch', 17: 'primary_insulation', 18: 'rooms', 19: 'single_window', 20: 'stairs', 21: 'thin_wall'}
145
+ img1_results = {}
146
+ for key in class_mask_dict_1.keys():
147
+ img1_results[label_dict[key]] = class_mask_dict_1[key]
148
+ img2_results = {}
149
+ for key in class_mask_dict_2.keys():
150
+ img2_results[label_dict[key]] = class_mask_dict_2[key]
151
+ image_1 , image_2 = img1 , img2
152
+ width, height = image_1.width, image_1.height
153
+ image_1_data = usable_data(img1_results, image_1)
154
+ image_2_data = usable_data(img2_results, image_2)
155
+
156
+ lines_1, text_data_1 = blueprint_analyzer(sv_path_1)
157
+ lines_2, text_data_2 = blueprint_analyzer(sv_path_2)
158
+
159
+ user_prompt_3 = f"""I have two construction blueprint images, Image 1 and Image 2, and here are their segmentation results (with bounding boxes, centers, and areas). Please compare them and provide a short Markdown summary of the differences, ignoring any objects that match in both images:
160
+
161
+ Image 1:
162
+ image: {image_1}
163
+ segmentation results: {image_1_data}
164
+ grid lines: {lines_1}
165
+ ocr results: {text_data_1}
166
+ Image 2:
167
+ image: {image_2}
168
+ segmentation results: {image_2_data}
169
+ grid lines: {lines_2}
170
+ ocr results: {text_data_2}
171
+
172
+ Please:
173
+ Compare the two images only in terms of differences—ignore any objects that match (same label and near-identical center).
174
+ For objects missing in Image 2 (but present in Image 1), or newly added in Image 2, indicate their relative position using known areas or approximate directions. For instance, mention if the missing doors were “towards the north side, near the elevator,” or if new walls appeared “in the southeastern corner, near the balcony.”
175
+ Summarize any changes in labels or text, again without giving raw bounding box or polygon coordinate data.
176
+ Provide your final output in a short, clear Markdown summary that describes where objects have changed.
177
+ Mention if there are text/label changes (e.g., from an OCR perspective) in any particular area or region
178
+ """
179
+
180
+
181
+
182
+
183
+
184
+ completion = client.chat.completions.create(
185
+ model="gpt-4o-mini",
186
+ messages=[
187
+ {"role": "system", "content": system_prompt_4},
188
+ {
189
+ "role": "user",
190
+ "content": user_prompt_3
191
+ }
192
+
193
+ ]
194
+ )
195
+
196
+ print(completion.choices[0].message.content)
197
+
198
+
199
+
200
+
201
+
202
+
203
+
best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b7fbbd7f37abf6d59606798c0224e5e5fea34689d8a2665b89677860c6a83fd3
3
+ size 54948245
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ libgl1-mesa-glx
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ anthropic==0.43.1
2
+ pillow==10.4.0
3
+ python-dotenv
4
+ opencv-python
5
+ openai
6
+ ultralytics
7
+ easyocr
8
+ pytesseract