Esempio n. 1
0
def run_eval(image_dir, anno_file, vis_dir, model, preprocess):
    """Run the evaluation on the test set and report mAP score
    :param model: the model to test
    :returns: float, the reported mAP score
    """   
    coco = COCO(anno_file)
    cat_ids = coco.getCatIds(catNms=['person'])    
    img_ids = coco.getImgIds(catIds=cat_ids)

    # img_ids = img_ids[81:82]
    # img_paths = img_paths[81:82]
    print("Total number of validation images {}".format(len(img_ids)))

    # iterate all val images
    outputs = []
    print("Processing Images in validation set")
    for i in range(len(img_ids)):
        if i % 10 == 0 and i != 0:
            print("Processed {} images".format(i))
        img = coco.loadImgs(img_ids[i])[0]
        file_name = img['file_name']
        file_path = os.path.join(image_dir, file_name)

        oriImg = cv2.imread(file_path)
        # Get the shortest side of the image (either height or width)
        shape_dst = np.min(oriImg.shape[0:2])

        # Get results of original image
        paf, heatmap, scale_img = get_outputs(oriImg, model,  preprocess)

        humans = paf_to_pose_cpp(heatmap, paf, cfg)
                
        out = draw_humans(oriImg, humans)
            
        vis_path = os.path.join(vis_dir, file_name)
        cv2.imwrite(vis_path, out)
        # subset indicated how many peoples foun in this image.
        upsample_keypoints = (heatmap.shape[0]*cfg.MODEL.DOWNSAMPLE/scale_img, heatmap.shape[1]*cfg.MODEL.DOWNSAMPLE/scale_img)
        append_result(img_ids[i], humans, upsample_keypoints, outputs)

    # Eval and show the final result!
    return eval_coco(outputs=outputs, annFile=anno_file, imgIds=img_ids)
                    help="Modify config options using the command-line",
                    default=None,
                    nargs=argparse.REMAINDER)
args = parser.parse_args()

# update config file
update_config(cfg, args)

weight_name = '/home/tensorboy/Downloads/pose_model.pth'

model = get_model('vgg19')
model.load_state_dict(torch.load(weight_name))
model = torch.nn.DataParallel(model).cuda()
model.float()
model.eval()

test_image = './readme/ski.jpg'
oriImg = cv2.imread(test_image)  # B,G,R order
shape_dst = np.min(oriImg.shape[0:2])

# Get results of original image

with torch.no_grad():
    paf, heatmap, im_scale = get_outputs(oriImg, model, 'rtpose')

print(im_scale)
humans = paf_to_pose_cpp(heatmap, paf, cfg)

out = draw_humans(oriImg, humans)
cv2.imwrite('result.png', out)
Esempio n. 3
0
# webcam image source
video_capture = cv2.VideoCapture(0)
imgnum=0

while True:
    
    ret, frame = video_capture.read()      
           
    shape_dst = np.min(frame.shape[0:2])   
       
    with torch.no_grad():
        paf, heatmap, im_scale = get_outputs(frame, model,  'rtpose')              
    
    humans = paf_to_pose_cpp(heatmap, paf, cfg)
            
    out = draw_humans(frame, humans)      
        
    cv2.imshow('Video', out)    
    
    # skip the first image and write the rest of the stream
    if (imgnum > 0):
        cv2.imwrite('./LiveImages/image' + str(imgnum) + '.png',out) 

    # break loop with key press
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    
    imgnum+=1
    

# create the video
Esempio n. 4
0
    x,y,w,h=([int(s) for s in lines[-1].split()]) 

    test_image = root_path+'images/'+filename+'.jpg'
	
    oriImg = cv2.imread(test_image) # B,G,R order
    shape_dst = np.min(oriImg.shape[0:2])

    # Get results of image
    with torch.no_grad():
        paf, heatmap, im_scale = get_outputs(oriImg, model,  'rtpose')
	
    # Get keypoints for each human  
    humans = paf_to_pose_cpp(heatmap, paf, cfg)

    # Get keypoint coordinates for each hand
    out,centers = draw_humans(oriImg, humans,x,y,w,h)

    f=open("data.csv",'a+')

    #TODO use some hueristic to select the hand automatically
    while True:
       l=[]
       cv2.imshow('result.png',out)   

       # Press right key if obj in right hand 
       if cv2.waitKey(0) & 0xFF == 83:
            for center,value in centers.items():
                if(center==2 or center==3 or center==4):
                    print(''.join(re.sub(r"\(*\)*", "", str(value)))+",")
                    val=''.join(re.sub(r"\(*\)*", "", str(value)))+",";
                    l1=(val.split(','))
Esempio n. 5
0
            time.sleep(2)
        type_, frame = device.get_next_frame()
        if type_ is FrameType.Color: # FrameFormat.BGRX
            rgb = frame.to_array().astype(np.uint8)[:,:,0:3]
            # cv2.imshow('rgb', rgb[:,:,0:3])
            oriImg = rgb
        else:
            continue
        
        shape_dst = np.min(oriImg.shape[0:2])

        with torch.no_grad():
            paf, heatmap, imscale = get_outputs(
                oriImg, model, 'rtpose')
                  
        humans = paf_to_pose_cpp(heatmap, paf, cfg)
                
        out = draw_humans(oriImg, humans, imgcopy=True)
        # print(type(out))

        # Display the resulting frame
        cv2.imshow('Video', out)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            device.stop()
            break

    # When everything is done, release the capture
    video_capture.release()
    cv2.destroyAllWindows()