yolov5的可视化界面如果接工业相机进行检测 ,这种视频流检测怎么才能实现在视频流中每隔1s抽取一张带检测框的图片进行保存啊,还有就是怎么再把检测出来的和没检测出来的图片分别存入不同文件夹啊
不知道你这个问题是否已经解决, 如果还没有解决的话:所以我们就需要分析一下数据的指标图如下:
Box : Giou loss 作为预测框与真实框的损失,通俗一点的话就是损失函数越小边框的预测准确率越高。
Objects:目标检测损失,越小越准确‘
classification:类别损失函数,我这里只是一个类别所以没有,越小的话类别准确率越高。
Precision:精度(找对的正类/所有找到的正类);
①. 真阳性(True Positive,TP):样本的真实类别是正例,并且模型预测的结果也是正例,预测正确
②. 真阴性(True Negative,TN):样本的真实类别是负例,并且模型将其预测成为负例,预测正确
③. 假阳性(False Positive,FP):样本的真实类别是负例,但是模型将其预测成为正例,预测错误
④. 假阴性(False Negative,FN):样本的真实类别是正例,但是模型将其预测成为负例,预测错误
原文链接:https://blog.csdn.net/m0_54111890/article/details/123362039
Recall:召回率(找对的正类/所有本应该被找对的正类);
mAP@0.5 & mAP@0.5:0.95:就是mAP是用Precision和Recall作为两轴作图后围成的面积,m表示平均,@后面的数表示判定iou为正负样本的阈值,@0.5:0.95表示阈值取0.5:0.05:0.95后取均值。(0.5是iou阈值=0.5时mAP的值),mAP只是一个形容PR曲线面积的代替词叫做平均准确率,越高越好。
3.2如何提高训练的效果(mAP)大家可以看一下
这篇对于这个问题,我可以给出以下解决方案:
以下是具体的实现步骤:
import cv2
cap = cv2.VideoCapture(0) # 0代表连接本机相机
while True:
ret, frame = cap.read() # 读取视频流中的一帧画面
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'): # 按下q键退出循环
break
cap.release() # 释放资源
cv2.destroyAllWindows() # 关闭所有的窗口
import cv2
import torch
from models.experimental import attempt_load
from utils.general import check_img_size, non_max_suppression, scale_coords
from utils.torch_utils import select_device
# 加载yolov5模型
weights = 'yolov5s.pt'
device = select_device('')
model = attempt_load(weights, map_location=device)
# 获取一帧画面并进行检测
def detect(frame):
imgsz = check_img_size(640, s=model.stride.max()) # check img_size
img = letterbox(frame, new_shape=imgsz)[0]
img = img.transpose(2, 0, 1) # HWC to CHW
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(device)
img /= 255.0 # normalize to 0-1
if img.ndimension() == 3:
img = img.unsqueeze(0)
# 推理过程
pred = model(img.float())[0]
pred = non_max_suppression(pred, 0.25, 0.45) # remove other classes
# 将boxes还原到原始画面上
for i, det in enumerate(pred):
if len(det):
# Rescale boxes from img_size to im0 size
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], frame.shape).round()
return det
cap = cv2.VideoCapture(0) # 0代表连接本机相机
while True:
ret, frame = cap.read() # 读取视频流中的一帧画面
result = detect(frame) # 使用yolov5模型进行检测
# 将检测到目标的帧保存为图片,并分别存储到检测出来和未检测出来的文件夹中
if result is not None:
cv2.imwrite('detected/frame.jpg', frame)
else:
cv2.imwrite('undetected/frame.jpg', frame)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'): # 按下q键退出循环
break
cap.release() # 释放资源
cv2.destroyAllWindows() # 关闭所有的窗口
import cv2
import torch
from models.experimental import attempt_load
from utils.general import check_img_size, non_max_suppression, scale_coords
from utils.torch_utils import select_device
# 加载yolov5模型
weights = 'yolov5s.pt'
device = select_device('')
model = attempt_load(weights, map_location=device)
# 获取一帧画面并进行检测
def detect(frame):
imgsz = check_img_size(640, s=model.stride.max()) # check img_size
img = letterbox(frame, new_shape=imgsz)[0]
img = img.transpose(2, 0, 1) # HWC to CHW
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(device)
img /= 255.0 # normalize to 0-1
if img.ndimension() == 3:
img = img.unsqueeze(0)
# 推理过程
pred = model(img.float())[0]
pred = non_max_suppression(pred, 0.25, 0.45) # remove other classes
# 将boxes还原到原始画面上
for i, det in enumerate(pred):
if len(det):
# Rescale boxes from img_size to im0 size
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], frame.shape).round()
return det
cap = cv2.VideoCapture(0) # 0代表连接本机相机
detected_folder = 'detected' # 检测出来的帧存储的文件夹
undetected_folder = 'undetected' # 没有检测出来的帧存储的文件夹
while True:
ret, frame = cap.read() # 读取视频流中的一帧画面
result = detect(frame) # 使用yolov5模型进行检测
# 将检测到目标的帧保存为图片,并分别存储到检测出来和未检测出来的文件夹中
if result is not None:
cv2.imwrite('{}/frame{}.jpg'.format(detected_folder, cap.get(1)), frame)
else:
cv2.imwrite('{}/frame{}.jpg'.format(undetected_folder, cap.get(1)), frame)
cv2.imshow('frame', frame)
if cv2.waitKey(1000) & 0xFF == ord('q'): # 每隔1秒保存一张图片,按下q键退出循环
break
cap.release() # 释放资源
cv2.destroyAllWindows() # 关闭所有的窗口