Python 边界框的分离
在这个问题中,我们试图在网络摄像头视频中实时检测人。该代码对于一个人来说运行良好,但当多人进入时,该代码将严重失败。代码如下:-Python 边界框的分离,python,opencv,contour,bounding-box,background-subtraction,Python,Opencv,Contour,Bounding Box,Background Subtraction,在这个问题中,我们试图在网络摄像头视频中实时检测人。该代码对于一个人来说运行良好,但当多人进入时,该代码将严重失败。代码如下:- import cv2 import numpy as np cap = cv2.VideoCapture(0) kernel = np.ones((5,5), np.uint8) background = None while True: ret,frame = cap.read() gray = frame.copy() gray =
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
kernel = np.ones((5,5), np.uint8)
background = None
while True:
ret,frame = cap.read()
gray = frame.copy()
gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (11,11), 0)
if background is None:
background = gray
continue
delta = cv2.absdiff(background, gray)
thresh = cv2.threshold(delta, 5, 255,
cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
thresh = cv2.dilate(thresh, kernel, iterations=2)
_,contours,hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
if(len(contours)==0):
continue
#areas = [cv2.contourArea(c) for c in contours]
#max_index = np.argmax(areas)
#cnt=contours[max_index]
#(x,y,w,h) = cv2.boundingRect(cnt)
#if(1.0*(w*h)/(640*480)<0.75):
#cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255), 3)
#print("Area: ",w*h)
for i in range(len(contours)):
(x,y,w,h) = cv2.boundingRect(contours[i])
if(w*h<=90000):
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255), 5)
#cv2.imshow('thresh', thresh)
cv2.imshow('frame', frame)
if cv2.waitKey(1)==27:
break
cap.release()
cv2.destroyAllWindows()
导入cv2
将numpy作为np导入
cap=cv2.视频捕获(0)
内核=np.ones((5,5),np.uint8)
背景=无
尽管如此:
ret,frame=cap.read()
灰色=frame.copy()
灰色=cv2.CVT颜色(灰色,cv2.COLOR\u BGR2GRAY)
gray=cv2.高斯模糊(gray,(11,11),0)
如果背景为无:
背景=灰色
持续
delta=cv2.absdiff(背景,灰色)
阈值=cv2。阈值(δ,5255,
cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
thresh=cv2.扩张(thresh,kernel,迭代次数=2)
_,轮廓,层次=cv2.findContours(阈值,cv2.RETR_外部,
cv2.链条(近似简单)
如果(len(轮廓)==0):
持续
#面积=[cv2.轮廓面积(c)表示轮廓中的c]
#max_index=np.argmax(面积)
#cnt=等高线[最大指数]
#(x,y,w,h)=cv2.boundingRect(cnt)
#如果(1.0*(w*h)/(640*480)有什么错误?编程上没有错误。我无法在同一帧中检测到多人。我需要有人指导我以最佳方向解决问题。