pyxllib 0.0.43__py3-none-any.whl → 0.3.197__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. pyxllib/__init__.py +9 -2
  2. pyxllib/algo/__init__.py +8 -0
  3. pyxllib/algo/disjoint.py +54 -0
  4. pyxllib/algo/geo.py +541 -0
  5. pyxllib/{util/mathlib.py → algo/intervals.py} +172 -36
  6. pyxllib/algo/matcher.py +389 -0
  7. pyxllib/algo/newbie.py +166 -0
  8. pyxllib/algo/pupil.py +629 -0
  9. pyxllib/algo/shapelylib.py +67 -0
  10. pyxllib/algo/specialist.py +241 -0
  11. pyxllib/algo/stat.py +494 -0
  12. pyxllib/algo/treelib.py +149 -0
  13. pyxllib/algo/unitlib.py +66 -0
  14. pyxllib/autogui/__init__.py +5 -0
  15. pyxllib/autogui/activewin.py +246 -0
  16. pyxllib/autogui/all.py +9 -0
  17. pyxllib/autogui/autogui.py +852 -0
  18. pyxllib/autogui/uiautolib.py +362 -0
  19. pyxllib/autogui/virtualkey.py +102 -0
  20. pyxllib/autogui/wechat.py +827 -0
  21. pyxllib/autogui/wechat_msg.py +421 -0
  22. pyxllib/autogui/wxautolib.py +84 -0
  23. pyxllib/cv/__init__.py +1 -11
  24. pyxllib/cv/expert.py +267 -0
  25. pyxllib/cv/{imlib.py → imfile.py} +18 -83
  26. pyxllib/cv/imhash.py +39 -0
  27. pyxllib/cv/pupil.py +9 -0
  28. pyxllib/cv/rgbfmt.py +1525 -0
  29. pyxllib/cv/slidercaptcha.py +137 -0
  30. pyxllib/cv/trackbartools.py +163 -49
  31. pyxllib/cv/xlcvlib.py +1040 -0
  32. pyxllib/cv/xlpillib.py +423 -0
  33. pyxllib/data/__init__.py +0 -0
  34. pyxllib/data/echarts.py +240 -0
  35. pyxllib/data/jsonlib.py +89 -0
  36. pyxllib/{util/oss2_.py → data/oss.py} +11 -9
  37. pyxllib/data/pglib.py +1127 -0
  38. pyxllib/data/sqlite.py +568 -0
  39. pyxllib/{util → data}/sqllib.py +13 -31
  40. pyxllib/ext/JLineViewer.py +505 -0
  41. pyxllib/ext/__init__.py +6 -0
  42. pyxllib/{util → ext}/demolib.py +119 -35
  43. pyxllib/ext/drissionlib.py +277 -0
  44. pyxllib/ext/kq5034lib.py +12 -0
  45. pyxllib/{util/main.py → ext/old.py} +122 -284
  46. pyxllib/ext/qt.py +449 -0
  47. pyxllib/ext/robustprocfile.py +497 -0
  48. pyxllib/ext/seleniumlib.py +76 -0
  49. pyxllib/{util/tklib.py → ext/tk.py} +10 -11
  50. pyxllib/ext/unixlib.py +827 -0
  51. pyxllib/ext/utools.py +351 -0
  52. pyxllib/{util/webhooklib.py → ext/webhook.py} +45 -17
  53. pyxllib/ext/win32lib.py +40 -0
  54. pyxllib/ext/wjxlib.py +88 -0
  55. pyxllib/ext/wpsapi.py +124 -0
  56. pyxllib/ext/xlwork.py +9 -0
  57. pyxllib/ext/yuquelib.py +1105 -0
  58. pyxllib/file/__init__.py +17 -0
  59. pyxllib/file/docxlib.py +761 -0
  60. pyxllib/{util → file}/gitlib.py +40 -27
  61. pyxllib/file/libreoffice.py +165 -0
  62. pyxllib/file/movielib.py +148 -0
  63. pyxllib/file/newbie.py +10 -0
  64. pyxllib/file/onenotelib.py +1469 -0
  65. pyxllib/file/packlib/__init__.py +330 -0
  66. pyxllib/{util → file/packlib}/zipfile.py +598 -195
  67. pyxllib/file/pdflib.py +426 -0
  68. pyxllib/file/pupil.py +185 -0
  69. pyxllib/file/specialist/__init__.py +685 -0
  70. pyxllib/{basic/_5_dirlib.py → file/specialist/dirlib.py} +364 -93
  71. pyxllib/file/specialist/download.py +193 -0
  72. pyxllib/file/specialist/filelib.py +2829 -0
  73. pyxllib/file/xlsxlib.py +3131 -0
  74. pyxllib/file/xlsyncfile.py +341 -0
  75. pyxllib/prog/__init__.py +5 -0
  76. pyxllib/prog/cachetools.py +64 -0
  77. pyxllib/prog/deprecatedlib.py +233 -0
  78. pyxllib/prog/filelock.py +42 -0
  79. pyxllib/prog/ipyexec.py +253 -0
  80. pyxllib/prog/multiprogs.py +940 -0
  81. pyxllib/prog/newbie.py +451 -0
  82. pyxllib/prog/pupil.py +1197 -0
  83. pyxllib/{sitepackages.py → prog/sitepackages.py} +5 -3
  84. pyxllib/prog/specialist/__init__.py +391 -0
  85. pyxllib/prog/specialist/bc.py +203 -0
  86. pyxllib/prog/specialist/browser.py +497 -0
  87. pyxllib/prog/specialist/common.py +347 -0
  88. pyxllib/prog/specialist/datetime.py +199 -0
  89. pyxllib/prog/specialist/tictoc.py +240 -0
  90. pyxllib/prog/specialist/xllog.py +180 -0
  91. pyxllib/prog/xlosenv.py +108 -0
  92. pyxllib/stdlib/__init__.py +17 -0
  93. pyxllib/{util → stdlib}/tablepyxl/__init__.py +1 -3
  94. pyxllib/{util → stdlib}/tablepyxl/style.py +1 -1
  95. pyxllib/{util → stdlib}/tablepyxl/tablepyxl.py +2 -4
  96. pyxllib/text/__init__.py +8 -0
  97. pyxllib/text/ahocorasick.py +39 -0
  98. pyxllib/text/airscript.js +744 -0
  99. pyxllib/text/charclasslib.py +121 -0
  100. pyxllib/text/jiebalib.py +267 -0
  101. pyxllib/text/jinjalib.py +32 -0
  102. pyxllib/text/jsa_ai_prompt.md +271 -0
  103. pyxllib/text/jscode.py +922 -0
  104. pyxllib/text/latex/__init__.py +158 -0
  105. pyxllib/text/levenshtein.py +303 -0
  106. pyxllib/text/nestenv.py +1215 -0
  107. pyxllib/text/newbie.py +300 -0
  108. pyxllib/text/pupil/__init__.py +8 -0
  109. pyxllib/text/pupil/common.py +1121 -0
  110. pyxllib/text/pupil/xlalign.py +326 -0
  111. pyxllib/text/pycode.py +47 -0
  112. pyxllib/text/specialist/__init__.py +8 -0
  113. pyxllib/text/specialist/common.py +112 -0
  114. pyxllib/text/specialist/ptag.py +186 -0
  115. pyxllib/text/spellchecker.py +172 -0
  116. pyxllib/text/templates/echart_base.html +11 -0
  117. pyxllib/text/templates/highlight_code.html +17 -0
  118. pyxllib/text/templates/latex_editor.html +103 -0
  119. pyxllib/text/vbacode.py +17 -0
  120. pyxllib/text/xmllib.py +747 -0
  121. pyxllib/xl.py +39 -0
  122. pyxllib/xlcv.py +17 -0
  123. pyxllib-0.3.197.dist-info/METADATA +48 -0
  124. pyxllib-0.3.197.dist-info/RECORD +126 -0
  125. {pyxllib-0.0.43.dist-info → pyxllib-0.3.197.dist-info}/WHEEL +4 -5
  126. pyxllib/basic/_1_strlib.py +0 -945
  127. pyxllib/basic/_2_timelib.py +0 -488
  128. pyxllib/basic/_3_pathlib.py +0 -916
  129. pyxllib/basic/_4_loglib.py +0 -419
  130. pyxllib/basic/__init__.py +0 -54
  131. pyxllib/basic/arrow_.py +0 -250
  132. pyxllib/basic/chardet_.py +0 -66
  133. pyxllib/basic/dirlib.py +0 -529
  134. pyxllib/basic/dprint.py +0 -202
  135. pyxllib/basic/extension.py +0 -12
  136. pyxllib/basic/judge.py +0 -31
  137. pyxllib/basic/log.py +0 -204
  138. pyxllib/basic/pathlib_.py +0 -705
  139. pyxllib/basic/pytictoc.py +0 -102
  140. pyxllib/basic/qiniu_.py +0 -61
  141. pyxllib/basic/strlib.py +0 -761
  142. pyxllib/basic/timer.py +0 -132
  143. pyxllib/cv/cv.py +0 -834
  144. pyxllib/cv/cvlib/_1_geo.py +0 -543
  145. pyxllib/cv/cvlib/_2_cvprcs.py +0 -309
  146. pyxllib/cv/cvlib/_2_imgproc.py +0 -594
  147. pyxllib/cv/cvlib/_3_pilprcs.py +0 -80
  148. pyxllib/cv/cvlib/_4_cvimg.py +0 -211
  149. pyxllib/cv/cvlib/__init__.py +0 -10
  150. pyxllib/cv/debugtools.py +0 -82
  151. pyxllib/cv/fitz_.py +0 -300
  152. pyxllib/cv/installer.py +0 -42
  153. pyxllib/debug/_0_installer.py +0 -38
  154. pyxllib/debug/_1_typelib.py +0 -277
  155. pyxllib/debug/_2_chrome.py +0 -198
  156. pyxllib/debug/_3_showdir.py +0 -161
  157. pyxllib/debug/_4_bcompare.py +0 -140
  158. pyxllib/debug/__init__.py +0 -49
  159. pyxllib/debug/bcompare.py +0 -132
  160. pyxllib/debug/chrome.py +0 -198
  161. pyxllib/debug/installer.py +0 -38
  162. pyxllib/debug/showdir.py +0 -158
  163. pyxllib/debug/typelib.py +0 -278
  164. pyxllib/image/__init__.py +0 -12
  165. pyxllib/torch/__init__.py +0 -20
  166. pyxllib/torch/modellib.py +0 -37
  167. pyxllib/torch/trainlib.py +0 -344
  168. pyxllib/util/__init__.py +0 -20
  169. pyxllib/util/aip_.py +0 -141
  170. pyxllib/util/casiadb.py +0 -59
  171. pyxllib/util/excellib.py +0 -495
  172. pyxllib/util/filelib.py +0 -612
  173. pyxllib/util/jsondata.py +0 -27
  174. pyxllib/util/jsondata2.py +0 -92
  175. pyxllib/util/labelmelib.py +0 -139
  176. pyxllib/util/onepy/__init__.py +0 -29
  177. pyxllib/util/onepy/onepy.py +0 -574
  178. pyxllib/util/onepy/onmanager.py +0 -170
  179. pyxllib/util/pyautogui_.py +0 -219
  180. pyxllib/util/textlib.py +0 -1305
  181. pyxllib/util/unorder.py +0 -22
  182. pyxllib/util/xmllib.py +0 -639
  183. pyxllib-0.0.43.dist-info/METADATA +0 -39
  184. pyxllib-0.0.43.dist-info/RECORD +0 -80
  185. pyxllib-0.0.43.dist-info/top_level.txt +0 -1
  186. {pyxllib-0.0.43.dist-info → pyxllib-0.3.197.dist-info/licenses}/LICENSE +0 -0
