Beispiel #1
0
def GetPoseandCostsF(cfg, dlc_cfg, sess, inputs, outputs, cap, nframes,
                     batchsize, c_engine):
    """ Batchwise prediction of pose """
    strwidth = int(np.ceil(np.log10(nframes)))  # width for strings
    batch_ind = 0  # keeps track of which image within a batch should be written to
    batch_num = 0  # keeps track of which batch you are at
    ny, nx = int(cap.get(4)), int(cap.get(3))
    if cfg["cropping"]:
        print(
            "Cropping based on the x1 = %s x2 = %s y1 = %s y2 = %s. You can adjust the cropping coordinates in the config.yaml file."
            % (cfg["x1"], cfg["x2"], cfg["y1"], cfg["y2"]))
        nx = cfg["x2"] - cfg["x1"]
        ny = cfg["y2"] - cfg["y1"]
        if nx > 0 and ny > 0:
            pass
        else:
            raise Exception("Please check the order of cropping parameter!")
        if (cfg["x1"] >= 0 and cfg["x2"] < int(cap.get(3) + 1)
                and cfg["y1"] >= 0 and cfg["y2"] < int(cap.get(4) + 1)):
            pass  # good cropping box
        else:
            raise Exception("Please check the boundary of cropping!")

    frames = np.empty((batchsize, ny, nx, 3),
                      dtype="ubyte")  # this keeps all frames in a batch
    pbar = tqdm(total=nframes)
    counter = 0
    step = max(10, int(nframes / 100))

    PredicteData = {}
    # initializing constants
    dist_grid = predict.make_nms_grid(dlc_cfg.nmsradius)
    stride, halfstride = dlc_cfg.stride, dlc_cfg.stride * 0.5
    num_joints = dlc_cfg.num_joints
    det_min_score = dlc_cfg.minconfidence

    num_idchannel = dlc_cfg.get("num_idchannel", 0)
    while cap.isOpened():
        if counter % step == 0:
            pbar.update(step)
        ret, frame = cap.read()
        if ret:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            if cfg["cropping"]:
                frames[batch_ind] = img_as_ubyte(frame[cfg["y1"]:cfg["y2"],
                                                       cfg["x1"]:cfg["x2"]])
            else:
                frames[batch_ind] = img_as_ubyte(frame)

            if batch_ind == batchsize - 1:
                # PredicteData['frame'+str(counter)]=predict.get_detectionswithcosts(frame, dlc_cfg, sess, inputs, outputs, outall=False,nms_radius=dlc_cfg.nmsradius,det_min_score=dlc_cfg.minconfidence)
                D = predict.get_batchdetectionswithcosts(
                    frames,
                    dlc_cfg,
                    dist_grid,
                    batchsize,
                    num_joints,
                    num_idchannel,
                    stride,
                    halfstride,
                    det_min_score,
                    sess,
                    inputs,
                    outputs,
                )
                for l in range(batchsize):
                    # pose = predict.getposeNP(frames,dlc_cfg, sess, inputs, outputs)
                    # PredicteData[batch_num*batchsize:(batch_num+1)*batchsize, :] = pose
                    PredicteData["frame" + str(batch_num * batchsize +
                                               l).zfill(strwidth)] = D[l]

                batch_ind = 0
                batch_num += 1
            else:
                batch_ind += 1
        else:
            nframes = counter
            print("Detected frames: ", nframes)
            if batch_ind > 0:
                # pose = predict.getposeNP(frames, dlc_cfg, sess, inputs, outputs) #process the whole batch (some frames might be from previous batch!)
                # PredicteData[batch_num*batchsize:batch_num*batchsize+batch_ind, :] = pose[:batch_ind,:]
                D = predict.get_batchdetectionswithcosts(
                    frames,
                    dlc_cfg,
                    dist_grid,
                    batchsize,
                    num_joints,
                    num_idchannel,
                    stride,
                    halfstride,
                    det_min_score,
                    sess,
                    inputs,
                    outputs,
                    c_engine=c_engine,
                )
                for l in range(batch_ind):
                    # pose = predict.getposeNP(frames,dlc_cfg, sess, inputs, outputs)
                    # PredicteData[batch_num*batchsize:(batch_num+1)*batchsize, :] = pose
                    PredicteData["frame" + str(batch_num * batchsize +
                                               l).zfill(strwidth)] = D[l]
            break
        counter += 1

    pbar.close()
    PredicteData["metadata"] = {
        "nms radius":
        dlc_cfg.nmsradius,
        "minimal confidence":
        dlc_cfg.minconfidence,
        "PAFgraph":
        dlc_cfg.partaffinityfield_graph,
        "all_joints": [[i] for i in range(len(dlc_cfg.all_joints))],
        "all_joints_names":
        [dlc_cfg.all_joints_names[i] for i in range(len(dlc_cfg.all_joints))],
        "nframes":
        nframes,
        "c_engine":
        c_engine,
    }
    return PredicteData, nframes
