Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -284,6 +284,8 @@ def process_video(video_path, process_seconds=20, conf_threshold=0.2, max_det=8)
|
|
284 |
if torch.cuda.is_available():
|
285 |
heatmap = torch.zeros((height, width), device=device)
|
286 |
for x, y in filtered_points:
|
|
|
|
|
287 |
if 0 <= x < width and 0 <= y < height:
|
288 |
temp_heatmap = torch.zeros((height, width), device=device)
|
289 |
temp_heatmap[y, x] = 1
|
@@ -376,15 +378,18 @@ def get_gaussian_kernel2d(kernel_size, sigma):
|
|
376 |
|
377 |
def draw_line_gpu(image, pt1, pt2, color, thickness=1):
|
378 |
"""GPU版本的线段绘制"""
|
379 |
-
x1, y1 = pt1
|
380 |
-
x2, y2 = pt2
|
381 |
dx = abs(x2 - x1)
|
382 |
dy = abs(y2 - y1)
|
383 |
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
|
|
|
|
|
|
388 |
|
389 |
x_inc = (x2 - x1) / steps
|
390 |
y_inc = (y2 - y1) / steps
|
@@ -398,18 +403,17 @@ def draw_line_gpu(image, pt1, pt2, color, thickness=1):
|
|
398 |
x += x_inc
|
399 |
y += y_inc
|
400 |
|
401 |
-
points = points.long()
|
402 |
valid_points = (points[:, 0] >= 0) & (points[:, 0] < image.shape[1]) & \
|
403 |
(points[:, 1] >= 0) & (points[:, 1] < image.shape[0])
|
404 |
points = points[valid_points]
|
405 |
|
406 |
-
|
407 |
-
color = color.to(image.dtype) # 修改这里,确保颜色张量与图像类型匹配
|
408 |
|
409 |
if thickness > 1:
|
410 |
for dx in range(-thickness//2, thickness//2 + 1):
|
411 |
for dy in range(-thickness//2, thickness//2 + 1):
|
412 |
-
offset_points = points + torch.tensor([dx, dy], device=device, dtype=torch.long)
|
413 |
valid_offset = (offset_points[:, 0] >= 0) & (offset_points[:, 0] < image.shape[1]) & \
|
414 |
(offset_points[:, 1] >= 0) & (offset_points[:, 1] < image.shape[0])
|
415 |
offset_points = offset_points[valid_offset]
|
|
|
284 |
if torch.cuda.is_available():
|
285 |
heatmap = torch.zeros((height, width), device=device)
|
286 |
for x, y in filtered_points:
|
287 |
+
# 确保坐标是整数
|
288 |
+
x, y = int(x), int(y)
|
289 |
if 0 <= x < width and 0 <= y < height:
|
290 |
temp_heatmap = torch.zeros((height, width), device=device)
|
291 |
temp_heatmap[y, x] = 1
|
|
|
378 |
|
379 |
def draw_line_gpu(image, pt1, pt2, color, thickness=1):
|
380 |
"""GPU版本的线段绘制"""
|
381 |
+
x1, y1 = map(int, pt1) # 确保是整数
|
382 |
+
x2, y2 = map(int, pt2) # 确保是整数
|
383 |
dx = abs(x2 - x1)
|
384 |
dy = abs(y2 - y1)
|
385 |
|
386 |
+
# 防止除零错误
|
387 |
+
steps = max(dx, dy)
|
388 |
+
if steps == 0:
|
389 |
+
# 如果是同一个点,直接画点
|
390 |
+
if 0 <= x1 < image.shape[1] and 0 <= y1 < image.shape[0]:
|
391 |
+
image[y1, x1] = color
|
392 |
+
return
|
393 |
|
394 |
x_inc = (x2 - x1) / steps
|
395 |
y_inc = (y2 - y1) / steps
|
|
|
403 |
x += x_inc
|
404 |
y += y_inc
|
405 |
|
406 |
+
points = points.long() # 转换为整数类型
|
407 |
valid_points = (points[:, 0] >= 0) & (points[:, 0] < image.shape[1]) & \
|
408 |
(points[:, 1] >= 0) & (points[:, 1] < image.shape[0])
|
409 |
points = points[valid_points]
|
410 |
|
411 |
+
color = color.to(image.dtype)
|
|
|
412 |
|
413 |
if thickness > 1:
|
414 |
for dx in range(-thickness//2, thickness//2 + 1):
|
415 |
for dy in range(-thickness//2, thickness//2 + 1):
|
416 |
+
offset_points = points + torch.tensor([dx, dy], device=device, dtype=torch.long)
|
417 |
valid_offset = (offset_points[:, 0] >= 0) & (offset_points[:, 0] < image.shape[1]) & \
|
418 |
(offset_points[:, 1] >= 0) & (offset_points[:, 1] < image.shape[0])
|
419 |
offset_points = offset_points[valid_offset]
|