fixed_label.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # 要求保证视频不能移动,且全过程没有任何遮挡物
  2. import os
  3. import sys
  4. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  5. from utils import draw_grid
  6. import cv2
  7. import numpy as np
  8. import tkinter as tk
  9. from tkinter import simpledialog
  10. from dotenv import load_dotenv
  11. load_dotenv() # 加载环境变量
  12. # 所要切分的图块宽高
  13. patch_w = int(os.getenv('PATCH_WIDTH', 256))
  14. patch_h = int(os.getenv('PATCH_HEIGHT', 256))
  15. scale = 2
  16. # 存储标记点
  17. clicked_points = []
  18. def get_text_input(prompt):
  19. """创建弹窗获取文本输入"""
  20. root = tk.Tk()
  21. root.withdraw() # 隐藏主窗口
  22. root.attributes('-topmost', True) # 确保弹窗置顶
  23. result = simpledialog.askstring(' ', prompt)
  24. root.destroy()
  25. if result is None:
  26. result = ""
  27. if not result.strip().isdigit():
  28. result = -1
  29. return int(result)
  30. def mouse_callback(event, x, y, flags, param):
  31. """
  32. 鼠标回调函数
  33. """
  34. global clicked_points
  35. global patch_w
  36. global patch_h
  37. global scale
  38. if event == cv2.EVENT_LBUTTONDOWN: # 左键点击
  39. # 在点击位置绘制红色圆点
  40. scale_patch_w = patch_w // scale
  41. scale_patch_h = patch_h // scale
  42. # 格子角点
  43. circle_x_corner = (x // scale_patch_w)*scale_patch_w
  44. circle_y_corner = (y // scale_patch_h)*scale_patch_h
  45. # 格子中心点
  46. circle_x_center = circle_x_corner + scale_patch_w//2
  47. circle_y_center = circle_y_corner + scale_patch_h//2
  48. cv2.circle(param, (circle_x_center, circle_y_center), 5, (0, 0, 255), -1)
  49. cv2.circle(param, (circle_x_corner, circle_y_corner), 5, (255, 0, 0), -1)
  50. # 更新显示
  51. cv2.imshow('img', param)
  52. cls = get_text_input('请输入类别:0.背景 1.浑浊 -1.不参与')
  53. # 显示标签文本
  54. cv2.putText(param, str(cls), (circle_x_center + 10, circle_y_center + 10),
  55. cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
  56. # 更新显示
  57. cv2.imshow('img', param)
  58. valid_cls = [0, 1, -1]
  59. if cls in valid_cls:
  60. print(f"点击网格角点: ({circle_x_corner}, {circle_y_corner}) 中心点: ({circle_x_center}, {circle_y_center}) 类别:{cls}")
  61. # 记录标注数据,角点 u v w h cls
  62. clicked_points.append([circle_x_corner, circle_y_corner, scale_patch_w, scale_patch_h, cls])
  63. else:
  64. print("请输入正确的类别!")
  65. elif event == cv2.EVENT_RBUTTONDOWN: # 右键点击
  66. removed_point = clicked_points.pop()
  67. print(f"撤销标注点: ({removed_point[0]}, {removed_point[1]}) 类别: {removed_point[4]}")
  68. # 将撤销点标记为黑色
  69. x = removed_point[0]
  70. y = removed_point[1]
  71. # 在点击位置绘制黑色圆点
  72. scale_patch_w = patch_w // scale
  73. scale_patch_h = patch_h // scale
  74. # 格子角点
  75. circle_x_corner = (x // scale_patch_w)*scale_patch_w
  76. circle_y_corner = (y // scale_patch_h)*scale_patch_h
  77. # 格子中心点
  78. circle_x_center = circle_x_corner + scale_patch_w//2
  79. circle_y_center = circle_y_corner + scale_patch_h//2
  80. cv2.circle(param, (circle_x_center, circle_y_center), 5, (128, 128, 128), -1)
  81. cv2.circle(param, (circle_x_corner, circle_y_corner), 5, (128, 128, 128), -1)
  82. # 更新显示
  83. cv2.imshow('img', param)
  84. def remove_duplicates(arr:list):
  85. """列表去重"""
  86. unique_list = []
  87. [unique_list.append(item) for item in arr if item not in unique_list]
  88. return unique_list
  89. def main():
  90. """
  91. 固定摄像头标注,只需要标注一张图像,后续图像保持一致
  92. 1.标注过程,先将图像划分为图框,用cv2划线工具在图像上划网格线
  93. 2.用鼠标进行交互,点击图块输入标签,按下空格键完成交互过程,保存标签
  94. 3.标签格式:u,v,w,h,label u,v为块左上角坐标,w,h为块的宽和高,label为块的标签
  95. """
  96. global clicked_points
  97. global patch_w
  98. global patch_h
  99. global scale
  100. # TODO: 需要更改为准备标注的图像路径,使用当前目录下的000000.jpg,结果保存在当前目录下label.txt
  101. img_path = r'D:\code\water_turbidity_det\data\video1_20251129120104_20251129123102\000000.jpg'
  102. img = cv2.imread(img_path)
  103. # resize 图像太大了显示不全
  104. img = cv2.resize(img, (img.shape[1] // scale, img.shape[0] // scale))
  105. # 绘制网格线
  106. draw_grid(img, patch_w // scale, patch_h // scale)
  107. # 交互标注
  108. print("操作说明:")
  109. print("- 点击鼠标左键在图像上添加红色标记点: 0.其他 1.浑浊 -1.忽略,不参与训练和测试")
  110. print("- 按 'c' 键清除所有标记点")
  111. print("- 按 ESC 键退出程序")
  112. cv2.namedWindow('img')
  113. cv2.setMouseCallback('img', mouse_callback, img)
  114. # 交互标注
  115. while True:
  116. cv2.imshow('img', img)
  117. key = cv2.waitKey(1) & 0xFF
  118. # 按 'c' 键清除所有标记点
  119. if key == ord('c'):
  120. img = cv2.imread(img_path)
  121. img = cv2.resize(img, (img.shape[1] // scale, img.shape[0] // scale))
  122. draw_grid(img, patch_w // scale, patch_h // scale)
  123. clicked_points.clear()
  124. cv2.setMouseCallback('img', mouse_callback, img)
  125. # 按 ESC 键退出
  126. elif key == 27: # ESC键
  127. break
  128. cv2.destroyAllWindows()
  129. # 输出所有点击位置
  130. # 列表去重
  131. clicked_points = remove_duplicates(clicked_points)
  132. print(f"总共标记了 {len(clicked_points)} 个点:")
  133. for i, point in enumerate(clicked_points):
  134. print(f" 点 {i + 1}: ({point[0]}, {point[1]}, {point[2]}, {point[3]}, {point[4]})")
  135. # 恢复尺寸
  136. clicked_points = [[p[0]*scale, p[1]*scale, p[2]*scale, p[3]*scale, p[4]] for p in clicked_points]
  137. # 写入txt
  138. if clicked_points:
  139. with open(os.path.join(os.path.dirname(img_path), 'label.txt'), 'w') as fw:
  140. for point in clicked_points:
  141. fw.write(f"{point[0]},{point[1]},{point[2]},{point[3]},{point[4]}\n")
  142. # 保存点
  143. print(f"保存标记点 {len(clicked_points)} 个:")
  144. for i, point in enumerate(clicked_points):
  145. print(f" 点 {i + 1}: ({point[0]}, {point[1]}, {point[2]}, {point[3]}, {point[4]})")
  146. else :
  147. print("没有标记点!不保存任何文件!")
  148. if __name__ == '__main__':
  149. main()