一、数据标注
1. 使用labelme软件标注每个细胞的边界信息,标注结果为JSON格式
2. JSON格式转yolo支持的txt格式
import json
import os
import glob
import os.path as osp
'''
此函数用来将labelme软件标注好的数据集转换为yolov5_7.0sege中使用的数据集
:param jsonfilePath: labelme标注好的*.json文件所在文件夹
:param resultDirPath: 转换好后的*.txt保存文件夹
:param classList: 数据集中的类别标签
'''
def labelme2yolov2Seg(jsonfilePath, resultDirPath, classList):
# 0.创建保存转换结果的文件夹
if (not os.path.exists(resultDirPath)):
os.mkdir(resultDirPath)
# 1.获取目录下所有的labelme标注好的Json文件,存入列表中
jsonfileList = glob.glob(osp.join(jsonfilePath, "*.json") )
print(jsonfileList) # 打印文件夹下的文件名称
# 2.遍历json文件,进行转换
for jsonfile in jsonfileList:
# 3. 打开json文件
with open(jsonfile, "r",encoding='UTF-8') as f:
file_in = json.load(f)
# 4. 读取文件中记录的所有标注目标
shapes = file_in["shapes"]
# 5. 使用图像名称创建一个txt文件,用来保存数据
with open(resultDirPath + "\\" + jsonfile.split("\\")[-1].replace(".json", ".txt"), "w") as file_handle:
# 6. 遍历shapes中的每个目标的轮廓
for shape in shapes:
# 7.根据json中目标的类别标签,从classList中寻找类别的ID,然后写入txt文件中
file_handle.writelines(str(classList.index(shape["label"])) + " ")
# 8. 遍历shape轮廓中的每个点,每个点要进行图像尺寸的缩放,即x/width, y/height
for point in shape["points"]:
x = point[0] / file_in["imageWidth"] # mask轮廓中一点的X坐标
y = point[1] / file_in["imageHeight"] # mask轮廓中一点的Y坐标
file_handle.writelines(str(x) + " " + str(y) + " ") # 写入mask轮廓点
# 9.每个物体一行数据,一个物体遍历完成后需要换行
file_handle.writelines("\n")
# 10.所有物体都遍历完,需要关闭文件
file_handle.close()
# 10.所有物体都遍历完,需要关闭文件
f.close()
if __name__ == "__main__":
jsonfilePath = r"D:/workspace/yolov8/datasets/json" # 要转换的json文件所在目录
resultDirPath = r"D:/workspace/yolov8/datasets/txt" # 要生成的txt文件夹
labelme2yolov2Seg(jsonfilePath=jsonfilePath, resultDirPath=resultDirPath, classList=['danhe','linba','yilin']) # 更改为自己的类别名
转换结果:
二、训练数据
1. 配置文件
路径:ultralytics\cfg\datasets\cell_seg.yaml
path: ../datasets/cell_seg # dataset root dir
train: images/train # train images (relative to 'path')
val: images/val # val images (relative to 'path')
test: test/images # test images (optional)
nc: 2
# Classes
names:
0: danhe
1: linba
2. 训练脚本
# 细胞实例分割训练
from ultralytics import YOLO
model = YOLO('yolov8n-seg.pt')
# 训练自己的数据集, 文件路径: ultralytics/cfg/datasets/cell_seg.yaml
model.train(data='cell_seg.yaml', epochs=100, imgsz=320)
# 使用验证集 验证效果
model.val()
3. 训练结果
三、使用上述训练的模型,批量检测细胞、计算细胞面积、灰度值计算
1. 预测细胞类别及轮廓
# 细胞实例分割训练
from ultralytics import YOLO
# best_seg.pt 训练结果中weight/best.pt
model = YOLO('best_seg.pt')
# 测试数据 批量检测
predict_results = model.predict(source="../datasets/cell/test/images", save=True)
2. 识别轮廓、计算面积
from ultralytics import YOLO
import cv2
import os
import numpy as np
# Load a pretrained YOLOv8n model
model = YOLO('best_seg.pt')
# Define path to the directory containing image files
source = '../datasets/cell/test/images'
# 指定输出路径
output_dir = './output_images'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 获取待预测文件名称,用于保存同名文件
def get_last_part_of_string(path):
return os.path.basename(path)
# hex to BGR
def hex_to_bgr(hex):
return tuple(int(hex[i:i+2], 16) for i in (4, 2, 0))
# 颜色,同plotting.py的设置
hexs = (
"FF3838", "FF9D97", "FF701F", "FFB21D", "CFD231",
"48F90A", "92CC17", "3DDB86", "1A9334", "00D4BB",
"2C99A8", "00C2FF", "344593", "6473FF", "0018EC",
"8438FF", "520085", "CB38FF", "FF95C8", "FF37C7"
)
colors = [hex_to_bgr(h) for h in hexs]
# 开始预测
results = model(source=source, save=False) # list of Results objects
for result in results:
image_path = result.path
image = cv2.imread(image_path)
boxes = result.boxes # Boxes 对象,用于边界框输出
masks = result.masks # Masks 对象,用于分割掩码输出
names = result.names # 获取类别名称字典
for box, mask in zip(boxes, masks):
for cls, contour in zip(box.cls, mask.xy):
class_id = int(cls.item()) # 获取张量的值并转换为整数
color = colors[class_id % len(colors)] # 获取颜色
contour = np.array(contour, dtype=np.int32) # 确保轮廓是整数类型
area = cv2.contourArea(contour) # 计算轮廓面积
class_name = names[class_id] # 获取类别名称
# 计算轮廓的中心
M = cv2.moments(contour)
if M['m00'] != 0:
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
else:
cx, cy = 0, 0
'''
cv2.drawContours 参数:
image: 输入图像,一般是二值图像或彩色图像;
contours: 要绘制的轮廓,是一个 Python 列表,每个列表元素都是一个 Numpy 数组,代表一个轮廓;
contourIdx: 要绘制的轮廓的索引,默认为 -1, 代表绘制所有轮廓;
color: 轮廓的颜色,是一个三元组,分别表示 RGB 颜色;
thickness: 线条的粗细,默认为 1;
lineType: 线条类型,默认为 cv2.LINE_8;
hierarchy: 轮廓的层次关系,是一个 Numpy 数组;
maxLevel: 最多绘制的轮廓层
'''
# 绘制掩码轮廓
cv2.drawContours(image, [contour], -1, color, 2)
# 在图像上绘制面积和类名
text = f'{class_name} {area:.2f}'
'''
cv2.putText 参数:
img: 需要绘制文本的图像。
text: 要绘制的文本内容。
org: 文本框的左下角坐标,即起始点(x,y)。
fontFace: 字体类型, 如cv2.FONT_HERSHEY_SIMPLEX、cv2.FONT_HERSHEY_PLAIN等。
fontScale: 字体大小, float类型, 一般设置为1.0。
color: 文本颜色, BGR格式。
thickness: 文本粗细, 如果设置为None, 则表示绘制实心文字。
lineType: 线型, 可选类型包括cv2.LINE_4、cv2.LINE_8、cv2.LINE_AA等。
bottomLeftOrigin: 如果为True, 则图像数据原点在左下角(默认情况下在左上角)。
'''
cv2.putText(image, text, (cx, cy), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1)
# 保存图像
output_path = os.path.join(output_dir, get_last_part_of_string(image_path))
'''
cv2.imwrite() 参数:
filename: 要保存的文件路径和名称,包括路径(如果文件不在当前工作目录下)和文件扩展名(如 .jpg, .png 等)。
img: 要保存的图像数据,通常是通过 OpenCV 读取或处理得到的。
'''
cv2.imwrite(output_path, image)
print(f'Saved: {output_path}')
3. 灰度值提取
from ultralytics import YOLO
import cv2
import os
import numpy as np
# Load a pretrained YOLOv8n model
model = YOLO('best_seg.pt')
# Define path to the directory containing image files
source = '../datasets/cell/test/images'
# 指定输出路径
output_dir = './output_images'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 获取待预测文件名称,用于保存同名文件
def get_last_part_of_string(path):
return os.path.basename(path)
# hex to BGR
def hex_to_bgr(hex):
return tuple(int(hex[i:i+2], 16) for i in (4, 2, 0))
# 颜色,同plotting.py的设置
hexs = (
"FF3838", "FF9D97", "FF701F", "FFB21D", "CFD231",
"48F90A", "92CC17", "3DDB86", "1A9334", "00D4BB",
"2C99A8", "00C2FF", "344593", "6473FF", "0018EC",
"8438FF", "520085", "CB38FF", "FF95C8", "FF37C7"
)
colors = [hex_to_bgr(h) for h in hexs]
# 开始预测
results = model(source=source, save=False) # list of Results objects
n_points = 5
spacing = 30
for result in results:
image_path = result.path
image = cv2.imread(image_path)
boxes = result.boxes # Boxes 对象,用于边界框输出
masks = result.masks # Masks 对象,用于分割掩码输出
names = result.names # 获取类别名称字典
# 将图片转换成灰度图
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
for box, mask in zip(boxes, masks):
for cls, contour in zip(box.cls, mask.xy):
class_id = int(cls.item()) # 获取张量的值并转换为整数
color = colors[class_id % len(colors)] # 获取颜色
contour = np.array(contour, dtype=np.int32) # 确保轮廓是整数类型
area = cv2.contourArea(contour) # 计算轮廓面积
class_name = names[class_id] # 获取类别名称
# 计算轮廓的中心
M = cv2.moments(contour)
if M['m00'] != 0:
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
else:
cx, cy = 0, 0
'''
cv2.drawContours 参数:
image: 输入图像,一般是二值图像或彩色图像;
contours: 要绘制的轮廓,是一个 Python 列表,每个列表元素都是一个 Numpy 数组,代表一个轮廓;
contourIdx: 要绘制的轮廓的索引,默认为 -1, 代表绘制所有轮廓;
color: 轮廓的颜色,是一个三元组,分别表示 RGB 颜色;
thickness: 线条的粗细,默认为 1;
lineType: 线条类型,默认为 cv2.LINE_8;
hierarchy: 轮廓的层次关系,是一个 Numpy 数组;
maxLevel: 最多绘制的轮廓层
'''
# 绘制掩码轮廓
cv2.drawContours(gray_image, [contour], -1, color, 2)
# 在图像上绘制面积和类名
text = f'{class_name} {area:.2f}'
cv2.putText(gray_image, text, (cx, cy-20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1)
# 以轮廓为中心,在图像上绘制点
for i in range(n_points):
# 从中心店左移2个点的距离
x = cx + i * spacing - spacing *2
y = cy
gray_value = gray_image[y, x]
# 画点
cv2.circle(gray_image, (x, y), 2, (255, 0, 0), 1)
# 点描述
cv2.putText(gray_image, str(i+1), (x-5, y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 0, 0), 1)
# cv2.circle(gray_image, (x-(i * spacing), y), 2, (0, 255, 0), 1)
gray_text = f"Point {i+1}({x},{y}): gray value = {gray_value} "
print(gray_text)
# 将点的灰度值绘制在图片的左上角
cv2.putText(gray_image, gray_text, (10, 15+i*15), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 0), 1)
# 保存图像
output_path = os.path.join(output_dir, get_last_part_of_string(image_path))
'''
cv2.imwrite() 参数:
filename: 要保存的文件路径和名称,包括路径(如果文件不在当前工作目录下)和文件扩展名(如 .jpg, .png 等)。
img: 要保存的图像数据,通常是通过 OpenCV 读取或处理得到的。
'''
cv2.imwrite(output_path, gray_image)
print(f'Saved: {output_path}')