window10下打开摄像头实现Pytorch-YOLOv3的实时监测
python+OpenCV+YOLOv3打开笔记本摄像头模型检测
基于yoloV3的目标检测
社交距离检测器——Tensorflow检测模型设计
import numpy as np
import cv2
def video_demo():
# 加载已经训练好的模型路径,可以是绝对路径或者相对路径
weightsPath = r'D:\YOLO\darknet-master\build\darknet\x64\yolov3.weights'
configPath = r"D:\YOLO\darknet-master\cfg\yolov3.cfg"
labelsPath = r"D:\YOLO\darknet-master\data\coco.names"
# 初始化一些参数
LABELS = open(labelsPath).read().strip().split("\n") # 物体类别
COLORS = np.random.randint(0, 255, size=(len(LABELS), 3), dtype="uint8") # 颜色
net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)
# net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
# net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
# 读入待检测的图像
# 0是代表摄像头编号,只有一个的话默认为0
# capture = cv2.VideoCapture(0)
capture = cv2.VideoCapture(r'C:\Users\lenovo\Desktop\shipin\01.mp4')
# 读入待检测的图像
# 0是代表摄像头编号,只有一个的话默认为0
yolo_num = 1
while (True):
boxes = []
confidences = []
classIDs = []
print("yolo%d" % yolo_num)
yolo_num = yolo_num + 1
ref, image = capture.read()
#image = cv2.resize(image, (300, 300), fx=0.25, fy=0.25)
(H, W) = image.shape[:2]
# 得到 YOLO需要的输出层
ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]
# 从输入图像构造一个blob,然后通过加载的模型,给我们提供边界框和相关概率
blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
layerOutputs = net.forward(ln)
# 在每层输出上循环
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)
# 极大值抑制
idxs = cv2.dnn.NMSBoxes(boxes, confidences, 0.2, 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]]]
if LABELS[classIDs[i]] == "person": ###只检测人
print(" Label: %s:(x,y,w,h)=(%d,%d,%d,%d)" % (LABELS[classIDs[i]], x, y, w, h))
cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
text = '{}: {:.3f}'.format(LABELS[classIDs[i]], confidences[i])
(text_w, text_h), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)
cv2.rectangle(image, (x, y - text_h - baseline), (x + text_w, y), color, -1)
cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2)
#cv2.rectangle(image, (x, y), (x + w, y + h), color, 2) ##左上,右下
cv2.imshow("Image", image)
c = cv2.waitKey(1) & 0xff
if c == 27:
capture.release()
break
cv2.destroyAllWindows()
video_demo()
根据框的质心或者框的底部中心点,检测两点之间的距离。
if math.sqrt(math.pow((pair[0][0] - pair[1][0]), 2) + math.pow((pair[0][1] - pair[1][1]), 2)) < int(distance_minimum) :
index_pt1 = list_indexes[i][0]
index_pt2 = list_indexes[i][1]
change_color_originalframe(index_pt1,index_pt2)
import numpy as np
import cv2
import itertools
import math
def video_demo():
# 加载已经训练好的模型路径,可以是绝对路径或者相对路径
weightsPath = r'D:\YOLO\darknet-master\build\darknet\x64\yolov3.weights'
configPath = r"D:\YOLO\darknet-master\cfg\yolov3.cfg"
labelsPath = r"D:\YOLO\darknet-master\data\coco.names"
# 初始化一些参数
LABELS = open(labelsPath).read().strip().split("\n") # 物体类别
#COLORS = np.random.randint(0, 255, size=(len(LABELS), 3), dtype="uint8") # 颜色
net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)
# net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
# net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
capture = cv2.VideoCapture(r'C:/Users/lenovo/Desktop/shipin/test.mp4')
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(r'C:\Users\lenovo\Desktop\shipin\camera_test2.mp4', fourcc, 24.0, (768, 576))
yolo_num = 1
while (True):
boxes = []
confidences = []
classIDs = []
print("yolo%d" % yolo_num)
yolo_num = yolo_num + 1
a = 1
ret, image = capture.read()
if ret is False:
break
(H, W) = image.shape[:2] #图片的大小
# 得到 YOLO需要的输出层
ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]
# 从输入图像构造一个blob,然后通过加载的模型,给我们提供边界框和相关概率
blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
layerOutputs = net.forward(ln)
# 在每层输出上循环
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)
# 极大值抑制
idxs = cv2.dnn.NMSBoxes(boxes, confidences, 0.2, 0.3)
if len(idxs) > 0:
transformed_downoids = []
transformed_g_list = []
for i in idxs.flatten():
(x, y) = (boxes[i][0], boxes[i][1])
(w, h) = (boxes[i][2], boxes[i][3])
if LABELS[classIDs[i]] == "person": ###只检测人
print(" Label: %s:(x,y,w,h,a)=(%d,%d,%d,%d,%d)" % (LABELS[classIDs[i]], x, y, w, h, a))
#print(" Label: %s:(x,y,w,h)=(%d,%d,%d,%d)" % (LABELS[classIDs[i]], x, y, w, h))
a = a + 1
cv2.rectangle(image, (x, y), (x + w, y + h), (0,255,0), 2)
text = '{}: {:.3f}'.format(LABELS[classIDs[i]], confidences[i])
(text_w, text_h), baseline = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)
cv2.rectangle(image, (x, y - text_h - baseline), (x + text_w, y), (255,0,0), -1)
cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2)
#cv2.rectangle(image, (x, y), (x + w, y + h), color, 2) ##左上,右下
p = x + w / 2.
q = y + h / 2.
list_downoids = [(p, q)]
d = list_downoids
for f in d:
transformed_downoids.append(f)
corner_points_list = [(x, y), (x + w, y + h)]
for o in corner_points_list:
transformed_g_list.append(o)
distance_minimum = 100
if len(transformed_downoids) >= 2:
# Iterate over every possible 2 by 2 between the points
list_indexes = list(itertools.combinations(range(len(transformed_downoids)), 2))
for i,pair in enumerate(itertools.combinations(transformed_downoids, r=2)):
# if math.sqrt( (pair[0][0] - pair[1][0])**2 + (pair[0][1] - pair[1][1])**2 ) < int(distance_minimum):
if math.sqrt(math.pow((pair[0][0] - pair[1][0]), 2) + math.pow((pair[0][1] - pair[1][1]), 2)) < int(distance_minimum) :
index_pt1 = list_indexes[i][0]
index_pt2 = list_indexes[i][1]
index_pt1_1 = index_pt1 * 2
index_pt2_1 = index_pt2 * 2
cv2.rectangle(image, transformed_g_list[index_pt1_1], transformed_g_list[index_pt1_1 + 1],(0, 0, 255), 2)
cv2.rectangle(image, transformed_g_list[index_pt2_1], transformed_g_list[index_pt2_1 + 1],(0, 0, 255), 2)
out.write(image)
cv2.imshow("Image", image)
c = cv2.waitKey(33) & 0xff
if c == 27:
break
capture.release()
# out.release()
cv2.destroyAllWindows()
video_demo()
可参考The Multiple Object Tracking Benchmark!