Luigi commited on
Commit
35b2a45
·
1 Parent(s): 6008f96

Resize in keeping aspect ratio in visualization

Browse files
Files changed (3) hide show
  1. rtmo_demo.py +2 -2
  2. rtmo_demo_batch.py +3 -3
  3. rtmo_gpu.py +19 -0
rtmo_demo.py CHANGED
@@ -5,7 +5,7 @@ import cv2
5
  from pathlib import Path
6
  import argparse
7
  import os
8
- from rtmo_gpu import RTMO_GPU, draw_skeleton
9
 
10
  if __name__ == "__main__":
11
 
@@ -50,6 +50,6 @@ if __name__ == "__main__":
50
  scores,
51
  kpt_thr=0.3,
52
  line_width=2)
53
- img_show = cv2.resize(img_show, (788, 525))
54
  cv2.imshow(f'{model}', img_show)
55
  cv2.waitKey(10)
 
5
  from pathlib import Path
6
  import argparse
7
  import os
8
+ from rtmo_gpu import RTMO_GPU, draw_skeleton, resize_to_fit_screen
9
 
10
  if __name__ == "__main__":
11
 
 
50
  scores,
51
  kpt_thr=0.3,
52
  line_width=2)
53
+ img_show = resize_to_fit_screen(img_show, 720, 480)
54
  cv2.imshow(f'{model}', img_show)
55
  cv2.waitKey(10)
rtmo_demo_batch.py CHANGED
@@ -4,7 +4,7 @@ import time
4
  import cv2
5
  from pathlib import Path
6
  import argparse
7
- from rtmo_gpu import RTMO_GPU_Batch, draw_skeleton # Ensure to import RTMO_GPU_Batch
8
 
9
  def process_video(video_path, body_estimator, batch_size=4):
10
  cap = cv2.VideoCapture(video_path)
@@ -33,7 +33,7 @@ def process_video(video_path, body_estimator, batch_size=4):
33
  frame = batch_frames[i]
34
  img_show = frame.copy()
35
  img_show = draw_skeleton(img_show, keypoints, scores, kpt_thr=0.3, line_width=2)
36
- img_show = cv2.resize(img_show, (788, 525))
37
  cv2.imshow(f'{video_path}', img_show)
38
  cv2.waitKey(10)
39
 
@@ -56,7 +56,7 @@ def process_video(video_path, body_estimator, batch_size=4):
56
  frame = batch_frames[i]
57
  img_show = frame.copy()
58
  img_show = draw_skeleton(img_show, keypoints, scores, kpt_thr=0.3, line_width=2)
59
- img_show = cv2.resize(img_show, (720, 480))
60
  cv2.imshow(f'{video_path}', img_show)
61
  #cv2.waitKey(10)
62
 
 
4
  import cv2
5
  from pathlib import Path
6
  import argparse
7
+ from rtmo_gpu import RTMO_GPU_Batch, draw_skeleton, resize_to_fit_screen # Ensure to import RTMO_GPU_Batch
8
 
9
  def process_video(video_path, body_estimator, batch_size=4):
10
  cap = cv2.VideoCapture(video_path)
 
33
  frame = batch_frames[i]
34
  img_show = frame.copy()
35
  img_show = draw_skeleton(img_show, keypoints, scores, kpt_thr=0.3, line_width=2)
36
+ img_show = resize_to_fit_screen(img_show, 720, 480)
37
  cv2.imshow(f'{video_path}', img_show)
38
  cv2.waitKey(10)
39
 
 
56
  frame = batch_frames[i]
57
  img_show = frame.copy()
58
  img_show = draw_skeleton(img_show, keypoints, scores, kpt_thr=0.3, line_width=2)
59
+ img_show = resize_to_fit_screen(img_show, 720, 480)
60
  cv2.imshow(f'{video_path}', img_show)
61
  #cv2.waitKey(10)
62
 
rtmo_gpu.py CHANGED
@@ -561,3 +561,22 @@ class RTMO_GPU_Batch(RTMO_GPU):
561
  outputs = self.inference(batch_img)
562
  keypoints, scores = self.postprocess_batch(outputs, ratios)
563
  return keypoints, scores
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
561
  outputs = self.inference(batch_img)
562
  keypoints, scores = self.postprocess_batch(outputs, ratios)
563
  return keypoints, scores
564
+
565
+ def resize_to_fit_screen(image, screen_width, screen_height):
566
+ # Get the dimensions of the image
567
+ h, w = image.shape[:2]
568
+
569
+ # Calculate the aspect ratio of the image
570
+ aspect_ratio = w / h
571
+
572
+ # Determine the scaling factor
573
+ scale = min(screen_width / w, screen_height / h)
574
+
575
+ # Calculate the new dimensions
576
+ new_width = int(w * scale)
577
+ new_height = int(h * scale)
578
+
579
+ # Resize the image
580
+ resized_image = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA)
581
+
582
+ return resized_image