Ejemplo n.º 1
0
def peopleDetect():

    CLASSES = ('aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car',
               'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse',
               'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train',
               'tvmonitor')
    cap = cv2.VideoCapture(1)
    net = None
    prefix = os.path.join(os.getcwd(), 'model', 'yolo2_darknet19_416')
    epoch = 0

    mean_pixels = (123, 117, 104)
    ctx = mx.gpu(0)
    global numPeople
    global isNotQuit
    count = 0

    ret1, frame1 = cap.read()
    detector = Detector(net,
                        prefix,
                        epoch,
                        data_shape,
                        mean_pixels,
                        ctx=ctx,
                        batch_size=batch)
    while isNotQuit:
        count += 1
        ret, frame = cap.read()
        ims = [
            cv2.resize(frame, (data_shape, data_shape)) for i in range(batch)
        ]

        data = None
        data = get_batch(ims)

        start = timer()

        det_batch = mx.io.DataBatch(data, [])
        detector.mod.forward(det_batch, is_train=False)
        detections = detector.mod.get_outputs()[0].asnumpy()
        result = []

        for i in range(detections.shape[0]):
            det = detections[i, :, :]
            res = det[np.where(det[:, 0] >= 0)[0]]
            result.append(res)
        time_elapsed = timer() - start
        # print("Detection time for {} images: {:.4f} sec , fps : {:.4f}".format(batch*1, time_elapsed , (batch*1/time_elapsed)))
        numPeople, numChair = detector.show_result(frame, det, CLASSES, 0.5,
                                                   batch * 1 / time_elapsed)
    # if count>40:
    #	isNotQuit = False
    #break
    cap.release()
    cv2.destroyAllWindows()