@@ -0,0 +1,137 @@
1
+ import os
2
+ import cv2
3
+ import numpy as np
4
+ from xlproject.code4101 import XlPath
5
+
6
+ from pyxllib.file.specialist import download_file
7
+ from pyxllib.prog.pupil import is_url
8
+
9
+
10
+ class SliderCaptchaLocator:
11
+ """ 滑动验证码的位置定位
12
+
13
+ 由于缺口一般是一个黑底图,整体颜色是偏暗的,可以利用这个特性来定位列所在位置
14
+ """
15
+
16
+ def __init__(self, background_image_path, slider_image_path=None):
17
+ """
18
+ :param background_image_path: 背景主图片
19
+ :param slider_image_path: 滑动条图片
20
+ """
21
+ # 支持输入是url的情况
22
+ if is_url(background_image_path):
23
+ background_image_path = download_file(background_image_path, temp=True, ext='.png')
24
+ if is_url(slider_image_path):
25
+ slider_image_path = download_file(slider_image_path, temp=True, ext='.png')
26
+ # 初始化图片等数据
27
+ self.image_path = background_image_path
28
+ self.image = cv2.imread(self.image_path)
29
+ self.darkest_rows = None
30
+ self.column_averages = None
31
+ self.enhanced_averages = None
32
+ self.radius = 24
33
+ if slider_image_path is not None:
34
+ # 先读取图片
35
+ template_image = cv2.imread(slider_image_path)
36
+ # 取图片宽度的40%作为radius
37
+ self.radius = int(template_image.shape[1] * 0.4)
38
+
39
+ def select_darkest_part_rows(self, fraction=0.1):
40
+ """
41
+ 选择最暗的部分行,供下游计算列的平均暗度
42
+
43
+ :param float fraction: 最暗行的比例
44
+ :return: 最暗行的图像部分
45
+ """
46
+ if self.darkest_rows is None:
47
+ average_row_values = np.mean(self.image, axis=(1, 2))
48
+ threshold_index = int(len(average_row_values) * fraction)
49
+ darkest_indices = np.argsort(average_row_values)[:threshold_index]
50
+ self.darkest_rows = self.image[darkest_indices]
51
+ return self.darkest_rows
52
+
53
+ def calculate_column_averages(self):
54
+ """
55
+ 计算列的平均强度。
56
+
57
+ :return: 列的平均强度
58
+ """
59
+ if self.column_averages is None:
60
+ rows = self.select_darkest_part_rows()
61
+ column_values = np.mean(rows, axis=0)
62
+ column_intensity = np.mean(column_values, axis=1)
63
+ min_val, max_val = np.min(column_intensity), np.max(column_intensity)
64
+ self.column_averages = ((column_intensity - min_val) / (max_val - min_val) * 255).astype(
65
+ np.uint8) if max_val != min_val else np.zeros_like(column_intensity)
66
+ return self.column_averages
67
+
68
+ def enhance_column_averages(self):
69
+ """
70
+ 增强列平均值,利用验证码的黑度图的对称性,进行一个增强,使得其他非验证码的零散黑条变白
71
+
72
+ :param int radius: 对比半径,这个建议设成模版图宽度的40%~50%
73
+ :return: 增强后的列平均值
74
+ """
75
+ if self.enhanced_averages is None:
76
+ averages = self.calculate_column_averages()
77
+ length = len(averages)
78
+ enhanced = np.zeros_like(averages)
79
+ for i in range(length):
80
+ max_value = averages[i]
81
+ for j in range(1, self.radius // 2 + 1):
82
+ if i - j >= 0 and i + j < length:
83
+ max_value = max(max_value, abs(int(averages[i - j]) - int(averages[i + j])))
84
+ enhanced[i] = max_value
85
+ self.enhanced_averages = enhanced
86
+ return self.enhanced_averages
87
+
88
+ def find_captcha_position(self, threshold=50):
89
+ """
90
+ 查找验证码的位置。
91
+
92
+ :param int threshold: 达到多少像素后,开始计算中位数
93
+ :return: 验证码的列中心位置
94
+ """
95
+ intensities = self.enhance_column_averages()
96
+ indices = []
97
+ for intensity in range(256):
98
+ new_indices = np.where(intensities == intensity)[0]
99
+ if len(indices) + len(new_indices) > threshold:
100
+ indices.extend(new_indices[:threshold - len(indices)])
101
+ break
102
+ indices.extend(new_indices)
103
+ return int(np.median(indices))
104
+
105
+ def draw_red_line(self, position, line_thickness=3):
106
+ """
107
+ 在图像上绘制红线。
108
+
109
+ :param int position: 红线位置
110
+ :param int line_thickness: 线宽
111
+ :return: 绘制红线后的图像
112
+ """
113
+ gray_image = np.tile(self.calculate_column_averages(), (100, 1)).astype(np.uint8)
114
+ color_image = cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR)
115
+ cv2.line(color_image, (position, 0), (position, gray_image.shape[0]), (0, 0, 255), line_thickness)
116
+ return color_image
117
+
118
+ def debug(self):
119
+ position = self.find_captcha_position()
120
+ final_image = self.draw_red_line(position)
121
+ cv2.imshow('Image with Red Line', final_image)
122
+ cv2.waitKey(0)
123
+ cv2.destroyAllWindows()
124
+
125
+
126
+ def main():
127
+ os.chdir(XlPath.desktop())
128
+ locator = SliderCaptchaLocator('cap_union_new_getcapbysig.png')
129
+ position = locator.find_captcha_position()
130
+ final_image = locator.draw_red_line(position)
131
+ cv2.imshow('Image with Red Line', final_image)
132
+ cv2.waitKey(0)
133
+ cv2.destroyAllWindows()
134
+
135
+
136
+ if __name__ == '__main__':
137
+ main()
@@ -2,7 +2,7 @@
2
2
  # -*- coding: utf-8 -*-
