utils.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. import re
  2. import cv2
  3. import sys
  4. import numpy as np
  5. import importlib
  6. from PIL import Image
  7. REGEX_NODE = re.compile(r'(-?[A-Za-z]+)(-?\d+)')
  8. def random_normal_distribution_int(a, b, n=3):
  9. """Generate a normal distribution int within the interval. Use the average value of several random numbers to
  10. simulate normal distribution.
  11. Args:
  12. a (int): The minimum of the interval.
  13. b (int): The maximum of the interval.
  14. n (int): The amount of numbers in simulation. Default to 3.
  15. Returns:
  16. int
  17. """
  18. if a < b:
  19. output = np.mean(np.random.randint(a, b, size=n))
  20. return int(output.round())
  21. else:
  22. return b
  23. def random_rectangle_point(area, n=3):
  24. """Choose a random point in an area.
  25. Args:
  26. area: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  27. n (int): The amount of numbers in simulation. Default to 3.
  28. Returns:
  29. tuple(int): (x, y)
  30. """
  31. x = random_normal_distribution_int(area[0], area[2], n=n)
  32. y = random_normal_distribution_int(area[1], area[3], n=n)
  33. return x, y
  34. def random_rectangle_vector(vector, box, random_range=(0, 0, 0, 0), padding=15):
  35. """Place a vector in a box randomly.
  36. Args:
  37. vector: (x, y)
  38. box: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  39. random_range (tuple): Add a random_range to vector. (x_min, y_min, x_max, y_max).
  40. padding (int):
  41. Returns:
  42. tuple(int), tuple(int): start_point, end_point.
  43. """
  44. vector = np.array(vector) + random_rectangle_point(random_range)
  45. vector = np.round(vector).astype(np.int)
  46. half_vector = np.round(vector / 2).astype(np.int)
  47. box = np.array(box) + np.append(np.abs(half_vector) + padding, -np.abs(half_vector) - padding)
  48. center = random_rectangle_point(box)
  49. start_point = center - half_vector
  50. end_point = start_point + vector
  51. return tuple(start_point), tuple(end_point)
  52. def random_rectangle_vector_opted(
  53. vector, box, random_range=(0, 0, 0, 0), padding=15, whitelist_area=None, blacklist_area=None):
  54. """
  55. Place a vector in a box randomly.
  56. When emulator/game stuck, it treats a swipe as a click, clicking at the end of swipe path.
  57. To prevent this, random results need to be filtered.
  58. Args:
  59. vector: (x, y)
  60. box: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  61. random_range (tuple): Add a random_range to vector. (x_min, y_min, x_max, y_max).
  62. padding (int):
  63. whitelist_area: (list[tuple[int]]):
  64. A list of area that safe to click. Swipe path will end there.
  65. blacklist_area: (list[tuple[int]]):
  66. If none of the whitelist_area satisfies current vector, blacklist_area will be used.
  67. Delete random path that ends in any blacklist_area.
  68. Returns:
  69. tuple(int), tuple(int): start_point, end_point.
  70. """
  71. vector = np.array(vector) + random_rectangle_point(random_range)
  72. vector = np.round(vector).astype(np.int)
  73. half_vector = np.round(vector / 2).astype(np.int)
  74. box_pad = np.array(box) + np.append(np.abs(half_vector) + padding, -np.abs(half_vector) - padding)
  75. box_pad = area_offset(box_pad, half_vector)
  76. segment = int(np.linalg.norm(vector) // 70) + 1
  77. def in_blacklist(end):
  78. if not blacklist_area:
  79. return False
  80. for x in range(segment + 1):
  81. point = - vector * x / segment + end
  82. for area in blacklist_area:
  83. if point_in_area(point, area, threshold=0):
  84. return True
  85. return False
  86. if whitelist_area:
  87. for area in whitelist_area:
  88. area = area_limit(area, box_pad)
  89. if all([x > 0 for x in area_size(area)]):
  90. end_point = random_rectangle_point(area)
  91. for _ in range(10):
  92. if in_blacklist(end_point):
  93. continue
  94. return point_limit(end_point - vector, box), point_limit(end_point, box)
  95. for _ in range(100):
  96. end_point = random_rectangle_point(box_pad)
  97. if in_blacklist(end_point):
  98. continue
  99. return point_limit(end_point - vector, box), point_limit(end_point, box)
  100. end_point = random_rectangle_point(box_pad)
  101. return point_limit(end_point - vector, box), point_limit(end_point, box)
  102. def random_line_segments(p1, p2, n, random_range=(0, 0, 0, 0)):
  103. """Cut a line into multiple segments.
  104. Args:
  105. p1: (x, y).
  106. p2: (x, y).
  107. n: Number of slice.
  108. random_range: Add a random_range to points.
  109. Returns:
  110. list[tuple]: [(x0, y0), (x1, y1), (x2, y2)]
  111. """
  112. return [tuple((((n - index) * p1 + index * p2) / n).astype(int) + random_rectangle_point(random_range))
  113. for index in range(0, n + 1)]
  114. def ensure_time(second, n=3, precision=3):
  115. """Ensure to be time.
  116. Args:
  117. second (int, float, tuple): time, such as 10, (10, 30), '10, 30'
  118. n (int): The amount of numbers in simulation. Default to 5.
  119. precision (int): Decimals.
  120. Returns:
  121. float:
  122. """
  123. if isinstance(second, tuple):
  124. multiply = 10 ** precision
  125. result = random_normal_distribution_int(second[0] * multiply, second[1] * multiply, n) / multiply
  126. return round(result, precision)
  127. elif isinstance(second, str):
  128. if ',' in second:
  129. lower, upper = second.replace(' ', '').split(',')
  130. lower, upper = int(lower), int(upper)
  131. return ensure_time((lower, upper), n=n, precision=precision)
  132. if '-' in second:
  133. lower, upper = second.replace(' ', '').split('-')
  134. lower, upper = int(lower), int(upper)
  135. return ensure_time((lower, upper), n=n, precision=precision)
  136. else:
  137. return int(second)
  138. else:
  139. return second
  140. def ensure_int(*args):
  141. """
  142. Convert all elements to int.
  143. Return the same structure as nested objects.
  144. Args:
  145. *args:
  146. Returns:
  147. list:
  148. """
  149. def to_int(item):
  150. try:
  151. return int(item)
  152. except TypeError:
  153. result = [to_int(i) for i in item]
  154. if len(result) == 1:
  155. result = result[0]
  156. return result
  157. return to_int(args)
  158. def area_offset(area, offset):
  159. """
  160. Move an area.
  161. Args:
  162. area: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  163. offset: (x, y).
  164. Returns:
  165. tuple: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  166. """
  167. upper_left_x, upper_left_y, bottom_right_x, bottom_right_y = area
  168. x, y = offset
  169. return upper_left_x + x, upper_left_y + y, bottom_right_x + x, bottom_right_y + y
  170. def area_pad(area, pad=10):
  171. """
  172. Inner offset an area.
  173. Args:
  174. area: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  175. pad (int):
  176. Returns:
  177. tuple: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  178. """
  179. upper_left_x, upper_left_y, bottom_right_x, bottom_right_y = area
  180. return upper_left_x + pad, upper_left_y + pad, bottom_right_x - pad, bottom_right_y - pad
  181. def expand_roi(roi, padding=2, screen_size=(1280, 720)):
  182. """Expand an (x, y, width, height) ROI and clip it to the screen bounds."""
  183. x, y, width, height = [int(value) for value in roi]
  184. screen_width, screen_height = screen_size
  185. x0 = min(max(0, x - padding), screen_width)
  186. y0 = min(max(0, y - padding), screen_height)
  187. x1 = min(max(0, x + width + padding), screen_width)
  188. y1 = min(max(0, y + height + padding), screen_height)
  189. return x0, y0, max(0, x1 - x0), max(0, y1 - y0)
  190. def limit_in(x, lower, upper):
  191. """
  192. Limit x within range (lower, upper)
  193. Args:
  194. x:
  195. lower:
  196. upper:
  197. Returns:
  198. int, float:
  199. """
  200. return max(min(x, upper), lower)
  201. def area_limit(area1, area2):
  202. """
  203. Limit an area in another area.
  204. Args:
  205. area1: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  206. area2: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  207. Returns:
  208. tuple: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  209. """
  210. x_lower, y_lower, x_upper, y_upper = area2
  211. return (
  212. limit_in(area1[0], x_lower, x_upper),
  213. limit_in(area1[1], y_lower, y_upper),
  214. limit_in(area1[2], x_lower, x_upper),
  215. limit_in(area1[3], y_lower, y_upper),
  216. )
  217. def area_size(area):
  218. """
  219. Area size or shape.
  220. Args:
  221. area: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  222. Returns:
  223. tuple: (x, y).
  224. """
  225. return (
  226. max(area[2] - area[0], 0),
  227. max(area[3] - area[1], 0)
  228. )
  229. def point_limit(point, area):
  230. """
  231. Limit point in an area.
  232. Args:
  233. point: (x, y).
  234. area: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  235. Returns:
  236. tuple: (x, y).
  237. """
  238. return (
  239. limit_in(point[0], area[0], area[2]),
  240. limit_in(point[1], area[1], area[3])
  241. )
  242. def point_in_area(point, area, threshold=5):
  243. """
  244. Args:
  245. point: (x, y).
  246. area: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  247. threshold: int
  248. Returns:
  249. bool:
  250. """
  251. return area[0] - threshold < point[0] < area[2] + threshold and area[1] - threshold < point[1] < area[3] + threshold
  252. def area_in_area(area1, area2, threshold=5):
  253. """
  254. Args:
  255. area1: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  256. area2: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  257. threshold: int
  258. Returns:
  259. bool:
  260. """
  261. return area2[0] - threshold <= area1[0] \
  262. and area2[1] - threshold <= area1[1] \
  263. and area1[2] <= area2[2] + threshold \
  264. and area1[3] <= area2[3] + threshold
  265. def area_cross_area(area1, area2, threshold=5):
  266. """
  267. Args:
  268. area1: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  269. area2: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  270. threshold: int
  271. Returns:
  272. bool:
  273. """
  274. # https://www.yiiven.cn/rect-is-intersection.html
  275. xa1, ya1, xa2, ya2 = area1
  276. xb1, yb1, xb2, yb2 = area2
  277. return abs(xb2 + xb1 - xa2 - xa1) <= xa2 - xa1 + xb2 - xb1 + threshold * 2 \
  278. and abs(yb2 + yb1 - ya2 - ya1) <= ya2 - ya1 + yb2 - yb1 + threshold * 2
  279. def float2str(n, decimal=3):
  280. """
  281. Args:
  282. n (float):
  283. decimal (int):
  284. Returns:
  285. str:
  286. """
  287. return str(round(n, decimal)).ljust(decimal + 2, "0")
  288. def point2str(x, y, length=4):
  289. """
  290. Args:
  291. x (int, float):
  292. y (int, float):
  293. length (int): Align length.
  294. Returns:
  295. str: String with numbers right aligned, such as '( 100, 80)'.
  296. """
  297. return '(%s, %s)' % (str(int(x)).rjust(length), str(int(y)).rjust(length))
  298. def col2name(col):
  299. """
  300. Convert a zero indexed column cell reference to a string.
  301. Args:
  302. col: The cell column. Int.
  303. Returns:
  304. Column style string.
  305. Examples:
  306. 0 -> A, 3 -> D, 35 -> AJ, -1 -> -A
  307. """
  308. col_neg = col < 0
  309. if col_neg:
  310. col_num = -col
  311. else:
  312. col_num = col + 1 # Change to 1-index.
  313. col_str = ''
  314. while col_num:
  315. # Set remainder from 1 .. 26
  316. remainder = col_num % 26
  317. if remainder == 0:
  318. remainder = 26
  319. # Convert the remainder to a character.
  320. col_letter = chr(remainder + 64)
  321. # Accumulate the column letters, right to left.
  322. col_str = col_letter + col_str
  323. # Get the next order of magnitude.
  324. col_num = int((col_num - 1) / 26)
  325. if col_neg:
  326. return '-' + col_str
  327. else:
  328. return col_str
  329. def name2col(col_str):
  330. """
  331. Convert a cell reference in A1 notation to a zero indexed row and column.
  332. Args:
  333. col_str: A1 style string.
  334. Returns:
  335. row, col: Zero indexed cell row and column indices.
  336. """
  337. # Convert base26 column string to number.
  338. expn = 0
  339. col = 0
  340. col_neg = col_str.startswith('-')
  341. col_str = col_str.strip('-').upper()
  342. for char in reversed(col_str):
  343. col += (ord(char) - 64) * (26 ** expn)
  344. expn += 1
  345. if col_neg:
  346. return -col
  347. else:
  348. return col - 1 # Convert 1-index to zero-index
  349. def node2location(node):
  350. """
  351. See location2node()
  352. Args:
  353. node (str): Example: 'E3'
  354. Returns:
  355. tuple[int]: Example: (4, 2)
  356. """
  357. res = REGEX_NODE.search(node)
  358. if res:
  359. x, y = res.group(1), res.group(2)
  360. y = int(y)
  361. if y > 0:
  362. y -= 1
  363. return name2col(x), y
  364. else:
  365. # Whatever
  366. return ord(node[0]) % 32 - 1, int(node[1:]) - 1
  367. def location2node(location):
  368. """
  369. Convert location tuple to an Excel-like cell.
  370. Accept negative values also.
  371. -2 -1 0 1 2 3
  372. -2 -B-2 -A-2 A-2 B-2 C-2 D-2
  373. -1 -B-1 -A-1 A-1 B-1 C-1 D-1
  374. 0 -B1 -A1 A1 B1 C1 D1
  375. 1 -B2 -A2 A2 B2 C2 D2
  376. 2 -B3 -A3 A3 B3 C3 D3
  377. 3 -B4 -A4 A4 B4 C4 D4
  378. # To generate the table above
  379. index = range(-2, 4)
  380. row = ' ' + ' '.join([str(i).rjust(4) for i in index])
  381. print(row)
  382. for y in index:
  383. row = str(y).rjust(2) + ' ' + ' '.join([location2node((x, y)).rjust(4) for x in index])
  384. print(row)
  385. def check(node):
  386. return point2str(*node2location(location2node(node)), length=2)
  387. row = ' ' + ' '.join([str(i).rjust(8) for i in index])
  388. print(row)
  389. for y in index:
  390. row = str(y).rjust(2) + ' ' + ' '.join([check((x, y)).rjust(4) for x in index])
  391. print(row)
  392. Args:
  393. location (tuple[int]):
  394. Returns:
  395. str:
  396. """
  397. x, y = location
  398. if y >= 0:
  399. y += 1
  400. return col2name(x) + str(y)
  401. def load_image(file, area=None):
  402. """
  403. Load an image like pillow and drop alpha channel.
  404. Args:
  405. file (str):
  406. area (tuple):
  407. Returns:
  408. np.ndarray:
  409. """
  410. image = Image.open(file)
  411. if area is not None:
  412. image = image.crop(area)
  413. image = np.array(image)
  414. channel = image.shape[2] if len(image.shape) > 2 else 1
  415. if channel > 3:
  416. image = image[:, :, :3].copy()
  417. return image
  418. def save_image(image, file):
  419. """
  420. Save an image like pillow.
  421. Args:
  422. image (np.ndarray):
  423. file (str):
  424. """
  425. # image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
  426. # cv2.imwrite(file, image)
  427. Image.fromarray(image).save(file)
  428. def crop(image, area, copy=True):
  429. """
  430. Crop image like pillow, when using opencv / numpy.
  431. Provides a black background if cropping outside of image.
  432. Args:
  433. image (np.ndarray):
  434. area:
  435. copy (bool):
  436. Returns:
  437. np.ndarray:
  438. """
  439. x1, y1, x2, y2 = map(int, map(round, area))
  440. h, w = image.shape[:2]
  441. border = np.maximum((0 - y1, y2 - h, 0 - x1, x2 - w), 0)
  442. x1, y1, x2, y2 = np.maximum((x1, y1, x2, y2), 0)
  443. image = image[y1:y2, x1:x2]
  444. if sum(border) > 0:
  445. image = cv2.copyMakeBorder(image, *border, borderType=cv2.BORDER_CONSTANT, value=(0, 0, 0))
  446. if copy:
  447. image = image.copy()
  448. return image
  449. def resize(image, size):
  450. """
  451. Resize image like pillow image.resize(), but implement in opencv.
  452. Pillow uses PIL.Image.NEAREST by default.
  453. Args:
  454. image (np.ndarray):
  455. size: (x, y)
  456. Returns:
  457. np.ndarray:
  458. """
  459. return cv2.resize(image, size, interpolation=cv2.INTER_NEAREST)
  460. def image_channel(image):
  461. """
  462. Args:
  463. image (np.ndarray):
  464. Returns:
  465. int: 0 for grayscale, 3 for RGB.
  466. """
  467. return image.shape[2] if len(image.shape) == 3 else 0
  468. def image_size(image):
  469. """
  470. Args:
  471. image (np.ndarray):
  472. Returns:
  473. int, int: width, height
  474. """
  475. shape = image.shape
  476. return shape[1], shape[0]
  477. def rgb2gray(image):
  478. """
  479. Args:
  480. image (np.ndarray): Shape (height, width, channel)
  481. Returns:
  482. np.ndarray: Shape (height, width)
  483. """
  484. r, g, b = cv2.split(image)
  485. return cv2.add(
  486. cv2.multiply(cv2.max(cv2.max(r, g), b), 0.5),
  487. cv2.multiply(cv2.min(cv2.min(r, g), b), 0.5)
  488. )
  489. def rgb2hsv(image):
  490. """
  491. Convert RGB color space to HSV color space.
  492. HSV is Hue Saturation Value.
  493. Args:
  494. image (np.ndarray): Shape (height, width, channel)
  495. Returns:
  496. np.ndarray: Hue (0~360), Saturation (0~100), Value (0~100).
  497. """
  498. image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV).astype(np.float)
  499. image *= (360 / 180, 100 / 255, 100 / 255)
  500. return image
  501. def rgb2yuv(image):
  502. """
  503. Convert RGB to YUV color space.
  504. Args:
  505. image (np.ndarray): Shape (height, width, channel)
  506. Returns:
  507. np.ndarray: Shape (height, width)
  508. """
  509. image = cv2.cvtColor(image, cv2.COLOR_RGB2YUV)
  510. return image
  511. def rgb2luma(image):
  512. """
  513. Convert RGB to the Y channel (Luminance) in YUV color space.
  514. Args:
  515. image (np.ndarray): Shape (height, width, channel)
  516. Returns:
  517. np.ndarray: Shape (height, width)
  518. """
  519. image = cv2.cvtColor(image, cv2.COLOR_RGB2YUV)
  520. luma, _, _ = cv2.split(image)
  521. return luma
  522. def get_color(image, area):
  523. """Calculate the average color of a particular area of the image.
  524. Args:
  525. image (np.ndarray): Screenshot.
  526. area (tuple): (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y)
  527. Returns:
  528. tuple: (r, g, b)
  529. """
  530. temp = crop(image, area, copy=False)
  531. color = cv2.mean(temp)
  532. return color[:3]
  533. def get_bbox(image, threshold=0):
  534. """
  535. A numpy implementation of the getbbox() in pillow.
  536. Args:
  537. image (np.ndarray): Screenshot.
  538. threshold (int): Color <= threshold will be considered black
  539. Returns:
  540. tuple: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y)
  541. """
  542. if image_channel(image) == 3:
  543. image = np.max(image, axis=2)
  544. x = np.where(np.max(image, axis=0) > threshold)[0]
  545. y = np.where(np.max(image, axis=1) > threshold)[0]
  546. return x[0], y[0], x[-1] + 1, y[-1] + 1
  547. def color_similarity(color1, color2):
  548. """
  549. Args:
  550. color1 (tuple): (r, g, b)
  551. color2 (tuple): (r, g, b)
  552. Returns:
  553. int:
  554. """
  555. diff = np.array(color1).astype(int) - np.array(color2).astype(int)
  556. diff = np.max(np.maximum(diff, 0)) - np.min(np.minimum(diff, 0))
  557. return diff
  558. def color_similar(color1, color2, threshold=10):
  559. """Consider two colors are similar, if tolerance lesser or equal threshold.
  560. Tolerance = Max(Positive(difference_rgb)) + Max(- Negative(difference_rgb))
  561. The same as the tolerance in Photoshop.
  562. Args:
  563. color1 (tuple): (r, g, b)
  564. color2 (tuple): (r, g, b)
  565. threshold (int): Default to 10.
  566. Returns:
  567. bool: True if two colors are similar.
  568. """
  569. # print(color1, color2)
  570. diff = np.array(color1).astype(int) - np.array(color2).astype(int)
  571. diff = np.max(np.maximum(diff, 0)) - np.min(np.minimum(diff, 0))
  572. return diff <= threshold
  573. def color_similar_1d(image, color, threshold=10):
  574. """
  575. Args:
  576. image (np.ndarray): 1D array.
  577. color: (r, g, b)
  578. threshold(int): Default to 10.
  579. Returns:
  580. np.ndarray: bool
  581. """
  582. diff = image.astype(int) - color
  583. diff = np.max(np.maximum(diff, 0), axis=1) - np.min(np.minimum(diff, 0), axis=1)
  584. return diff <= threshold
  585. def color_similarity_2d(image, color):
  586. """
  587. Args:
  588. image: 2D array.
  589. color: (r, g, b)
  590. Returns:
  591. np.ndarray: uint8
  592. """
  593. r, g, b = cv2.split(cv2.subtract(image, (*color, 0)))
  594. positive = cv2.max(cv2.max(r, g), b)
  595. r, g, b = cv2.split(cv2.subtract((*color, 0), image))
  596. negative = cv2.max(cv2.max(r, g), b)
  597. return cv2.subtract(255, cv2.add(positive, negative))
  598. def extract_letters(image, letter=(255, 255, 255), threshold=128):
  599. """Set letter color to black, set background color to white.
  600. Args:
  601. image: Shape (height, width, channel)
  602. letter (tuple): Letter RGB.
  603. threshold (int):
  604. Returns:
  605. np.ndarray: Shape (height, width)
  606. """
  607. r, g, b = cv2.split(cv2.subtract(image, (*letter, 0)))
  608. positive = cv2.max(cv2.max(r, g), b)
  609. r, g, b = cv2.split(cv2.subtract((*letter, 0), image))
  610. negative = cv2.max(cv2.max(r, g), b)
  611. return cv2.multiply(cv2.add(positive, negative), 255.0 / threshold)
  612. def extract_white_letters(image, threshold=128):
  613. """Set letter color to black, set background color to white.
  614. This function will discourage color pixels (Non-gray pixels)
  615. Args:
  616. image: Shape (height, width, channel)
  617. threshold (int):
  618. Returns:
  619. np.ndarray: Shape (height, width)
  620. """
  621. r, g, b = cv2.split(cv2.subtract((255, 255, 255, 0), image))
  622. minimum = cv2.min(cv2.min(r, g), b)
  623. maximum = cv2.max(cv2.max(r, g), b)
  624. return cv2.multiply(cv2.add(maximum, cv2.subtract(maximum, minimum)), 255.0 / threshold)
  625. def color_mapping(image, max_multiply=2):
  626. """
  627. Mapping color to 0-255.
  628. Minimum color to 0, maximum color to 255, multiply colors by 2 at max.
  629. Args:
  630. image (np.ndarray):
  631. max_multiply (int, float):
  632. Returns:
  633. np.ndarray:
  634. """
  635. image = image.astype(float)
  636. low, high = np.min(image), np.max(image)
  637. multiply = min(255 / (high - low), max_multiply)
  638. add = (255 - multiply * (low + high)) / 2
  639. image = cv2.add(cv2.multiply(image, multiply), add)
  640. image[image > 255] = 255
  641. image[image < 0] = 0
  642. return image.astype(np.uint8)
  643. def image_left_strip(image, threshold, length):
  644. """
  645. In `DAILY:200/200` strip `DAILY:` and leave `200/200`
  646. Args:
  647. image (np.ndarray): (height, width)
  648. threshold (int):
  649. 0-255
  650. The first column with brightness lower than this
  651. will be considered as left edge.
  652. length (int):
  653. Strip this length of image after the left edge
  654. Returns:
  655. np.ndarray:
  656. """
  657. brightness = np.mean(image, axis=0)
  658. match = np.where(brightness < threshold)[0]
  659. if len(match):
  660. left = match[0] + length
  661. total = image.shape[1]
  662. if left < total:
  663. image = image[:, left:]
  664. return image
  665. def red_overlay_transparency(color1, color2, red=247):
  666. """Calculate the transparency of red overlay.
  667. Args:
  668. color1: origin color.
  669. color2: changed color.
  670. red(int): red color 0-255. Default to 247.
  671. Returns:
  672. float: 0-1
  673. """
  674. return (color2[0] - color1[0]) / (red - color1[0])
  675. def color_bar_percentage(image, area, prev_color, reverse=False, starter=0, threshold=30):
  676. """
  677. Args:
  678. image:
  679. area:
  680. prev_color:
  681. reverse: True if bar goes from right to left.
  682. starter:
  683. threshold:
  684. Returns:
  685. float: 0 to 1.
  686. """
  687. image = crop(image, area)
  688. image = image[:, ::-1, :] if reverse else image
  689. length = image.shape[1]
  690. prev_index = starter
  691. for _ in range(1280):
  692. bar = color_similarity_2d(image, color=prev_color)
  693. index = np.where(np.any(bar > 255 - threshold, axis=0))[0]
  694. if not index.size:
  695. return prev_index / length
  696. else:
  697. index = index[-1]
  698. if index <= prev_index:
  699. return index / length
  700. prev_index = index
  701. prev_row = bar[:, prev_index] > 255 - threshold
  702. if not prev_row.size:
  703. return prev_index / length
  704. prev_color = np.mean(image[:, prev_index], axis=0)
  705. return 0.
  706. def load_module(moduleName :str, moduleFile :str):
  707. """
  708. 加载模块
  709. :param moduleName:
  710. :param moduleFile: 文件路径带py
  711. :return:
  712. """
  713. spec = importlib.util.spec_from_file_location(moduleName, moduleFile)
  714. module = importlib.util.module_from_spec(spec)
  715. spec.loader.exec_module(module)
  716. sys.modules[moduleName] = module
  717. return module
  718. def angle(p1, p2, p3):
  719. """计算三点形成的角度(单位:度)"""
  720. v1 = p2 - p1
  721. v2 = p3 - p2
  722. cos_angle = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
  723. return np.degrees(np.arccos(np.clip(cos_angle, -1.0, 1.0))) # 限制范围以防止浮点错误
  724. def is_approx_rectangle(points, tolerance=30):
  725. """判断四个点是否近似于矩形
  726. :param points: 坐上开始,逆时针
  727. :param tolerance: 角度 不是弧度
  728. """
  729. if points.shape[0] != 4:
  730. return False
  731. # # 计算边长
  732. # distances = [distance(points[i], points[j]) for i in range(4) for j in range(i + 1, 4)]
  733. # distances.sort()
  734. # # 最小边和最大边
  735. # short_sides = distances[:4]
  736. # long_sides = distances[4:]
  737. # # 判断边长是否接近
  738. # if not (np.isclose(short_sides[0], short_sides[1], atol=tolerance) and
  739. # np.isclose(long_sides[0], long_sides[1], atol=tolerance)):
  740. # return False
  741. # 检查角度
  742. angles = [
  743. angle(points[0], points[1], points[2]),
  744. angle(points[1], points[2], points[3]),
  745. angle(points[2], points[3], points[0]),
  746. angle(points[3], points[0], points[1])
  747. ]
  748. # 判断角度是否接近90度
  749. return all(np.isclose(a, 90, atol=tolerance) for a in angles)