欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 通过摄像头检测步频

通过摄像头检测步频

2024/10/24 9:27:01 来源:https://blog.csdn.net/liuxs2009/article/details/139782078  浏览:    关键词:通过摄像头检测步频

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

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

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

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() 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com