3
3
  # @Author : 陈坤泽
4
4
  # @Email : 877362867@qq.com
5
- # @Data : 2020/09/30 10:45
5
+ # @Date : 2020/09/30 10:45
6
6
 
7
7
  """
8
8
  这个文件默认不加载到cv里,需要使用的时候再导入
@@ -11,59 +11,151 @@ from pyxlib.cv.debugtools import *
11
11
  """
12
12
 
13
13
  import math
14
+ import time
14
15
 
15
- from pyxllib.basic import TicToc
16
- from pyxllib.cv.cvlib import *
16
+ import cv2
17
+ import numpy as np
18
+
19
+ from pyxllib.prog.specialist import TicToc
20
+ from pyxllib.cv.expert import xlcv
17
21
 
18
22
 
19
23
  class TrackbarTool:
20
24
  """ 滑动条控件组
21
25
  """
22
26
 
23
- def __init__(self, winname, img, flags=0):
24
- img = xlcv.read(img)
25
- cv2.namedWindow(winname, flags)
26
- cv2.imshow(winname, img)
27
+ def __init__(self, winname=None, *, imgproc=None, img=None, flags=1, verbose=0, delay=100):
28
+ """
29
+ Args:
30
+ winname: 窗口名可以不输入,可以从func获取,或者有默认窗口名
31
+ imgproc: 有时候明确要对指定的函数进行可视化分析
32
+ img: 图片可以不输入,可以内置一个图片
33
+ flags: 0 窗口可以调节大小,1 窗口按照图片大小固定
34
+ verbose: 0 静默模式,1 显示每次运行时间,2 显示每次运行时间及参数值
35
+ delay: 有时候改动滑动条,并不想立即生效,可以设置延时,这里单位是ms
36
+ 即如果delay时间段内已经有执行过回调函数,会避免重复执行
37
+ """
38
+
39
+ if winname is None:
40
+ if imgproc:
41
+ winname = imgproc.__name__
42
+ else:
43
+ winname = 'TrackbarTool'
44
+
45
+ if img is None:
46
+ img = np.zeros([500, 100, 3], dtype='uint8')
47
+ else:
48
+ img = xlcv.read(img)
49
+
27
50
  self.winname = winname
