图像识别相关
1、airtest项目:airtest、poco; 地址: https://github.com/AirtestProject
** pip install -U airtest
** 使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from airtest.core.api import * # connect an android phone with adb init_device( "Android" ) # or use connect_device api # connect_device("Android:///") install( "path/to/your/apk" ) start_app( "package_name_of_your_apk" ) touch(Template( "image_of_a_button.png" )) swipe(Template( "slide_start.png" ), Template( "slide_end.png" )) assert_exists(Template( "success.png" )) keyevent( "BACK" ) home() uninstall( "package_name_of_your_apk" ) |
2、图像识别:形状+颜色:https://blog.csdn.net/zgr957254329/article/details/123891504
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | import collections import math import cv2 import matplotlib.pyplot as plt import numpy as np #颜色字典 def ColorList(): dict = collections.defaultdict( list ) #黑色 lower_black = np.array([ 0 , 0 , 0 ]) upper_black = np.array([ 180 , 255 , 46 ]) color_list = [] color_list.append(lower_black) color_list.append(upper_black) dict [ 'black' ] = color_list #灰色 lower_gray = np.array([ 0 , 0 , 46 ]) upper_gray = np.array([ 180 , 43 , 220 ]) color_list = [] color_list.append(lower_gray) color_list.append(upper_gray) dict [ 'gray' ] = color_list #白色 lower_white = np.array([ 0 , 0 , 221 ]) upper_white = np.array([ 180 , 30 , 255 ]) color_list = [] color_list.append(lower_white) color_list.append(upper_white) dict [ 'white' ] = color_list #粉色 lower_pink = np.array([ 156 , 43 , 46 ]) upper_pink = np.array([ 180 , 255 , 255 ]) color_list = [] color_list.append(lower_pink) color_list.append(upper_pink) dict [ 'pink' ] = color_list #红色 lower_red = np.array([ 0 , 43 , 46 ]) upper_red = np.array([ 10 , 255 , 255 ]) color_list = [] color_list.append(lower_red) color_list.append(upper_red) dict [ 'red' ] = color_list #橙色 lower_orange = np.array([ 11 , 43 , 46 ]) upper_orange = np.array([ 25 , 255 , 255 ]) color_list = [] color_list.append(lower_orange) color_list.append(upper_orange) dict [ 'orange' ] = color_list #黄色 lower_yellow = np.array([ 26 , 43 , 46 ]) upper_yellow = np.array([ 34 , 255 , 255 ]) color_list = [] color_list.append(lower_yellow) color_list.append(upper_yellow) dict [ 'yellow' ] = color_list #绿色 lower_green = np.array([ 35 , 43 , 46 ]) upper_green = np.array([ 77 , 255 , 255 ]) color_list = [] color_list.append(lower_green) color_list.append(upper_green) dict [ 'green' ] = color_list #青色 lower_cyan = np.array([ 78 , 43 , 46 ]) upper_cyan = np.array([ 99 , 255 , 255 ]) color_list = [] color_list.append(lower_cyan) color_list.append(upper_cyan) dict [ 'cyan' ] = color_list #蓝色 lower_blue = np.array([ 100 , 43 , 46 ]) upper_blue = np.array([ 124 , 255 , 255 ]) color_list = [] color_list.append(lower_blue) color_list.append(upper_blue) dict [ 'blue' ] = color_list # 紫色 lower_purple = np.array([ 125 , 43 , 46 ]) upper_purple = np.array([ 155 , 255 , 255 ]) color_list = [] color_list.append(lower_purple) color_list.append(upper_purple) dict [ 'purple' ] = color_list return dict #颜色判断 def findColor(imgcut): img_hsv = cv2.cvtColor(imgcut,cv2.COLOR_BGR2HSV) color_dict = ColorList() #print(color_dict) color_most = 0 color_now = None for color in color_dict: #二值化 和颜色字典比较 在上下限之间的像素变为255,之外的所有像素变为0 color_cmp = cv2.inRange(img_hsv,color_dict[color][ 0 ],color_dict[color][ 1 ]) #膨胀 使颜色分割成块并更突出 color_boom = cv2.dilate(color_cmp, None ,iterations = 1 ) #取出每一小块 contours,hierarchy = cv2.findContours(color_boom.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) color_area = 0 for img in contours: color_area + = cv2.contourArea(img) if (color_area>color_most): color_most = color_area color_now = color return color_now #计算斜率 def k_count(x1,y1,x2,y2): if ((x2 - x1) = = 0 ): x2 + = 0.01 k = (y2 - y1) / (x2 - x1) if (k = = 0 ): k + = 0.01 return k #计算角度 def angle_count(k1,k2): angle = math.atan2((k2 - k1),( 1 + k1 * k2)) angle = angle * 180 / math.pi return abs (angle) #图形处理 def LastButNotLeast(imginit,imgcopy): # 灰度化 img_Gray = cv2.cvtColor(imginit, cv2.COLOR_BGR2GRAY) # 高斯平滑 img_Blur = cv2.GaussianBlur(img_Gray, ( 3 , 3 ), 1 ) # 边缘检测 img_Canny = cv2.Canny(img_Blur, 50 , 50 ) #得到图片中所有图形的轮廓 #findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> contours, hierarchy contours,hierarchy = cv2.findContours(img_Canny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) #参数:输入图像,霍夫梯度法,分辨率,最小距离,检测方法的对应的参数*2,半径 for img in contours: #计算面积 太小就不算了 area = cv2.contourArea(img) if area> 80 : perimeter = cv2.arcLength(img, True ) #折线化 side = cv2.approxPolyDP(img, 0.01 * perimeter, True ) #print(side) #计算有几条线 sideNum = len (side) #print(sideNum) #计算边长 length = [] k = [] for i in range ( 0 ,sideNum): if (i + 1 <sideNum): l = ((side[i][ 0 ][ 0 ] - side[i + 1 ][ 0 ][ 0 ]) * * 2 + (side[i][ 0 ][ 1 ] - side[i + 1 ][ 0 ][ 1 ]) * * 2 ) * * ( 1 / 2 ) ktemp = k_count(side[i][ 0 ][ 0 ],side[i][ 0 ][ 1 ],side[i + 1 ][ 0 ][ 0 ],side[i + 1 ][ 0 ][ 1 ]) else : l = ((side[i][ 0 ][ 0 ] - side[ 0 ][ 0 ][ 0 ]) * * 2 + (side[i][ 0 ][ 1 ] - side[ 0 ][ 0 ][ 1 ]) * * 2 ) * * ( 1 / 2 ) ktemp = k_count(side[i][ 0 ][ 0 ],side[i][ 0 ][ 1 ],side[ 0 ][ 0 ][ 0 ],side[ 0 ][ 0 ][ 1 ]) length.append(l) k.append(ktemp) #print(length) #计算角度 angle = [] for i in range ( 0 ,sideNum): if (i + 1 <sideNum): ang = angle_count(k[i],k[i + 1 ]) else : ang = angle_count(k[i],k[ 0 ]) angle.append(ang) #print(angle) #形状判断 #三角形 if sideNum = = 3 : tag = "triangle" #其他四边形 elif sideNum = = 4 : tag = "Other quadrilateral" flag = 0 #菱形 err = 5 if (length[ 1 ] - err< = length[ 0 ]< = length[ 1 ] + err): if (length[ 2 ] - err< = length[ 1 ]< = length[ 2 ] + err): if (length[ 3 ] - err< = length[ 2 ]< = length[ 3 ] + err): if (length[ 0 ] - err< = length[ 3 ]< = length[ 0 ] + err): tag = "diamond" flag = 1 #矩形 if ( 89 <angle[ 0 ]< 91 ): if ( 89 <angle[ 1 ]< 91 ): if ( 89 <angle[ 2 ]< 91 ): if ( 89 <angle[ 3 ]< 91 ): tag = "rectangular" if (flag = = 1 ): tag = "square" elif sideNum = = 5 : tag = "pentagon" elif sideNum = = 6 : tag = "hexagon" elif sideNum = = 10 : tag = "five-pointed star" elif sideNum> 10 : tag = "circle" else : tag = "None" #定个位 x,y,wide,high = cv2.boundingRect(side) #裁剪中心位置 x0 = int (x + (wide / 2 )) y0 = int (y + (high / 2 )) err = 25 imgCut = imginit[(y0 - err):(y0 + err),(x0 - err):(x0 + err)] color = findColor(imgCut) #添加标签 cv2.rectangle(imgcopy, (x - 5 , y - 5 ), (x + wide + 5 , y + high + 5 ), ( 0 , 235 , 6 ), 2 ) cv2.putText(imgcopy, tag,(x, y - 28 ), cv2.FONT_HERSHEY_TRIPLEX, 0.85 ,( 0 , 0 , 255 ), 1 ) cv2.putText(imgcopy, color,(x, y - 5 ), cv2.FONT_HERSHEY_TRIPLEX, 0.85 ,( 0 , 0 , 255 ), 1 ) if __name__ = = "__main__" : path = input ( "图片路径:" ) #path=r"D:\test.png" img = cv2.imread(path) while (np. all (img = = None )): print ( "无法读取图片" ) path = input ( "图片路径:" ) img = cv2.imread(path) img_Copy = img.copy() #图像处理函数 LastButNotLeast(img,img_Copy) #输出结果 plt.subplot( 1 , 2 , 1 ) img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) plt.imshow(img) plt.title( "The original image" ) plt.subplot( 1 , 2 , 2 ) img_Copy = cv2.cvtColor(img_Copy,cv2.COLOR_BGR2RGB) plt.imshow(img_Copy) plt.title( "The image after processing" ) plt.show() |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?