加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.py 4.18 KB
一键复制 编辑 原始数据 按行查看 历史
xbing 提交于 2021-11-16 17:42 . 初始化提交
import argparse
import os
import sys
import cv2
import action
import motion
if __name__ == '__main__':
try:
# 导入 openpose 库
dir_path = "C:\\Users\\Administrator\\Desktop\\openpose-master\\build\\examples\\tutorial_api_python"
path = "C:\\Users\\Administrator\\Desktop\\openpose-master\\build\\python\\openpose\\Release"
bin_path = "C:\\Users\\Administrator\\Desktop\\openpose-master\\build\\bin"
sys.path.insert(0, path);
# 追加到系统环境变量
os.environ['PATH'] ="C:\\Users\\Administrator\\Desktop\\openpose-master\\build\\x64\\Release;"+bin_path+";"+ os.environ['PATH'] +";"
# os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/../../x64/Release;' + dir_path + '/../../bin;'
print(os.environ["PATH"])
import pyopenpose as op
# Flags
parser = argparse.ArgumentParser()
args = parser.parse_known_args()
# Custom Params (refer to include/openpose/flags.hpp for more parameters)
params = dict()
# 需要加载模型
params["model_folder"] = "C:\\Users\\Administrator\\Desktop\\openpose-master\\models/"
# Add others in path?
for i in range(0, len(args[1])):
curr_item = args[1][i]
if i != len(args[1]) - 1:
next_item = args[1][i + 1]
else:
next_item = "1"
if "--" in curr_item and "--" in next_item:
key = curr_item.replace('-', '')
if key not in params: params[key] = "1"
elif "--" in curr_item and "--" not in next_item:
key = curr_item.replace('-', '')
if key not in params: params[key] = next_item
# Construct it from system arguments
# op.init_argv(args[1])
# oppython = op.OpenposePython()
opWrapper = op.WrapperPython()
opWrapper.configure(params)
opWrapper.start()
#获取摄像头的视频(opencv是按帧来获取的,然后用while进行一帧一帧的识别
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID') # 视频存储的格式
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
out = cv2.VideoWriter("output.avi", fourcc, 8, size)
# True表示在(其他页面) False表示(工作页面)
pc = False
while cap.isOpened():
ref, image = cap.read()
datum = op.Datum()
imageToProcess = image
datum.cvInputData = imageToProcess
#识别操作
opWrapper.emplaceAndPop(op.VectorDatum([datum]))
if datum.poseKeypoints is not None:
# 生成的结果 datum.poseKeypoints 这是一个数组.详细数组的解释请查看 README 这个打印可以去掉
print(datum.poseKeypoints)
# 检测到的人数
personNum = len(datum.poseKeypoints)
for i in range(personNum):
result = motion.is_see_my_pc(datum.poseKeypoints[i])
# print(result)
# 如果有人观看我的电脑,并且屏幕在游戏页面的话 则切换到工作页面
if result:
if pc:
action.have_boss()
# 工作页面
pc = False
# 没人看切换到其他页面
if not result:
action.not_boss()
#游戏页面
pc = True
else:
# 在工作页面 但是没人看 切换到游戏页面
if not pc:
action.not_boss()
# 游戏页面
pc = True
# 在你的电脑进行展示
cv2.imshow("危险识别", datum.cvOutputData)
# 视频的录制,生成一个 (out = cv2.VideoWriter("output.avi", fourcc, 8, size)) output.avi的视频
out.write(datum.cvOutputData)
cv2.waitKey(127)
cap.release()
except Exception as e:
print(e)
sys.exit(-1)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化