28
51
  self.img = img
52
+ cv2.namedWindow(winname, flags)
53
+ cv2.imshow(winname, self.img)
54
+
55
+ self.imgproc = imgproc
29
56
  self.trackbar_names = {}
30
57
 
58
+ self.verbose = verbose
59
+
60
+ self.delay = delay
61
+ self.last_run_time = 0
62
+
63
+ @classmethod
64
+ def from_album(cls, obj, **kwargs):
65
+ """ albumentations的类功能 """
66
+
67
+ def imgproc(img, **kws):
68
+ f = obj(**kws, p=1) # p是概率,这里做可视化,是百分百执行
69
+ return f(image=img)['image']
70
+
71
+ return cls(**kwargs, imgproc=imgproc, winname=obj.__name__)
72
+
31
73
  def imshow(self, img=None):
32
74
  """ 刷新显示的图片 """
33
75
  if img is None:
34
76
  img = self.img
35
77
  cv2.imshow(self.winname, img)
36
78
 
37
- def default_run(self, x):
79
+ def create_trackbar(self, name, count, value=0, *, vmaps=None, on_change=None):
80
+ """ 创建一个滑动条
81
+
82
+ :param name: 滑动条名称
83
+ :param int count: 上限值
84
+ :param int value: 初始值
85
+ :param list[func(int)] vmaps: 滑动条只能设置整数值,但有些参数不一定是整数,可以写个自定义的映射函数
86
+ :param on_change: 自定义,额外扩展的回调函数
87
+ :return:
88
+ """
89
+ if on_change is None:
90
+ on_change = self._on_change
91
+ self.trackbar_names[name] = {'count': count, 'value': value, 'vmaps': vmaps}
92
+ cv2.createTrackbar(name, self.winname, value, count, on_change)
93
+
94
+ def default_run(self, **kwargs):
38
95
  """ 默认执行器,这个在类继承后,基本都是要自定义成自己的功能的
39
96
 
40
- TODO 从1滑到20,会运行20次,可以研究一个机制,来只运行一次
97
+ kwargs里存储了当前所有滑动条的取值
98
+ 如果有设置vmap,
41
99
  """
