当前位置: 首页 > news >正文

通过摄像头检测步频

通过摄像头识别运动频率,比如步频。

打开摄像头
循环读取视频帧
灰度转换
统计中间一行数值和
人在摄像头前运动,该数值会呈现周期变化
通过快速傅里叶转换,将和步频相似频率显示出来。

(尝试人脸检测,跟着人脸位置变化计算频率。
这个对机器算力要求较高,视频帧处理能力不能满足需求)

import pyqtgraph as pg
import numpy as npimport cv2 from scipy.fftpack import fft, fftfreqimport timetimestamp = time.time()
print("当前时间戳:", timestamp)
print("cv2", cv2)# 训练一组人脸
face_detector = cv2.CascadeClassifier("C:\\Users\\13361\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python39\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml")data_list = []
fcount = 0
f_periods = []
interval = 0.0
infos = 'TEXT ON VIDEO'def show_info():global fcount, intervalglobal timestampfcount += 1res = []if fcount == 100:fcount = 0new_time = time.time()interval = new_time - timestamptimestamp = new_timeinterval /= 100.0print('show_info interval ', interval)# pos_mask = f_periods[np.where(f_periods < 10)]if len(f_periods) :for period in f_periods:res.append(int(60/(period*interval)))# res = res[np.where(res > 100)]return resdef get_data():global data_list, interval, fcountglobal f_periods, last_y, infosret, frame = vid.read()     # conversion of BGR to grayscale is necessary to apply this operationgray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)# # print(gray.shape)# # 检测人脸(用灰度图检测,返回人脸矩形坐标(4个角))# #  param:    灰度图  图像尺寸缩小比例  至少检测次数(若为3,表示一个目标至少检测到3次才是真正目标)# ret = face_detector.detectMultiScale(gray, 1.05, 5)# ww = 0# for x, y, w, h in ret:#     cv2.rectangle(gray, (x, y), (x + w, y + h), (0, 0, 255), 3)  #画出矩形框#     # print("rect ", x, y, w, h)#     if w > 100 and w > ww:#         ww = w#         last_y = y# # print("pend ", last_y)# data_list.append(last_y)data = np.sum(gray, axis=1)data_list.append(float(data[200]))# print(float(data[200]))if len(data_list) > 200:data_list = data_list[1:]f_periods = do_fft(data_list)plot.setData(data_list,pen='g')res = show_info()if len(res) :infos = ''for info in res:if info > 100:infos += str(info)infos += ' '# infos = str(res)if fcount % 30 == 29:print(f"fft_periods: {f_periods}", interval)print(f"freq : {res}")print(f"infos : {infos}")# describe the type of font  to be used. font = cv2.FONT_HERSHEY_SIMPLEX # Use putText() method for inserting text on video cv2.putText(gray, infos, (50, 50), font, 1,  (0, 255, 255),  2,  cv2.LINE_4) # adaptive thresholding to use different threshold values on different regions of the frame.# Thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,#                                         cv2.THRESH_BINARY_INV, 11, 2)cv2.imshow('Thresh', gray)def do_fft(dl):# print(data.shape)# print(len(dl))# print(dl[:5])fft_series = fft(dl)     # fft-返回复数数组power = np.abs(fft_series)   # 取模sample_freq = fftfreq(fft_series.size)pos_mask = np.where(sample_freq > 0)freqs = sample_freq[pos_mask]powers = power[pos_mask]top_k_seasons = 3# top K=3 indextop_k_idxs = np.argpartition(powers, -top_k_seasons)[-top_k_seasons:]top_k_power = powers[top_k_idxs]fft_periods = (1 / freqs[top_k_idxs]).astype(int)# pos_mask = fft_periods[np.where(fft_periods > 100)]# print(f"fft_periods: {fft_periods}")# print(f"pos_mask: {pos_mask}")# print(pos_mask)return fft_periodsif __name__ == '__main__':import syssys.setrecursionlimit(10000)# threading.stack_size(200000000)# thread = threading.Thread(target=your_code)# thread.start()vid = cv2.VideoCapture(0) # pyqtgragh初始化app = pg.mkQApp()  # 建立appwin = pg.GraphicsLayoutWidget(show=True)  # 建立窗口win.setWindowTitle(u'pyqtgraph 实时波形显示工具')win.resize(800, 500)  # 小窗口大小# 创建图表historyLength = 200  # 横坐标长度p = win.addPlot()  # 把图p加入到窗口中p.showGrid(x=True, y=True)  # 把X和Y的表格打开p.setRange(xRange=[0, historyLength], yRange=[50000, 150000], padding=0)# p.setRange(xRange=[0, historyLength], yRange=[0, 500], padding=0)p.setLabel(axis='left', text='gray')  # 靠左p.setLabel(axis='bottom', text='时间')p.setTitle('gray graph')  # 表格的名字plot = p.plot()timer = pg.QtCore.QTimer()timer.timeout.connect(get_data) # 定时刷新数据显示timer.start(10) # 多少ms调用一次app.exec_()vid.release()cv2.destroyAllWindows() 

