河南郑州建设网站,建设网站商城需要多少费用吗,wordpress 设置缩略图,小程序商城名字目标检测就是对目标进行动态实时跟踪定位#xff0c;常见的目标检测算法有 R-CNN、Fast R-CNN、Faster R-CNN、SSD、Yolo 等#xff0c;其中 Yolo 的速度和精确度都比较高#xff0c;且只需训练一次#xff0c;使用起来比较方便。 
这里我们就使用官方现成的模型来检测图片…目标检测就是对目标进行动态实时跟踪定位常见的目标检测算法有 R-CNN、Fast R-CNN、Faster R-CNN、SSD、Yolo 等其中 Yolo 的速度和精确度都比较高且只需训练一次使用起来比较方便。 
这里我们就使用官方现成的模型来检测图片看一看效果先学会使用流程以后再训练自己的模型。 
注意opencv-python 目前只有 4.4.0 版本适配了 YOLOv4 导入库 
import numpy as np
import time
import cv2123设置标签和标注颜色 
LABELS  open(coco.names).read().strip().split(\n)
np.random.seed(666)
COLORS  np.random.randint(0, 255, size(len(LABELS), 3), dtypeuint8)123
coco.names 内包含了很多目标标签如 person、bicycle、car 等且按一定顺序排列里面基本包含了 Yolo 官方模型中可检测的对象。该文件可从以下链接中提取https://gitee.com/public_sharing/ObjectDetection-YOLO/blob/master/coco.names 
每个对象配备了不一样的颜色以便在图片中标记时便于区分。 加载网络 
# 导入 YOLO 配置和权重文件并加载网络
net  cv2.dnn_DetectionModel(yolov4.cfg, yolov4.weights)
# 获取 YOLO 未连接的输出图层
layer  net.getUnconnectedOutLayersNames()1234
yolov4.cfg 和 yolov4.weights 文件就是官方提供的模型下载链接https://pan.baidu.com/s/1XrcPHdp2_4c-dKge2Guw4w 提取码xsxb 。如果失效可以直接百度搜索 Yolov4模型下载有很多人都分享出来了。 
cv2.dnn_DetectionModel 是 opencv 4.1.2 开始新增的方法用于加载网络。以前是使用 cv2.dnn.readNetFromDarknet 此处使用也可以达到同样的效果。 
getUnconnectedOutLayersNames() 用于提取输出图层的名称yolo 含有很多的图层可以使用 getLayerNames() 将所有图层的名称提取出来。但在这里我们只需要使用 yolo 的最后输出图层。 检测图片 
# 导入图片
image  cv2.imread(timg.jpg)
# 获取图片尺寸
(H, W)  image.shape[:2]# 从输入图像构造一个 blob然后执行 YOLO 对象检测器的前向传递给我们边界盒和相关概率
blob  cv2.dnn.blobFromImage(image, 1/255.0, (416, 416),swapRBTrue, cropFalse)
net.setInput(blob)
start  time.time()
# 前向传递获得信息
layerOutputs  net.forward(layer)
# 用于得出检测时间
end  time.time()
print([INFO] YOLO took {:.6f} seconds.format(end - start))123456789101112131415
blobFromImage 用于对图像进行预处理cv2.dnn.blobFromImage(image[, scalefactor[, size[, mean[, swapRB[, crop[, ddepth]]]]]]) 
image输入图像scalefactor图像各通道数值的缩放比例size输出图像的空间尺寸mean用于各通道减去的值以降低光照的影响swapRB交换 RB 通道默认为 Falsecrop图像裁剪默认为 False。当值为 True 时先按比例缩放然后从中心裁剪成 size 尺寸ddepth输出的图像深度可选 CV_32F 或者 CV_8U数据提取 
boxes  []
confidences  []
classIDs  []# 循环提取每个输出层
for output in layerOutputs:# 循环提取每个框for detection in output:# 提取当前目标的类 ID 和置信度scores  detection[5:]classID  np.argmax(scores)confidence  scores[classID]# 通过确保检测概率大于最小概率来过滤弱预测if confidence  0.5:# 将边界框坐标相对于图像的大小进行缩放YOLO 返回的是边界框的中心(x, y)坐标# 后面是边界框的宽度和高度box  detection[0:4] * np.array([W, H, W, H])(centerX, centerY, width, height)  box.astype(int)# 转换出边框左上角坐标x  int(centerX - (width / 2))y  int(centerY - (height / 2))# 更新边界框坐标、置信度和类 id 的列表boxes.append([x, y, int(width), int(height)])confidences.append(float(confidence))classIDs.append(classID)12345678910111213141516171819202122232425
3 个列表内保存的内容 
boxes对象的边界框confidences YOLO 分配给对象的置信度值较低的置信度值表示该对象可能不是网络认为的对象。上面的代码中将过滤掉小于 0.5 阈值的对象classIDs检测到的对象的类标签
这样每个被提取出的对象都确定了标签和区域坐标就、位置。接下来就是在图片中标记出来便于我们观看。 标记显示 
# 非最大值抑制确定唯一边框
idxs  cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.3)
# 确定每个对象至少有一个框存在
if len(idxs)  0:# 循环画出保存的边框for i in idxs.flatten():# 提取坐标和宽度(x, y)  (boxes[i][0], boxes[i][1])(w, h)  (boxes[i][2], boxes[i][3])# 画出边框和标签color  [int(c) for c in COLORS[classIDs[i]]]cv2.rectangle(image, (x, y), (x  w, y  h), color, 1, lineTypecv2.LINE_AA)text  {}: {:.4f}.format(LABELS[classIDs[i]], confidences[i])cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX,0.5, color, 1, lineTypecv2.LINE_AA)
cv2.imshow(Tag, image)
cv2.waitKey(0)1234567891011121314151617
对于每个对象Yolo 会框出 3 个左右的区域我们只需要显示出最合适的区域。非最大值抑制就是搜索出局部最大值将置信度最大的框保存其余剔除。 
cv2.dnn.NMSBoxes(bboxes, scores, score_threshold, nms_threshold, etaNone, top_kNone) 
bboxes一组边框scores一组对应的置信度score_threshold置信度的阈值nms_threshold非最大抑制的阈值
之后将每个对象的方框和标签都画出来 
结果展示 完整代码 
import numpy as np
import time
import cv2LABELS  open(coco.names).read().strip().split(\n)
np.random.seed(666)
COLORS  np.random.randint(0, 255, size(len(LABELS), 3), dtypeuint8)
# 导入 YOLO 配置和权重文件并加载网络
net  cv2.dnn_DetectionModel(yolov4.cfg, yolov4.weights)
# 获取 YOLO 未连接的输出图层
layer  net.getUnconnectedOutLayersNames()
image  cv2.imread(timg.jpg)
# 获取图片尺寸
(H, W)  image.shape[:2]
# 从输入图像构造一个 blob然后执行 YOLO 对象检测器的前向传递给我们边界盒和相关概率
blob  cv2.dnn.blobFromImage(image, 1/255.0, (416, 416),swapRBTrue, cropFalse)
net.setInput(blob)
start  time.time()
# 前向传递获得信息
layerOutputs  net.forward(layer)
# 用于得出检测时间
end  time.time()
print(YOLO took {:.6f} seconds.format(end - start))boxes  []
confidences  []
classIDs  []# 循环提取每个输出层
for output in layerOutputs:# 循环提取每个框for detection in output:# 提取当前目标的类 ID 和置信度scores  detection[5:]classID  np.argmax(scores)confidence  scores[classID]# 通过确保检测概率大于最小概率来过滤弱预测if confidence  0.5:# 将边界框坐标相对于图像的大小进行缩放YOLO 返回的是边界框的中心(x, y)坐标# 后面是边界框的宽度和高度box  detection[0:4] * np.array([W, H, W, H])(centerX, centerY, width, height)  box.astype(int)# 转换出边框左上角坐标x  int(centerX - (width / 2))y  int(centerY - (height / 2))# 更新边界框坐标、置信度和类 id 的列表boxes.append([x, y, int(width), int(height)])confidences.append(float(confidence))classIDs.append(classID)
# 非最大值抑制确定唯一边框
idxs  cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.3)
# 确定每个对象至少有一个框存在
if len(idxs)  0:# 循环画出保存的边框for i in idxs.flatten():# 提取坐标和宽度(x, y)  (boxes[i][0], boxes[i][1])(w, h)  (boxes[i][2], boxes[i][3])# 画出边框和标签color  [int(c) for c in COLORS[classIDs[i]]]cv2.rectangle(image, (x, y), (x  w, y  h), color, 1, lineTypecv2.LINE_AA)text  {}: {:.4f}.format(LABELS[classIDs[i]], confidences[i])cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX,0.5, color, 1, lineTypecv2.LINE_AA)
cv2.imshow(Tag, image)
cv2.waitKey(0)