Jayce-Ping's picture
Add files using upload-large-folder tool
0ce0b49 verified
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Ranking</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
padding-top: 80px;
}
/* 进度条样式 */
.progress-container {
position: fixed;
top: 0;
left: 0;
right: 0;
background-color: white;
padding: 15px 20px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
z-index: 999;
display: flex;
align-items: center;
gap: 15px;
justify-content: flex-end; /* 右上角对齐 */
}
.progress-info {
display: flex;
align-items: center;
gap: 8px;
font-size: 16px;
font-weight: bold;
color: #333;
}
.progress-input {
width: 80px;
padding: 5px 8px;
border: 2px solid #2196F3;
border-radius: 4px;
font-size: 16px;
font-weight: bold;
text-align: center;
}
.progress-input:focus {
outline: none;
border-color: #0b7dda;
box-shadow: 0 0 0 3px rgba(33, 150, 243, 0.1);
}
.progress-bar-wrapper {
flex: 1;
height: 20px;
background-color: #e0e0e0;
border-radius: 10px;
overflow: hidden;
position: relative;
}
.progress-bar {
height: 100%;
background: linear-gradient(90deg, #4CAF50 0%, #2196F3 100%);
transition: width 0.3s ease;
border-radius: 10px;
}
.progress-percentage {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 12px;
font-weight: bold;
color: #333;
text-shadow: 0 0 3px white;
}
.jump-btn {
padding: 6px 15px;
background-color: #2196F3;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
font-weight: bold;
transition: background-color 0.3s;
}
.jump-btn:hover {
background-color: #0b7dda;
}
.jump-btn:active {
transform: scale(0.98);
}
.rank-btn {
padding: 5px 15px;
cursor: pointer;
border: 2px solid #4CAF50;
background-color: white;
color: #4CAF50;
border-radius: 4px;
font-weight: bold;
min-width: 60px;
}
.rank-btn.ranked {
background-color: #4CAF50;
color: white;
border-color: #4CAF50;
}
.rank-btn:hover {
opacity: 0.8;
}
.submit-btn {
margin-top: 20px;
padding: 10px 30px;
background-color: #2196F3;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
.submit-btn:hover {
background-color: #0b7dda;
}
.submit-btn:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
.show-annotation-btn {
margin-top: 10px;
padding: 8px 20px;
background-color: #FF9800;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
}
.show-annotation-btn:hover {
background-color: #F57C00;
}
.annotation-status {
display: inline-block;
margin-left: 15px;
padding: 5px 12px;
background-color: #4CAF50;
color: white;
border-radius: 4px;
font-size: 14px;
font-weight: bold;
}
/* 收藏按钮样式 */
.star-btn {
display: inline-block;
margin-left: 15px;
padding: 8px 15px;
background-color: transparent;
border: 2px solid #FFC107;
color: #FFC107;
border-radius: 4px;
cursor: pointer;
font-size: 20px;
transition: all 0.3s ease;
vertical-align: middle;
}
.star-btn:hover {
transform: scale(1.1);
}
.star-btn.starred {
background-color: #FFC107;
color: white;
}
.discard-btn {
display: inline-block;
margin-left: 15px;
padding: 8px 15px;
background-color: transparent;
border: 2px solid #F44336;
color: #F44336;
border-radius: 4px;
cursor: pointer;
font-size: 18px;
font-weight: bold;
transition: all 0.3s ease;
vertical-align: middle;
}
.discard-btn:hover {
transform: scale(1.1);
}
.discard-btn.discarded {
background-color: #F44336;
color: white;
}
/* 网格问题按钮样式 */
.grid-issue-btn {
display: inline-block;
margin-left: 15px;
padding: 8px 15px;
background-color: transparent;
border: 2px solid #9C27B0;
color: #9C27B0;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
font-weight: bold;
transition: all 0.3s ease;
vertical-align: middle;
}
.grid-issue-btn:hover {
transform: scale(1.1);
}
.grid-issue-btn.grid-issue {
background-color: #9C27B0;
color: white;
}
/* 消息提示样式 */
.message-box {
position: fixed;
top: 20px;
right: 20px;
padding: 15px 25px;
border-radius: 4px;
font-size: 16px;
font-weight: bold;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
z-index: 1000;
display: none;
animation: slideIn 0.3s ease-out;
}
.message-box.success {
background-color: #4CAF50;
color: white;
}
.message-box.error {
background-color: #f44336;
color: white;
}
.message-box.show {
display: block;
}
@keyframes slideIn {
from {
transform: translateX(100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
@keyframes slideOut {
from {
transform: translateX(0);
opacity: 1;
}
to {
transform: translateX(100%);
opacity: 0;
}
}
.message-box.hiding {
animation: slideOut 0.3s ease-out;
}
</style>
<script>
let rankCounter = 1;
const rankedImages = new Map();
let allCompareImages = [];
// 获取当前页面和总页数
const currentPage = {{ page }};
const totalPages = {{ total_pages }};
function updatePageMetadata(page) {
// 异步更新页码到后端
fetch('/update_page', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ page: page })
}).catch(error => {
console.error('Error updating page metadata:', error);
});
}
function updateProgressBar() {
const progressBar = document.querySelector('.progress-bar');
const progressPercentage = document.querySelector('.progress-percentage');
const percentage = (currentPage / totalPages * 100).toFixed(1);
progressBar.style.width = percentage + '%';
progressPercentage.textContent = percentage + '%';
}
function jumpToPage() {
const input = document.getElementById('pageInput');
const targetPage = parseInt(input.value);
if (isNaN(targetPage) || targetPage < 1 || targetPage > totalPages) {
showMessage(`请输入1到${totalPages}之间的页码`, 'error');
input.value = currentPage;
return;
}
// 在跳转前更新元数据
updatePageMetadata(targetPage);
window.location.href = `/?page=${targetPage}`;
}
function initializeCompareImages() {
allCompareImages = [];
const rankButtons = document.querySelectorAll('.rank-btn');
rankButtons.forEach(btn => {
const imagePath = btn.getAttribute('data-image');
const filename = imagePath.split('/').pop();
if (!allCompareImages.includes(filename)) {
allCompareImages.push(filename);
}
});
}
window.addEventListener('DOMContentLoaded', function() {
initializeCompareImages();
setupKeyboardShortcuts();
updateProgressBar();
// 页面加载时更新元数据
updatePageMetadata(currentPage);
});
function setupKeyboardShortcuts() {
const pageInput = document.getElementById('pageInput');
document.addEventListener('keydown', function(event) {
// 在输入框中按Enter键跳转页面
if (event.target === pageInput && event.key === 'Enter') {
jumpToPage();
event.preventDefault();
return;
}
// 忽略在输入框中的其他按键
if (event.target.tagName === 'INPUT' || event.target.tagName === 'TEXTAREA') {
return;
}
// 数字键 1-4 对应第 0-3 张图像
if (event.key >= '1' && event.key <= '4') {
const index = parseInt(event.key) - 1;
const rankButtons = document.querySelectorAll('.rank-btn');
if (index < rankButtons.length) {
const button = rankButtons[index];
const imagePath = button.getAttribute('data-image');
rankImage(button, imagePath);
}
event.preventDefault();
}
// S 键收藏/取消收藏
if (event.key === 's' || event.key === 'S') {
const starButton = document.querySelector('.star-btn');
if (starButton) {
starButton.click();
}
event.preventDefault();
}
// D 键丢弃/取消丢弃
if (event.key === 'd' || event.key === 'D') {
const discardButton = document.querySelector('.discard-btn');
if (discardButton) {
discardButton.click();
}
event.preventDefault();
}
// G 键网格问题/取消网格问题
if (event.key === 'g' || event.key === 'G') {
const gridIssueButton = document.querySelector('.grid-issue-btn');
if (gridIssueButton) {
gridIssueButton.click();
}
event.preventDefault();
}
// Shift + 左箭头/上箭头 = 上一页
if (event.shiftKey && (event.key === 'ArrowLeft' || event.key === 'ArrowUp')) {
const prevLink = document.querySelector('a[href*="page="]');
if (prevLink && prevLink.textContent.includes('上一页')) {
const url = new URL(prevLink.href);
const prevPage = parseInt(url.searchParams.get('page'));
updatePageMetadata(prevPage);
window.location.href = prevLink.href;
}
event.preventDefault();
}
// Shift + 右箭头/下箭头 = 下一页
if (event.shiftKey && (event.key === 'ArrowRight' || event.key === 'ArrowDown')) {
const links = document.querySelectorAll('a[href*="page="]');
links.forEach(link => {
if (link.textContent.includes('下一页')) {
const url = new URL(link.href);
const nextPage = parseInt(url.searchParams.get('page'));
updatePageMetadata(nextPage);
window.location.href = link.href;
}
});
event.preventDefault();
}
// Enter 键提交排序
if (event.key === 'Enter') {
const form = document.getElementById('rankingForm');
const submitEvent = new Event('submit', { bubbles: true, cancelable: true });
form.dispatchEvent(submitEvent);
event.preventDefault();
}
});
}
function rankImage(button, imagePath) {
const currentRank = button.dataset.rank;
if (!currentRank) {
button.textContent = rankCounter;
button.dataset.rank = rankCounter;
button.classList.add('ranked');
rankedImages.set(imagePath, rankCounter);
const input = document.createElement('input');
input.type = 'hidden';
input.name = 'sorted_images';
input.value = `${rankCounter}:${imagePath}`;
input.id = `input-${imagePath.replace(/[^a-zA-Z0-9]/g, '_')}`;
document.getElementById('rankingForm').appendChild(input);
rankCounter++;
} else {
const rankToRemove = parseInt(currentRank);
button.textContent = "排序";
button.removeAttribute('data-rank');
button.classList.remove('ranked');
rankedImages.delete(imagePath);
const inputId = `input-${imagePath.replace(/[^a-zA-Z0-9]/g, '_')}`;
const inputToRemove = document.getElementById(inputId);
if (inputToRemove) {
inputToRemove.remove();
}
const allButtons = document.querySelectorAll('.rank-btn[data-rank]');
allButtons.forEach(btn => {
const btnRank = parseInt(btn.dataset.rank);
if (btnRank > rankToRemove) {
const newRank = btnRank - 1;
btn.dataset.rank = newRank;
btn.textContent = newRank;
const btnImagePath = btn.getAttribute('data-image');
rankedImages.set(btnImagePath, newRank);
const btnInputId = `input-${btnImagePath.replace(/[^a-zA-Z0-9]/g, '_')}`;
const btnInput = document.getElementById(btnInputId);
if (btnInput) {
btnInput.value = `${newRank}:${btnImagePath}`;
}
}
});
rankCounter--;
}
button.blur();
}
function toggleStar(button, folder, refImage) {
const isStarred = button.classList.contains('starred');
const newStarState = !isStarred;
// 获取当前排序结果
const currentRankImages = [];
for (let i = 1; i < rankCounter; i++) {
for (const [imagePath, rank] of rankedImages.entries()) {
if (rank === i) {
currentRankImages.push(imagePath.split('/').pop());
break;
}
}
}
fetch('/star', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
folder: folder,
ref_image: refImage,
stared: newStarState,
compare_images: allCompareImages,
rank_images: currentRankImages
})
})
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
if (newStarState) {
button.classList.add('starred');
button.textContent = '★';
} else {
button.classList.remove('starred');
button.textContent = '☆';
}
showMessage(data.message, 'success');
} else {
showMessage(data.message || '收藏操作失败', 'error');
}
})
.catch(error => {
console.error('Error:', error);
showMessage('收藏操作时发生错误', 'error');
});
}
function toggleDiscard(button, folder, refImage) {
const isDiscarded = button.classList.contains('discarded');
const newDiscardState = !isDiscarded;
// 获取当前排序结果
const currentRankImages = [];
for (let i = 1; i < rankCounter; i++) {
for (const [imagePath, rank] of rankedImages.entries()) {
if (rank === i) {
currentRankImages.push(imagePath.split('/').pop());
break;
}
}
}
fetch('/discard', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
folder: folder,
ref_image: refImage,
discarded: newDiscardState,
compare_images: allCompareImages,
rank_images: currentRankImages
})
})
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
if (newDiscardState) {
button.classList.add('discarded');
button.textContent = '🗑️';
} else {
button.classList.remove('discarded');
button.textContent = '丢弃';
}
showMessage(data.message, 'success');
} else {
showMessage(data.message || '丢弃操作失败', 'error');
}
})
.catch(error => {
console.error('Error:', error);
showMessage('丢弃操作时发生错误', 'error');
});
}
function toggleGridIssue(button, folder, refImage) {
const isGridIssue = button.classList.contains('grid-issue');
const newGridIssueState = !isGridIssue;
// 获取当前排序结果
const currentRankImages = [];
for (let i = 1; i < rankCounter; i++) {
for (const [imagePath, rank] of rankedImages.entries()) {
if (rank === i) {
currentRankImages.push(imagePath.split('/').pop());
break;
}
}
}
fetch('/grid_issue', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
folder: folder,
ref_image: refImage,
grid_issue: newGridIssueState,
compare_images: allCompareImages,
rank_images: currentRankImages
})
})
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
if (newGridIssueState) {
button.classList.add('grid-issue');
button.textContent = '⚠️';
} else {
button.classList.remove('grid-issue');
button.textContent = '网格问题';
}
showMessage(data.message, 'success');
} else {
showMessage(data.message || '网格问题标记失败', 'error');
}
})
.catch(error => {
console.error('Error:', error);
showMessage('网格问题标记时发生错误', 'error');
});
}
function loadExistingAnnotation(button, folder, refImage, rankImagesFilenames) {
const isLoaded = button.dataset.loaded === 'true';
if (isLoaded) {
const allButtons = document.querySelectorAll('.rank-btn');
allButtons.forEach(btn => {
btn.textContent = "排序";
btn.removeAttribute('data-rank');
btn.classList.remove('ranked');
});
rankedImages.clear();
rankCounter = 1;
const hiddenInputs = document.querySelectorAll('input[name="sorted_images"]');
hiddenInputs.forEach(input => input.remove());
button.textContent = '显示已有标注';
button.dataset.loaded = 'false';
showMessage('已清除标注结果', 'success');
} else {
const allButtons = document.querySelectorAll('.rank-btn');
allButtons.forEach(btn => {
btn.textContent = "排序";
btn.removeAttribute('data-rank');
btn.classList.remove('ranked');
});
rankedImages.clear();
rankCounter = 1;
const hiddenInputs = document.querySelectorAll('input[name="sorted_images"]');
hiddenInputs.forEach(input => input.remove());
rankImagesFilenames.forEach((filename, index) => {
const rank = index + 1;
const imagePath = `/images/${folder}/${filename}`;
const rankBtn = document.querySelector(`.rank-btn[data-image="${imagePath}"]`);
if (rankBtn) {
rankBtn.textContent = rank;
rankBtn.dataset.rank = rank;
rankBtn.classList.add('ranked');
rankedImages.set(imagePath, rank);
const input = document.createElement('input');
input.type = 'hidden';
input.name = 'sorted_images';
input.value = `${rank}:${imagePath}`;
input.id = `input-${imagePath.replace(/[^a-zA-Z0-9]/g, '_')}`;
document.getElementById('rankingForm').appendChild(input);
}
});
rankCounter = rankImagesFilenames.length + 1;
button.textContent = '关闭已有标注';
button.dataset.loaded = 'true';
showMessage('已加载现有标注结果', 'success');
}
}
function showMessage(message, type = 'success', duration = 2000) {
const messageBox = document.getElementById('messageBox');
messageBox.textContent = message;
messageBox.className = `message-box ${type} show`;
setTimeout(() => {
messageBox.classList.add('hiding');
setTimeout(() => {
messageBox.classList.remove('show', 'hiding');
}, 300);
}, duration);
}
function submitRanking(event) {
event.preventDefault();
if (rankedImages.size === 0) {
showMessage('请至少选择一张图片进行排序!', 'error');
return false;
}
const form = document.getElementById('rankingForm');
const formData = new FormData(form);
formData.append('all_compare_images', JSON.stringify(allCompareImages));
const submitBtn = form.querySelector('.submit-btn');
submitBtn.disabled = true;
submitBtn.textContent = '提交中...';
fetch('/sort', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
showMessage(data.message, 'success');
const statusBadge = document.querySelector('.annotation-status');
if (!statusBadge) {
const h3 = document.querySelector('h3');
if (h3) {
const badge = document.createElement('span');
badge.className = 'annotation-status';
badge.textContent = '已标注';
h3.appendChild(badge);
}
}
} else {
showMessage(data.message || '提交失败', 'error');
}
})
.catch(error => {
console.error('Error:', error);
showMessage('提交时发生错误', 'error');
})
.finally(() => {
submitBtn.disabled = false;
submitBtn.textContent = '提交排序';
});
return false;
}
</script>
</head>
<body>
<div class="progress-container">
<div class="progress-info">
<input id="pageInput"
class="progress-input"
type="number"
min="1"
max="{{ total_pages }}"
value="{{ page }}"
aria-label="页码">
<span>/ {{ total_pages }}</span>
</div>
<div class="progress-bar-wrapper" aria-label="进度">
<div class="progress-bar" style="width: 0%"></div>
<div class="progress-percentage">0%</div>
</div>
<button class="jump-btn" type="button" onclick="jumpToPage()">跳转</button>
</div>
<div id="messageBox" class="message-box"></div>
<h1>Image Ranking</h1>
<div style="background-color: #e3f2fd; padding: 12px 20px; margin-bottom: 20px; border-radius: 4px; border-left: 4px solid #2196F3;">
<div style="background-color: #e3f2fd; padding: 12px 20px; margin-bottom: 20px; border-radius: 4px; border-left: 4px solid #2196F3;">
<p style="margin: 0; font-size: 14px; color: #1976d2; line-height: 1.8;">
<strong>快捷键提示:</strong><br>
<kbd style="background: #fff; padding: 2px 6px; border: 1px solid #ccc; border-radius: 3px; font-family: monospace;">S</kbd> 收藏 |
<kbd style="background: #fff; padding: 2px 6px; border: 1px solid #ccc; border-radius: 3px; font-family: monospace;">D</kbd> 丢弃 |
<kbd style="background: #fff; padding: 2px 6px; border: 1px solid #ccc; border-radius: 3px; font-family: monospace;">G</kbd> 网格问题 |
<kbd style="background: #fff; padding: 2px 6px; border: 1px solid #ccc; border-radius: 3px; font-family: monospace;">Enter</kbd> 提交排序<br>
对前4张图像排序 |
<kbd style="background: #fff; padding: 2px 6px; border: 1px solid #ccc; border-radius: 3px; font-family: monospace;">S</kbd> 收藏 |
<kbd style="background: #fff; padding: 2px 6px; border: 1px solid #ccc; border-radius: 3px; font-family: monospace;">D</kbd> 丢弃 |
<kbd style="background: #fff; padding: 2px 6px; border: 1px solid #ccc; border-radius: 3px; font-family: monospace;">Enter</kbd> 提交排序<br>
<kbd style="background: #fff; padding: 2px 6px; border: 1px solid #ccc; border-radius: 3px; font-family: monospace;">Shift+←/↑</kbd> 上一条 |
<kbd style="background: #fff; padding: 2px 6px; border: 1px solid #ccc; border-radius: 3px; font-family: monospace;">Shift+→/↓</kbd> 下一条
</p>
</div>
</div>
<form id="rankingForm" action="/sort" method="POST" onsubmit="return submitRanking(event)">
{% for item in data %}
<div>
{% if item.prompt %}
<div style="background-color: #f0f0f0; padding: 15px; margin-bottom: 20px; border-radius: 4px; border-left: 4px solid #2196F3;">
<h2 style="margin-top: 0; color: #333;">Prompt:</h2>
<p style="font-size: 16px; line-height: 1.6; margin-bottom: 0; color: #555;">{{ item.prompt }}</p>
</div>
{% endif %}
<h3>
Reference Image: {{ item.ref_image }}
{% if item.is_annotated %}
<span class="annotation-status">已标注</span>
{% endif %}
<button type="button"
class="star-btn {% if item.stared %}starred{% endif %}"
onclick="toggleStar(this, '{{ item.folder }}', '{{ item.ref_image.split('/')[-1] }}')">
{% if item.stared %}★{% else %}☆{% endif %}
</button>
<button type="button"
class="discard-btn {% if item.discarded %}discarded{% endif %}"
onclick="toggleDiscard(this, '{{ item.folder }}', '{{ item.ref_image.split('/')[-1] }}')">
{% if item.discarded %}🗑️{% else %}丢弃{% endif %}
</button>
<button type="button"
class="grid-issue-btn {% if item.grid_issue %}grid-issue{% endif %}"
onclick="toggleGridIssue(this, '{{ item.folder }}', '{{ item.ref_image.split('/')[-1] }}')">
{% if item.grid_issue %}⚠️{% else %}网格问题{% endif %}
</button>
</h3>
</h3>
<p>Folder: {{ item.folder }}</p>
<div style="text-align: center; margin: 20px 0;">
<img src="{{ item.ref_image }}" alt="ref_image" width="400">
</div>
<input type="hidden" name="ref_image" value="{{ item.ref_image }}">
{% if item.is_annotated %}
<button type="button" class="show-annotation-btn"
onclick='loadExistingAnnotation(this, "{{ item.folder }}", "{{ item.ref_image.split("/")[-1] }}", {{ item.existing_rank_images|tojson|safe }})'>
显示已有标注
</button>
{% endif %}
<h4>Compare Images:</h4>
<div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 20px; width: 100%;">
{% for compare_image in item.compare_images %}
<div class="image-item" style="display: flex; flex-direction: column; align-items: center; gap: 8px;">
<img src="{{ compare_image }}" alt="compare_image" style="width: 100%; height: auto; max-width: 100%;">
<button type="button"
class="rank-btn"
data-image="{{ compare_image }}"
onclick="rankImage(this, '{{ compare_image }}')">
排序
</button>
</div>
{% endfor %}
</div>
</div>
{% endfor %}
<button type="submit" class="submit-btn">提交排序</button>
</form>
<div style="margin-top: 20px;">
{% if page > 1 %}
<a href="/?page={{ page - 1 }}" style="margin-right: 10px;">上一页</a>
{% endif %}
{% if page < total_pages %}
<a href="/?page={{ page + 1 }}">下一页</a>
{% endif %}
</div>
</body>
</html>