OpenCV基于dlib进行目标检测

运行之后,先把药要检测的目标用鼠标框起来,直接坐上到右下即可
之后按 键盘1 表示开始检测
按2 重置 检测框,自己可以再次画矩形
q 键表推出

教程链接 19-3

"""
Author: Mark

date: 2022/4/9 12:07
"""
# 加入库
import cv2
import dlib

points = []

# 定义方法,鼠标点击事件
def mouse_event_handler(event, x, y, flags, params):
    global points  # 全局调用
    if event == cv2.EVENT_LBUTTONDOWN:  # 鼠标左键按下
        points = [(x, y)]
    elif event == cv2.EVENT_LBUTTONUP:  # 鼠标左键松开
        points.append((x, y))

# 定义方法 显示信息
def show_info(frame, tracking_state):
    pos1 = (10, 20)
    pos2 = (10, 40)
    pos3 = (10, 60)

    info1 = 'hold left button, select an area, stract tracking'
    info2 = "'1': start tracking, '2': stop stacking, 'q': exit"
    cv2.putText(frame, info1, pos1, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255))
    cv2.putText(frame, info2, pos2, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255))

    if tracking_state:
        cv2.putText(frame, 'tracking now...', pos3, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255))
    else:
        cv2.putText(frame, 'stop stracking...', pos3, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255))

# 打开摄像头
capture = cv2.VideoCapture(0)
# 设定窗口名称
nameWindow = 'Object Tracking'
# 将鼠标事件绑定到窗口上
cv2.namedWindow(nameWindow)
cv2.setMouseCallback(nameWindow, mouse_event_handler)

# 启动跟踪器
tracker = dlib.correlation_tracker()
# 假设跟踪状态
tracking_state = False
# 循环读取视频流
while True:
    # 获取每一帧
    ret, frame = capture.read()
    # 显示提示信息:调用方法
    show_info(frame, tracking_state)

    # 如果获取到的坐标点为2个,那么就绘制出矩形框,以及要让dlib的rectangle()知道坐标在哪
    if len(points) == 2:
        cv2.rectangle(frame, points[0], points[1], (0, 255, 0), 3)  # point[0] (x, y)  [1]同样
        dlib_rect = dlib.rectangle(points[0][0], points[0][1], points[1][0], points[1][1])
    # 判断:如果跟踪状态为True,那么更新跟踪,获取位置,绘制矩形框
    if tracking_state is True:
        tracker.update(frame)  # 更新画面
        pos = tracker.get_position()  # 获取位置的坐标
        cv2.rectangle(frame, (int(pos.left()), int(pos.top())), (int(pos.right()), int(pos.bottom())), (255, 0, 0), 3)

    # 事件的判断,根据按键 '1', '2', 'q'
    key = cv2.waitKey(1) & 0xFF
    if key == ord('1'):
        if len(points) == 2:
            tracker.start_track(frame, dlib_rect)
            tracking_state = True
            points = []

    if key == ord('2'):
        points = []
        tracking_state = False

    if key == ord('q'):
        break

    # 显示整体
    cv2.imshow(nameWindow, frame)

capture.release()
cv2.destroyAllWindows()

你可能感兴趣的:(opencv,python)