读取json文件 (常用于UI与逻辑间的数据传递)
import json
f = open('setting.json', 'r')
data = f.read()
f.close()
# 使用json.loads()函数将读取到的JSON格式的字符串数据转换为Python字典对象
setting = json.loads(data)
# setting = {
# "x_offset": 1,
# "y_offset": 1,
# "box_conf": 0.5,
# "auto_shot": False,
# "open_y": 1138
# }
cv2 播放本地视频
import cv2
# 播放本地视频
capture = cv2.VideoCapture('video.mp4')
while(capture.isOpened()):
ret, frame = capture.read()
#改变尺寸,宽高都变为原来的30%
res2 = cv2.resize(frame, None, fx=0.3,fy=0.3, interpolation=cv2.INTER_LINEAR)
cv2.imshow('frame', res2)
#按q退出
if cv2.waitKey(30) == ord('q'):
break
yolo初始化
import torch
# 有 GPU 就用 GPU,没有就用 CPU
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# 载入姿势监测预训练模型
model = YOLO('yolov8n-pose.pt')
# 切换计算设备cpu/gpu
model.to(device)
PyQt5库截图指定窗口
# 定义一个函数get_screenshot,其作用是从给定的窗口句柄hwnd中获取屏幕截图
def get_screenshot(hwnd):
# 使用QApplication类获取主屏幕对象,这是Qt库中的功能,用于获取计算机的主要显示屏幕
screen = QApplication.primaryScreen()
# 调用屏幕对象的grabWindow方法,传入hwnd参数以捕捉指定窗口的内容,并将其转化为QImage对象
img = screen.grabWindow(hwnd).toImage()
# 将QImage对象转换成OpenCV可用的Mat格式,此处假设存在一个自定义的QImage2Mat函数实现这一转换
image = QImage2Mat(img)
# 设置dpi为1,这里的dpi可能是用来缩放截图的分辨率,具体取决于应用场景
dpi = 1
# 获取原始截图的高度h_img和宽度w_img
h_img, w_img = image.shape[:2]
# 对截图进行裁剪,按照dpi的比例缩小高度和宽度
# 注意:若dpi为1,则实际上不做缩放处理;若dpi大于1,则会按比例缩小图像
image = image[0:int(h_img/dpi), 0:int(w_img/dpi)]
# 返回经过处理后的截图图像(Mat格式)和窗口句柄hwnd
return image, hwnd
PyQt5图片对象转为opencv的mat格式
def QImage2Mat(incomingImage):
incomingImage = incomingImage.convertToFormat(4)
width = incomingImage.width()
height = incomingImage.height()
ptr = incomingImage.bits()
ptr.setsize(incomingImage.byteCount())
arr = np.array(ptr).reshape(height, width, 4) # Copies the data
return arr
.split只能用于分割字符串,若分割字典中的字符串则如下所示
dict_example = {"fruit": "apple,banana,grape"}
fruits_list = dict_example["fruit"].split(",")
print(fruits_list) # 输出:['apple', 'banana', 'grape']
pycharm中批量修改多个相同变量,双击变量后按多次alt+j
列表元组字典间的相互转换
# 元组转列表
my_tuple = (1, 2, 3)
my_list = list(my_tuple)
print(my_list) # 输出:[1, 2, 3]
# 列表转元组,简单地使用 tuple() 函数即可
list_data = [1, 2, 3]
tuple_data = tuple(list_data)
print(tuple_data) # 输出:(1, 2, 3)
#字典转列表
# 转为[(key1, value1), (key2, value2), ...]这样的元组列表
my_dict = {'a': 1, 'b': 2, 'c': 3}
tuple_list = list(my_dict.items())
print(tuple_list) # 输出:[('a', 1), ('b', 2), ('c', 3)]
# 或者只转换值为列表
values_list = list(my_dict.values())
print(values_list) # 输出:[1, 2, 3]
# 如果需要转换为只包含键的列表
keys_list = list(my_dict.keys())
print(keys_list) # 输出:['a', 'b', 'c']
# 若要转换为元组
key_value_tuples = tuple(my_dict.items())
print(key_value_tuples) # 输出:('a', 1), ('b', 2), ('c', 3)]
字典的数据类型
setting = {
"x_offset": 1,
"y_offset": 1,
"box_conf": 0.5,
"auto_shot": False,
"open_y": 1138,
"abc":'hello'
}
setting['open_y']为整数类型
setting['abc']为字符串型
map函数
open_xy = enum.setting['open_xy'].split(",")
#分开后为列表类型即['100','300']
#类型分别转换为int
open_xy = list(map(int, open_xy))
dots = [open_xy, ]
#逗号表示 open_xy 是列表中唯一的元素
max = 15
for dot in dots:
#例子
# 对于多参数函数,可以同时传入多个可迭代对象
def add(a, b):
return a + b
nums1 = [1, 2, 3]
nums2 = [4, 5, 6]
sums = map(add, nums1, nums2)
pycharm中复制本行ctrl+d
-
logic.py
- 获取窗口句柄
- 调用tool.get_screenshot获取图像帧
- 调用yolov.process_frame获取处理好的视频和检测结果
- 调用shot.shot检测结果传入
-
yolov.py
- 加载预训练模型
- 设定姿态检测的骨架边框颜色等细节
- 帧处理函数process_frame(img_bgr)的定义,画框关键点坐标,画骨架
-
tool.py 获取指定句柄窗口截图并转为opencv的mat格式
调试模式得先打个断点在开头某个句子,不然直接就跑程序了
报错:
AttributeError: module ‘re’ has no attribute ‘compile’
enum.py和python中的一个迭代器重名了