建商城网站需要什么条件,制作网站得多少钱,logo生成器免费,合肥小吃培训网页设计今天是24年的最后一天#xff0c;终于要向新世界开始破门了#xff0c;开始深度学习#xff0c;YOLO来敲门~ 最近做了一些皮肤检测的功能#xff0c;在传统的处理中经历了反复挣扎#xff0c;终于要上YOLO了。听过、看过#xff0c;不如上手体会过~ 1、YOLO是什么#x…今天是24年的最后一天终于要向新世界开始破门了开始深度学习YOLO来敲门~ 最近做了一些皮肤检测的功能在传统的处理中经历了反复挣扎终于要上YOLO了。听过、看过不如上手体会过~ 1、YOLO是什么 看了GPT的回答首次上手的人还是不太懂只知道从2016年出第一版到现在已经过多轮迭代更新了是很成熟的框架可以放心大胆用。在做目标检测方面杠杠的。特别是对于特征一致性相对较好的不大不小需求绝了。安全帽、头盔、口罩检测已经是典型应用了。 2、开启YOLOv8框架 为什么是v8这个还是源于大佬的经验小白都是先模仿。也因为我的皮肤检测属于小型检测目标。 1准备数据文件夹dataset数据用来建立模型 images:图片文件夹里面有两个文件夹分别放了训练图片原图80%和用作测试的图片20% labels:标注文件夹里面放置了训练和测试图片文件夹对应的标注文件格式是txt。 标注文件就是采用标注软件对图片中需要识别的目标进行标签化的结果文件。
标注很重要需要尽量圈出自己的目标前景且保持多帧间的一致性和稳定性。 常用又好用的标注软件是Labelme 和LabelImg。我挺喜欢Labelme 的有很多个版本还有windows可以直接运行的Labelme.exe。总之不用编码直接使用方便好get。需要注意的是Labelme的输出是jason文档为了顺利转成满足YOLO输入的格式需要将jason转成txt(后面有给出可运行的代码。 Labelme.exe我已经上传资源文档了windows可以直接使用的labelme.exe 注意标注是需要提前做好的且images 和labels 两个文件夹中的训练和测试的数据应保持匹配和对应关系。
2在python编辑器中用代码建立模型并推理计算 先安装好YOLO包默认已安装好python及常用依赖库如CV)
训练代码 train.py
from ultralytics import YOLO# 初始化 YOLO 模型
model YOLO(yolov8n.pt) # 使用预训练的 YOLOv8 nano 模型# 训练模型
model.train(dataE:/skin_data/spot_dataset/dataset.yaml, # 数据配置文件路径epochs60, # 训练轮数imgsz640, # 输入图片尺寸batch16, # 批次大小根据显存大小调整namespot_detection60 # 保存运行结果的名称
)推测代码
from ultralytics import YOLO#加载已经训练好的模型
trained_model YOLO(E:/skin_yolo/runs/detect/spot_detection60/weights/best.pt) # 加载最优权重#进行推理测试
results trained_model.predict(
sourceE:/skin_data/spot_dataset/images/val, # 推理图片的路径支持文件夹、单个图片、视频等conf0.5, # 置信度阈值saveTrue, # 保存结果到运行目录save_txtTrue, # 保存结果到文本文件imgsz640 # 推理图片尺寸)
#输出推理结果
print(results)觉着标注框和注释太粗大了那么修改一下推测
from ultralytics import YOLO
import cv2
import os# Load the trained YOLO model
trained_model YOLO(E:/skin_yolo/runs/detect/spot_detection60/weights/best.pt)# Define directories
input_dir E:/skin_data/spot_dataset/images/val/ # Directory containing images to infer
output_dir E:/skin_yolo/runs/detect/result2/ # Directory to save inference results
os.makedirs(output_dir, exist_okTrue) # Create output directory if it doesnt exist# Custom settings for bounding boxes
box_color (0, 255, 0) # Green color for the bounding box
line_thickness 1 # Thickness of the bounding box lines# Iterate through all images in the input directory
for filename in os.listdir(input_dir):# Process only image filesif filename.endswith((.jpg, .jpeg, .png, .bmp)):image_path os.path.join(input_dir, filename)# Run inferenceresults trained_model.predict(sourceimage_path, conf0.3, saveFalse)# Get the first (and usually only) resultresult results[0] # Access the first result in the list# Get the original image from the resultimg result.orig_img# Accessing the detection boxes and labelsboxes result.boxes # This contains the detection boxesclass_ids boxes.cls # Class indices for each boxconfidences boxes.conf # Confidence scores for each detection# Draw bounding boxes on the imagefor i in range(len(boxes)):# Get the coordinates of the box (x1, y1, x2, y2) from xyxy formatx1, y1, x2, y2 boxes.xyxy[i].int().tolist() # Convert tensor to list of integers# Draw the bounding boxcv2.rectangle(img, (x1, y1), (x2, y2), box_color, line_thickness)# Get the label (class name) and confidencelabel f{trained_model.names[int(class_ids[i])]} {confidences[i]:.2f}# Put the label on the image#cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, box_color, 2)# Save the result imageresult_image_path os.path.join(output_dir, fresult_{filename})cv2.imwrite(result_image_path, img) # Save image with bounding boxesprint(fSaved result for {filename} at {result_image_path})print(Inference for all images in the folder is complete.)show一张处理结果我的“目标橙”都检测到了~ 3涉及的代码附件 jason2txt
import os
import json# 类别名称与ID的映射
classes [ix] # 根据你的类别修改def convert_labelme_to_yolo(json_dir, output_dir):os.makedirs(output_dir, exist_okTrue)for file in os.listdir(json_dir):if not file.endswith(.json):continue# 读取 JSON 文件json_path os.path.join(json_dir, file)with open(json_path, r, encodingutf-8) as f:data json.load(f)# 调试输出检查 JSON 数据结构print(f正在处理文件: {file})print(fJSON 数据: {data})image_width data.get(imageWidth, 0)image_height data.get(imageHeight, 0)# 检查图像尺寸print(f图像宽度: {image_width}, 图像高度: {image_height})if image_width 0 or image_height 0:print(f错误图像尺寸信息丢失: {file})continueyolo_labels []for shape in data[shapes]:label shape[label]if label not in classes:print(f未识别的类别: {label}, 请在 classes 中添加该类别。)continue# 获取类别 IDclass_id classes.index(label)# 解析边界框的点points shape[points]print(f标注类型: {label}, 标注坐标: {points})if len(points) 2: # 如果不是矩形框可能需要其他处理print(f警告{file} 中的标注无效跳过。)continuex_min, y_min points[0]x_max, y_max points[1]# 计算 YOLO 格式的中心点和宽高归一化x_center (x_min x_max) / 2 / image_widthy_center (y_min y_max) / 2 / image_heightwidth (x_max - x_min) / image_widthheight (y_max - y_min) / image_heightyolo_labels.append(f{class_id} {x_center:.6f} {y_center:.6f} {width:.6f} {height:.6f})# 检查是否有标注数据if not yolo_labels:print(f警告文件 {file} 没有有效的标注数据。)continue# 保存到 YOLO 格式的 .txt 文件output_file os.path.join(output_dir, file.replace(.json, .txt))with open(output_file, w) as f:f.write(\n.join(yolo_labels))print(f已生成: {output_file})# 使用示例
json_dir E:/skin_data/json/ # Labelme JSON 文件路径
output_dir E:/skin_data/yolo_labels/ # YOLO 格式标注文件保存路径
convert_labelme_to_yolo(json_dir, output_dir)输入图像具有多样性而标注输出则具有一定的相似性。一般标注的图像输出文件数是小于参与标注输入图像数目的(输入图像中有混入无目标的图像所以可能需要基于标注完的jason文件数目来反选出适合接入训练的数据。 copypic2dest.py
import os
import shutildef copy_images_from_json(json_folder, image_folder, target_folder, image_extension.jpg):# 创建目标文件夹如果不存在的话os.makedirs(target_folder, exist_okTrue)# 遍历 JSON 文件夹中的所有 .json 文件for json_file in os.listdir(json_folder):if json_file.endswith(.json):# 获取不带扩展名的文件名file_name_without_extension os.path.splitext(json_file)[0]# 查找对应的图片文件假设图片扩展名为 .jpg 或其他格式image_file file_name_without_extension image_extensionimage_path os.path.join(image_folder, image_file)# 检查图片文件是否存在if os.path.exists(image_path):# 复制图片到目标文件夹target_image_path os.path.join(target_folder, image_file)shutil.copy(image_path, target_image_path)print(f已复制图片: {image_file} 到目标路径)else:print(f未找到对应的图片: {image_file})# 使用示例
json_folder E:/skin_data/json/ # JSON 文件所在的文件夹
image_folder E:/skin_data/pic/ # 所有原图片所在的文件夹
target_folder E:/skin_data/yolo_img/ # 符合需求的目标文件夹# 调用函数进行拷贝
copy_images_from_json(json_folder, image_folder, target_folder, image_extension.jpg)