42
- kwargs = {}
43
- for k in self.trackbar_names.keys():
44
- kwargs[k] = self[k]
45
- print(kwargs)
100
+ pass
46
101
 
47
- def create_trackbar(self, trackbar_name, count, value=0, on_change=None):
48
- """ 创建一个滑动条
102
+ def _on_change(self, x):
103
+ """ 默认的回调函数,x是对应滑动条当前的取值
49
104
 
50
- :param trackbar_name: 滑动条名称
51
- :param count: 上限值
52
- :param on_change: 回调函数
53
- :param value: 初始值
54
- :return:
105
+ 如果需要对每个特定滑动条做特殊定制操作,可以create_trackbar传入特殊on_change
106
+ 这里的_on_change是所有滑动条统一回调版本,不区分是哪个滑动条改动了
55
107
  """
56
- if on_change is None:
57
- on_change = self.default_run
58
- cv2.createTrackbar(trackbar_name, self.winname, value, count, on_change)
108
+ # 1 记录运行时间
109
+ cur_time = time.time() * 1000
110
+ if cur_time - self.last_run_time < self.delay:
111
+ return
112
+ else:
113
+ self.last_run_time = cur_time
114
+
115
+ # 2 生成、解析滑动条的参数值
116
+ kwargs = {}
117
+ for k, v in self.trackbar_names.items():
118
+ x = cv2.getTrackbarPos(k, self.winname)
119
+ if v['vmaps']:
120
+ for kk, func in v['vmaps'].items():
121
+ kwargs[kk] = func(x)
122
+ else:
123
+ kwargs[k] = x
124
+
125
+ # 3 执行绑定的功能
126
+ tt = TicToc()
127
+ if self.imgproc:
128
+ try:
129
+ dst = self.imgproc(self.img, **kwargs)
130
+ self.imshow(dst)
131
+ except Exception as e:
132
+ if 'missing required argument' in e.args[0]:
133
+ pass
134
+ else:
135
+ raise e
136
+ else:
137
+ self.default_run(**kwargs)
138
+
139
+ # 4 是否显示运行信息
140
+ if self.verbose == 1:
141
+ tt.toc()
142
+ elif self.verbose == 2:
143
+ tt.toc(kwargs)
59
144
 
60
145
  def __getitem__(self, item):
61
146
  """ 可以通过 Trackbars 来获取滑动条当前值
