import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
bbox = cv2.selectROI('frame', frame, fromCenter=False, showCrosshair=True)
track = cv2.legacy.TrackerCSRT_create()
track.init(frame, bbox)
while True:
_, frame = cap.read()
ok, box = track.update(frame)
if ok:
(x, y, w, h) = [int(i) for i in box]
cv2.rectangle(frame, pt1=(x, y), pt2=(x+w, y+h), color=(0, 255, 0), thickness=2)
cv2.imshow('frame', frame)
if cv2.waitKey(1) == 27:
break
cap.release()
cv2.destroyAllWindows()
运行代码,在当前界面中截取一roi,敲下回车或者空格,持续运行,esc推出,感觉效果还是可以的。
报错:
module 'cv2' has no attribute 'legacy'
解决方法
pip uninstall opencv-python
pip install opencv-contrib-python --user
安装完再次运行即可