Jetson tx2 error

in my tx2 board
i’ve succeeded running openpose&kcftracker each.


and now i’m trying to combine these two.
i want to track human by kcf tracker
and then i want to use openpose for that area acquired by kcftracker.

so my code is like this.


import argparse
import logging
import time
#from time import time

import cv2
import numpy as np
import sys#

import kcftracker#

from tf_pose.estimator import TfPoseEstimator
from tf_pose.networks import get_graph_path, model_wh

selectingObject = False#
initTracking = False#
onTracking = False#
ix, iy, cx, cy = -1, -1, -1, -1#
w, h = 0, 0#

inteval = 1#
duration = 0.01#

logger = logging.getLogger(‘TfPoseEstimator-WebCam’)
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter(’[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s’)
ch.setFormatter(formatter)
logger.addHandler(ch)

fps_time = 0

def draw_boundingbox(event, x, y, flags, param):
global selectingObject, initTracking, onTracking, ix, iy, cx, cy, w, h

if event == cv2.EVENT_LBUTTONDOWN: 
    selectingObject = True 
    onTracking = False 
    ix, iy = x, y
    cx, cy = x, y

elif event == cv2.EVENT_MOUSEMOVE:
    cx, cy = x, y

elif event == cv2.EVENT_LBUTTONUP:
    selectingObject = False
    if(abs(x - ix) > 10 and abs(y - iy) > 10):
        w, h = abs(x - ix), abs(y - iy)
        ix, iy = min(x, ix), min(y, iy)
        initTracking = True
    else:
        onTracking = False

elif event == cv2.EVENT_RBUTTONDOWN:
    onTracking = False
    if(w > 0):
        ix, iy = x - w / 2, y - h / 2
        initTracking = True

def str2bool(v):
return v.lower() in (“yes”, “true”, “t”, “1”)

if name == ‘main’:
parser = argparse.ArgumentParser(description=‘tf-pose-estimation realtime webcam’)
parser.add_argument(’–camera’, type=int, default=0)

parser.add_argument('--resize', type=str, default='0x0',
                    help='if provided, resize images before they are processed. default=0x0, Recommends : 432x368 or 656x368 or 1312x736 ')
parser.add_argument('--resize-out-ratio', type=float, default=4.0,
                    help='if provided, resize heatmaps before they are post-processed. default=1.0')

parser.add_argument('--model', type=str, default='mobilenet_thin', help='cmu / mobilenet_thin / mobilenet_v2_large / mobilenet_v2_small')
parser.add_argument('--show-process', type=bool, default=False,
                    help='for debug purpose, if enabled, speed for inference is dropped.')

parser.add_argument('--tensorrt', type=str, default="False",
                    help='for tensorrt process.')
args = parser.parse_args()

logger.debug('initialization %s : %s' % (args.model, get_graph_path(args.model)))
w, h = model_wh(args.resize)
if w > 0 and h > 0:
    e = TfPoseEstimator(get_graph_path(args.model), target_size=(w, h), trt_bool=str2bool(args.tensorrt))
else:
    e = TfPoseEstimator(get_graph_path(args.model), target_size=(432, 368), trt_bool=str2bool(args.tensorrt))
logger.debug('cam read+')
cap = cv2.VideoCapture(args.camera)
tracker = kcftracker.KCFTracker(True, True, True)
# if you use hog feature, there will be a short pause after you draw a first boundingbox, that is due to the use of Numba.

cv2.namedWindow('tracking') 
cv2.setMouseCallback('tracking', draw_boundingbox) 

while(cap.isOpened()):
    ret, frame = cap.read()
    if not ret:
        break

    if(selectingObject):    
        cv2.rectangle(frame, (ix, iy), (cx, cy), (0, 255, 255), 1)
    elif(initTracking): 
        cv2.rectangle(frame, (ix, iy), (ix + w, iy + h), (0, 255, 255), 2)
        print([ix, iy, w, h])
        tracker.init([ix, iy, w, h], frame)
        try:
            crop_img = frame[iy:iy+h, ix: ix+w]
            rect_img = cv2.resize(crop_img, dsize=(640, 480), interpolation=cv2.INTER_AREA)
        except Exception as e:
            print(str(e))

        initTracking = False
        onTracking = True   
    elif(onTracking):   
        t0 = time()

        logger.debug('image process+')
        humans = e.inference(rect_img, resize_to_default=(w > 0 and h > 0), upsample_size=args.resize_out_ratio)

        logger.debug('postprocess+')
        image = TfPoseEstimator.draw_humans(rect_img, humans, imgcopy=False)

        logger.debug('show+')
        cv2.putText(rect_img,
                    "FPS: %f" % (1.0 / (time.time() - fps_time)),
                    (10, 10),  cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                    (0, 255, 0), 2)
        cv2.imshow('tf-pose-estimation result', rect_img)
        fps_time = time.time()            
        boundingbox = tracker.update(frame) 
        t1 = time() 
        boundingbox = list(map(int, boundingbox))
        print(boundingbox)
        cv2.rectangle(frame, (boundingbox[0], boundingbox[1]), (boundingbox[0] + boundingbox[2], boundingbox[1] + boundingbox[3]), (0, 255, 255), 1)
        try:
            crop_img = frame[boundingbox[1]:boundingbox[1] + boundingbox[3], boundingbox[0]: boundingbox[0] + boundingbox[2]]
            rect_img = cv2.resize(crop_img, dsize=(640, 480), interpolation=cv2.INTER_AREA)

        except Exception as e:
            print(str(e))

        cv2.imshow('cut',rect_img)
        duration = 0.8 * duration + 0.2 * (t1 - t0)
        #duration = t1-t0
        cv2.putText(frame, 'FPS: ' + str(1 / duration)[:4].strip('.'), (8, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) 

    cv2.imshow('tracking', frame)
    
    c = cv2.waitKey(inteval) & 0xFF
    if c == 27 or c == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

i combined kcf-tracker’s run.py, and openpose’s run-webcam.py
it works but after few seconds, it stops and this error comes out.

humans = e.inference(rect_img, resize_to_default=(w > 0 and h > 0), upsample_size=args.resize_out_ratio)
NameError : name ‘e’ is not defined

Is this because of the sizing problem?
i have no idea about how to run openpose for the partial area.

Hi,

This error is usually caused by the zero bounding box case.
Have you added the handling when there is no bbox output?

Thanks.

Hi!
thanks for your kind reply.
But there’s still a question.
when is the bounding box goes to zero?
How do i handle when there is no bbox output?

Hi,

Suppose you should check if the width/height is valid before using it.
Thanks.