62
147
 
63
148
  :param item: 滑动条名称
149
+ 扩展:也支持获取vmaps后对应的名称及值
64
150
  :return: 当前取值
65
151
  """
66
- return cv2.getTrackbarPos(item, self.winname)
152
+ if item in self.trackbar_names:
153
+ return cv2.getTrackbarPos(item, self.winname)
154
+ else:
155
+ for k, v in self.trackbar_names.items():
156
+ if v['vmaps'] and item in v['vmaps'].keys():
157
+ x = cv2.getTrackbarPos(k, self.winname)
158
+ return v['vmaps']['item'](x)
67
159
 
68
160
 
69
161
  class ErodeTool(TrackbarTool):
@@ -72,25 +164,18 @@ class ErodeTool(TrackbarTool):
72
164
  HoughLinesPTool('handwriting/C100001448573-002.png')
73
165
  """
74
166
 
75
- def __init__(self, img, winname='ErodeTool', flags=1):
76
- super().__init__(winname, img, flags)
77
-
78
- # 1 增加控件
167
+ def __init__(self, img, winname='ErodeTool', flags=1, *, verbose=2):
168
+ super().__init__(winname, img=img, flags=flags, verbose=verbose)
79
169
  self.create_trackbar('size_x', 100, 15)
80
170
  self.create_trackbar('size_y', 100, 15)
