diff --git a/cam_web.py b/cam_web.py index 05e3639..e13b582 100644 --- a/cam_web.py +++ b/cam_web.py @@ -145,18 +145,29 @@ def get_images_api(): try: cursor.execute(""" SELECT id, left_filename, right_filename, left_marked_filename, right_marked_filename, - timestamp, metadata, comment, created_at, manual_detections, is_manual_labeled + timestamp, metadata, comment, created_at, manual_detections, is_manual_labeled, + left_position, right_position FROM images ORDER BY timestamp DESC """) except sqlite3.OperationalError: # 如果字段不存在,使用基本查询 - cursor.execute(""" - SELECT id, left_filename, right_filename, left_marked_filename, right_marked_filename, - timestamp, metadata, comment, created_at, NULL as manual_detections, 0 as is_manual_labeled - FROM images - ORDER BY timestamp DESC - """) + try: + cursor.execute(""" + SELECT id, left_filename, right_filename, left_marked_filename, right_marked_filename, + timestamp, metadata, comment, created_at, manual_detections, is_manual_labeled, + 0 as left_position, 0 as right_position + FROM images + ORDER BY timestamp DESC + """) + except sqlite3.OperationalError: + cursor.execute(""" + SELECT id, left_filename, right_filename, left_marked_filename, right_marked_filename, + timestamp, metadata, comment, created_at, NULL as manual_detections, 0 as is_manual_labeled, + 0 as left_position, 0 as right_position + FROM images + ORDER BY timestamp DESC + """) rows = cursor.fetchall() conn.close() @@ -174,7 +185,9 @@ def get_images_api(): "comment": row[7] or "", # 如果没有 comment 则显示空字符串 "created_at": row[8], "manual_detections": row[9] or "[]", # 人工标注检测框结果 - "is_manual_labeled": bool(row[10]) if row[10] is not None else False # 是否已完成人工标注 + "is_manual_labeled": bool(row[10]) if row[10] is not None else False, # 是否已完成人工标注 + "left_position": row[11] if row[11] is not None else 0, # 左侧位置编号 + "right_position": row[12] if row[12] is not None else 0 # 右侧位置编号 }) return jsonify(images) @@ -438,6 +451,43 @@ def update_image_comment(): return jsonify({"message": f"Comment for image {image_id} updated successfully"}) +@app.route('/api/images/position', methods=['PUT']) +def update_image_position(): + """API: 更新图片的位置编号""" + data = request.json + image_id = data.get('id') + left_position = data.get('left_position', 0) + right_position = data.get('right_position', 0) + + if not image_id: + return jsonify({"error": "Image ID is required"}), 400 + + conn = sqlite3.connect(DATABASE_PATH) + cursor = conn.cursor() + + # 添加位置字段(如果不存在) + try: + cursor.execute(""" + ALTER TABLE images ADD COLUMN left_position INTEGER DEFAULT 0 + """) + except sqlite3.OperationalError: + pass + + try: + cursor.execute(""" + ALTER TABLE images ADD COLUMN right_position INTEGER DEFAULT 0 + """) + except sqlite3.OperationalError: + pass + + # 更新位置字段 + cursor.execute("UPDATE images SET left_position = ?, right_position = ? WHERE id = ?", + (left_position, right_position, image_id)) + conn.commit() + conn.close() + + return jsonify({"message": f"Position for image {image_id} updated successfully"}) + @app.route('/status') def status(): with frame_lock: @@ -450,7 +500,7 @@ def update_manual_detections(): """API: 更新图片的人工标注检测框结果,支持左右图像分别标注""" data = request.json image_id = data.get('id') - side = data.get('side', 'left') # 获取side参数,默认为左侧 + side = data.get('side', 'left') # 获取 side 参数,默认为左侧 detections = data.get('detections') if not image_id or detections is None: @@ -469,7 +519,7 @@ def update_manual_detections(): if key not in detection: return jsonify({"error": f"Missing required key '{key}' in detection"}), 400 - # 验证ID + # 验证 ID if not isinstance(detection['id'], int) or detection['id'] not in [1, 2, 3, 4]: return jsonify({"error": f"Invalid ID in detection: {detection['id']}"}), 400 @@ -524,7 +574,7 @@ def update_manual_detections(): except sqlite3.OperationalError: pass - # 根据side参数更新对应的人工标注结果 + # 根据 side 参数更新对应的人工标注结果 if side == 'left': cursor.execute(""" UPDATE images @@ -566,7 +616,7 @@ def regenerate_marked_images(image_id, detections, side): left_filename, right_filename, left_marked_filename, right_marked_filename = row - # 根据指定的side重新生成对应的标注图片 + # 根据指定的 side 重新生成对应的标注图片 if side == 'left' and left_marked_filename: left_path = os.path.join(SAVE_PATH_LEFT, left_filename) left_marked_path = os.path.join(SAVE_PATH_LEFT_MARKED, left_marked_filename) diff --git a/templates/list_images.html b/templates/list_images.html index 2cc07c3..1a15cf7 100644 --- a/templates/list_images.html +++ b/templates/list_images.html @@ -174,25 +174,42 @@ // Timestamp row.insertCell(4).textContent = new Date(image.timestamp * 1000).toISOString(); - // Comment - const commentCell = row.insertCell(5); - const commentInput = document.createElement('input'); - commentInput.type = 'text'; - commentInput.value = image.comment || ''; - commentInput.dataset.id = image.id; - commentInput.className = 'comment-input'; - commentInput.style.width = '100%'; - commentInput.addEventListener('change', function () { - updateComment(image.id, this.value); - }); - commentCell.appendChild(commentInput); - + // 评论 + row += ``; // Actions - row.insertCell(6).innerHTML = ` - - - - `; + row += ` + + + + + + + + + `; + row += ''; }); }