OpenCV图像人脸识别人数统计

Python+OpenCV图像人脸识别人数统计

如需远程调试,可加QQ905733049由专业技术人员远程协助!

运行代码如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from tkinter import messagebox
from tkinter import *
import tkinter.font as tkFont
from tkinter import filedialog
import tkinter.messagebox
import cv2

root = tkinter.Tk()
ft2 = tkFont.Font(family='Microsoft YaHei', size=20, weight=tkFont.BOLD, underline=0, overstrike=0)

def set_win_center(root, curWidth='', curHight=''):
    '''
    设置窗口大小,并居中显示
    :param root:主窗体实例
    :param curWidth:窗口宽度,非必填,默认200
    :param curHight:窗口高度,非必填,默认200
    :return:无
    '''
    if not curWidth:
        '''获取窗口宽度,默认200'''
        curWidth = root.winfo_width()
    if not curHight:
        '''获取窗口高度,默认200'''
        curHight = root.winfo_height()
    # print(curWidth, curHight)

    # 获取屏幕宽度和高度
    scn_w, scn_h = root.maxsize()
    # print(scn_w, scn_h)

    # 计算中心坐标
    cen_x = (scn_w - curWidth) / 2
    cen_y = (scn_h - curHight) / 2
    # print(cen_x, cen_y)

    # 设置窗口初始大小和位置
    size_xy = '%dx%d+%d+%d' % (curWidth, curHight, cen_x, cen_y)
    root.geometry(size_xy)

def openfile():

    def is_inside(o, i):
        ox, oy, ow, oh = o
        return ox > ix and oy > iy and ox + ow < ix + iw and oy + oh < iy + ih
    def draw_person(image, person):
        x, y, w, h = person
        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 2)

    path = filedialog.askopenfilename(title='打开', filetypes=[('S2out', '*.*'), ('All Files', '*')])
    print(path)
    img = cv2.imread(path)
    # a, b = img.shape[:2]
    # img = cv2.resize(img, (a//5, b//5))
    hog = cv2.HOGDescriptor()
    hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
     
    found, w = hog.detectMultiScale(img)
     
    found_filtered = []
    l=len(w)
    print(l)

    for ri, r in enumerate(found):
        for qi, q in enumerate(found):
            if ri != qe and is_inside(r, q):
                break
            else:
                found_filtered.append(r)
    for person in found_filtered:
        draw_person(img, person)
    cv2.imshow('people detection', img)


    if l >= 3:
        Label(root, text='那儿有3个人以上', font=ft2).pack(padx=0,pady=20)
        Label(root, text=('总共有', l,'个人'), font=ft2).pack(padx=0,pady=20)
        root.mainloop()
    else:
        Label(root, text='那儿有少于2个人', font=ft2).pack(anchor=CENTER)
        Label(root, text=('总共有', l,'个人'), font=ft2).pack(padx=0,pady=20)
        root.mainloop()

    cv2.waitKey(0) 
    cv2.destroyAllWindows()

root.title("图像人数统计")  
root.geometry('500x350+500+200')
btn2 = tkinter.Button(root, text='图像人数统计',font = ('microsoft yahei',14,''),width=10,height=2, command=openfile)
btn3 = tkinter.Button(root, text='退出',font = ('microsoft yahei',14,''),width=10,height=2, command=root.quit)

root.mainloop()








运行结果:

 

 

要进行人脸识别统计人数,可以使用PythonOpenCV库。具体步骤如下: 1. 导入必要的库: ```python import cv2 import numpy as np ``` 2. 加载人脸识别分类器: ```python face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') ``` 这里使用了OpenCV自带的分类器,可以从官网下载。 3. 打开摄像头或读取视频: ```python cap = cv2.VideoCapture(0) # 打开摄像头 # 或者 cap = cv2.VideoCapture('video.mp4') # 读取视频文件 ``` 4. 读取每一帧图像,进行人脸检测: ```python while True: ret, frame = cap.read() # 读取一帧图像 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 转为灰度图像 faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5) # 人脸检测 ``` 5. 统计人数并绘制边框: ```python count = 0 # 初始化人数为0 for (x, y, w, h) in faces: count += 1 # 每检测到一个人脸,人数加1 cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) # 绘制人脸边框 cv2.putText(frame, f'Total: {count}', (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) # 显示人数 cv2.imshow('frame', frame) # 显示图像 if cv2.waitKey(1) == ord('q'): # 按q键退出 break ``` 完整代码如下: ```python import cv2 import numpy as np face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5) count = 0 for (x, y, w, h) in faces: count += 1 cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) cv2.putText(frame, f'Total: {count}', (30, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) cv2.imshow('frame', frame) if cv2.waitKey(1) == ord('q'): break cap.release() cv2.destroyAllWindows() ``` 注意,这里只是简单的统计人数,如果有人进入或离开画面,统计结果可能会有误差。如果需要更精确的统计,可以考虑使用深度学习等方法。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荷塘月色2

您的鼓励将是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值