我有屏幕记录的代码,并且每一帧我都有一组我想在每一帧上显示的边界框。我可以使用matplotlib
或其他方式执行此操作,但我mss
的工作速度为 30fps,我需要能够快速显示边界框。
我在文档中注意到这个例子,但我尝试运行它并且无法让它显示任何东西。而且我什至不确定这是否适用于我的示例。
import cv2
import time
import numpy as np
from mss import mss
with mss() as sct:
# Part of the screen to capture
monitor = {"top": 79, "left": 265, "width": 905, "height": 586}
while "Screen capturing":
last_time = time.time()
# Get raw pixels from the screen, save it to a Numpy array
screen = np.array(sct.grab(monitor))
# print("fps: {}".format(1 / (time.time() - last_time)))
print('loop took {} seconds'.format(time.time()-last_time))
last_time = time.time()
screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)
screen = cv2.resize(screen, (224,224)).astype(np.float32)/255
# Display the picture
cv2.imshow("OpenCV/Numpy normal", screen)
# Press "q" to quit
if cv2.waitKey(25) & 0xFF == ord("q"):
cv2.destroyAllWindows()
break
现在假设我有一组边界框要显示在每一帧上,例如,
bboxes = [np.array([12, 16, 29, 25]), np.array([5, 5, 38, 35])]
我可以以某种方式更改像素以显示它吗?我想我也可以通过它来做到这一点opencv
,因为这是最终显示屏幕的内容。
编辑:参考关于边界框的评论,它们是x1, y1, width, height
,并且在调整大小的(224,224)
图像中