标签:img person Python self cv2 Opencv video found 行人
# coding:utf-8
import cv2
from timeit import default_timer as timer
class Predict(object):
def __init__(self):
# 获取hog检测器对象
self.hog = cv2.HOGDescriptor()
# 设置检测人的默认检测器
self.hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
print("模型加载完成!")
# 检测i方框 包含o方框
def is_inside(self, o, i):
ox, oy, ow, oh = o
ix, iy, iw, ih = i
return ox > ix and ox + ow < ix + iw and oy + oh < iy + ih
# 将人外面的方框画出来
def draw_person(self, image, person):
x, y, w, h = person
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 255), 2)
def detect_img(self, img):
# 在图片中检测人,
# 返回found列表 每个元素是一个(x, y, w, h)的矩形,w是每一个矩形的置信度
found, w = self.hog.detectMultiScale(img)
found_filtered = []
# 如果方框有包含,只留下内部的小方块
for ri, r in enumerate(found):
for qi, q in enumerate(found):
if ri != qi and self.is_inside(r, q):
break
else:
found_filtered.append(r)
# 将每一个方块画出来
for person in found_filtered:
self.draw_person(img, person)
return img
def detect_video(self, video_path):
vid = cv2.VideoCapture(video_path)
if not vid.isOpened():
raise IOError("Couldn't open webcam or video")
while True:
return_value, frame = vid.read()
t1 = timer()
result = self.detect_img(frame)
t2 = timer()
fps = 1 / (t2 - t1)
print(fps)
cv2.imshow("person detection", result)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if __name__ == '__main__':
predict = Predict()
# 检测图片
if False:
img = cv2.imread("person1.jpg")
result = predict.detect_img(img)
cv2.imshow("person detection", result)
cv2.waitKey()
cv2.destroyAllWindows()
# 检测视频
if True:
video_path = 'video1.mp4'
predict.detect_video(video_path=video_path)
标签:img,person,Python,self,cv2,Opencv,video,found,行人 来源: https://blog.csdn.net/See_Star/article/details/110896825
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。