我正在使用 opencv 和 YOLO python
the time of appearance of objects i am using opencv and YOLO python
我正在使用 python opencv 和 yolo 来检测等候室里的人,我想知道人们在等候室里呆了多少时间。
我的检测人员的代码有效,但我不知道如何计算人员出现的时间
时间是为出现在网络摄像头中的每个人计算的,一旦该人消失,该人的时间就会停止,并将他的时间发送回等候室
import cv2 as cv
import numpy as np
#Write down conf, nms thresholds,inp width/height
confThreshold = 0.25
nmsThreshold = 0.40
inpWidth = 416
inpHeight = 416
#Load names of classes and turn that into a list
classesFile = "coco.names"
classes = None
with open(classesFile,'rt') as f:
classes = f.read().rstrip('\n').split('\n')
#Model configuration
modelConf = 'yolov3.cfg'
modelWeights = 'yolov3.weights'
def postprocess(frame, outs):
frameHeight = frame.shape[0]
frameWidth = frame.shape[1]
classIDs = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection [5:]
classID = np.argmax(scores)
confidence = scores[classID]
if confidence > confThreshold:
centerX = int(detection[0] * frameWidth)
centerY = int(detection[1] * frameHeight)
width = int(detection[2]* frameWidth)
height = int(detection[3]*frameHeight )
left = int(centerX - width/2)
top = int(centerY - height/2)
classIDs.append(classID)
confidences.append(float(confidence))
boxes.append([left, top, width, height])
indices = cv.dnn.NMSBoxes (boxes,confidences, confThreshold, nmsThreshold )
indices = cv.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold)
for i in indices:
i = i[0]
box = boxes[i]
left = box[0]
top = box[1]
width = box[2]
height = box[3]
if classIDs[i] == 0 :
drawPred(classIDs[i], confidences[i], left, top, left + width, top + height)
def drawPred(classId, conf, left, top, right, bottom):
# Draw a bounding box.
cv.rectangle(frame, (left, top), (right, bottom), (255, 178, 50), 3)
label = '%.2f' % conf
# Get the label for the class name and its confidence
if classes:
assert (classId < len(classes))
label = '%s:%s' % (classes[classId],label)
#A fancier display of the label from learnopencv.com
# Display the label at the top of the bounding box
#labelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)
#top = max(top, labelSize[1])
#cv.rectangle(frame, (left, top - round(1.5 * labelSize[1])), (left + round(1.5 * labelSize[0]), top + baseLine),
#(255, 255, 255), cv.FILLED)
# cv.rectangle(frame, (left,top),(right,bottom), (255,255,255), 1 )
#cv.putText(frame, label, (left, top), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1)
cv.putText(frame, label, (left,top), cv.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 3)
def getOutputsNames(net):
# Get the names of all the layers in the network
layersNames = net.getLayerNames()
# Get the names of the output layers, i.e. the layers with unconnected outputs
return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]
#Set up the net
net = cv.dnn.readNetFromDarknet(modelConf, modelWeights)
net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)
#Process inputs
winName = 'DL OD with OpenCV'
cv.namedWindow(winName, cv.WINDOW_NORMAL)
cv.resizeWindow(winName, 1000,1000)
cap = cv.VideoCapture(0)
while cv.waitKey(1) < 0:
#get frame from video
hasFrame, frame = cap.read()
#Create a 4D blob from a frame
blob = cv.dnn.blobFromImage(frame, 1/255, (inpWidth, inpHeight), [0,0,0], 1, crop = False)
#Set the input the the net
net.setInput(blob)
outs = net.forward (getOutputsNames(net))
postprocess (frame, outs)
#show the image
cv.imshow(winName, frame)
因此,如果我理解正确的话,你的问题是关于计算 Python 中经过的时间。这与 OpenCV 或 YOLO 无关,可以通过以下方式实现:
import time
start_time = time.time()
# one eternity later
elapsed_time = time.time() - start_time
然后当新人进入房间时启动计时器,当有人离开时停止计时器。
根据您刚刚添加的代码,您需要跟踪对象。检测它们不足以计算它们的年龄。
我所说的跟踪是指您需要知道一个对象是新对象还是在过去的帧中存在过。
一旦您确定了所有对象,您就可以增加一个时间计数器。
当它们消失时,您可以查看计数器以了解它们停留了多长时间。
跟踪是一个很大的主题,我认为不能用一个 SO 线程来概括。
而且没有神奇的解决方案,你可以使用几种不同的算法,结果取决于你的视频输入。
算法简单
一个简单的方法是比较帧之间找到的对象的位置。例如,在第 0 帧,您在位置 p0 处有一个对象,您称它为 'A'。
在下一帧,你有两个对象,你计算它们与 p0 之间的距离。如果一个足够近(低于你应该确定的阈值),那么你可以认为它是你的对象 A。然后你可以增加它的年龄。并将第二个称为 B。
然后进入下一帧。如果在这一帧上没有足够接近对象A的最后位置的对象,则可以认为A消失并获取其年龄。
我正在使用 python opencv 和 yolo 来检测等候室里的人,我想知道人们在等候室里呆了多少时间。 我的检测人员的代码有效,但我不知道如何计算人员出现的时间
时间是为出现在网络摄像头中的每个人计算的,一旦该人消失,该人的时间就会停止,并将他的时间发送回等候室
import cv2 as cv
import numpy as np
#Write down conf, nms thresholds,inp width/height
confThreshold = 0.25
nmsThreshold = 0.40
inpWidth = 416
inpHeight = 416
#Load names of classes and turn that into a list
classesFile = "coco.names"
classes = None
with open(classesFile,'rt') as f:
classes = f.read().rstrip('\n').split('\n')
#Model configuration
modelConf = 'yolov3.cfg'
modelWeights = 'yolov3.weights'
def postprocess(frame, outs):
frameHeight = frame.shape[0]
frameWidth = frame.shape[1]
classIDs = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection [5:]
classID = np.argmax(scores)
confidence = scores[classID]
if confidence > confThreshold:
centerX = int(detection[0] * frameWidth)
centerY = int(detection[1] * frameHeight)
width = int(detection[2]* frameWidth)
height = int(detection[3]*frameHeight )
left = int(centerX - width/2)
top = int(centerY - height/2)
classIDs.append(classID)
confidences.append(float(confidence))
boxes.append([left, top, width, height])
indices = cv.dnn.NMSBoxes (boxes,confidences, confThreshold, nmsThreshold )
indices = cv.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold)
for i in indices:
i = i[0]
box = boxes[i]
left = box[0]
top = box[1]
width = box[2]
height = box[3]
if classIDs[i] == 0 :
drawPred(classIDs[i], confidences[i], left, top, left + width, top + height)
def drawPred(classId, conf, left, top, right, bottom):
# Draw a bounding box.
cv.rectangle(frame, (left, top), (right, bottom), (255, 178, 50), 3)
label = '%.2f' % conf
# Get the label for the class name and its confidence
if classes:
assert (classId < len(classes))
label = '%s:%s' % (classes[classId],label)
#A fancier display of the label from learnopencv.com
# Display the label at the top of the bounding box
#labelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)
#top = max(top, labelSize[1])
#cv.rectangle(frame, (left, top - round(1.5 * labelSize[1])), (left + round(1.5 * labelSize[0]), top + baseLine),
#(255, 255, 255), cv.FILLED)
# cv.rectangle(frame, (left,top),(right,bottom), (255,255,255), 1 )
#cv.putText(frame, label, (left, top), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1)
cv.putText(frame, label, (left,top), cv.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 3)
def getOutputsNames(net):
# Get the names of all the layers in the network
layersNames = net.getLayerNames()
# Get the names of the output layers, i.e. the layers with unconnected outputs
return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]
#Set up the net
net = cv.dnn.readNetFromDarknet(modelConf, modelWeights)
net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)
#Process inputs
winName = 'DL OD with OpenCV'
cv.namedWindow(winName, cv.WINDOW_NORMAL)
cv.resizeWindow(winName, 1000,1000)
cap = cv.VideoCapture(0)
while cv.waitKey(1) < 0:
#get frame from video
hasFrame, frame = cap.read()
#Create a 4D blob from a frame
blob = cv.dnn.blobFromImage(frame, 1/255, (inpWidth, inpHeight), [0,0,0], 1, crop = False)
#Set the input the the net
net.setInput(blob)
outs = net.forward (getOutputsNames(net))
postprocess (frame, outs)
#show the image
cv.imshow(winName, frame)
因此,如果我理解正确的话,你的问题是关于计算 Python 中经过的时间。这与 OpenCV 或 YOLO 无关,可以通过以下方式实现:
import time
start_time = time.time()
# one eternity later
elapsed_time = time.time() - start_time
然后当新人进入房间时启动计时器,当有人离开时停止计时器。
根据您刚刚添加的代码,您需要跟踪对象。检测它们不足以计算它们的年龄。
我所说的跟踪是指您需要知道一个对象是新对象还是在过去的帧中存在过。
一旦您确定了所有对象,您就可以增加一个时间计数器。 当它们消失时,您可以查看计数器以了解它们停留了多长时间。
跟踪是一个很大的主题,我认为不能用一个 SO 线程来概括。 而且没有神奇的解决方案,你可以使用几种不同的算法,结果取决于你的视频输入。
算法简单
一个简单的方法是比较帧之间找到的对象的位置。例如,在第 0 帧,您在位置 p0 处有一个对象,您称它为 'A'。
在下一帧,你有两个对象,你计算它们与 p0 之间的距离。如果一个足够近(低于你应该确定的阈值),那么你可以认为它是你的对象 A。然后你可以增加它的年龄。并将第二个称为 B。
然后进入下一帧。如果在这一帧上没有足够接近对象A的最后位置的对象,则可以认为A消失并获取其年龄。