def GetPoseandCostsF(cfg, dlc_cfg, sess, inputs, outputs, cap, nframes,
                     batchsize, c_engine):
    """ Batchwise prediction of pose """
    strwidth = int(np.ceil(np.log10(nframes)))  # width for strings
    batch_ind = 0  # keeps track of which image within a batch should be written to
    batch_num = 0  # keeps track of which batch you are at
    if cfg["cropping"]:
        cap.set_bbox(cfg["x1"], cfg["x2"], cfg["y1"], cfg["y2"])
    nx, ny = cap.dimensions

    frames = np.empty((batchsize, ny, nx, 3),
                      dtype="ubyte")  # this keeps all frames in a batch
    pbar = tqdm(total=nframes)
    counter = 0
    step = max(10, int(nframes / 100))
    inds = []

    PredicteData = {}
    # initializing constants
    dist_grid = predict.make_nms_grid(dlc_cfg['nmsradius'])
    stride = dlc_cfg['stride']
    halfstride = stride * 0.5
    num_joints = dlc_cfg['num_joints']
    det_min_score = dlc_cfg['minconfidence']

    num_idchannel = dlc_cfg.get("num_idchannel", 0)
    while cap.video.isOpened():
        if counter % step == 0:
            pbar.update(step)
        frame = cap.read_frame(crop=cfg["cropping"])
        if frame is not None:
            frames[batch_ind] = img_as_ubyte(frame)
            inds.append(counter)
            if batch_ind == batchsize - 1:
                # PredicteData['frame'+str(counter)]=predict.get_detectionswithcosts(frame, dlc_cfg, sess, inputs, outputs, outall=False,nms_radius=dlc_cfg.nmsradius,det_min_score=dlc_cfg.minconfidence)
                D = predict.get_batchdetectionswithcosts(
                    frames,
                    dlc_cfg,
                    dist_grid,
                    batchsize,
                    num_joints,
                    num_idchannel,
                    stride,
                    halfstride,
                    det_min_score,
                    sess,
                    inputs,
                    outputs,
                )
                for ind, data in zip(inds, D):
                    PredicteData["frame" + str(ind).zfill(strwidth)] = data
                batch_ind = 0
                inds.clear()
                batch_num += 1
            else:
                batch_ind += 1
        elif counter >= nframes:
            if batch_ind > 0:
                # pose = predict.getposeNP(frames, dlc_cfg, sess, inputs, outputs) #process the whole batch (some frames might be from previous batch!)
                # PredicteData[batch_num*batchsize:batch_num*batchsize+batch_ind, :] = pose[:batch_ind,:]
                D = predict.get_batchdetectionswithcosts(
                    frames,
                    dlc_cfg,
                    dist_grid,
                    batchsize,
                    num_joints,
                    num_idchannel,
                    stride,
                    halfstride,
                    det_min_score,
                    sess,
                    inputs,
                    outputs,
                    c_engine=c_engine,
                )
                for ind, data in zip(inds, D):
                    PredicteData["frame" + str(ind).zfill(strwidth)] = data
            break
        counter += 1

    cap.close()
    pbar.close()
    PredicteData["metadata"] = {
        "nms radius":
        dlc_cfg['nmsradius'],
        "minimal confidence":
        dlc_cfg['minconfidence'],
        "PAFgraph":
        dlc_cfg['partaffinityfield_graph'],
        "all_joints": [[i] for i in range(len(dlc_cfg['all_joints']))],
        "all_joints_names": [
            dlc_cfg['all_joints_names'][i]
            for i in range(len(dlc_cfg['all_joints']))
        ],
        "nframes":
        nframes,
        "c_engine":
        c_engine,
    }
    return PredicteData, nframes
