Hakureirm commited on
Commit
5a8da17
·
verified ·
1 Parent(s): 11c09d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -46
app.py CHANGED
@@ -100,49 +100,24 @@ def process_video(video_path, process_seconds=20, conf_threshold=0.2, max_det=8)
100
 
101
  # 收集位置信息
102
  if hasattr(r, 'keypoints') and r.keypoints is not None:
103
- # 打印关键点对象信息
104
- print(f"Keypoints type: {type(r.keypoints)}")
105
- print(f"Keypoints data: {r.keypoints}")
106
 
107
- for kpts in r.keypoints:
108
- if isinstance(kpts, torch.Tensor):
109
- kpts = kpts.cpu().numpy()
110
- print(f"Single keypoints shape: {kpts.shape}") # 打印形状
111
- print(f"Single keypoints data: {kpts}") # 打印数据
112
 
113
- # 确保关键点数据是正确的格式
114
- if isinstance(kpts, np.ndarray):
115
- if len(kpts.shape) == 3: # [num_objects, num_keypoints, 3]
116
- for obj_kpts in kpts:
117
- if len(obj_kpts) > 0:
118
- x, y = obj_kpts[0][:2] # 使用第一个关键点的x,y坐标
119
- if isinstance(x, (int, float)) and isinstance(y, (int, float)):
120
- x, y = int(x), int(y)
121
- all_positions.append([x, y])
122
- # 更新热图,使用高斯核来平滑
123
- if 0 <= x < width and 0 <= y < height:
124
- # 创建高斯核心点
125
- sigma = 5 # 调整这个值来改变热点大小
126
- kernel_size = 15 # 必须是奇数
127
- temp_heatmap = np.zeros((height, width), dtype=np.float32)
128
- temp_heatmap[y, x] = 1
129
- temp_heatmap = cv2.GaussianBlur(temp_heatmap, (kernel_size, kernel_size), sigma)
130
- heatmap += temp_heatmap
131
- elif len(kpts.shape) == 2: # [num_keypoints, 3]
132
- if len(kpts) > 0:
133
- x, y = kpts[0][:2] # 使用第一个关键点的x,y坐标
134
- if isinstance(x, (int, float)) and isinstance(y, (int, float)):
135
- x, y = int(x), int(y)
136
- all_positions.append([x, y])
137
- # 更新热图,使用高斯核来平滑
138
- if 0 <= x < width and 0 <= y < height:
139
- # 创建高斯核心点
140
- sigma = 5 # 调整这个值来改变热点大小
141
- kernel_size = 15 # 必须是奇数
142
- temp_heatmap = np.zeros((height, width), dtype=np.float32)
143
- temp_heatmap[y, x] = 1
144
- temp_heatmap = cv2.GaussianBlur(temp_heatmap, (kernel_size, kernel_size), sigma)
145
- heatmap += temp_heatmap
146
 
147
  # 收集检测信息
148
  frame_info = {
@@ -204,20 +179,35 @@ def process_video(video_path, process_seconds=20, conf_threshold=0.2, max_det=8)
204
  # 绘制轨迹线,使用渐变色
205
  for i in range(len(points) - 1):
206
  ratio = i / (len(points) - 1)
 
207
  color = (
208
  int((1 - ratio) * 255), # B
209
- 0, # G
210
  int(ratio * 255) # R
211
  )
212
  cv2.line(trajectory_img, tuple(points[i]), tuple(points[i + 1]), color, 2)
213
 
214
  # 绘制起点和终点
215
  cv2.circle(trajectory_img, tuple(points[0]), 8, (0, 255, 0), -1) # 绿色起点
216
- cv2.circle(trajectory_img, tuple(points[-1]), 8, (255, 0, 0), -1) # 红色终点
 
 
 
 
 
 
 
 
 
 
217
 
218
- # 生成热图
219
- heatmap_normalized = cv2.normalize(heatmap, None, 0, 255, cv2.NORM_MINMAX)
220
- heatmap_colored = cv2.applyColorMap(heatmap_normalized.astype(np.uint8), cv2.COLORMAP_JET)
 
 
 
 
221
 
222
  # 保存图像
223
  trajectory_path = output_path.replace('.mp4', '_trajectory.png')
 
100
 
101
  # 收集位置信息
102
  if hasattr(r, 'keypoints') and r.keypoints is not None:
103
+ kpts = r.keypoints.data
104
+ if isinstance(kpts, torch.Tensor):
105
+ kpts = kpts.cpu().numpy()
106
 
107
+ # 取第一个检测目标的第一个关键点(通常是头部)
108
+ if kpts.shape == (1, 8, 3): # [num_objects, num_keypoints, xyz]
109
+ x, y = int(kpts[0, 0, 0]), int(kpts[0, 0, 1]) # 使用第一个关键点
110
+ all_positions.append([x, y])
 
111
 
112
+ # 更新热图,使用高斯核来平滑
113
+ if 0 <= x < width and 0 <= y < height:
114
+ # 创建高斯核心点
115
+ sigma = 10 # 调整这个值来改变热点大小
116
+ kernel_size = 31 # 必须是奇数
117
+ temp_heatmap = np.zeros((height, width), dtype=np.float32)
118
+ temp_heatmap[y, x] = 1
119
+ temp_heatmap = cv2.GaussianBlur(temp_heatmap, (kernel_size, kernel_size), sigma)
120
+ heatmap += temp_heatmap
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
  # 收集检测信息
123
  frame_info = {
 
179
  # 绘制轨迹线,使用渐变色
180
  for i in range(len(points) - 1):
181
  ratio = i / (len(points) - 1)
182
+ # 使用从蓝到红的渐变色
183
  color = (
184
  int((1 - ratio) * 255), # B
185
+ 50, # G
186
  int(ratio * 255) # R
187
  )
188
  cv2.line(trajectory_img, tuple(points[i]), tuple(points[i + 1]), color, 2)
189
 
190
  # 绘制起点和终点
191
  cv2.circle(trajectory_img, tuple(points[0]), 8, (0, 255, 0), -1) # 绿色起点
192
+ cv2.circle(trajectory_img, tuple(points[-1]), 8, (0, 0, 255), -1) # 红色终点
193
+
194
+ # ��隔一定帧数添加方向箭头
195
+ arrow_interval = max(len(points) // 20, 1) # 控制箭头数量
196
+ for i in range(0, len(points) - arrow_interval, arrow_interval):
197
+ pt1 = tuple(points[i])
198
+ pt2 = tuple(points[i + arrow_interval])
199
+ # 计算箭头方向
200
+ angle = np.arctan2(pt2[1] - pt1[1], pt2[0] - pt1[0])
201
+ # 绘制箭头
202
+ cv2.arrowedLine(trajectory_img, pt1, pt2, (100, 100, 100), 1, tipLength=0.2)
203
 
204
+ # 处理热图
205
+ if np.max(heatmap) > 0: # 确保有数据
206
+ heatmap_normalized = cv2.normalize(heatmap, None, 0, 255, cv2.NORM_MINMAX)
207
+ heatmap_colored = cv2.applyColorMap(heatmap_normalized.astype(np.uint8), cv2.COLORMAP_JET)
208
+ # 添加一些透明度
209
+ alpha = 0.7
210
+ heatmap_colored = cv2.addWeighted(heatmap_colored, alpha, np.full_like(heatmap_colored, 255), 1-alpha, 0)
211
 
212
  # 保存图像
213
  trajectory_path = output_path.replace('.mp4', '_trajectory.png')