数美的缺口识别 + 轨迹生成
- import math
- import random
-
- import cv2
- import numpy as np
- import requests
-
-
- def get_distance(fg, bg):
- fg_image = np.asarray(
- bytearray(requests.get(url=fg, verify=False).content), dtype="uint8"
- )
- fg_image = cv2.imdecode(fg_image, 1)
- shape = fg_image.shape
- x_points, y_points = [], []
- for x_point in range(shape[0]):
- for y_point in range(shape[1]):
- if list(fg_image[x_point][y_point]) != [0, 0, 0]:
- x_points.append(x_point)
- y_points.append(y_point)
- fg_cut_image = fg_image[
- min(x_points) : max(x_points),
- ]
-
- bg_image = np.asarray(
- bytearray(requests.get(url=bg, verify=False).content), dtype="uint8"
- )
- bg_image = cv2.imdecode(bg_image, 1)
- bg_cut_image = bg_image[
- min(x_points) : max(x_points),
- ]
-
- result = cv2.matchTemplate(bg_cut_image, fg_cut_image, cv2.TM_CCOEFF_NORMED)
- min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
- distance = max_loc[0]
- return int(distance)
-
-
- def get_track(distance: int):
- """
- 获取轨迹
- Args:
- distance: 距离
-
- Returns:
-
- """
- x = [0, 0]
- y = [0, 0, 0]
- z = [0]
- count = np.linspace(-math.pi / 2, math.pi / 2, random.randrange(20, 30))
- func = list(map(math.sin, count))
- nx = [i + 1 for i in func]
- add = random.randrange(10, 15)
- sadd = distance + add
- x.extend(list(map(lambda x: x * (sadd / 2), nx)))
- x.extend(np.linspace(sadd, distance, 3 if add > 12 else 2))
- x = [math.floor(i) for i in x]
- for i in range(len(x) - 2):
- if y[-1] < 30:
- y.append(y[-1] + random.choice([0, 0, 1, 1, 2, 2, 1, 2, 0, 0, 3, 3]))
- else:
- y.append(
- y[-1] + random.choice([0, 0, -1, -1, -2, -2, -1, -2, 0, 0, -3, -3])
- )
- for i in range(len(x) - 1):
- z.append(
- (z[-1] // 100 * 100)
- + 100
- + random.choice([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2])
- )
- trace = list(map(list, zip(x, y, z)))
- times = trace[-1][-1] + random.randint(1, 5)
- return trace, times
-
- fg = 'https://castatic.fengkongcloud.cn/crb/slide-atlas-default-without-logo-20230423/v4/c7db06c0414b3faa735cbc324bdfd7ff_fg.png'
- bg = 'https://castatic.fengkongcloud.cn/crb/slide-atlas-default-without-logo-20230423/v4/c7db06c0414b3faa735cbc324bdfd7ff_bg.jpg'
- print(get_distance(fg, bg))
- print(get_track(get_distance(fg, bg)))