Add files using upload-large-folder tool
Browse files- annotator/__pycache__/utils.cpython-312.pyc +0 -0
- annotator/annotate.py +237 -0
- annotator/data/extended_data_summary_v2.jsonl +0 -0
- annotator/templates/index.html +255 -0
- annotator/test.ipynb +130 -0
- annotator/utils.py +13 -0
- images_divided/0005/1_0.png +3 -0
- images_divided/0005/1_4.png +3 -0
- images_divided/0005/2_0.png +3 -0
- images_divided/0005/2_3.png +3 -0
- images_divided/0005/3_3.png +3 -0
- images_divided/0006/1_1.png +3 -0
- images_divided/0006/1_2.png +3 -0
- images_divided/0006/2_0.png +3 -0
- images_divided/0006/2_1.png +3 -0
- images_divided/0006/2_2.png +3 -0
- images_divided/0006/3_0.png +3 -0
- images_divided/0006/3_1.png +3 -0
- images_divided/0006/3_2.png +3 -0
- images_divided/0007/0_0.png +3 -0
- images_divided/0007/0_1.png +3 -0
- images_divided/0007/0_2.png +3 -0
- images_divided/0007/0_3.png +3 -0
- images_divided/0007/0_4.png +3 -0
- images_divided/0007/1_0.png +3 -0
- images_divided/0007/1_1.png +3 -0
- images_divided/0007/1_2.png +3 -0
- images_divided/0007/1_3.png +3 -0
- images_divided/0007/2_0.png +3 -0
- images_divided/0007/2_1.png +3 -0
- images_divided/0007/2_2.png +3 -0
- images_divided/0007/2_3.png +3 -0
- images_divided/0007/2_4.png +3 -0
- images_divided/0007/3_0.png +3 -0
- images_divided/0007/3_1.png +3 -0
- images_divided/0007/3_3.png +3 -0
- images_divided/0007/3_4.png +3 -0
- images_divided/0008/0_0.png +3 -0
- images_divided/0008/0_1.png +3 -0
- images_divided/0008/0_3.png +3 -0
- images_divided/0008/1_0.png +3 -0
- images_divided/0008/1_2.png +3 -0
- images_divided/0008/2_1.png +3 -0
- images_divided/0008/2_3.png +3 -0
- images_divided/0008/3_0.png +3 -0
- images_divided/0008/3_1.png +3 -0
- images_divided/0008/3_3.png +3 -0
- images_divided/0009/0_0.png +3 -0
- images_divided/0009/0_1.png +3 -0
- images_divided/0009/2_0.png +3 -0
annotator/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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
from utils import divide_prompt
|
| 5 |
+
from flask import Flask, render_template, request, send_from_directory, jsonify
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# 设置图像目录路径
|
| 10 |
+
IMAGE_DIR = "../images_divided"
|
| 11 |
+
ITEMS_PER_PAGE = 1 # 每页显示的 data 项目数
|
| 12 |
+
DATA_FILE = "data/annotation.jsonl" # 保存排序结果的文件
|
| 13 |
+
PROMPT_DATA_FILE = 'data/extended_data_summary_v2.jsonl'
|
| 14 |
+
|
| 15 |
+
with open(PROMPT_DATA_FILE, 'r', encoding='utf-8') as f:
|
| 16 |
+
PROMPT_DATA = [json.loads(line) for line in f if line.strip()]
|
| 17 |
+
PROMPT_DATA = {item['idx']: item for item in PROMPT_DATA}
|
| 18 |
+
|
| 19 |
+
@app.route('/')
|
| 20 |
+
def index():
|
| 21 |
+
# 读取所有文件夹
|
| 22 |
+
folders = sorted([
|
| 23 |
+
f for f in os.listdir(IMAGE_DIR)
|
| 24 |
+
if os.path.isdir(os.path.join(IMAGE_DIR, f)) and not f.startswith('.')
|
| 25 |
+
])
|
| 26 |
+
data = []
|
| 27 |
+
|
| 28 |
+
# 加载已有的标注记录
|
| 29 |
+
existing_records = load_existing_records()
|
| 30 |
+
existing_dict = {}
|
| 31 |
+
for record in existing_records:
|
| 32 |
+
folder = record.get('folder')
|
| 33 |
+
ref_image = record.get('ref_image')
|
| 34 |
+
compare_images = tuple(sorted(record.get('compare_images', []))) # 使用排序后的元组作为键的一部分
|
| 35 |
+
key = (folder, ref_image, compare_images)
|
| 36 |
+
existing_dict[key] = {
|
| 37 |
+
'compare_images': record.get('compare_images', []),
|
| 38 |
+
'rank_images': record.get('rank_images', [])
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
for cnt, idx in enumerate(folders):
|
| 42 |
+
folder_path = os.path.join(IMAGE_DIR, idx)
|
| 43 |
+
images = sorted([img for img in os.listdir(folder_path) if img.endswith('.png')])
|
| 44 |
+
first_indices, second_indices = zip(*[
|
| 45 |
+
list(map(int, img.split('.')[0].split('_')))
|
| 46 |
+
for img in images
|
| 47 |
+
])
|
| 48 |
+
first_indices = sorted(set(first_indices))
|
| 49 |
+
second_indices = sorted(set(second_indices))
|
| 50 |
+
|
| 51 |
+
for i in first_indices:
|
| 52 |
+
for j in second_indices:
|
| 53 |
+
ref_img = f"{i}_{j}.png"
|
| 54 |
+
candidates = [
|
| 55 |
+
[
|
| 56 |
+
f"{x}_{y}.png"
|
| 57 |
+
for x in first_indices
|
| 58 |
+
]
|
| 59 |
+
for y in second_indices if y != j
|
| 60 |
+
]
|
| 61 |
+
|
| 62 |
+
items = [
|
| 63 |
+
{
|
| 64 |
+
'ref_image': f"/images/{idx}/{ref_img}",
|
| 65 |
+
'compare_images': [f"/images/{idx}/{cand_img}" for cand_img in cand], # 待比较的图像列表
|
| 66 |
+
'folder': idx,
|
| 67 |
+
'ref_index': (i, j),
|
| 68 |
+
'candidate_column': y,
|
| 69 |
+
'prompt': divide_prompt(PROMPT_DATA[idx]['prompt'])[0] if idx in PROMPT_DATA and 'prompt' in PROMPT_DATA[idx] else '',
|
| 70 |
+
'existing_compare_images': [], # 临时占位,后面会更新
|
| 71 |
+
'existing_rank_images': [], # 临时占位,后面会更新
|
| 72 |
+
'is_annotated': False, # 临时占位,后面会更新
|
| 73 |
+
} for y, cand in zip([y for y in second_indices if y != j], candidates)
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
# 对每个 item 检查是否已有标注
|
| 77 |
+
for item in items:
|
| 78 |
+
# 从完整路径中提取文件名
|
| 79 |
+
current_compare_images = [img.split('/')[-1] for img in item['compare_images']]
|
| 80 |
+
|
| 81 |
+
# 检查是否已有标注(使用 ref_image 和 compare_images 一起判定)
|
| 82 |
+
ref_filename = item['ref_image'].split('/')[-1]
|
| 83 |
+
lookup_key = (idx, ref_filename, tuple(sorted(current_compare_images)))
|
| 84 |
+
existing_data = existing_dict.get(lookup_key, {})
|
| 85 |
+
|
| 86 |
+
# 更新 item 的标注信息
|
| 87 |
+
item['existing_compare_images'] = existing_data.get('compare_images', [])
|
| 88 |
+
item['existing_rank_images'] = existing_data.get('rank_images', [])
|
| 89 |
+
item['is_annotated'] = len(existing_data.get('rank_images', [])) > 0
|
| 90 |
+
|
| 91 |
+
data.extend(items)
|
| 92 |
+
|
| 93 |
+
# 分页逻辑
|
| 94 |
+
page = int(request.args.get('page', 1))
|
| 95 |
+
start = (page - 1) * ITEMS_PER_PAGE
|
| 96 |
+
end = start + ITEMS_PER_PAGE
|
| 97 |
+
paginated_data = data[start:end]
|
| 98 |
+
|
| 99 |
+
return render_template(
|
| 100 |
+
'index.html',
|
| 101 |
+
data=paginated_data,
|
| 102 |
+
page=page,
|
| 103 |
+
total_pages=(len(data) + ITEMS_PER_PAGE - 1) // ITEMS_PER_PAGE
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
@app.route('/sort', methods=['POST'])
|
| 107 |
+
def sort_images():
|
| 108 |
+
try:
|
| 109 |
+
# 获取排序后的数据
|
| 110 |
+
sorted_images = request.form.getlist('sorted_images')
|
| 111 |
+
|
| 112 |
+
if not sorted_images:
|
| 113 |
+
return jsonify({'status': 'error', 'message': '没有排序数据'}), 400
|
| 114 |
+
|
| 115 |
+
# 解析排序数据
|
| 116 |
+
ranking = []
|
| 117 |
+
for item in sorted_images:
|
| 118 |
+
rank, image_path = item.split(':', 1)
|
| 119 |
+
ranking.append({
|
| 120 |
+
'rank': int(rank),
|
| 121 |
+
'image_path': image_path
|
| 122 |
+
})
|
| 123 |
+
|
| 124 |
+
# 排序以确保顺序正确
|
| 125 |
+
ranking.sort(key=lambda x: x['rank'])
|
| 126 |
+
|
| 127 |
+
# 提取有用信息
|
| 128 |
+
if ranking:
|
| 129 |
+
first_image = ranking[0]['image_path']
|
| 130 |
+
parts = first_image.split('/')
|
| 131 |
+
folder = parts[2] # folder_name
|
| 132 |
+
|
| 133 |
+
# 获取参考图像信息(从表单中)
|
| 134 |
+
ref_image = request.form.get('ref_image', '')
|
| 135 |
+
|
| 136 |
+
# 解析参考图像文件名
|
| 137 |
+
ref_filename = ref_image.split('/')[-1] # x_y.png
|
| 138 |
+
|
| 139 |
+
# 获取原始的 compare_images(所有候选图像的原始顺序)
|
| 140 |
+
all_compare_images = request.form.get('all_compare_images', '')
|
| 141 |
+
if all_compare_images:
|
| 142 |
+
all_compare_images = json.loads(all_compare_images)
|
| 143 |
+
else:
|
| 144 |
+
all_compare_images = []
|
| 145 |
+
|
| 146 |
+
# 解析排序图像的文件名 - 这是用户的排序结果
|
| 147 |
+
ranked_filenames = []
|
| 148 |
+
for item in ranking:
|
| 149 |
+
filename = item['image_path'].split('/')[-1]
|
| 150 |
+
ranked_filenames.append(filename)
|
| 151 |
+
|
| 152 |
+
# 创建唯一的数据记录
|
| 153 |
+
record = {
|
| 154 |
+
'folder': folder,
|
| 155 |
+
'ref_image': ref_filename,
|
| 156 |
+
'compare_images': all_compare_images, # 原始顺序的所有候选图像
|
| 157 |
+
'rank_images': ranked_filenames, # 用户的排序结果
|
| 158 |
+
'timestamp': datetime.now().isoformat()
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
# 读取现有数据以检查唯一性
|
| 162 |
+
existing_records = load_existing_records()
|
| 163 |
+
|
| 164 |
+
# 检查是否已存在相同的记录(基于 folder, ref_image 和 compare_images)
|
| 165 |
+
updated = False
|
| 166 |
+
for i, existing in enumerate(existing_records):
|
| 167 |
+
existing_compare_sorted = tuple(sorted(existing.get('compare_images', [])))
|
| 168 |
+
current_compare_sorted = tuple(sorted(all_compare_images))
|
| 169 |
+
|
| 170 |
+
if (existing.get('folder') == folder and
|
| 171 |
+
existing.get('ref_image') == ref_filename and
|
| 172 |
+
existing_compare_sorted == current_compare_sorted):
|
| 173 |
+
# 更新现有记录
|
| 174 |
+
existing_records[i] = record
|
| 175 |
+
updated = True
|
| 176 |
+
break
|
| 177 |
+
|
| 178 |
+
if not updated:
|
| 179 |
+
# 添加新记录
|
| 180 |
+
existing_records.append(record)
|
| 181 |
+
|
| 182 |
+
# 保存到文件
|
| 183 |
+
save_records(existing_records)
|
| 184 |
+
|
| 185 |
+
print(f"{'更新' if updated else '添加'}排序记录:", record)
|
| 186 |
+
|
| 187 |
+
return jsonify({
|
| 188 |
+
'status': 'success',
|
| 189 |
+
'message': f"排序已{'更新' if updated else '保存'}!",
|
| 190 |
+
'record': record
|
| 191 |
+
})
|
| 192 |
+
|
| 193 |
+
except Exception as e:
|
| 194 |
+
print(f"错误: {str(e)}")
|
| 195 |
+
import traceback
|
| 196 |
+
traceback.print_exc()
|
| 197 |
+
return jsonify({'status': 'error', 'message': str(e)}), 500
|
| 198 |
+
|
| 199 |
+
def load_existing_records():
|
| 200 |
+
"""加载现有的记录"""
|
| 201 |
+
if not os.path.exists(DATA_FILE):
|
| 202 |
+
return []
|
| 203 |
+
|
| 204 |
+
records = []
|
| 205 |
+
with open(DATA_FILE, 'r', encoding='utf-8') as f:
|
| 206 |
+
for line in f:
|
| 207 |
+
line = line.strip()
|
| 208 |
+
if line:
|
| 209 |
+
records.append(json.loads(line))
|
| 210 |
+
return records
|
| 211 |
+
|
| 212 |
+
def save_records(records):
|
| 213 |
+
"""保存记录到JSONL文件"""
|
| 214 |
+
# 确保目录存在
|
| 215 |
+
os.makedirs(os.path.dirname(DATA_FILE), exist_ok=True)
|
| 216 |
+
|
| 217 |
+
with open(DATA_FILE, 'w', encoding='utf-8') as f:
|
| 218 |
+
for record in records:
|
| 219 |
+
f.write(json.dumps(record, ensure_ascii=False) + '\n')
|
| 220 |
+
|
| 221 |
+
# 添加一个路由来提供图片文件
|
| 222 |
+
@app.route('/images/<path:filename>')
|
| 223 |
+
def serve_images(filename):
|
| 224 |
+
return send_from_directory(IMAGE_DIR, filename)
|
| 225 |
+
|
| 226 |
+
# 添加一个查看已保存数据的路由
|
| 227 |
+
@app.route('/view_data')
|
| 228 |
+
def view_data():
|
| 229 |
+
"""查看已保存的排序数据"""
|
| 230 |
+
records = load_existing_records()
|
| 231 |
+
return jsonify({
|
| 232 |
+
'total': len(records),
|
| 233 |
+
'records': records
|
| 234 |
+
})
|
| 235 |
+
|
| 236 |
+
if __name__ == '__main__':
|
| 237 |
+
app.run(debug=True)
|
annotator/data/extended_data_summary_v2.jsonl
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
annotator/templates/index.html
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Image Ranking</title>
|
| 7 |
+
<style>
|
| 8 |
+
body {
|
| 9 |
+
font-family: Arial, sans-serif;
|
| 10 |
+
padding: 20px;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
.rank-btn {
|
| 14 |
+
padding: 5px 15px;
|
| 15 |
+
cursor: pointer;
|
| 16 |
+
border: 2px solid #4CAF50;
|
| 17 |
+
background-color: white;
|
| 18 |
+
color: #4CAF50;
|
| 19 |
+
border-radius: 4px;
|
| 20 |
+
font-weight: bold;
|
| 21 |
+
min-width: 60px;
|
| 22 |
+
}
|
| 23 |
+
.rank-btn.ranked {
|
| 24 |
+
background-color: #4CAF50;
|
| 25 |
+
color: white;
|
| 26 |
+
border-color: #4CAF50;
|
| 27 |
+
}
|
| 28 |
+
.rank-btn:hover {
|
| 29 |
+
opacity: 0.8;
|
| 30 |
+
}
|
| 31 |
+
.submit-btn {
|
| 32 |
+
margin-top: 20px;
|
| 33 |
+
padding: 10px 30px;
|
| 34 |
+
background-color: #2196F3;
|
| 35 |
+
color: white;
|
| 36 |
+
border: none;
|
| 37 |
+
border-radius: 4px;
|
| 38 |
+
cursor: pointer;
|
| 39 |
+
font-size: 16px;
|
| 40 |
+
}
|
| 41 |
+
.submit-btn:hover {
|
| 42 |
+
background-color: #0b7dda;
|
| 43 |
+
}
|
| 44 |
+
.submit-btn:disabled {
|
| 45 |
+
background-color: #cccccc;
|
| 46 |
+
cursor: not-allowed;
|
| 47 |
+
}
|
| 48 |
+
.show-annotation-btn {
|
| 49 |
+
margin-top: 10px;
|
| 50 |
+
padding: 8px 20px;
|
| 51 |
+
background-color: #FF9800;
|
| 52 |
+
color: white;
|
| 53 |
+
border: none;
|
| 54 |
+
border-radius: 4px;
|
| 55 |
+
cursor: pointer;
|
| 56 |
+
font-size: 14px;
|
| 57 |
+
}
|
| 58 |
+
.show-annotation-btn:hover {
|
| 59 |
+
background-color: #F57C00;
|
| 60 |
+
}
|
| 61 |
+
.annotation-status {
|
| 62 |
+
display: inline-block;
|
| 63 |
+
margin-left: 15px;
|
| 64 |
+
padding: 5px 12px;
|
| 65 |
+
background-color: #4CAF50;
|
| 66 |
+
color: white;
|
| 67 |
+
border-radius: 4px;
|
| 68 |
+
font-size: 14px;
|
| 69 |
+
font-weight: bold;
|
| 70 |
+
}
|
| 71 |
+
/* 消息提示样式 */
|
| 72 |
+
.message-box {
|
| 73 |
+
position: fixed;
|
| 74 |
+
top: 20px;
|
| 75 |
+
right: 20px;
|
| 76 |
+
padding: 15px 25px;
|
| 77 |
+
border-radius: 4px;
|
| 78 |
+
font-size: 16px;
|
| 79 |
+
font-weight: bold;
|
| 80 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 81 |
+
z-index: 1000;
|
| 82 |
+
display: none;
|
| 83 |
+
animation: slideIn 0.3s ease-out;
|
| 84 |
+
}
|
| 85 |
+
.message-box.success {
|
| 86 |
+
background-color: #4CAF50;
|
| 87 |
+
color: white;
|
| 88 |
+
}
|
| 89 |
+
.message-box.error {
|
| 90 |
+
background-color: #f44336;
|
| 91 |
+
color: white;
|
| 92 |
+
}
|
| 93 |
+
.message-box.show {
|
| 94 |
+
display: block;
|
| 95 |
+
}
|
| 96 |
+
@keyframes slideIn {
|
| 97 |
+
from {
|
| 98 |
+
transform: translateX(100%);
|
| 99 |
+
opacity: 0;
|
| 100 |
+
}
|
| 101 |
+
to {
|
| 102 |
+
transform: translateX(0);
|
| 103 |
+
opacity: 1;
|
| 104 |
+
}
|
| 105 |
+
}
|
| 106 |
+
@keyframes slideOut {
|
| 107 |
+
from {
|
| 108 |
+
transform: translateX(0);
|
| 109 |
+
opacity: 1;
|
| 110 |
+
}
|
| 111 |
+
to {
|
| 112 |
+
transform: translateX(100%);
|
| 113 |
+
opacity: 0;
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
.message-box.hiding {
|
| 117 |
+
animation: slideOut 0.3s ease-out;
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
/* 固定在右上角的分页条 */
|
| 121 |
+
.page-control {
|
| 122 |
+
position: fixed;
|
| 123 |
+
top: 20px;
|
| 124 |
+
right: 20px;
|
| 125 |
+
background: rgba(255,255,255,0.9);
|
| 126 |
+
border: 1px solid #ddd;
|
| 127 |
+
padding: 8px 12px;
|
| 128 |
+
border-radius: 6px;
|
| 129 |
+
box-shadow: 0 2px 6px rgba(0,0,0,0.1);
|
| 130 |
+
z-index: 999;
|
| 131 |
+
display: flex;
|
| 132 |
+
align-items: center;
|
| 133 |
+
gap: 8px;
|
| 134 |
+
font-size: 14px;
|
| 135 |
+
}
|
| 136 |
+
.page-control input {
|
| 137 |
+
width: 60px;
|
| 138 |
+
padding: 3px 5px;
|
| 139 |
+
border: 1px solid #ccc;
|
| 140 |
+
border-radius: 4px;
|
| 141 |
+
}
|
| 142 |
+
.page-control button {
|
| 143 |
+
padding: 4px 8px;
|
| 144 |
+
background-color: #2196F3;
|
| 145 |
+
color: white;
|
| 146 |
+
border: none;
|
| 147 |
+
border-radius: 4px;
|
| 148 |
+
cursor: pointer;
|
| 149 |
+
}
|
| 150 |
+
.page-control button:hover {
|
| 151 |
+
background-color: #0b7dda;
|
| 152 |
+
}
|
| 153 |
+
</style>
|
| 154 |
+
<script>
|
| 155 |
+
let rankCounter = 1;
|
| 156 |
+
const rankedImages = new Map();
|
| 157 |
+
let allCompareImages = [];
|
| 158 |
+
|
| 159 |
+
function initializeCompareImages() {
|
| 160 |
+
allCompareImages = [];
|
| 161 |
+
const rankButtons = document.querySelectorAll('.rank-btn');
|
| 162 |
+
rankButtons.forEach(btn => {
|
| 163 |
+
const imagePath = btn.getAttribute('data-image');
|
| 164 |
+
const filename = imagePath.split('/').pop();
|
| 165 |
+
if (!allCompareImages.includes(filename)) {
|
| 166 |
+
allCompareImages.push(filename);
|
| 167 |
+
}
|
| 168 |
+
});
|
| 169 |
+
}
|
| 170 |
+
window.addEventListener('DOMContentLoaded', function() {
|
| 171 |
+
initializeCompareImages();
|
| 172 |
+
});
|
| 173 |
+
|
| 174 |
+
/* 这里省略 rankImage、loadExistingAnnotation、showMessage、submitRanking 的原有实现 */
|
| 175 |
+
/* ... 你的原始脚本保持不变 ... */
|
| 176 |
+
|
| 177 |
+
// 新增跳转函数
|
| 178 |
+
function jumpToPage(event) {
|
| 179 |
+
event.preventDefault();
|
| 180 |
+
const input = document.getElementById('pageInput');
|
| 181 |
+
let page = parseInt(input.value);
|
| 182 |
+
if (isNaN(page) || page < 1) page = 1;
|
| 183 |
+
if (page > {{ total_pages }}) page = {{ total_pages }};
|
| 184 |
+
window.location.href = "/?page=" + page;
|
| 185 |
+
return false;
|
| 186 |
+
}
|
| 187 |
+
</script>
|
| 188 |
+
</head>
|
| 189 |
+
<body>
|
| 190 |
+
<!-- 消息提示框 -->
|
| 191 |
+
<div id="messageBox" class="message-box"></div>
|
| 192 |
+
|
| 193 |
+
<!-- 固定在右上角的分页控制条 -->
|
| 194 |
+
<div class="page-control">
|
| 195 |
+
<span>{{ page }}/{{ total_pages }}</span>
|
| 196 |
+
<form id="pageJumpForm" onsubmit="return jumpToPage(event)" style="display:inline;">
|
| 197 |
+
<input type="number" id="pageInput" min="1" max="{{ total_pages }}" value="{{ page }}">
|
| 198 |
+
<button type="submit">跳转</button>
|
| 199 |
+
</form>
|
| 200 |
+
</div>
|
| 201 |
+
|
| 202 |
+
<h1>Image Ranking</h1>
|
| 203 |
+
<form id="rankingForm" action="/sort" method="POST" onsubmit="return submitRanking(event)">
|
| 204 |
+
{% for item in data %}
|
| 205 |
+
<div>
|
| 206 |
+
{% if item.prompt %}
|
| 207 |
+
<div style="background-color: #f0f0f0; padding: 15px; margin-bottom: 20px; border-radius: 4px; border-left: 4px solid #2196F3;">
|
| 208 |
+
<h2 style="margin-top: 0; color: #333;">Prompt:</h2>
|
| 209 |
+
<p style="font-size: 16px; line-height: 1.6; margin-bottom: 0; color: #555;">{{ item.prompt }}</p>
|
| 210 |
+
</div>
|
| 211 |
+
{% endif %}
|
| 212 |
+
<h3>Reference Image: {{ item.ref_image }}
|
| 213 |
+
{% if item.is_annotated %}
|
| 214 |
+
<span class="annotation-status">已标注</span>
|
| 215 |
+
{% endif %}
|
| 216 |
+
</h3>
|
| 217 |
+
<p>Folder: {{ item.folder }}</p>
|
| 218 |
+
<img src="{{ item.ref_image }}" alt="ref_image" width="200">
|
| 219 |
+
<input type="hidden" name="ref_image" value="{{ item.ref_image }}">
|
| 220 |
+
{% if item.is_annotated %}
|
| 221 |
+
<button type="button" class="show-annotation-btn"
|
| 222 |
+
data-loaded="false"
|
| 223 |
+
onclick='loadExistingAnnotation(this, "{{ item.folder }}", "{{ item.ref_image.split("/")[-1] }}", {{ item.existing_rank_images|tojson|safe }})'>
|
| 224 |
+
显示已有标注
|
| 225 |
+
</button>
|
| 226 |
+
{% endif %}
|
| 227 |
+
<h4>Compare Images:</h4>
|
| 228 |
+
<div style="display: flex; gap: 15px; flex-wrap: wrap; align-items: flex-start;">
|
| 229 |
+
{% for compare_image in item.compare_images %}
|
| 230 |
+
<div class="image-item" style="display: flex; flex-direction: column; align-items: center; gap: 5px;">
|
| 231 |
+
<img src="{{ compare_image }}" alt="compare_image" width="100">
|
| 232 |
+
<button type="button"
|
| 233 |
+
class="rank-btn"
|
| 234 |
+
data-image="{{ compare_image }}"
|
| 235 |
+
onclick="rankImage(this, '{{ compare_image }}')">
|
| 236 |
+
排序
|
| 237 |
+
</button>
|
| 238 |
+
</div>
|
| 239 |
+
{% endfor %}
|
| 240 |
+
</div>
|
| 241 |
+
</div>
|
| 242 |
+
{% endfor %}
|
| 243 |
+
<button type="submit" class="submit-btn">提交排序</button>
|
| 244 |
+
</form>
|
| 245 |
+
|
| 246 |
+
<div style="margin-top: 20px;">
|
| 247 |
+
{% if page > 1 %}
|
| 248 |
+
<a href="/?page={{ page - 1 }}" style="margin-right: 10px;">上一页</a>
|
| 249 |
+
{% endif %}
|
| 250 |
+
{% if page < total_pages %}
|
| 251 |
+
<a href="/?page={{ page + 1 }}">下一页</a>
|
| 252 |
+
{% endif %}
|
| 253 |
+
</div>
|
| 254 |
+
</body>
|
| 255 |
+
</html>
|
annotator/test.ipynb
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 16,
|
| 6 |
+
"id": "5bdfe87c",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"import os\n",
|
| 11 |
+
"import json\n",
|
| 12 |
+
"from itertools import combinations\n",
|
| 13 |
+
"IMAGE_DIR = \"/root/siton-tmp/images_divided\""
|
| 14 |
+
]
|
| 15 |
+
},
|
| 16 |
+
{
|
| 17 |
+
"cell_type": "code",
|
| 18 |
+
"execution_count": null,
|
| 19 |
+
"id": "fb8c9036",
|
| 20 |
+
"metadata": {},
|
| 21 |
+
"outputs": [],
|
| 22 |
+
"source": [
|
| 23 |
+
"folders = sorted([\n",
|
| 24 |
+
" f for f in os.listdir(IMAGE_DIR)\n",
|
| 25 |
+
" if os.path.isdir(os.path.join(IMAGE_DIR, f)) and not f.startswith('.')\n",
|
| 26 |
+
"])\n",
|
| 27 |
+
"data = []\n",
|
| 28 |
+
"for cnt, idx in enumerate(folders):\n",
|
| 29 |
+
" folder_path = os.path.join(IMAGE_DIR, idx)\n",
|
| 30 |
+
" images = sorted([img for img in os.listdir(folder_path) if img.endswith('.png')])\n",
|
| 31 |
+
" first_indices, second_indices = zip(*[\n",
|
| 32 |
+
" list(map(int, img.split('.')[0].split('_')))\n",
|
| 33 |
+
" for img in images\n",
|
| 34 |
+
" ])\n",
|
| 35 |
+
" first_indices = sorted(set(first_indices))\n",
|
| 36 |
+
" second_indices = sorted(set(second_indices))\n",
|
| 37 |
+
" \n",
|
| 38 |
+
" for i in first_indices:\n",
|
| 39 |
+
" for j in second_indices:\n",
|
| 40 |
+
" ref_img = f\"{i}_{j}.png\"\n",
|
| 41 |
+
" candidates = [\n",
|
| 42 |
+
" [\n",
|
| 43 |
+
" f\"{x}_{y}.png\"\n",
|
| 44 |
+
" for x in first_indices\n",
|
| 45 |
+
" ]\n",
|
| 46 |
+
" for y in second_indices if y != j\n",
|
| 47 |
+
" ]\n",
|
| 48 |
+
" items = [\n",
|
| 49 |
+
" {\n",
|
| 50 |
+
" 'ref_image': ref_img,\n",
|
| 51 |
+
" 'rank_images': cand,\n",
|
| 52 |
+
" 'idx': idx,\n",
|
| 53 |
+
" } for cand in candidates\n",
|
| 54 |
+
" ]\n",
|
| 55 |
+
" data.extend(items)\n"
|
| 56 |
+
]
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
"cell_type": "code",
|
| 60 |
+
"execution_count": 19,
|
| 61 |
+
"id": "36e0603b",
|
| 62 |
+
"metadata": {},
|
| 63 |
+
"outputs": [
|
| 64 |
+
{
|
| 65 |
+
"data": {
|
| 66 |
+
"text/plain": [
|
| 67 |
+
"[{'ref_image': '0_0.png',\n",
|
| 68 |
+
" 'rank_images': ['0_1.png', '1_1.png', '2_1.png', '3_1.png'],\n",
|
| 69 |
+
" 'idx': '0000'},\n",
|
| 70 |
+
" {'ref_image': '0_0.png',\n",
|
| 71 |
+
" 'rank_images': ['0_2.png', '1_2.png', '2_2.png', '3_2.png'],\n",
|
| 72 |
+
" 'idx': '0000'},\n",
|
| 73 |
+
" {'ref_image': '0_1.png',\n",
|
| 74 |
+
" 'rank_images': ['0_0.png', '1_0.png', '2_0.png', '3_0.png'],\n",
|
| 75 |
+
" 'idx': '0000'},\n",
|
| 76 |
+
" {'ref_image': '0_1.png',\n",
|
| 77 |
+
" 'rank_images': ['0_2.png', '1_2.png', '2_2.png', '3_2.png'],\n",
|
| 78 |
+
" 'idx': '0000'},\n",
|
| 79 |
+
" {'ref_image': '0_2.png',\n",
|
| 80 |
+
" 'rank_images': ['0_0.png', '1_0.png', '2_0.png', '3_0.png'],\n",
|
| 81 |
+
" 'idx': '0000'},\n",
|
| 82 |
+
" {'ref_image': '0_2.png',\n",
|
| 83 |
+
" 'rank_images': ['0_1.png', '1_1.png', '2_1.png', '3_1.png'],\n",
|
| 84 |
+
" 'idx': '0000'},\n",
|
| 85 |
+
" {'ref_image': '1_0.png',\n",
|
| 86 |
+
" 'rank_images': ['0_1.png', '1_1.png', '2_1.png', '3_1.png'],\n",
|
| 87 |
+
" 'idx': '0000'},\n",
|
| 88 |
+
" {'ref_image': '1_0.png',\n",
|
| 89 |
+
" 'rank_images': ['0_2.png', '1_2.png', '2_2.png', '3_2.png'],\n",
|
| 90 |
+
" 'idx': '0000'},\n",
|
| 91 |
+
" {'ref_image': '1_1.png',\n",
|
| 92 |
+
" 'rank_images': ['0_0.png', '1_0.png', '2_0.png', '3_0.png'],\n",
|
| 93 |
+
" 'idx': '0000'},\n",
|
| 94 |
+
" {'ref_image': '1_1.png',\n",
|
| 95 |
+
" 'rank_images': ['0_2.png', '1_2.png', '2_2.png', '3_2.png'],\n",
|
| 96 |
+
" 'idx': '0000'}]"
|
| 97 |
+
]
|
| 98 |
+
},
|
| 99 |
+
"execution_count": 19,
|
| 100 |
+
"metadata": {},
|
| 101 |
+
"output_type": "execute_result"
|
| 102 |
+
}
|
| 103 |
+
],
|
| 104 |
+
"source": [
|
| 105 |
+
"data[:10]"
|
| 106 |
+
]
|
| 107 |
+
}
|
| 108 |
+
],
|
| 109 |
+
"metadata": {
|
| 110 |
+
"kernelspec": {
|
| 111 |
+
"display_name": "pytorch-env",
|
| 112 |
+
"language": "python",
|
| 113 |
+
"name": "python3"
|
| 114 |
+
},
|
| 115 |
+
"language_info": {
|
| 116 |
+
"codemirror_mode": {
|
| 117 |
+
"name": "ipython",
|
| 118 |
+
"version": 3
|
| 119 |
+
},
|
| 120 |
+
"file_extension": ".py",
|
| 121 |
+
"mimetype": "text/x-python",
|
| 122 |
+
"name": "python",
|
| 123 |
+
"nbconvert_exporter": "python",
|
| 124 |
+
"pygments_lexer": "ipython3",
|
| 125 |
+
"version": "3.12.11"
|
| 126 |
+
}
|
| 127 |
+
},
|
| 128 |
+
"nbformat": 4,
|
| 129 |
+
"nbformat_minor": 5
|
| 130 |
+
}
|
annotator/utils.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def divide_prompt(prompt):
|
| 5 |
+
# seqis like ". [TOP-LEFT]:"
|
| 6 |
+
match_sep = re.compile(r"\.\s+[A-Z0-9-\[\]]+:")
|
| 7 |
+
seps = match_sep.findall(prompt)
|
| 8 |
+
# Add '.' for each sentence
|
| 9 |
+
sub_prompts = [
|
| 10 |
+
p + '.' if p.strip()[-1] != '.' else p
|
| 11 |
+
for p in re.split('|'.join(map(re.escape, seps)), prompt)
|
| 12 |
+
]
|
| 13 |
+
return sub_prompts
|
images_divided/0005/1_0.png
ADDED
|
Git LFS Details
|
images_divided/0005/1_4.png
ADDED
|
Git LFS Details
|
images_divided/0005/2_0.png
ADDED
|
Git LFS Details
|
images_divided/0005/2_3.png
ADDED
|
Git LFS Details
|
images_divided/0005/3_3.png
ADDED
|
Git LFS Details
|
images_divided/0006/1_1.png
ADDED
|
Git LFS Details
|
images_divided/0006/1_2.png
ADDED
|
Git LFS Details
|
images_divided/0006/2_0.png
ADDED
|
Git LFS Details
|
images_divided/0006/2_1.png
ADDED
|
Git LFS Details
|
images_divided/0006/2_2.png
ADDED
|
Git LFS Details
|
images_divided/0006/3_0.png
ADDED
|
Git LFS Details
|
images_divided/0006/3_1.png
ADDED
|
Git LFS Details
|
images_divided/0006/3_2.png
ADDED
|
Git LFS Details
|
images_divided/0007/0_0.png
ADDED
|
Git LFS Details
|
images_divided/0007/0_1.png
ADDED
|
Git LFS Details
|
images_divided/0007/0_2.png
ADDED
|
Git LFS Details
|
images_divided/0007/0_3.png
ADDED
|
Git LFS Details
|
images_divided/0007/0_4.png
ADDED
|
Git LFS Details
|
images_divided/0007/1_0.png
ADDED
|
Git LFS Details
|
images_divided/0007/1_1.png
ADDED
|
Git LFS Details
|
images_divided/0007/1_2.png
ADDED
|
Git LFS Details
|
images_divided/0007/1_3.png
ADDED
|
Git LFS Details
|
images_divided/0007/2_0.png
ADDED
|
Git LFS Details
|
images_divided/0007/2_1.png
ADDED
|
Git LFS Details
|
images_divided/0007/2_2.png
ADDED
|
Git LFS Details
|
images_divided/0007/2_3.png
ADDED
|
Git LFS Details
|
images_divided/0007/2_4.png
ADDED
|
Git LFS Details
|
images_divided/0007/3_0.png
ADDED
|
Git LFS Details
|
images_divided/0007/3_1.png
ADDED
|
Git LFS Details
|
images_divided/0007/3_3.png
ADDED
|
Git LFS Details
|
images_divided/0007/3_4.png
ADDED
|
Git LFS Details
|
images_divided/0008/0_0.png
ADDED
|
Git LFS Details
|
images_divided/0008/0_1.png
ADDED
|
Git LFS Details
|
images_divided/0008/0_3.png
ADDED
|
Git LFS Details
|
images_divided/0008/1_0.png
ADDED
|
Git LFS Details
|
images_divided/0008/1_2.png
ADDED
|
Git LFS Details
|
images_divided/0008/2_1.png
ADDED
|
Git LFS Details
|
images_divided/0008/2_3.png
ADDED
|
Git LFS Details
|
images_divided/0008/3_0.png
ADDED
|
Git LFS Details
|
images_divided/0008/3_1.png
ADDED
|
Git LFS Details
|
images_divided/0008/3_3.png
ADDED
|
Git LFS Details
|
images_divided/0009/0_0.png
ADDED
|
Git LFS Details
|
images_divided/0009/0_1.png
ADDED
|
Git LFS Details
|
images_divided/0009/2_0.png
ADDED
|
Git LFS Details
|