如何使用multiprocessing.Pool与dlib跟踪器

时间:2019-06-07 20:19:33

标签: python-3.x dlib

具有多处理功能的dlib相关跟踪器。池冻结

我正在尝试使用dlib相关性跟踪器和池来实现多对象跟踪器。系统不断冻结,我不确定为什么。

   import cv2
   import dlib
   import sys
   import os
   import time
   import numpy as np
   import multiprocessing as mp

   mousePoints = []

def tracking(tracker, image):
    # for tracker in trackers:
    rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    tracker.update(rgb)
    position = tracker.get_position()
    # unpack the position of the object
    startX = int(position.left())
    startY = int(position.top())
    endX = int(position.right())
    endY = int(position.bottom())
    return (startX, startY, endX, endY)

def start_tracker(image, box, trackers):
    tracker = dlib.correlation_tracker()
    rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    rectangle = dlib.rectangle(box[0], box[1], box[2], box[3])
    tracker.start_track(rgb, rectangle)
    trackers.append(tracker)
    return trackers

def mouseEventHandler(event, x, y, flags, param):
    # references to the global mousePoints variable
    global mousePoints

    # if the left mouse button was clicked, record the starting 
    # coordinates.
    if event == cv2.EVENT_LBUTTONDOWN:
        mousePoints = [(x, y)]

    # when the left mouse button is released, record the ending 
    # coordinates.
    elif event == cv2.EVENT_LBUTTONUP:
        mousePoints.append((x, y))

def main():

    # references to the global mousePoints variable
    trackers = []
    positions = []
    global mousePoints


    # variable to the hold rectangle coordinates
    box = []

    # this variable indicats whether program is tracking
    tracked = False

    print("Starting WebCam")
    # webcam
    webcam = cv2.VideoCapture(0)
    # warm up webcam
    time.sleep(2.0)

    # create a named window and attach the mouse event handler to 
    # it.
   cv2.namedWindow("Webcam")
   cv2.setMouseCallback("Webcam", mouseEventHandler)

    while True:

        ret, frame = webcam.read()

        # if we have two sets of coordinates from the mouse event, 
        # draw a rectangle.
        if len(mousePoints) == 2:
            cv2.rectangle(frame, mousePoints[0], mousePoints[1], 
                (0, 255, 0), 2)
        box = (mousePoints[0][0], mousePoints[0][1], mousePoints[1] 
        [0], mousePoints[1][1])

        # tracking in progress, update the correlation tracker and 
        # get the object position.
        if tracked == True:
            positions = [pool.apply(tracking, args=(tracker, 
                         frame)) for tracker in trackers]
            for position in positions:
                (x, y, x1, y1) = position
                cv2.rectangle(frame, (x, y), (x1, y1), 
                              (0, 0, 255), 2)

        # show the current frame.
        cv2.imshow("Webcam", frame)

        ch = cv2.waitKey(1) & 0xFF

        # press "r" to stop tracking and reset the points.
        if ch == ord("r"):
            mousePoints = []
            trackers = []
            tracked = False

       # press "t" to start tracking the currently selected 
       # object/area.
        if ch == ord("t"):
            if len(mousePoints) == 2:
                trackers = start_tracker(frame, box, trackers)
                tracked = True
                mousePoints = []

        # press "q" to quit the program.
        if ch == ord('q'):
            break

    # cleanup.
    webcam.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    pool = mp.Pool(mp.cpu_count()- 1)
    main()

程序冻结,键盘中断然后释放它。不知道出了什么问题。使用不带多处理池功能的单核时,代码工作正常

0 个答案:

没有答案