Beispiel #3
0
def GetPoseandCostsF(cfg, dlc_cfg, sess, inputs, outputs, cap, nframes,
                     batchsize, c_engine):
    """ Batchwise prediction of pose """
    strwidth = int(np.ceil(np.log10(nframes)))  # width for strings
    batch_ind = 0  # keeps track of which image within a batch should be written to
    batch_num = 0  # keeps track of which batch you are at
    if cfg["cropping"]:
        cap.set_bbox(cfg["x1"], cfg["x2"], cfg["y1"], cfg["y2"])
    nx, ny = cap.dimensions

    frames = np.empty((batchsize, ny, nx, 3),
                      dtype="ubyte")  # this keeps all frames in a batch
    pbar = tqdm(total=nframes)
    counter = 0
    step = max(10, int(nframes / 100))

    PredicteData = {}
    # initializing constants
    dist_grid = predict.make_nms_grid(dlc_cfg.nmsradius)
    stride, halfstride = dlc_cfg.stride, dlc_cfg.stride * 0.5
    num_joints = dlc_cfg.num_joints
    det_min_score = dlc_cfg.minconfidence

    num_idchannel = dlc_cfg.get("num_idchannel", 0)
    # TODO Fix the code below...
    #  We can't just break the whole thing if there is one corrupted frame
    #  in the middle of the video. Rather iterate over all frames and simply skip corruptions
    while cap.video.isOpened():
        if counter % step == 0:
            pbar.update(step)
        frame = cap.read_frame(crop=cfg["cropping"])
        if frame is not None:
            frames[batch_ind] = img_as_ubyte(frame)
            if batch_ind == batchsize - 1:
                # PredicteData['frame'+str(counter)]=predict.get_detectionswithcosts(frame, dlc_cfg, sess, inputs, outputs, outall=False,nms_radius=dlc_cfg.nmsradius,det_min_score=dlc_cfg.minconfidence)
                D = predict.get_batchdetectionswithcosts(
                    frames,
                    dlc_cfg,
                    dist_grid,
                    batchsize,
                    num_joints,
                    num_idchannel,
                    stride,
                    halfstride,
                    det_min_score,
                    sess,
                    inputs,
                    outputs,
                )
                for l in range(batchsize):
                    # pose = predict.getposeNP(frames,dlc_cfg, sess, inputs, outputs)
                    # PredicteData[batch_num*batchsize:(batch_num+1)*batchsize, :] = pose
                    PredicteData["frame" + str(batch_num * batchsize +
                                               l).zfill(strwidth)] = D[l]

                batch_ind = 0
                batch_num += 1
            else:
                batch_ind += 1
        else:
            nframes = counter
            print("Detected frames: ", nframes)
            if batch_ind > 0:
                # pose = predict.getposeNP(frames, dlc_cfg, sess, inputs, outputs) #process the whole batch (some frames might be from previous batch!)
                # PredicteData[batch_num*batchsize:batch_num*batchsize+batch_ind, :] = pose[:batch_ind,:]
                D = predict.get_batchdetectionswithcosts(
                    frames,
                    dlc_cfg,
                    dist_grid,
                    batchsize,
                    num_joints,
                    num_idchannel,
                    stride,
                    halfstride,
                    det_min_score,
                    sess,
                    inputs,
                    outputs,
                    c_engine=c_engine,
                )
                for l in range(batch_ind):
                    # pose = predict.getposeNP(frames,dlc_cfg, sess, inputs, outputs)
                    # PredicteData[batch_num*batchsize:(batch_num+1)*batchsize, :] = pose
                    PredicteData["frame" + str(batch_num * batchsize +
                                               l).zfill(strwidth)] = D[l]
            break
        counter += 1
    cap.close()
    pbar.close()
    PredicteData["metadata"] = {
        "nms radius":
        dlc_cfg.nmsradius,
        "minimal confidence":
        dlc_cfg.minconfidence,
        "PAFgraph":
        dlc_cfg.partaffinityfield_graph,
        "all_joints": [[i] for i in range(len(dlc_cfg.all_joints))],
        "all_joints_names":
        [dlc_cfg.all_joints_names[i] for i in range(len(dlc_cfg.all_joints))],
        "nframes":
        nframes,
        "c_engine":
        c_engine,
    }
    return PredicteData, nframes