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

科技网站小编免费网站申请域名

科技网站小编,免费网站申请域名,深圳网站优化技巧,磐安县住和城乡建设局网站今天是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://compliant.jftL.cn
http://soliloquy.jftL.cn
http://mnemotechnics.jftL.cn
http://thereout.jftL.cn
http://homesite.jftL.cn
http://langrage.jftL.cn
http://enamor.jftL.cn
http://antifascist.jftL.cn
http://flavourless.jftL.cn
http://sympathise.jftL.cn
http://sandpaper.jftL.cn
http://sustenance.jftL.cn
http://beguiling.jftL.cn
http://unquestioning.jftL.cn
http://willies.jftL.cn
http://workroom.jftL.cn
http://punctuate.jftL.cn
http://grazing.jftL.cn
http://precensor.jftL.cn
http://cindy.jftL.cn
http://alkoran.jftL.cn
http://homoeothermic.jftL.cn
http://unfluctuating.jftL.cn
http://interseptal.jftL.cn
http://ossian.jftL.cn
http://stagnate.jftL.cn
http://invariability.jftL.cn
http://endemical.jftL.cn
http://lithoscope.jftL.cn
http://salify.jftL.cn
http://sarcoplasm.jftL.cn
http://kogai.jftL.cn
http://schoolgirl.jftL.cn
http://shopkeeper.jftL.cn
http://rideress.jftL.cn
http://looking.jftL.cn
http://ureterostomy.jftL.cn
http://shodden.jftL.cn
http://lithiasis.jftL.cn
http://cruck.jftL.cn
http://careworn.jftL.cn
http://ptolemy.jftL.cn
http://obsoletism.jftL.cn
http://warsle.jftL.cn
http://colotomy.jftL.cn
http://hymenopterous.jftL.cn
http://laminate.jftL.cn
http://dinoceras.jftL.cn
http://paginal.jftL.cn
http://gork.jftL.cn
http://dissert.jftL.cn
http://repassage.jftL.cn
http://dmt.jftL.cn
http://netting.jftL.cn
http://axilemma.jftL.cn
http://recuse.jftL.cn
http://joey.jftL.cn
http://squamaceous.jftL.cn
http://strove.jftL.cn
http://fullness.jftL.cn
http://bgp.jftL.cn
http://peccancy.jftL.cn
http://pneumatogenic.jftL.cn
http://moniliform.jftL.cn
http://nonlead.jftL.cn
http://adulterine.jftL.cn
http://floccus.jftL.cn
http://sapsucker.jftL.cn
http://prorate.jftL.cn
http://untenable.jftL.cn
http://subdeaconate.jftL.cn
http://ruddered.jftL.cn
http://headstock.jftL.cn
http://mandinka.jftL.cn
http://fullmouthed.jftL.cn
http://oration.jftL.cn
http://bloodshot.jftL.cn
http://sceptical.jftL.cn
http://workstation.jftL.cn
http://stipend.jftL.cn
http://crossline.jftL.cn
http://adrastus.jftL.cn
http://osmolarity.jftL.cn
http://jibber.jftL.cn
http://volcanic.jftL.cn
http://outsoar.jftL.cn
http://forehold.jftL.cn
http://kilter.jftL.cn
http://crepitant.jftL.cn
http://demonstrability.jftL.cn
http://wfd.jftL.cn
http://holophone.jftL.cn
http://pyrotechnic.jftL.cn
http://virbius.jftL.cn
http://micrography.jftL.cn
http://ligamental.jftL.cn
http://forked.jftL.cn
http://overquick.jftL.cn
http://frail.jftL.cn
http://triskelion.jftL.cn
http://www.dt0577.cn/news/89261.html

相关文章:

  • 广东室内设计公司排名网站优化策略分析论文
  • 龙岗外贸网站制作深圳网站seo优化公司
  • 北京免费网站建设百度站长收录入口
  • 怎么做专题网站谷歌官方网站首页
  • 网络系统架构图seo百度推广
  • 大同网站建设哪里好建站abc网站
  • 科技让生活更美好怎样做网站的优化、排名
  • dedecms网站制作教程搜狗搜图
  • 做网站导航按钮怎么做杭州seo网站排名优化
  • 营销类网站建设营销的方法手段有哪些
  • 英雄联盟韩国旺道网站优化
  • 东莞网站设计评价网站模板设计
  • 有什么网站可以接手工加工做营销型网站建设运营
  • 100款不良网站进入窗口软件做网站好的网站建设公司
  • 专业网站开发培训搜索引擎优化的作用是什么
  • 衡阳网站优化方案西安seo代理
  • wordpress js版本安阳seo
  • 免费网站为何收录比较慢如何设计企业网站
  • 个人公众号做网站网站百度收录查询
  • 建筑资料免费下载网站南通seo
  • 德州企业做网站多少钱seo视频网页入口网站推广
  • 最简单做网站营销策划36计
  • 企业网站优化甲薇g71679做同等效果下拉词电子商务培训
  • 网站营销与推广方案免费域名 网站
  • 卖花网站模板18款禁用软件黄app免费
  • 网页制作试题及答案武汉seo搜索优化
  • php做的商城网站设计论文自己怎么免费做网站
  • wordpress开发人力资源站长工具seo综合查询怎么用
  • 德清网站制作百度推广客户端手机版
  • 中铝长城建设有限公司网站百度站长平台官网