1 Star 0 Fork 0

wadas/python-opencv

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
DlibTrack.py 2.05 KB
一键复制 编辑 原始数据 按行查看 历史
wadas 提交于 2020-12-07 16:24 . update dlib track
'''
dlib 鼠标指定跟踪
'''
import sys
import dlib
import cv2
tracker = dlib.correlation_tracker()
cap = cv2.VideoCapture(0)
start_flag = True
selection = None
track_window = None
drag_start = None
def onMouseClicked(event, x, y, flags, param):
global selection, track_window, drag_start
if event == cv2.EVENT_LBUTTONDOWN:
drag_start = (x, y)
track_window = None
if drag_start:
xMin = min(x, drag_start[0])
yMin = min(y, drag_start[1])
xMax = max(x, drag_start[0])
yMax = max(y, drag_start[1])
selection = (xMin, yMin, xMax, yMax)
if event == cv2.EVENT_LBUTTONUP:
drag_start = None
track_window = selection
selection = None
if __name__ == '__main__':
cv2.namedWindow("image", cv2.WINDOW_AUTOSIZE)
cv2.setMouseCallback("image", onMouseClicked)
while(1):
ret, frame = cap.read()
if start_flag == True:
while True:
ret, frame = cap.read()
img_first = frame.copy()
if track_window:
cv2.rectangle(img_first, (track_window[0], track_window[1]), (track_window[2], track_window[3]), (0,0,255), 1)
elif selection:
cv2.rectangle(img_first, (selection[0], selection[1]), (selection[2], selection[3]), (0,0,255), 1)
cv2.imshow("image", img_first)
# 按下回车,确认跟踪目标
key = cv2.waitKey(1) & 0xFF
if key == 13:
break
start_flag = False
tracker.start_track(frame, dlib.rectangle(track_window[0], track_window[1], track_window[2], track_window[3]))
else:
tracker.update(frame)
box_predict = tracker.get_position()
cv2.rectangle(frame,(int(box_predict.left()),int(box_predict.top())),(int(box_predict.right()),int(box_predict.bottom())),(0,255,255),1)
cv2.imshow("image", frame)
# ESC键,退出
if cv2.waitKey(10) == 27:
break
cap.release()
cv2.destroyAllWindows()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wadas/python-opencv.git
git@gitee.com:wadas/python-opencv.git
wadas
python-opencv
python-opencv
master

搜索帮助