-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot_analysis_function.py
More file actions
56 lines (42 loc) · 1.78 KB
/
Copy pathrobot_analysis_function.py
File metadata and controls
56 lines (42 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# This is my analysis function for the robot video.
# It will do the following:
# 1. Statefully track how many frames the robot is upside down
# 2. Draw the state on each frame
# Notice that this class can be arbitrarily complex and/or stateful. You could keep track of the previous frames, or whatever else you need.
import cv2
import numpy as np
from framework.frames import ProcessedFrame
class RobotUpsideDownAnalysis:
def __init__(self):
self.robot_upside_down_frame_count = 0
def analyze_frame(self, frame: ProcessedFrame) -> ProcessedFrame:
metadata = frame.metadata
is_upside_down = metadata["is_upside_down"]
# update the state if appropriate
if is_upside_down:
self.robot_upside_down_frame_count += 1
# draw the current state onto the frame
frame.image = self.draw_state(frame.image)
# return the modified frame so it can be written to the video
return frame
def draw_state(self, image) -> np.ndarray:
text = f"Number of frames upside down: {self.robot_upside_down_frame_count}"
# Get image dimensions and calculate text position
height, width = image.shape[:2]
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1.5
thickness = 2
# Get text size to position it properly
(text_width, text_height), _ = cv2.getTextSize(
text, font, font_scale, thickness
)
text_x = 10
text_y = height - 20
# Draw text with black background for better visibility
cv2.putText(
image, text, (text_x, text_y), font, font_scale, (0, 0, 0), thickness + 1
)
cv2.putText(
image, text, (text_x, text_y), font, font_scale, (255, 255, 255), thickness
)
return image