Coverage for skcvideo/images_list_incrustation.py: 0%

72 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-10-02 14:10 +0200

1import os 

2 

3import cv2 

4import numpy as np 

5 

6 

7class ImagesListIncrustation: 

8 def __init__(self, box=None, images_paths=None, shuffle=False, maximum_length=None): 

9 if box is None: 

10 box = [0, 575, 720, 575 + 1280] 

11 self.box = box 

12 self.images_paths = images_paths 

13 

14 if shuffle: 

15 indices = np.random.permutation(len(self.images_paths)) 

16 if maximum_length is not None: 

17 indices = indices[:maximum_length] 

18 self.images_paths = [self.images_paths[i] for i in indices] 

19 elif maximum_length is not None: 

20 self.images_paths = self.images_paths[:maximum_length] 

21 

22 self.image_name_to_i = {} 

23 for i, image_path in enumerate(self.images_paths): 

24 image_name = os.path.splitext(os.path.basename(image_path))[0] 

25 self.image_name_to_i[image_name] = i 

26 

27 self.images_list = [] 

28 for image_path in self.images_paths: 

29 image = cv2.imread(image_path) 

30 self.images_list.append(image) 

31 

32 @property 

33 def max_frame(self): 

34 return len(self.images_list) - 1 

35 

36 def build(self, *args, **kwargs): 

37 pass 

38 

39 def get_display_box(self, image): 

40 y1, x1, y2, x2 = self.box 

41 box_height, box_width = y2 - y1, x2 - x1 

42 im_height, im_width = image.shape[:2] 

43 

44 ratio = min(box_height / im_height, box_width / im_width) 

45 new_im_height, new_im_width = int(im_height * ratio), int(im_width * ratio) 

46 y_offset, x_offset = y1 + int((box_height - new_im_height) / 2), x1 + int((box_width - new_im_width) / 2) 

47 return y_offset, x_offset, y_offset + new_im_height, x_offset + new_im_width 

48 

49 def incrust_image(self, big_image, image, frame): 

50 im_height, im_width = image.shape[:2] 

51 y1, x1, y2, x2 = self.get_display_box(image) 

52 

53 if im_height != (y2 - y1) or im_width != (x2 - x1): 

54 image = cv2.resize(image, (x2 - x1, y2 - y1)) 

55 image = self.process_displayed_image(image, frame) 

56 big_image[y1:y2, x1:x2, :] = image 

57 

58 def process_original_image(self, image, frame): 

59 return image 

60 

61 def process_displayed_image(self, image, frame): 

62 return image 

63 

64 def refresh(self, big_image, frame): 

65 image = self.images_list[frame].copy() 

66 image = self.process_original_image(image, frame) 

67 self.incrust_image(big_image, image, frame) 

68 

69 def get_image_path(self, frame): 

70 return self.images_paths[frame] 

71 

72 def get_frame(self, image_name): 

73 return self.image_name_to_i.get(image_name, None) 

74 

75 def convert_to_original_coordinates(self, x, y, frame): 

76 image = self.images_list[frame] 

77 y1, x1, y2, x2 = self.get_display_box(image) 

78 h, w = image.shape[:2] 

79 x = int(np.round((x - x1) / (x2 - x1) * w)) 

80 y = int(np.round((y - y1) / (y2 - y1) * h)) 

81 x = min(max(x, 0), w - 1) 

82 y = min(max(y, 0), h - 1) 

83 return x, y 

84 

85 def convert_to_display_coordinates(self, x, y, frame): 

86 image = self.images_list[frame] 

87 y1, x1, y2, x2 = self.get_display_box(image) 

88 h, w = image.shape[:2] 

89 x = int(np.round(x / w * (x2 - x1) + x1)) 

90 y = int(np.round(y / h * (y2 - y1) + y1)) 

91 return x, y