Phani-1 commited on
Commit
4caa06a
·
verified ·
1 Parent(s): d9b157e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -8
app.py CHANGED
@@ -1,4 +1,3 @@
1
- import gc
2
  import cv2
3
  import tempfile
4
  import numpy as np
@@ -18,61 +17,80 @@ POSE_CONNECTIONS = [
18
  (30, 32)
19
  ]
20
 
 
21
  @st.cache_resource
22
  def load_model():
23
  return tf.saved_model.load("Models/ssd_mobilenet/saved_model")
24
 
 
25
  model = load_model()
26
  mp_pose = mp.solutions.pose
27
 
28
  labels = {1: 'person'}
29
 
 
30
  def detect_persons(image):
31
  tensor_img = tf.convert_to_tensor(image)
32
  tensor_img = tensor_img[tf.newaxis, ...]
 
33
  detections = model(tensor_img)
 
34
  boxes = detections['detection_boxes'][0].numpy()
35
  scores = detections['detection_scores'][0].numpy()
36
  classes = detections['detection_classes'][0].numpy().astype(np.int32)
 
37
  return boxes, scores, classes
38
 
 
39
  def draw_landmarks(img, landmarks):
40
  height, width, _ = img.shape
41
  for lm in landmarks.landmark:
42
  cx, cy = int(lm.x * width), int(lm.y * height)
43
  cv2.circle(img, (cx, cy), 8, (0, 0, 255), -1)
 
44
  for connection in POSE_CONNECTIONS:
45
  start_idx, end_idx = connection
46
  if landmarks.landmark[start_idx] and landmarks.landmark[end_idx]:
47
  start_point = landmarks.landmark[start_idx]
48
  end_point = landmarks.landmark[end_idx]
 
49
  start_coordinates = (int(start_point.x * width), int(start_point.y * height))
50
  end_coordinates = (int(end_point.x * width), int(end_point.y * height))
 
51
  cv2.line(img, start_coordinates, end_coordinates, (0, 255, 0), 3)
 
52
  return img
53
 
 
54
  def draw_bounding_box(img, box, width, height):
55
  y_min, x_min, y_max, x_max = box
56
  left, right, top, bottom = x_min * width, x_max * width, y_min * height, y_max * height
57
  cv2.rectangle(img, (int(left), int(top)), (int(right), int(bottom)), (255, 0, 0), 2)
58
 
 
59
  def process_frame(frame, pose, draw_box):
60
  image_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
61
  boxes, scores, classes = detect_persons(image_rgb)
 
62
  height, width, _ = frame.shape
63
  for i in range(len(scores)):
64
  if scores[i] > 0.5 and classes[i] == 1:
65
  y_min, x_min, y_max, x_max = boxes[i]
66
  left, right, top, bottom = x_min * width, x_max * width, y_min * height, y_max * height
67
  person_roi = frame[int(top):int(bottom), int(left):int(right)]
 
68
  results = pose.process(cv2.cvtColor(person_roi, cv2.COLOR_BGR2RGB))
 
69
  if results.pose_landmarks:
70
  person_roi = draw_landmarks(person_roi, results.pose_landmarks)
 
71
  frame[int(top):int(bottom), int(left):int(right)] = person_roi
72
  if draw_box:
73
  draw_bounding_box(frame, boxes[i], width, height)
 
74
  return frame
75
 
 
76
  def main():