81
171
 
82
- # 2 初始化执行一遍
83
- self.default_run(0)
84
-
85
- def default_run(self, x):
172
+ def default_run(self, **kwargs):
86
173
  """ 默认执行器
87
174
  """
88
- tt = TicToc()
89
- size = self['size_x'], self['size_y']
175
+ size = kwargs['size_x'], kwargs['size_y']
90
176
  element = cv2.getStructuringElement(cv2.MORPH_RECT, size)
91
177
  dst = cv2.erode(self.img, element)
92
178
  self.imshow(dst)
93
- tt.toc(f'element_size={size}')
94
179
 
95
180
 
96
181
  class HoughLinesPTool(TrackbarTool):
@@ -99,14 +184,14 @@ class HoughLinesPTool(TrackbarTool):
99
184
  HoughLinesPTool('handwriting/C100001448573-002.png')
100
185
  """
101
186
 
102
- def __init__(self, img, winname='HoughLinesPTool', flags=1):
103
- super().__init__(winname, img, flags)
187
+ def __init__(self, img, winname='HoughLinesPTool', flags=1, *, verbose=2):
188
+ super().__init__(winname, img=img, flags=flags, verbose=verbose)
104
189
 
105
190
  # 1 增加控件
106
191
  # 以像素为单位的距离精度
107
- self.create_trackbar('rho*10', 100, 10)
192
+ self.create_trackbar('rho*10', 100, 10, vmaps={'rho': lambda x: x / 10})
108
193
  # 以弧度为单位的角度精度
109
- self.create_trackbar('theta*pi/360', 10, 2)
194
+ self.create_trackbar('theta*pi/360', 10, 2, vmaps={'theta': lambda x: x * math.pi / 360})
110
195
  # 累加平面的阈值参数
111
196
  self.create_trackbar('threshold', 200, 80)
112
197
  # 最低线段长度
@@ -121,17 +206,46 @@ class HoughLinesPTool(TrackbarTool):
121
206
  gray_img = self.img
122
207
  self.binary_img = cv2.adaptiveThreshold(gray_img, 255, 0, 1, 11, 3)
123
208
 
124
- # 3 初始化执行一遍
125
- self.default_run(0)
126
-
127
- def default_run(self, x):
209
+ def default_run(self, **kwargs):
128
210
  """ 默认执行器
