TFocus.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. # -*- coding:utf-8 -*-
  2. from ssat_sdk import getMenuTree3SelectedPExcelPath
  3. from ssat_sdk.picture.RGB import RGBColor
  4. from ssat_sdk.picture.color_space import CIEluvCaculator
  5. from ssat_sdk.picture.feature_detect import FeatureDetect
  6. from ssat_sdk.picture.image_util import *
  7. import os
  8. import sys
  9. import time
  10. import numpy as np
  11. import cv2 as cv
  12. import json
  13. from ssat_sdk.MenuTree3.TConfig import TConfig
  14. from ssat_sdk.MenuTree3.TExcelParser import CExcelParser
  15. g_level = ['First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth',
  16. 'Seventh', 'Eighth', 'Ninth', 'Tenth', 'Eleventh', 'Twelfth']
  17. class TFocus():
  18. def __init__(self, project_path, uitRunner=None, screen=[1920, 1080], icon_shape=[1920, 1080]):
  19. self.params_path = project_path
  20. self.rgbColor = RGBColor()
  21. self.CIE = CIEluvCaculator()
  22. self.feature_detect = FeatureDetect()
  23. self.xlsparser = uitRunner.uitPathManage
  24. # icon分辨率width*height;
  25. self.icon_shape = icon_shape
  26. # 当前屏幕分辨率;
  27. self.screen = screen
  28. # 比率;
  29. self.rate = self.icon_shape[0] / self.screen[0]
  30. def findFocusByIcon(self, src_pic, icon_pic,
  31. dcfg={"offset": 20, "minPeri": 0, "maxPeri": 0, "minArea": 0, "maxArea": 0, "morphology": []}):
  32. iconImg = cv.imread(icon_pic)
  33. # 获取小图平均bgr值;
  34. bgr = self.rgbColor.getAvgBGR(iconImg)
  35. print "findFocusByIcon,bgr:%s, src_pic=%s, icon_pic=%s, dcfg=%s" % (
  36. str(bgr), src_pic, icon_pic, str(dcfg))
  37. if dcfg.has_key("morphology"):
  38. morphology = dcfg["morphology"]
  39. else:
  40. morphology = []
  41. return self.feature_detect.findCurrentFoucsByColor(
  42. src_pic,
  43. [],
  44. bgr,
  45. dcfg["offset"],
  46. dcfg["minPeri"],
  47. dcfg["maxPeri"],
  48. dcfg["minArea"],
  49. dcfg["maxArea"],
  50. morphology)
  51. # 文本颜色焦点区域查找;
  52. def findFramelessBoxByIcon(self, src_pic, icon_pic, offset=20):
  53. iconImg = cv.imread(icon_pic)
  54. # 获取小图平均bgr值;
  55. bgr = self.rgbColor.getAvgBGR(iconImg)
  56. print "findFramelessBoxByIcon,bgr:%s, src_pic=%s, icon_pic=%s, offset=%s" % (
  57. str(bgr), src_pic, icon_pic, str(offset))
  58. return self.feature_detect.findFramelessFocusByColor(src_pic, [], bgr, offset)
  59. '''
  60. 在传入的图片上,查找指定小图片bgr颜色框矩形区域,返回矩形区域的坐标
  61. :param bigPic:图片路径
  62. :param level:excel层次
  63. :param first_parent:大部分情况下是根菜单;少部分情况是当前option的parent;
  64. :return rectArea:矩形区域和区域图像内容
  65. '''
  66. def findRectByIcon(self, bigPic, level, first_parent, option=""):
  67. print "level:", level
  68. print "first_parent:", first_parent
  69. if option.__len__() == 0:
  70. icon_path = os.path.join(self.params_path, "icon\\", str(
  71. first_parent) + "." + str(level) + ".png")
  72. else:
  73. icon_path = os.path.join(self.params_path, "icon\\", str(
  74. first_parent) + "." + str(level) + "_" + option + ".png")
  75. print "icon_path_option=", icon_path
  76. if os.path.exists(icon_path) is False:
  77. icon_path = os.path.join(self.params_path, "icon\\", str(
  78. first_parent) + "." + str(level) + ".png")
  79. print "icon_path=", icon_path
  80. if os.path.exists(icon_path) is False:
  81. icon_path = os.path.join(self.params_path, "icon\\", str(
  82. first_parent) + "." + str('First') + ".png")
  83. print "icon_path_First=", icon_path
  84. if os.path.exists(icon_path) is False:
  85. return False, []
  86. # 获取配置文件值;
  87. dcfg = {"offset": 20, "minPeri": 0,
  88. "maxPeri": 0, "minArea": 0, "maxArea": 0}
  89. ini_path = os.path.join(self.params_path, "menutree.ini")
  90. if os.path.exists(ini_path) is True:
  91. tconfig = TConfig(ini_path)
  92. if tconfig.has_option(level, first_parent):
  93. dcfg = tconfig.get_dict(tconfig.get_value(level, first_parent))
  94. dcfg['minPeri'] = self.rate * dcfg['minPeri']
  95. dcfg['maxPeri'] = self.rate * dcfg['maxPeri']
  96. dcfg['minArea'] = pow(self.rate, 2) * dcfg['minArea']
  97. dcfg['maxArea'] = pow(self.rate, 2) * dcfg['maxArea']
  98. if "morphology" not in dcfg:
  99. dcfg["morphology"] = []
  100. return self.findFocusByIcon(bigPic, icon_path, dcfg)
  101. '''
  102. 获取option配置信息;
  103. '''
  104. def getOptionConfig(self, cur_option, is_value_sheet=False):
  105. # 获取Option的others字段信息;
  106. if self.xlsparser is None:
  107. self.xlsparser = CExcelParser(getMenuTree3SelectedPExcelPath())
  108. # 读取excel并解析出内容;
  109. self.xlsparser.read_excel()
  110. # 获取option的路径表;
  111. pp = self.xlsparser.get_option_paths(cur_option)
  112. if pp.__len__() == 0:
  113. print 'getOptionConfig.get_option_paths(%s) is null' % (cur_option)
  114. return False, {"icon_path": "", "dcfg": {}, "dir_path": ""}
  115. # 获取first_parent;
  116. first_parent = pp[g_level[0]]['parent']
  117. # 当前option所在level;
  118. cur_level = g_level[pp.__len__() - 1]
  119. # 当前父路径;
  120. cur_parent = pp[cur_level]['parent']
  121. if is_value_sheet is True:
  122. cur_level = 'value'
  123. # print first_parent, cur_level, cur_parent, cur_option
  124. # 获取配置文件值;
  125. dcfg = {}
  126. ini_path = os.path.join(self.params_path, "menutree.ini")
  127. tconfig = TConfig(ini_path)
  128. # 首先找当前路径图;
  129. icon_path = os.path.join(self.params_path, "icon\\", str(
  130. cur_parent) + "." + str(cur_level) + "_" + cur_option + ".png")
  131. icon_dir_path = os.path.join(self.params_path, "icon\\", str(
  132. cur_parent) + "." + str(cur_level) + "_" + cur_option + ".dir.png")
  133. dcfg = tconfig.get_value_dict(cur_level, cur_parent + '.' + cur_option)
  134. print u"trying icon:%s"%icon_path
  135. print u"trying dcfg:get_value_dict(%s, %s)"%(cur_level, cur_parent + '.' + cur_option)
  136. # icon路径;
  137. if os.path.exists(icon_path) is False:
  138. print u'图标优先级0:[cur_parent].[cur_level]_[cur_option].png=%s is null' % (
  139. icon_path)
  140. # 当前次路径图;
  141. icon_path = os.path.join(self.params_path, "icon\\", str(
  142. cur_parent) + "." + str(cur_level) + ".png")
  143. icon_dir_path = os.path.join(self.params_path, "icon\\", str(
  144. cur_parent) + "." + str(cur_level) + ".dir.png")
  145. dcfg = tconfig.get_value_dict(cur_level, cur_parent)
  146. print u"trying icon:%s" % icon_path
  147. print u"trying dcfg:get_value_dict(%s, %s)" % (cur_level, cur_parent)
  148. if os.path.exists(icon_path) is False:
  149. print u'图标优先级1:[cur_parent].[cur_level].png=%s is null' % (
  150. icon_path)
  151. # first层;
  152. icon_path = os.path.join(self.params_path, "icon\\", str(
  153. first_parent) + "." + str(cur_level) + "_" + cur_option + ".png")
  154. icon_dir_path = os.path.join(self.params_path, "icon\\", str(
  155. first_parent) + "." + str(cur_level) + "_" + cur_option + ".dir.png")
  156. dcfg = tconfig.get_value_dict(
  157. cur_level, first_parent + '.' + cur_option)
  158. print u"trying icon:%s" % icon_path
  159. print u"trying dcfg:get_value_dict(%s, %s)" % (cur_level, first_parent + '.' + cur_option)
  160. if os.path.exists(icon_path) is False:
  161. print u'图标优先级2:[first_parent].[cur_level]_[cur_option].png=%s is null' % (
  162. icon_path)
  163. # first层次路径图;
  164. icon_path = os.path.join(self.params_path, "icon\\", str(
  165. first_parent) + "." + str(cur_level) + ".png")
  166. icon_dir_path = os.path.join(self.params_path, "icon\\", str(
  167. first_parent) + "." + str(cur_level) + ".dir.png")
  168. dcfg = tconfig.get_value_dict(cur_level, first_parent)
  169. print u"trying icon:%s" % icon_path
  170. print u"trying dcfg:get_value_dict(%s, %s)" % (cur_level, first_parent)
  171. if os.path.exists(icon_path) is False:
  172. print u'图标优先级3:[first_parent].[cur_level].png =%s is null' % (
  173. icon_path)
  174. # endif;
  175. # endif;
  176. # endif;
  177. if dcfg.__len__() == 0:
  178. dcfg = {"offset": 20, "minPeri": 0,
  179. "maxPeri": 0, "minArea": 0, "maxArea": 0, "morphology": []}
  180. print u"get dcfg fail!Using default para!"
  181. if os.path.exists(icon_path) is False:
  182. print 'getOptionConfig:current use icon_path=%s is null' % (
  183. icon_path)
  184. return False, {"icon_path": "", "dcfg": {}, "dir_path": ""}
  185. print u"实际使用的icon图片路径icon_path:%s, 实际使用INI配置:%s" % (
  186. str(icon_path), str(dcfg))
  187. # 返回路径;
  188. return True, {"icon_path": icon_path, "dcfg": dcfg, "dir_path": icon_dir_path}
  189. def findRectByIcon2(self, src_pic, cur_option, is_value_sheet=False):
  190. # 获取参数;
  191. result, opcfg = self.getOptionConfig(cur_option, is_value_sheet)
  192. if result is False:
  193. return False, []
  194. icon_path = opcfg['icon_path']
  195. dcfg = opcfg['dcfg']
  196. dcfg['minPeri'] = self.rate * dcfg['minPeri']
  197. dcfg['maxPeri'] = self.rate * dcfg['maxPeri']
  198. dcfg['minArea'] = pow(self.rate, 2) * dcfg['minArea']
  199. dcfg['maxArea'] = pow(self.rate, 2) * dcfg['maxArea']
  200. print u"实际使用的聚焦参数字典dcfg:%s" % str(dcfg)
  201. if "morphology" not in dcfg:
  202. dcfg["morphology"] = []
  203. return self.findFocusByIcon(src_pic, icon_path, dcfg)
  204. def rgb2hsv(r, g, b):
  205. r, g, b = r / 255.0, g / 255.0, b / 255.0
  206. mx = max(r, g, b)
  207. mn = min(r, g, b)
  208. df = mx - mn
  209. # h值
  210. if mx == mn:
  211. h = 0
  212. elif mx == r and g >= b:
  213. h = (60 * (g - b) / df) % 360
  214. elif mx == r and g < b:
  215. h = (60 * ((g - b) / df) + 360) % 360
  216. elif mx == g:
  217. h = (60 * ((b - r) / df) + 120) % 360
  218. elif mx == b:
  219. h = (60 * ((r - g) / df) + 240) % 360
  220. # s值
  221. if mx == 0:
  222. s = 0
  223. else:
  224. s = df / mx * 255
  225. # v值
  226. v = mx * 255
  227. # 返回;
  228. return h, s, v
  229. if __name__ == "__main__":
  230. # print '\n\n====>\n', pow(10, 2)
  231. tFocus = TFocus(r"D:\SAT\resource\MenuTree\MS6586\ISDB")
  232. tFocus.getOptionConfig('backlight', True)
  233. tFocus.findRectByIcon2('', 'backlight') # lock
  234. if 0:
  235. bigPic_2 = r"D:\CH01.JPG"
  236. icon = r"D:\SAT\resource\MenuTree\mi\AM950_IND_V27N\result.png"
  237. result, contourRect = tFocus.findRectByIcon(bigPic_2, 'Third', 'lock')
  238. print "__name__,rect:", result, contourRect
  239. if result is True:
  240. saveCropPic(bigPic_2, icon, contourRect)