python检测人数代码?

Python015

python检测人数代码?,第1张

您好!下面是一个使用 OpenCV 库来检测人数的 Python 代码

import cv2

# 读入视频文件

video = cv2.VideoCapture("video.mp4")

# 创建人体检测器

body_cascade = cv2.CascadeClassifier('haarcascade_fullbody.xml')

# 初始化人数计数器

people_count = 0

# 循环读取每一帧

while True:

# 读取当前帧

ret, frame = video.read()

# 如果视频结束,退出循环

if not ret:

break

# 转为灰度图

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# 使用人体检测器检测人体

bodies = body_cascade.detectMultiScale(gray, 1.1, 3)

# 将检测到的人体绘制矩形框

for (x, y, w, h) in bodies:

cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)

# 将矩形框中的人数加 1

people_count += len(bodies)

# 显示当前帧

cv2.imshow("Frame", frame)

# 如果按下 'q' 键,退出循环

if cv2.waitKey(1) == ord('q'):

break

# 关闭窗口

cv2.destroyAllWindows()

# 输出总人数

print(f'Total people count: {people_count}')

在上面的代码中,我们还初始化了一个 people_count 变量来记录人数,并在每一帧中通过检测到的人体数量来更新人数计数器。最后,我们使用了 OpenCV 的 imshow 函数来显示当前帧的图像,并使用 waitKey 函数来检测是否按下了 'q' 键。如果按下 'q' 键,就会退出循环。

在循环结束后,我们使用了 destroyAllWindows 函数来关闭所有打开的窗口,并使用 print 函数输出总人数。

如果我的回答对您有帮助,望采纳!谢谢

、使用循环输出列表,利用 计数器控制输出数量,当输出到第十个,计数器归零,重新开始计数

2、print输出增加end参数可以控制输出后以什么结尾

3、这里使用range方法快速生成10-90的数字添加进list列表

results = list(range(10, 90))

n = 10 # 每10个数换一行

for i in range(len(results)):

print("早", end=' ')

if (i+1) % 10 == 0:

print("\n") # \n为转义符 换行的意思

#out:

1、创建python代码,testread()file.py;

2、编写python代码,

import re

def getFileContent(str):

str_value = str

len_str_value = len(str_value)

print(str_value)

print(len_str_value)

len_capital = len(re.compile(r'[A-Z]').findall(str_value))

print(u'大写字母有%d个'%len_capital)

len_lowercase = len(re.compile(r'[a-z]').findall(str_value))

print(u'小写字母有%d个'%len_lowercase)

len_num = len(re.compile(r'\d').findall(str_value))

print(u'数字有%d个'%len_num)

len_others = len_str_value -len_capital-len_lowercase-len_num

print(u'其他的字符有%d个'%len_others)

dict1 = {'capital':len_capital,'lowercase':len_lowercase,'others':len_others,'num':len_num}

return dict1

if __name__ == '__main__':

str = open('D:\\test.txt','r',encoding='UTF-8').read().replace('\t','').replace('\n','').replace(' ','').replace('space','')

print(getFileContent(str))

3、右击‘在终端中运行Python文件’;

4、查看运行结果,满足需求;