非常奇怪,不知道为什么导出来电脑上打不开out
# main processing function to run pose estimation
def run_pose_estimation(source=0, flip=False, use_popup=True, skip_first_frames=0):
pafs_output_key = compiled_model.output("Mconv7_stage2_L1")
heatmaps_output_key = compiled_model.output("Mconv7_stage2_L2")#输出层的两个key
player = None
#输入源,可以是摄像头设备的编号(默认为0,即默认使用第一个摄像头),也可以是视频文件的路径或者图像文件的路径
#flip:指定是否翻转输入图像,如果为True,则在估计姿势时会同时对输入图像进行水平翻转,然后将两次估计的结果合并,以提高估计准确度(默认为False)
#use_popup 显示新窗口
#skip_first_frames:指定在开始进行姿势估计前需要跳过的帧数,默认为0(即从第一帧开始进行姿势估计)
try:
# create video player to play with target fps
player = utils.VideoPlayer(source, flip=flip, fps=30, skip_first_frames=skip_first_frames)
# start capturing
player.start()
if use_popup:
title = "Press ESC to Exit"
cv2.namedWindow(title, cv2.WINDOW_GUI_NORMAL | cv2.WINDOW_AUTOSIZE)
processing_times = collections.deque()#双端队列processing_times用于存储每帧处理的时间
fourcc = cv2.VideoWriter_fourcc(*"H264") # 设置编码器
output_video = cv2.VideoWriter("D:/桌面/output.mp4", fourcc, 30, (width,height))
while True:
# grab the frame
frame = player.next()
if frame is None:
print("Source ended")
break#不断读取视频帧并进行处理 如果视频已经结束 则退出循环。
# if frame larger than full HD, reduce size to improve the performance
#检查视频的大小是否超过全高清分辨率(1920 x 1080),如果是,则将视频帧大小缩小,以提高性能
scale = 1280 / max(frame.shape)
if scale < 1:
frame = cv2.resize(frame, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
# resize image and change dims to fit neural network input
# (see https://github.com/openvinotoolkit/open_model_zoo/tree/master/models/intel/human-pose-estimation-0001)
input_img = cv2.resize(frame, (width, height), interpolation=cv2.INTER_AREA)
# create batch of images (size = 1)
input_img = input_img.transpose((2,0,1))[np.newaxis, ...]
#将调整图像大小和格式以适应神经网络输入
# measure processing time
start_time = time.time()
# get results
results = compiled_model([input_img])
stop_time = time.time()
#记录编译推断模型时间
pafs = results[pafs_output_key]
heatmaps = results[heatmaps_output_key] #pafs和heatmaps都是神经网络的输出结果
# get poses from network results
poses, scores = process_results(frame, pafs, heatmaps)#关节点 置信度
# draw poses on a frame
frame = draw_poses(frame, poses, 0.1)#frame是指视频流中的一帧图像
processing_times.append(stop_time - start_time)
# use processing times from last 200 frames
if len(processing_times) > 200:
processing_times.popleft()
_, f_width = frame.shape[:2]
# mean processing time [ms]
processing_time = np.mean(processing_times) * 1000
fps = 1000 / processing_time
output_video.write(frame)
cv2.putText(frame, f"Inference time: {processing_time:.1f}ms ({fps:.1f} FPS)", (20, 40),
cv2.FONT_HERSHEY_COMPLEX, f_width / 1000, (0, 0, 255), 1, cv2.LINE_AA)
#这段代码的作用是计算并在窗口上显示每秒处理的帧数(FPS)和推断时间(即处理一帧所需的平均时间)
# use this workaround if there is flickering
if use_popup:
cv2.imshow(title, frame) #在窗口显示
key = cv2.waitKey(1)
# escape = 27
if key == 27:#在OpenCV中,按下ESC键的ASCII码为27
break
else:
# encode numpy array to jpg
_, encoded_img = cv2.imencode(".jpg", frame, params=[cv2.IMWRITE_JPEG_QUALITY, 90])
# create IPython image
i = display.Image(data=encoded_img)
# display the image in this notebook #在vscode显示
display.clear_output(wait=True)
display.display(i)
output_video.release()
# ctrl-c异常处理
except KeyboardInterrupt:
print("Interrupted")
# any different error
except RuntimeError as e:
print(e)
finally:
if player is not None:
# stop capturing
player.stop()
if use_popup:
cv2.destroyAllWindows()
video_file = "https://github.com/intel-iot-devkit/sample-videos/blob/master/store-aisle-detection.mp4?raw=true"
run_pose_estimation(video_file, flip=False, use_popup=False, skip_first_frames=0)