thinh-huynh-re commited on
Commit
6b89aad
1 Parent(s): 0d6e19f
Files changed (4) hide show
  1. .gitignore +1 -0
  2. run_opencv.py +26 -7
  3. utils/frame_rate.py +7 -2
  4. utils/img_container.py +3 -0
.gitignore CHANGED
@@ -2,3 +2,4 @@ __pycache__
2
  env
3
  tmp/*
4
  !tmp/.gitkeep
 
 
2
  env
3
  tmp/*
4
  !tmp/.gitkeep
5
+ *.mp4
run_opencv.py CHANGED
@@ -83,12 +83,23 @@ SKIP_FRAMES = 4
83
  num_skips = 0
84
 
85
  # define a video capture object
86
- vid = cv2.VideoCapture(0)
87
 
88
- while vid.isOpened():
 
 
 
 
 
 
 
 
 
 
 
89
  # Capture the video frame
90
  # by frame
91
- ret, frame = vid.read()
92
 
93
  num_skips = (num_skips + 1) % SKIP_FRAMES
94
 
@@ -97,19 +108,27 @@ while vid.isOpened():
97
 
98
  if num_skips == 0:
99
  img_container.add_frame(frame)
100
- inference()
101
- rs = img_container.frame_rate.show_fps(frame)
102
 
103
  # Display the resulting frame
104
  cv2.imshow("ActivityTracking", rs)
105
 
 
 
 
106
  # the 'q' button is set as the
107
  # quitting button you may use any
108
  # desired button of your choice
109
- if cv2.waitKey(1) & 0xFF == ord("q"):
 
 
110
  break
 
 
111
 
112
  # After the loop release the cap object
113
- vid.release()
 
114
  # Destroy all the windows
115
  cv2.destroyAllWindows()
 
83
  num_skips = 0
84
 
85
  # define a video capture object
86
+ camera = cv2.VideoCapture(0)
87
 
88
+ frame_width = int(camera.get(3))
89
+ frame_height = int(camera.get(4))
90
+ size = (frame_width, frame_height)
91
+
92
+ video_output = cv2.VideoWriter(
93
+ "activities.mp4", cv2.VideoWriter_fourcc(*"MJPG"), 10, size
94
+ )
95
+
96
+ if camera.isOpened() == False:
97
+ print("Error reading video file")
98
+
99
+ while camera.isOpened():
100
  # Capture the video frame
101
  # by frame
102
+ ret, frame = camera.read()
103
 
104
  num_skips = (num_skips + 1) % SKIP_FRAMES
105
 
 
108
 
109
  if num_skips == 0:
110
  img_container.add_frame(frame)
111
+ # inference()
112
+ rs = img_container.frame_rate.show_fps(frame, img_container.is_recording)
113
 
114
  # Display the resulting frame
115
  cv2.imshow("ActivityTracking", rs)
116
 
117
+ if img_container.is_recording:
118
+ video_output.write(rs)
119
+
120
  # the 'q' button is set as the
121
  # quitting button you may use any
122
  # desired button of your choice
123
+ k = cv2.waitKey(1)
124
+
125
+ if k == ord("q"):
126
  break
127
+ elif k == ord("r"):
128
+ img_container.toggle_recording()
129
 
130
  # After the loop release the cap object
131
+ camera.release()
132
+ video_output.release()
133
  # Destroy all the windows
134
  cv2.destroyAllWindows()
utils/frame_rate.py CHANGED
@@ -27,9 +27,9 @@ class FrameRate:
27
  self.fps = self.NO_FRAMES / (end_time - self.start_time)
28
  self.start_time = end_time
29
 
30
- def show_fps(self, image: np.ndarray) -> np.ndarray:
31
  if self.fps != -1:
32
- return cv2.putText(
33
  image,
34
  f"FPS {self.fps:.0f} _ {self.label}",
35
  (50, 50),
@@ -38,5 +38,10 @@ class FrameRate:
38
  color=(255, 0, 0),
39
  thickness=2,
40
  )
 
 
 
 
 
41
  else:
42
  return image
 
27
  self.fps = self.NO_FRAMES / (end_time - self.start_time)
28
  self.start_time = end_time
29
 
30
+ def show_fps(self, image: np.ndarray, is_recording=False) -> np.ndarray:
31
  if self.fps != -1:
32
+ image = cv2.putText(
33
  image,
34
  f"FPS {self.fps:.0f} _ {self.label}",
35
  (50, 50),
 
38
  color=(255, 0, 0),
39
  thickness=2,
40
  )
41
+ if is_recording:
42
+ image = cv2.circle(
43
+ image, (50, 100), radius=10, color=(0, 0, 255), thickness=-1
44
+ )
45
+ return image
46
  else:
47
  return image
utils/img_container.py CHANGED
@@ -20,6 +20,9 @@ class ImgContainer:
20
  self.imgs.pop(0)
21
  self.imgs.append(frame)
22
 
 
 
 
23
  @property
24
  def ready(self):
25
  return len(self.imgs) == self.frames_per_video
 
20
  self.imgs.pop(0)
21
  self.imgs.append(frame)
22
 
23
+ def toggle_recording(self) -> None:
24
+ self.is_recording = not self.is_recording
25
+
26
  @property
27
  def ready(self):
28
  return len(self.imgs) == self.frames_per_video