当前位置: 首页 > news >正文

科技网站小编seo商城

科技网站小编,seo商城,黄冈网站推广代运营,江苏住房和城乡建设厅网站首页今天是24年的最后一天,终于要向新世界开始破门了,开始深度学习,YOLO来敲门~ 最近做了一些皮肤检测的功能,在传统的处理中经历了反复挣扎,终于要上YOLO了。听过、看过,不如上手体会过~ 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(data="E:/skin_data/spot_dataset/dataset.yaml",  # 数据配置文件路径epochs=60,                   # 训练轮数imgsz=640,                  # 输入图片尺寸batch=16,                     # 批次大小,根据显存大小调整name="spot_detection60"        # 保存运行结果的名称
)

推测代码

from ultralytics import YOLO#加载已经训练好的模型
trained_model = YOLO("E:/skin_yolo/runs/detect/spot_detection60/weights/best.pt")  # 加载最优权重#进行推理测试
results = trained_model.predict(
source="E:/skin_data/spot_dataset/images/val",  # 推理图片的路径(支持文件夹、单个图片、视频等)conf=0.5,                     # 置信度阈值save=True,                    # 保存结果到运行目录save_txt=True,                # 保存结果到文本文件imgsz=640                    # 推理图片尺寸)
#输出推理结果
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_ok=True)  # Create output directory if it doesn't 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(source=image_path, conf=0.3, save=False)# 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, f"result_{filename}")cv2.imwrite(result_image_path, img)  # Save image with bounding boxesprint(f"Saved 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_ok=True)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', encoding='utf-8') as f:data = json.load(f)# 调试输出,检查 JSON 数据结构print(f"正在处理文件: {file}")print(f"JSON 数据: {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_ok=True)# 遍历 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")
http://www.dinnco.com/news/21032.html

相关文章:

  • 小程序wordpress视频短视频优化
  • 如何提高网站的用户体验ue南宁seo
  • 怎么做cc网站微信营销的方法7种
  • 香港网站怎么做QQ第三方登录山东企业网站建设
  • 有做兼职赚钱的网站吗最近一周新闻热点回顾
  • 专业推广app团队网站优化排名哪家好
  • 公司网站开发可行性报告电脑培训班速成班
  • 萧山住房和城乡建设委员会网站企业邮箱登录
  • 衡阳网站备案tool站长工具
  • 百度如何建网站群seo排名软件价格
  • 苏州工业园区疫情最新消息seo免费优化工具
  • 企业如何全面开展品牌工程建设网络营销优化推广
  • wordpress的上传大小网站搜索引擎优化方案的案例
  • 宁夏百度网站怎么做百度seo可能消失
  • 网页制作与网站建设技术大全南京网络优化培训
  • 日照疫情最新情况优化网站
  • 安徽建设工程信息网网快速优化seo软件
  • ps 做儿童摄影网站首页免费广州seo
  • seo关键词优化到首页seo关键词优化是什么意思
  • 专门做图片的网站吗重庆森林百度网盘
  • cms网站建设系统杭州seo代理公司
  • 营销网站用户体验有哪些西安seo站内优化
  • 帮别人做诈骗网站获利 判刑网站收录情况查询
  • 国内网站建设哪家好建站平台哪个比较权威
  • 垃圾网站怎么做的外贸如何推广
  • 简洁网站欣赏2024会爆发什么病毒
  • 网站建设怎么申请空间广州网络推广外包
  • 单页产品销售网站如何做推广广州外贸推广
  • 优化型网站建设锦州seo推广
  • 免费社区建站系统企业广告宣传