#yolo算法提取非机动车并进行截图
import numpy as np
import time
import cv2
import os
import matplotlib as plt
import skimage
labelsPath = r"E:\AI_projects\object_detect\YOLOV4(have_done)\coco.names"
weightsPath = r"E:\AI_projects\object_detect\YOLOV4(have_done)\yolov3.weights"
configPath = r"E:\AI_projects\object_detect\YOLOV4(have_done)\yolov3.cfg"
#读取图片并
img_path=r'E:\AI_projects\object_detect\YOLOV4(have_done)\dataset\motor2.jpg'
# image=skimage.io.imread(img_path)
# image=plt.image.imread(img_path)
image0 = cv2.imread(img_path)
image0=cv2.resize(image0,(1200,800))
image=image0
#将可能含有4通道(250,250,4)带有透明度的图像转换成3通道(250,250,3),模型只接受3通道图像
image=cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
#锐化
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32)
image = cv2.filter2D(image, -1, kernel=kernel)
#读取coco80种物体标签
LABELS = None
with open(labelsPath,'rt') as f:
LABELS = f.read().rstrip('\n').split("\n")
#初始化随机的颜色,这里主要是为后续检测到不同的对象,进行不同颜色的边框画图
np.random.seed(42)
COLORS = np.random.randint(0, 255, size=(len(LABELS), 3),dtype="uint8")
net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)
(H, W) = image.shape[:2]
#计算图片的blob值,输入神经网络,进行前向反馈预测图片,只不过net.forward里面是ln, 神经网络的所有out层
ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]
blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416),swapRB=True, crop=False)
net.setInput(blob)
start = time.time()
layerOutputs = net.forward(ln)
end = time.time()
print("[INFO] YOLO took {:.6f} seconds".format(end - start))
#我们过滤到置信度小于0.5的对象,提取大于0.5置信度的对象,
#分别计算每个检测对象的BOX、置信度以及label ID,并保存在如下先前建立的初始化参数list里面
boxes = []
confidences = []
classIDs = []
for output in layerOutputs:
for detection in output:
scores = detection[5:]
classID = np.argmax(scores)
confidence = scores[classID]
if confidence > 0.5:
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))
boxes.append([x, y, int(width), int(height)])
confidences.append(float(confidence))
classIDs.append(classID)
#对于每个对象,Yolo4 会框出 3 个左右的区域,但是我们只需要显示出最合适的区域。
#非最大值抑制算法NMS,就是搜索出局部最大值,将置信度最大的框保存,其余删除。
big_set=[]
idxs = cv2.dnn.NMSBoxes(boxes, confidences, 0.5,0.4)
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, 2)
text = "{}: {:.4f}".format(LABELS[classIDs[i]], confidences[i])
cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX,0.5, color, 2)
big_set.append([x,y,w,h,LABELS[classIDs[i]],confidences[i]])
cv2.imshow("Original Image", image0)
cv2.imshow("Image", image)
cv2.waitKey(0)
person=[]
motorbike=[]
for i in range(len(big_set)):
if big_set[i][4]=='motorbike':
motorbike.append(big_set[i])
if big_set[i][4]=='person':
person.append(big_set[i])
# print('person:',person)
# print('motorbike:',motorbike)
if len(person)>0 and len(motorbike)>0 :
for i in range(len(person)):
for j in range(len(motorbike)):
Ax=person[i][0]
Ay=person[i][1]
Aw=person[i][2]
Ah=person[i][3]
A1x=motorbike[j][0]
A1y=motorbike[j][1]
A1w=motorbike[j][2]
A1h=motorbike[j][3]
if (abs(Ax+Aw/2-A1x-A1w/2)<10 and abs(Ay+Ah/2-A1y-A1h/2)<200):
x=min(Ax,A1x)
y=min(Ay,A1y)
xw=max(Ax+Aw,A1x+A1w)
yh=max(Ay+Ah,A1y+A1h)
dst=image0[y-20:yh+20,x-20:xw+20]
cv2.imshow('imge',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
source.zip (6个子文件)
source
yolov3.weights 236.52MB
test.py 4KB
yolov3.cfg 8KB
coco.names 625B
motor3.jpg 300KB
motor2.jpg 255KB
共 6 条
- 1
quantam
- 粉丝: 74
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 使用 SSM(Spring MVC + Spring + MyBatis)框架实现申报项目信息管理系统实验报告
- 这本书深入探讨了MySQL数据库系统的内部工作原理,特别适合高级用户、数据库管理员和开发者,希望了解MySQL在低层次上如何运行
- 停车场车位自动检测系统电路图
- 所有指定格式的Excel文件的工作表合并到一个新的Excel文件中
- 从零开始三天学会微信小程序开发
- GZ099 集成电路应用开发赛题第1套
- 2024-“她经济”洞察报告-QuestMobile-41页
- 2024中国企业人力资源数字化转型现状与趋势报告
- 70个黑色常用PNG图标.zip
- C++ Builder 2010 绘制坐标
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0