iDrops commited on
Commit
02d8c15
·
verified ·
1 Parent(s): 5651657

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +64 -68
utils.py CHANGED
@@ -9,16 +9,14 @@ incorrect = cv2.imread('wrong.png')
9
  incorrect = cv2.cvtColor(incorrect, cv2.COLOR_BGR2RGB)
10
 
11
  def draw_rounded_rect(img, rect_start, rect_end, corner_width, box_color):
12
- """
13
- This function draws a rectangle with rounded corners on an image.
14
- Args:
15
- img: The image to draw on.
16
- rect_start: The top-left corner of the rectangle as a tuple (x1, y1).
17
- rect_end: The bottom-right corner of the rectangle as a tuple (x2, y2).
18
- corner_width: The width of the rounded corners.
19
- box_color: The color of the rectangle in BGR format.
20
- """
21
-
22
 
23
  x1, y1 = rect_start
24
  x2, y2 = rect_end
@@ -47,16 +45,16 @@ def draw_rounded_rect(img, rect_start, rect_end, corner_width, box_color):
47
  return img
48
 
49
  def draw_dotted_line(frame, lm_coord, start, end, line_color):
50
- """
51
- This function draws a dotted line on a frame based on landmark coordinates.
52
 
53
- Args:
54
- frame: The image to draw on.
55
- lm_coord: The landmark coordinates as a NumPy array.
56
- start: The index of the starting landmark in the lm_coord array.
57
- end: The index of the ending landmark in the lm_coord array.
58
- line_color: The color of the line in BGR format.
59
- """
60
 
61
  pix_step = 0
62
 
@@ -80,25 +78,25 @@ def draw_text(
80
  overlay_image = False,
81
  overlay_type = None
82
  ):
83
- """
84
- This function draws text with a customizable background box on an image.
85
-
86
- Args:
87
- img: The image to draw on.
88
- msg: The message to display as a string.
89
- width: The thickness of the background box border (default: 7).
90
- font: The font style for the text (default: cv2.FONT_HERSHEY_SIMPLEX).
91
- pos: The top-left corner coordinates of the text box (default: (0, 0)).
92
- font_scale: The scaling factor for the font size (default: 1).
93
- font_thickness: The thickness of the text (default: 2).
94
- text_color: The color of the text in BGR format (default: green - (0, 255, 0)).
95
- text_color_bg: The color of the background box in BGR format (default: black - (0, 0, 0)).
96
- box_offset: The offset for the background box relative to the text (default: (20, 10)).
97
- overlay_image: Flag to display an overlay image inside the box (default: False).
98
- overlay_type: Type of overlay image ("correct" or "incorrect") - used when overlay_image is True.
99
- Returns:
100
- The size of the drawn text (width, height) as a NumPy array.
101
- """
102
 
103
  offset = box_offset
104
  x, y = pos
@@ -150,17 +148,17 @@ def draw_text(
150
  return text_size
151
 
152
  def find_angle(p1, p2, ref_pt = np.array([0,0])):
153
- """
154
- This function calculates the angle between two points relative to a reference point.
155
 
156
- Args:
157
- p1: The first point coordinates as a NumPy array (x, y).
158
- p2: The second point coordinates as a NumPy array (x, y).
159
- ref_pt: The reference point coordinates as a NumPy array (default: [0, 0]).
 
 
 
 
 
160
 
161
- Returns:
162
- The angle between the two points in degrees (int).
163
- """
164
  # Subtract the reference point from both points for normalization
165
  p1_ref = p1 - ref_pt
166
  p2_ref = p2 - ref_pt
@@ -176,18 +174,18 @@ def find_angle(p1, p2, ref_pt = np.array([0,0])):
176
  return int(degree)
177
 
178
  def get_landmark_array(pose_landmark, key, frame_width, frame_height):
179
- """
180
- This function extracts the normalized image coordinates for a landmark.
181
 
182
- Args:
183
- pose_landmark: A MediaPipe pose landmark object.
184
- key: The key name of the landmark to extract (e.g., 'nose', 'shoulder.x').
185
- frame_width: The width of the image frame.
186
- frame_height: The height of the image frame.
 
 
 
 
 
187
 
188
- Returns:
189
- A NumPy array containing the normalized x and y coordinates of the landmark.
190
- """
191
 
192
  denorm_x = int(pose_landmark[key].x * frame_width)
193
  denorm_y = int(pose_landmark[key].y * frame_height)
@@ -195,20 +193,18 @@ def get_landmark_array(pose_landmark, key, frame_width, frame_height):
195
  return np.array([denorm_x, denorm_y])
196
 
197
  def get_landmark_features(kp_results, dict_features, feature, frame_width, frame_height):
198
- """
199
- This function extracts landmark coordinates for various body parts based on a feature name.
200
 
201
- Args:
202
- kp_results: The MediaPipe pose landmark results object.
203
- dict_features: A dictionary containing landmark key names for different body parts.
204
- feature: The name of the body part feature to extract (e.g., 'nose', 'left', 'right').
205
- frame_width: The width of the image frame.
206
- frame_height: The height of the image frame.
207
 
208
- Returns:
209
- A list containing the landmark coordinates (as NumPy arrays) or raises an error if the feature is invalid.
210
- """
 
 
 
211
 
 
 
212
 
213
  if feature == 'nose':
214
  return get_landmark_array(kp_results, dict_features[feature], frame_width, frame_height)
 
9
  incorrect = cv2.cvtColor(incorrect, cv2.COLOR_BGR2RGB)
10
 
11
  def draw_rounded_rect(img, rect_start, rect_end, corner_width, box_color):
12
+
13
+ # This function draws a rectangle with rounded corners on an image.
14
+ # Args:
15
+ #img: The image to draw on.
16
+ #rect_start: The top-left corner of the rectangle as a tuple (x1, y1).
17
+ #rect_end: The bottom-right corner of the rectangle as a tuple (x2, y2).
18
+ #corner_width: The width of the rounded corners.
19
+ #box_color: The color of the rectangle in BGR format.
 
 
20
 
21
  x1, y1 = rect_start
22
  x2, y2 = rect_end
 
45
  return img
46
 
47
  def draw_dotted_line(frame, lm_coord, start, end, line_color):
48
+
49
+ #This function draws a dotted line on a frame based on landmark coordinates.
50
 
51
+ #Args:
52
+ #frame: The image to draw on.
53
+ #lm_coord: The landmark coordinates as a NumPy array.
54
+ #start: The index of the starting landmark in the lm_coord array.
55
+ #end: The index of the ending landmark in the lm_coord array.
56
+ #line_color: The color of the line in BGR format.
57
+
58
 
59
  pix_step = 0
60
 
 
78
  overlay_image = False,
79
  overlay_type = None
80
  ):
