# Script execution but don't display images

**URL:** <https://forums.developer.nvidia.com/t/script-execution-but-dont-display-images/179727>\
**Category:** Jetson Nano\
**Tags:** tensorrt\
**Created:** [June 3, 2021, 12:51pm UTC](https://forums.developer.nvidia.com/t/script-execution-but-dont-display-images/179727 "2021-06-03T12:51:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![theo17300](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@theo17300](https://forums.developer.nvidia.com/u/theo17300)\
**Post date:** [June 3, 2021, 12:51pm UTC](https://forums.developer.nvidia.com/t/script-execution-but-dont-display-images/179727/1 "2021-06-03T12:51:03Z")

</div>

Hi all, I would do to work the script named detect\_video.py available on this website [NVIDIA Jetson and Raspberry Pi: JetsonNano - NVIDIA AI IOT - Human Pose estimation using TensorRT](https://spyjetson.blogspot.com/2019/12/jetsonnano-human-pose-estimation-using.html) (bottom of the page), but I have a great problem when in the while loop I add cv2.imshow(“Video”, img), it can’t display this window, but the number of frame per second is displayed on my terminal. However the save of MP4 file has done the result is good with the skeleton done by this algorithm. Why can’t I display this images with my cam directly ? Thanks for your help.

I have many error messages.:

```
`Matplotlib created a tempory config/cache directory at /tmp/matplotlib-wz7cf4ja because the default path (/home/vpl/.cache/matplotlib) is not writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing `

```

The second is :  
`[WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (933) open OpenCV | GStreamer warning : Cannot query video position: status=0, value=-1, duartion`

> import json  
> import trt\_pose.coco  
> import trt\_pose.models  
> import torch  
> import torch2trt  
> from torch2trt import TRTModule  
> import time, sys  
> import cv2  
> import torchvision.transforms as transforms  
> import PIL.Image  
> from trt\_pose.draw\_objects import DrawObjects  
> from trt\_pose.parse\_objects import ParseObjects  
> import argparse  
> import os.path

> def get\_keypoint(humans, hnum, peaks):  
> #check invalid human index  
> kpoint =   
> human = humans[0][hnum]  
> C = human.shape[0]  
> for j in range(C):  
> k = int(human[j])  
> if k \>= 0:  
> peak = peaks[0][j][k] # peak[1]:width, peak[0]:height  
> peak = (j, float(peak[0]), float(peak[1]))  
> kpoint.append(peak)  
> #print(‘index:%d : success [%5.3f, %5.3f]’%(j, peak[1], peak[2]) )  
> else:  
> peak = (j, None, None)  
> kpoint.append(peak)  
> #print(‘index:%d : None %d’%(j, k) )  
> return kpoint

> parser = argparse.ArgumentParser(description=‘TensorRT pose estimation run’)  
> parser.add\_argument(‘–model’, type=str, default=‘resnet’, help = ‘resnet or densenet’ )  
> args = parser.parse\_args()

> with open(‘human\_pose.json’, ‘r’) as f:  
> human\_pose = json.load(f)  
> topology = trt\_pose.coco.coco\_category\_to\_topology(human\_pose)  
> num\_parts = len(human\_pose[‘keypoints’])  
> num\_links = len(human\_pose[‘skeleton’])

> if ‘resnet’ in args.model:  
> print(‘------ model = resnet--------’)  
> MODEL\_WEIGHTS = ‘resnet18\_baseline\_att\_224x224\_A\_epoch\_249.pth’  
> OPTIMIZED\_MODEL = ‘resnet18\_baseline\_att\_224x224\_A\_epoch\_249\_trt.pth’  
> model = trt\_pose.models.resnet18\_baseline\_att(num\_parts, 2 \* num\_links).cuda().eval()  
> WIDTH = 224  
> HEIGHT = 224  
> else:  
> print(‘------ model = densenet--------’)  
> MODEL\_WEIGHTS = ‘densenet121\_baseline\_att\_256x256\_B\_epoch\_160.pth’  
> OPTIMIZED\_MODEL = ‘densenet121\_baseline\_att\_256x256\_B\_epoch\_160\_trt.pth’  
> model = trt\_pose.models.densenet121\_baseline\_att(num\_parts, 2 \* num\_links).cuda().eval()  
> WIDTH = 256  
> HEIGHT = 256

> data = torch.zeros((1, 3, HEIGHT, WIDTH)).cuda()  
> if os.path.exists(OPTIMIZED\_MODEL) == False:  
> model.load\_state\_dict(torch.load(MODEL\_WEIGHTS))  
> model\_trt = torch2trt.torch2trt(model, [data], fp16\_mode=True, max\_workspace\_size=1\<\<25)  
> torch.save(model\_trt.state\_dict(), OPTIMIZED\_MODEL)

> model\_trt = TRTModule()  
> model\_trt.load\_state\_dict(torch.load(OPTIMIZED\_MODEL))  
> t0 = time.time()  
> torch.cuda.current\_stream().synchronize()  
> for i in range(50):  
> y = model\_trt(data)

> torch.cuda.current\_stream().synchronize()  
> t1 = time.time()  
> print(50.0 / (t1 - t0))

> mean = torch.Tensor([0.485, 0.456, 0.406]).cuda()  
> std = torch.Tensor([0.229, 0.224, 0.225]).cuda()  
> device = torch.device(‘cuda’)

> def preprocess(image):  
> global device  
> device = torch.device(‘cuda’)  
> image = cv2.cvtColor(image, cv2.COLOR\_BGR2RGB)  
> image = PIL.Image.fromarray(image)  
> image = transforms.functional.to\_tensor(image).to(device)  
> image.sub\_(mean[:, None, None]).div\_(std[:, None, None])  
> return image[None, …]

> def execute(img, src, t):  
> color = (0, 255, 0)  
> data = preprocess(img)  
> cmap, paf = model\_trt(data)  
> cmap, paf = cmap.detach().cpu(), paf.detach().cpu()  
> counts, objects, peaks = parse\_objects(cmap, paf)#, cmap\_threshold=0.15, link\_threshold=0.15)  
> fps = 1.0 / (time.time() - t)  
> for i in range(counts[0]):  
> keypoints = get\_keypoint(objects, i, peaks)  
> for j in range(len(keypoints)):  
> if keypoints[j][1]:  
> x = round(keypoints[j][2] \* WIDTH \* X\_compress)  
> y = round(keypoints[j][1] \* HEIGHT \* Y\_compress)  
> cv2.circle(src, (x, y), 3, color, 2)  
> cv2.putText(src , “%d” % int(keypoints[j][0]), (x + 5, y), cv2.FONT\_HERSHEY\_SIMPLEX, 1, (0, 255, 255), 1)  
> cv2.circle(src, (x, y), 3, color, 2)  
> print("FPS:%f "%(fps))  
> #draw\_objects(img, counts, objects, peaks)  
> cv2.putText(src , “FPS: %f” % (fps), (20, 20), cv2.FONT\_HERSHEY\_SIMPLEX, 1, (0, 255, 0), 1)  
> out\_video.write(src)

> cap = cv2.VideoCapture(0)  
> cap.set(cv2.CAP\_PROP\_FRAME\_WIDTH, 640)  
> cap.set(cv2.CAP\_PROP\_FRAME\_HEIGHT, 480)

> ret\_val, img = cap.read()  
> fourcc = cv2.VideoWriter\_fourcc(‘m’, ‘p’, ‘4’, ‘v’)  
> out\_video = cv2.VideoWriter(‘/tmp/output.mp4’, fourcc, cap.get(cv2.CAP\_PROP\_FPS), (640, 480))  
> count = 0

> X\_compress = 640.0 / WIDTH \* 1.0  
> Y\_compress = 480.0 / HEIGHT \* 1.0

> if cap is None:  
> print(“Camera Open Error”)  
> sys.exit(0)

> parse\_objects = ParseObjects(topology)  
> draw\_objects = DrawObjects(topology)

> while cap.isOpened() and count \< 500:  
> t = time.time()  
> ret\_val, dst = cap.read()  
> if ret\_val == False:  
> print(“Camera read Error”)  
> break

> img = cv2.resize(dst, dsize=(WIDTH, HEIGHT), interpolation=cv2.INTER\_AREA)  
> execute(img, dst, t)  
> cv2.imshow(“Video”, img)  
> count += 1

> cv2.destroyAllWindows()  
> out\_video.release()  
> cap.release()

---

<div class="post-metadata">

**Author:** ![AastaLLL](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/aastalll/32/14043_2.png) [@AastaLLL](https://forums.developer.nvidia.com/u/AastaLLL)\
**Post date:** [June 4, 2021, 3:18am UTC](https://forums.developer.nvidia.com/t/script-execution-but-dont-display-images/179727/3 "2021-06-04T03:18:07Z")

</div>

Hi,

Could you add a waitKey to force OpenCV display the window immediately?

```auto
cv2.imshow(“Video”, img)
cv2.waitKey(0)

```

Thanks.

---

<div class="post-metadata">

**Author:** ![nadeemm](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/nadeemm/32/367020_2.png) [@nadeemm](https://forums.developer.nvidia.com/u/nadeemm)\
**Post date:** [October 15, 2021, 3:20am UTC](https://forums.developer.nvidia.com/t/script-execution-but-dont-display-images/179727/5 "2021-10-15T03:20:39Z")

</div>

Closed on October 15, 2021, 3:20am UTC