77
  st.markdown(
78
  """
@@ -153,9 +171,7 @@ def main():
153
  frame = process_frame(frame, pose, draw_box)
154
 
155
  st_frame.image(frame, channels='BGR', use_column_width=True)
156
-
157
- # Ensure proper synchronization and frame display
158
- st_frame.empty()
159
 
160
  st.text("Completed")
161
  cam.release()
@@ -166,8 +182,6 @@ def main():
166
 
167
  st.image(processed_image, channels='BGR', use_column_width=True)
168
 
169
- gc.collect()
170
-
171
  elif operation_type == "Demo":
172
  st.empty()
173
  st.markdown("<p class='intro'>Demo video will be shown below:</p>", unsafe_allow_html=True)
@@ -185,11 +199,11 @@ def main():
185
  frame = process_frame(frame, pose, draw_box=False)
186
 
187
  st_frame.image(frame, channels='BGR', use_column_width=True)
188
- st_frame.empty()
189
 
190
  st.text("Completed")
191
  cam.release()
192
- gc.collect()
193
 
194
  if __name__ == "__main__":
195
  main()
 
 
1
  import cv2
2
  import tempfile
3
  import numpy as np
 
17
  (30, 32)
18
  ]
19
 
20
+
21
  @st.cache_resource
22
  def load_model():
23
  return tf.saved_model.load("Models/ssd_mobilenet/saved_model")
24
 
25
+
26
  model = load_model()
27
  mp_pose = mp.solutions.pose
28
 
29
  labels = {1: 'person'}
30
 
31
+
32
  def detect_persons(image):
33
  tensor_img = tf.convert_to_tensor(image)
34
  tensor_img = tensor_img[tf.newaxis, ...]
35
+
36
  detections = model(tensor_img)
37
+
38
  boxes = detections['detection_boxes'][0].numpy()
39
  scores = detections['detection_scores'][0].numpy()
40
  classes = detections['detection_classes'][0].numpy().astype(np.int32)
41
+
42
  return boxes, scores, classes
43
 
44
+
45
  def draw_landmarks(img, landmarks):
46
  height, width, _ = img.shape
47
  for lm in landmarks.landmark:
48
  cx, cy = int(lm.x * width), int(lm.y * height)
49
  cv2.circle(img, (cx, cy), 8, (0, 0, 255), -1)
50
+
51
  for connection in POSE_CONNECTIONS:
52
  start_idx, end_idx = connection
53
  if landmarks.landmark[start_idx] and landmarks.landmark[end_idx]:
54
  start_point = landmarks.landmark[start_idx]
55
  end_point = landmarks.landmark[end_idx]
56
+
57
  start_coordinates = (int(start_point.x * width), int(start_point.y * height))
58
  end_coordinates = (int(end_point.x * width), int(end_point.y * height))
59
+
60
  cv2.line(img, start_coordinates, end_coordinates, (0, 255, 0), 3)
61
+
62
  return img
63
 
64
+
65
  def draw_bounding_box(img, box, width, height):
66
  y_min, x_min, y_max, x_max = box
67
  left, right, top, bottom = x_min * width, x_max * width, y_min * height, y_max * height
68
  cv2.rectangle(img, (int(left), int(top)), (int(right), int(bottom)), (255, 0, 0), 2)
69
 
70
+
71
  def process_frame(frame, pose, draw_box):
72
  image_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
73
  boxes, scores, classes = detect_persons(image_rgb)
74
+
75
  height, width, _ = frame.shape
76
  for i in range(len(scores)):
77
  if scores[i] > 0.5 and classes[i] == 1:
78
  y_min, x_min, y_max, x_max = boxes[i]
79
  left, right, top, bottom = x_min * width, x_max * width, y_min * height, y_max * height
80
  person_roi = frame[int(top):int(bottom), int(left):int(right)]
81
+
82
  results = pose.process(cv2.cvtColor(person_roi, cv2.COLOR_BGR2RGB))
83
+
84
  if results.pose_landmarks:
85
  person_roi = draw_landmarks(person_roi, results.pose_landmarks)
86
+
87
  frame[int(top):int(bottom), int(left):int(right)] = person_roi
88
  if draw_box:
89
  draw_bounding_box(frame, boxes[i], width, height)
90
+
91
  return frame
92
 
93
+
94
  def main():
95
  st.markdown(
96
  """
 
171
  frame = process_frame(frame, pose, draw_box)
172
 
173
  st_frame.image(frame, channels='BGR', use_column_width=True)
174
+ st.empty()
 
 
175
 
176
  st.text("Completed")
177
  cam.release()
 
182
 
183
  st.image(processed_image, channels='BGR', use_column_width=True)
184
 
 
 
185
  elif operation_type == "Demo":
186
  st.empty()
187
  st.markdown("<p class='intro'>Demo video will be shown below:</p>", unsafe_allow_html=True)
 
199
  frame = process_frame(frame, pose, draw_box=False)
200
 
201
  st_frame.image(frame, channels='BGR', use_column_width=True)
202
+ st.empty()
203
 
204
  st.text("Completed")
205
  cam.release()
206
+
207
 
208
  if __name__ == "__main__":
209
  main()