相关文章:

  • 【C语言】数组参数和指针参数详解
  • MOS参数详解
  • nginx ws长连接配置
  • web端即时通信技术
  • Python for循环中的引用传递和值传递
  • Redis 面试热点(二)
  • 每日一练:攻防世界:Ditf
  • Golang并发控制的三种方案
  • 一文理清GO语言日志库实现开发项目中的日志功能(rotatelogs/zap分析)
  • 基于多头注意力机制卷积神经网络结合双向门控单元CNN-BIGRU-Mutilhead-Attention实现柴油机故障诊断附matlab代码
  • MongoDB~高可用集群介绍:复制集群(副本集)、分片集群
  • SQL MAX() 函数深入解析
  • PyQt5设计登录跳转界面
  • 使用net.sf.mpxj读取project的.mpp文件
  • 文件操作(2)(C语言版)
  • 【刷算法】求1+2+3+...+n
  • 0x05 Python数据分析,Anaconda八斩刀
  • 0基础学习移动端适配
  • DOM的那些事
  • dva中组件的懒加载
  • export和import的用法总结
  • Idea+maven+scala构建包并在spark on yarn 运行
  • JavaScript设计模式系列一:工厂模式
  • JS+CSS实现数字滚动
  • LeetCode18.四数之和 JavaScript
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • PHP的类修饰符与访问修饰符
  • Swift 中的尾递归和蹦床
  • vue学习系列(二)vue-cli
  • 动手做个聊天室,前端工程师百无聊赖的人生
  • 浮现式设计
  • 基于Volley网络库实现加载多种网络图片(包括GIF动态图片、圆形图片、普通图片)...
  • 基于阿里云移动推送的移动应用推送模式最佳实践
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 设计模式(12)迭代器模式(讲解+应用)
  • 深度学习在携程攻略社区的应用
  • 事件委托的小应用
  • 一文看透浏览器架构
  • postgresql行列转换函数
  • ​Linux Ubuntu环境下使用docker构建spark运行环境(超级详细)
  • ​TypeScript都不会用,也敢说会前端?
  • ​创新驱动,边缘计算领袖:亚马逊云科技海外服务器服务再进化
  • ​你们这样子,耽误我的工作进度怎么办?
  • #### go map 底层结构 ####
  • (C++20) consteval立即函数
  • (MonoGame从入门到放弃-1) MonoGame环境搭建
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (附源码)计算机毕业设计SSM在线影视购票系统
  • (六)什么是Vite——热更新时vite、webpack做了什么
  • (一)Kafka 安全之使用 SASL 进行身份验证 —— JAAS 配置、SASL 配置
  • (译)计算距离、方位和更多经纬度之间的点
  • (转)GCC在C语言中内嵌汇编 asm __volatile__
  • (转)Linux NTP配置详解 (Network Time Protocol)
  • (转)平衡树
  • .bat批处理(一):@echo off