129
211
  """
130
- tt = TicToc()
131
- lines = cv2.HoughLinesP(self.binary_img,
132
- self['rho*10'] / 10, self['theta*pi/360'] * math.pi / 360,
133
- self['threshold'], self['minLineLength'], self['maxLineGap'])
212
+ lines = cv2.HoughLinesP(self.binary_img, kwargs['rho'], kwargs['theta'],
213
+ kwargs['threshold'], kwargs['minLineLength'], kwargs['maxLineGap'])
134
214
  if lines is None: lines = np.array([])
135
215
  # 处理用二值化,显示用原图
136
- self.imshow(CvPlot.lines(self.img, lines))
137
- tt.toc(f'x={x} lines.shape={lines.squeeze().shape}')
216
+ self.imshow(xlcv.lines(self.img, lines))
217
+
218
+
219
+ def test_adaptiveThreshold(img=None):
220
+ """ 滑动条工具使用方法的示例 """
221
+ # 1 图片可以传入路径,这里做一个随机图片
222
+ if img is None:
223
+ img = np.random.randint(0, 255, [500, 200], dtype='uint8')
224
+ else:
225
+ img = xlcv.read(img, 0) # 确保是单通道灰度图
226
+
227
+ # 2 指定要分析的函数
228
+ t = TrackbarTool(imgproc=cv2.adaptiveThreshold, img=img, verbose=2, delay=100)
229
+ t.create_trackbar('maxValue', 255, 255) # 添加滑动条:名称,最大值,默认值
230
+ # 创建滑动条过程中,可能会报错,因为参数还不全,指定的imgproc还无法运行,可以先不用管
231
+ t.create_trackbar('adaptiveMethod', 10, 0)
232
+ t.create_trackbar('thresholdType', 10, 1)
233
+ t.create_trackbar('blockSize', 100, 11)
234
+ t.create_trackbar('C', 20, 3)
235
+ # t.imshow(img2) # 临时显式其他图片处理效果。如果需要持久修改,可以更改self.img的值
236
+ cv2.waitKey()
237
+
238
+
239
+ def test_CoarseDropout(img=None):
240
+ """ 做albumentations库里类功能的示例写法 """
241
+ import albumentations as A
242
+
243
+ if img is None:
244
+ img = np.random.randint(0, 255, [500, 200], dtype='uint8')
245
+
246
+ t = TrackbarTool.from_album(A.CoarseDropout, img=img, verbose=2)
247
+ t.create_trackbar('max_holes', 50, 8)
248
+ t.create_trackbar('max_height', 50, 8)
249
+ t.create_trackbar('max_width', 50, 8)
250
+ t.create_trackbar('fill_value', 255, 0)
251
+ cv2.waitKey()