81
+
82
+ #This function draws text with a customizable background box on an image.
83
+
84
+ #Args:
85
+ #img: The image to draw on.
86
+ #msg: The message to display as a string.
87
+ #width: The thickness of the background box border (default: 7).
88
+ #font: The font style for the text (default: cv2.FONT_HERSHEY_SIMPLEX).
89
+ #pos: The top-left corner coordinates of the text box (default: (0, 0)).
90
+ #font_scale: The scaling factor for the font size (default: 1).
91
+ #font_thickness: The thickness of the text (default: 2).
92
+ #text_color: The color of the text in BGR format (default: green - (0, 255, 0)).
93
+ #text_color_bg: The color of the background box in BGR format (default: black - (0, 0, 0)).
94
+ #box_offset: The offset for the background box relative to the text (default: (20, 10)).
95
+ #overlay_image: Flag to display an overlay image inside the box (default: False).
96
+ #overlay_type: Type of overlay image ("correct" or "incorrect") - used when overlay_image is True.
97
+ #Returns:
98
+ #The size of the drawn text (width, height) as a NumPy array.
99
+
100
 
101
  offset = box_offset
102
  x, y = pos
 
148
  return text_size
149
 
150
  def find_angle(p1, p2, ref_pt = np.array([0,0])):
 
 
151
 
152
+ #This function calculates the angle between two points relative to a reference point.
153
+
154
+ #Args:
155
+ #p1: The first point coordinates as a NumPy array (x, y).
156
+ #p2: The second point coordinates as a NumPy array (x, y).
157
+ #ref_pt: The reference point coordinates as a NumPy array (default: [0, 0]).
158
+
159
+ #Returns:
160
+ #The angle between the two points in degrees (int).
161
 
 
 
 
162
  # Subtract the reference point from both points for normalization
163
  p1_ref = p1 - ref_pt
164
  p2_ref = p2 - ref_pt
 
174
  return int(degree)
175
 
176
  def get_landmark_array(pose_landmark, key, frame_width, frame_height):
 
 
177
 
178
+ #This function extracts the normalized image coordinates for a landmark.
179
+
180
+ #Args:
181
+ #pose_landmark: A MediaPipe pose landmark object.
182
+ #key: The key name of the landmark to extract (e.g., 'nose', 'shoulder.x').
183
+ #frame_width: The width of the image frame.
184
+ #frame_height: The height of the image frame.
185
+
186
+ #Returns:
187
+ #A NumPy array containing the normalized x and y coordinates of the landmark.
188
 
 
 
 
189
 
190
  denorm_x = int(pose_landmark[key].x * frame_width)
191
  denorm_y = int(pose_landmark[key].y * frame_height)
 
193
  return np.array([denorm_x, denorm_y])
194
 
195
  def get_landmark_features(kp_results, dict_features, feature, frame_width, frame_height):
 
 
196
 
197
+ #This function extracts landmark coordinates for various body parts based on a feature name.
 
 
 
 
 
198
 
199
+ #Args:
200
+ #kp_results: The MediaPipe pose landmark results object.
201
+ #dict_features: A dictionary containing landmark key names for different body parts.
202
+ #feature: The name of the body part feature to extract (e.g., 'nose', 'left', 'right').
203
+ #frame_width: The width of the image frame.
204
+ #frame_height: The height of the image frame.
205
 
206
+ #Returns:
207
+ #A list containing the landmark coordinates (as NumPy arrays) or raises an error if the feature is invalid.
208
 
209
  if feature == 'nose':
210
  return get_landmark_array(kp_results, dict_features[feature], frame_width, frame_height)