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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sklearn.cluster import KMeans
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
from matplotlib import pyplot as plt
|
| 5 |
+
from bs4 import BeautifulSoup
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
def is_ring_contour(contour):
|
| 9 |
+
"""
|
| 10 |
+
简单的检查轮廓是否可能是环状的。
|
| 11 |
+
这个函数可以根据需要进一步完善。
|
| 12 |
+
"""
|
| 13 |
+
# 计算轮廓的面积
|
| 14 |
+
area = cv2.contourArea(contour)
|
| 15 |
+
# 计算轮廓的周长
|
| 16 |
+
perimeter = cv2.arcLength(contour, True)
|
| 17 |
+
# 使用简单的准则判断轮廓是否是环状的
|
| 18 |
+
if perimeter == 0:
|
| 19 |
+
return False
|
| 20 |
+
else:
|
| 21 |
+
compactness = 4 * np.pi * area / (perimeter ** 2)
|
| 22 |
+
return compactness<1 # 这个阈值可以调整
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def get_the_parts(img_dir):
|
| 26 |
+
|
| 27 |
+
img = cv2.imread(img_dir)
|
| 28 |
+
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
| 29 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 转换为灰度图像
|
| 30 |
+
_, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV) # 可能需要调整阈值
|
| 31 |
+
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) #整体的外轮廓
|
| 32 |
+
mask = np.zeros_like(img)
|
| 33 |
+
for contour in contours:
|
| 34 |
+
cv2.drawContours(mask, [contour], -1, (255, 255, 255), thickness=cv2.FILLED)
|
| 35 |
+
result = cv2.bitwise_and(img, mask)
|
| 36 |
+
# 将BGR图像转换为RGB图像
|
| 37 |
+
result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
|
| 38 |
+
# 步骤2: 准备数据
|
| 39 |
+
pixels = result_rgb.reshape((-1, 3))
|
| 40 |
+
pixels = np.float32(pixels)
|
| 41 |
+
|
| 42 |
+
# 步骤3: 应用K-means聚类
|
| 43 |
+
k = 12 # 聚类的数量
|
| 44 |
+
kmeans = KMeans(n_clusters=k, random_state=0).fit(pixels)
|
| 45 |
+
# 获取每个聚类的颜色中心
|
| 46 |
+
colors = kmeans.cluster_centers_
|
| 47 |
+
|
| 48 |
+
# 步骤4: 显示聚类颜色结果
|
| 49 |
+
# 创建一个空白图片,用于显示聚类的颜色
|
| 50 |
+
colors_img = np.zeros((50, 50*k, 3), dtype=np.uint8)
|
| 51 |
+
for i, color in enumerate(colors):
|
| 52 |
+
colors_img[:, i*50:(i+1)*50] = color
|
| 53 |
+
cnt_list=np.zeros(len(colors))
|
| 54 |
+
all_C=[]
|
| 55 |
+
|
| 56 |
+
for i, color in enumerate(colors):
|
| 57 |
+
# 将RGB颜色转换为HSV颜色
|
| 58 |
+
color_hsv = cv2.cvtColor(np.uint8([[color]]), cv2.COLOR_RGB2HSV)[0][0]
|
| 59 |
+
lower_bound = color_hsv - np.array([15, 20, 20])
|
| 60 |
+
upper_bound = color_hsv + np.array([15, 20, 20])
|
| 61 |
+
mask = cv2.inRange(hsv, lower_bound, upper_bound)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# 轮廓检测
|
| 65 |
+
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
| 66 |
+
all_C.append(contours)
|
| 67 |
+
# print(contours)
|
| 68 |
+
# 检查是否有环状轮廓
|
| 69 |
+
for contour in contours:
|
| 70 |
+
if is_ring_contour(contour):
|
| 71 |
+
# 环状轮廓绘制到原图上
|
| 72 |
+
# cv2.drawContours(img, [contour], -1, (0, 255, 0), 2)
|
| 73 |
+
cnt_list[i]+=1
|
| 74 |
+
# print(f"环状轮廓在颜色 {i} 中找到。")
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
cnt_list_copy = cnt_list.copy()
|
| 79 |
+
|
| 80 |
+
# 将第一个元素和所有0值替换为np.max(cnt_list) + 1,确保这些值不会是最小值
|
| 81 |
+
cnt_list_copy[0] = np.max(cnt_list) + 1
|
| 82 |
+
cnt_list_copy[cnt_list_copy == 0] = np.max(cnt_list) + 1
|
| 83 |
+
min_value_non_zero_non_first = np.min(cnt_list_copy)
|
| 84 |
+
|
| 85 |
+
# 找到所有等于这个最小值的索引
|
| 86 |
+
min_indices_non_zero_non_first = np.where(np.abs(cnt_list_copy - min_value_non_zero_non_first) <= 3)[0]
|
| 87 |
+
# print(cnt_list)
|
| 88 |
+
# print(min_indices_non_zero_non_first)
|
| 89 |
+
mask_p = np.zeros_like(img)
|
| 90 |
+
output_list=[]
|
| 91 |
+
for this_c in min_indices_non_zero_non_first:
|
| 92 |
+
# this_c = np.argmin(cnt_list_copy)
|
| 93 |
+
|
| 94 |
+
# print(this_c)
|
| 95 |
+
|
| 96 |
+
for c in all_C[this_c]:
|
| 97 |
+
cv2.drawContours(mask_p, [c], -1, (255, 255, 255), thickness=cv2.FILLED)
|
| 98 |
+
result_p = cv2.bitwise_and(img, mask_p)
|
| 99 |
+
# 将BGR图像转换为RGB图像
|
| 100 |
+
result_p_rgb = cv2.cvtColor(result_p, cv2.COLOR_BGR2RGB)
|
| 101 |
+
# print(colors[this_c])
|
| 102 |
+
output_list.append(colors[this_c])
|
| 103 |
+
return output_list
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
# 读取HTML文件
|
| 107 |
+
|
| 108 |
+
def make_new_json(fn):
|
| 109 |
+
with open('lego/'+fn+'/'+fn+'.html', 'r', encoding='utf-8') as file:
|
| 110 |
+
html_content = file.read()
|
| 111 |
+
|
| 112 |
+
# 使用BeautifulSoup解析HTML
|
| 113 |
+
soup = BeautifulSoup(html_content, 'lxml')
|
| 114 |
+
rows = soup.find_all(class_='row')
|
| 115 |
+
# print(soup)
|
| 116 |
+
|
| 117 |
+
# 将提取的数据转换为JSON
|
| 118 |
+
data = []
|
| 119 |
+
snellius_dir='/gpfs/home4/hhuang/MiniGPT/MiniGPT-4/lego/'+fn+'/images/'
|
| 120 |
+
instruction_id = 0 # 初始化instruction_id
|
| 121 |
+
for row in rows:
|
| 122 |
+
# row_data = {'instruction_id': instruction_id}
|
| 123 |
+
row_data = {
|
| 124 |
+
'instruction_id': instruction_id,
|
| 125 |
+
|
| 126 |
+
'text':'None',
|
| 127 |
+
'VLM': {
|
| 128 |
+
'img_path':'/gpfs/home4/hhuang/MiniGPT/MiniGPT-4/lego/',
|
| 129 |
+
'id_sign':'None',
|
| 130 |
+
'step_num':'None',
|
| 131 |
+
'step_class':'None',
|
| 132 |
+
'other_sign':'None',
|
| 133 |
+
'bound_color':[],
|
| 134 |
+
'task_label': 'None',
|
| 135 |
+
'query': 'None',
|
| 136 |
+
'MiniGPTv2_output': 'None'
|
| 137 |
+
}
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
# 提取class="img"的元素
|
| 142 |
+
img = row.find(class_='image').find('img') # 假设class="img"内有<img>标签
|
| 143 |
+
row_data['img'] = img['src'].replace("./LEGO 60274 Elite Police Lighthouse Capture_files/","https://legoaudioinstructions.com/wp-content/themes/mtt-wordpress-theme/assets/manual/manual-images/60274/").replace("#", "%23").replace(" ", "%20")if img and img.has_attr('src') else 'No image found'
|
| 144 |
+
# print(row_data['img'])
|
| 145 |
+
|
| 146 |
+
img_name=img['src'].split('/')[-1].replace("#", "%23").replace(" ", "%20").replace("./LEGO 60274 Elite Police Lighthouse Capture_files/https://legoaudioinstructions.com/wp-content/themes/mtt-wordpress-theme/assets/manual/manual-images/60274/", "%20")
|
| 147 |
+
print(img_name)
|
| 148 |
+
detect_list=img_name.split('-')[1:]
|
| 149 |
+
|
| 150 |
+
row_data['VLM']['id_sign']=detect_list[0].split('.png')[0]
|
| 151 |
+
if len(detect_list)==1:
|
| 152 |
+
row_data['VLM']['id_sign']='None'
|
| 153 |
+
else:
|
| 154 |
+
more_list=detect_list[1].split('_')
|
| 155 |
+
row_data['VLM']['step_num']=more_list[0]
|
| 156 |
+
row_data['VLM']['step_class']=more_list[1]
|
| 157 |
+
row_data['VLM']['other_sign']=detect_list[-1].split('%23')[-1][:-4]
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
# print('lego/'+fn+'/images/'+img['src'].split('/')[-1].replace("#", "%23").replace(" ", "%20").replace("./LEGO 60274 Elite Police Lighthouse Capture_files/https://legoaudioinstructions.com/wp-content/themes/mtt-wordpress-theme/assets/manual/manual-images/60274/", "%20")
|
| 164 |
+
# )
|
| 165 |
+
if row_data['img'] != 'No image found':
|
| 166 |
+
row_data['VLM']['img_path']= snellius_dir+img['src'].split('/')[-1].replace("#", "%23").replace(" ", "%20").replace("./LEGO 60274 Elite Police Lighthouse Capture_files/https://legoaudioinstructions.com/wp-content/themes/mtt-wordpress-theme/assets/manual/manual-images/60274/", "%20")
|
| 167 |
+
img_dir='lego/'+fn+'/images/'+img['src'].split('/')[-1].replace("#", "%23").replace(" ", "%20").replace("./LEGO 60274 Elite Police Lighthouse Capture_files/https://legoaudioinstructions.com/wp-content/themes/mtt-wordpress-theme/assets/manual/manual-images/60274/", "%20")
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
if row_data['VLM']['step_class']=="step":
|
| 171 |
+
row_data['VLM']['bound_color']=get_the_parts(img_dir)
|
| 172 |
+
print(row_data['VLM']['bound_color'])
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
text = row.find(class_='text')
|
| 176 |
+
# print(text.find_all(class_='txtbox'))
|
| 177 |
+
if text:
|
| 178 |
+
# 处理所有class="txtbox"的子元素
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
txtboxes = text.find_all(class_='txtbox')
|
| 186 |
+
row_data['text'] = [txtbox.text.strip() for txtbox in txtboxes]
|
| 187 |
+
row_data['entities'] = []
|
| 188 |
+
query_texts = []
|
| 189 |
+
for txtbox in txtboxes:
|
| 190 |
+
txtbox_data = [{child['class'][0] if child.has_attr('class') else 'this_line': child.text.strip()} for child in txtbox.find_all()]
|
| 191 |
+
row_data['entities'].append(txtbox_data)
|
| 192 |
+
query_texts.extend([child.text.strip() for child in txtbox.find_all() if child.has_attr('class') and child['class'][0] != 'verb'])
|
| 193 |
+
|
| 194 |
+
# 检查是否需要更改VLM的task_label
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
# 检查是否需要更改VLM的task_label
|
| 198 |
+
for line in row_data['entities']:
|
| 199 |
+
if len(line) > 1:
|
| 200 |
+
if 'verb' in line[1]:
|
| 201 |
+
if line[1]['verb'] == 'Collect':
|
| 202 |
+
row_data['VLM']['task_label'] = '[detection-collect]'
|
| 203 |
+
row_data['VLM']['query'] = 'Collect '+' '.join(query_texts)
|
| 204 |
+
break
|
| 205 |
+
elif line[1]['verb'] == 'Find':
|
| 206 |
+
row_data['VLM']['task_label'] = '[detection]'
|
| 207 |
+
# print(line[0]['this_line'])
|
| 208 |
+
row_data['VLM']['query'] = ''.join(line[0]['this_line'])
|
| 209 |
+
break
|
| 210 |
+
else:
|
| 211 |
+
row_data['text'] = 'No text found'
|
| 212 |
+
|
| 213 |
+
# 添加到数据列表中
|
| 214 |
+
data.append(row_data)
|
| 215 |
+
|
| 216 |
+
# 为下一个row元素增加instruction_id
|
| 217 |
+
instruction_id += 1
|
| 218 |
+
whole_json={
|
| 219 |
+
"manual_id":fn,
|
| 220 |
+
"manual_type":"lego",
|
| 221 |
+
"instructions":data
|
| 222 |
+
}
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
json_data = json.dumps(whole_json, indent=4, ensure_ascii=False)
|
| 226 |
+
|
| 227 |
+
# 输出或保存JSON数据
|
| 228 |
+
# print(json_data)
|
| 229 |
+
# 或者写入文件
|
| 230 |
+
# json_name
|
| 231 |
+
with open('new_json_color/'+fn+'_new_color.json', 'w', encoding='utf-8') as json_file:
|
| 232 |
+
json_file.write(json_data)
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
import wandb
|
| 237 |
+
import random
|
| 238 |
+
|
| 239 |
+
# start a new wandb run to track this script
|
| 240 |
+
wandb.init(
|
| 241 |
+
# set the wandb project where this run will be logged
|
| 242 |
+
project="my-awesome-project",
|
| 243 |
+
|
| 244 |
+
# track hyperparameters and run metadata
|
| 245 |
+
config={
|
| 246 |
+
"learning_rate": 0.02,
|
| 247 |
+
"architecture": "CNN",
|
| 248 |
+
"dataset": "CIFAR-100",
|
| 249 |
+
"epochs": 10,
|
| 250 |
+
}
|
| 251 |
+
)
|
| 252 |
+
|
| 253 |
+
# simulate training
|
| 254 |
+
epochs = 10
|
| 255 |
+
offset = random.random() / 5
|
| 256 |
+
for epoch in range(2, epochs):
|
| 257 |
+
acc = 1 - 2 ** -epoch - random.random() / epoch - offset
|
| 258 |
+
loss = 2 ** -epoch + random.random() / epoch + offset
|
| 259 |
+
|
| 260 |
+
# log metrics to wandb
|
| 261 |
+
wandb.log({"acc": acc, "loss": loss})
|
| 262 |
+
|
| 263 |
+
# [optional] finish the wandb run, necessary in notebooks
|
| 264 |
+
wandb.finish()
|