Python图像识别对比
在电脑中查找是否有模板图片,MD5值可以比较,但是有些图片压缩了。所以不能使用校验码比对。所以想用图片识别。
先将模板图片读入内存中,然后在获取文件夹内的所有图片,对所有图片与目标文件夹中的图片对比,但是对比速度比较慢,目标图片少的时候每张图片识别的0.9秒多,当目标图片到达上万张以后,一夜才识别九千多张图片。帮忙看看代码要怎么改才能提高速度或者使用其他什么方法达到相同的目的。
def getAllImage(folderPath, imageList):
global now_time
extend_name = ["jpg", "jpeg", "png", "bmp", "JPEG", "PNG", "JPG", "BMP"]
if os.path.isfile(folderPath):
if item.split('.')[-1] in extend_name:
# print('这是第', sys._getframe().f_lineno, filePath)
imageList.append(filePath)
if time.time() - now_time > 2:
print("检索图片中。。。。。。。", '已检索', len(imageList), '张图片')
now_time = time.time()
return imageList
for item in os.listdir(folderPath):
if os.path.isdir(os.path.join(folderPath, item)):
subFolderPath = os.path.join(folderPath, item)
getAllImage(subFolderPath, imageList)
else:
filePath = os.path.join(folderPath, item)
if os.path.isfile(filePath):
if item.split('.')[-1] in extend_name:
# filePath.replace('\\\\','/')
# print('这是第', sys._getframe().f_lineno, filePath)
imageList.append(filePath)
now_temp=time.time()
if now_temp - now_time>2:
print("检索图片中。。。。。。。",'已检索',len(imageList),'张图片')
now_time=time.time()
return imageList
def calculate(image1, image2):
image1 = cv2.cvtColor(numpy.asarray(image1), cv2.COLOR_RGB2BGR)
image2 = cv2.cvtColor(numpy.asarray(image2), cv2.COLOR_RGB2BGR)
hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0])
hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0])
# 计算直?图的重合度
degree = 0
for i in range(len(hist1)):
if hist1[i] != hist2[i]:
degree = degree + (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i]))
else:
degree = degree + 1
degree = degree / len(hist1)
return degree
def classify_hist_with_split(sub_image, image2, size=(256, 256)):
image_path=image2
image2 = Image.open(image2)
# 将图像resize后,分离为RGB三个通道,再计算每个通道的相似值
# xy=os.stat(image_path).st_size
if os.stat(image_path).st_size > 2000000:
(x,y)=image2.size
x_s = 600
y_s = int(y * x_s / x)
image2 = image2.resize((x_s, y_s), Image.LANCZOS)
image2 = cv2.cvtColor(numpy.asarray(image2), cv2.COLOR_RGB2BGR)
image2 = cv2.resize(image2, size)
sub_image2 = cv2.split(image2)
sub_data = 0
for im1, im2 in zip(sub_image, sub_image2):
sub_data += calculate(im1, im2)
sub_data = sub_data / 3
return sub_data
def load_mud(model_list, size=(256, 256)):
model_temp = []
n=0
for image1 in model_list:
n=n+1
print('模板计算加载中。。。。',n)
image_open = Image.open(image1)
image_open = cv2.cvtColor(numpy.asarray(image_open), cv2.COLOR_RGB2BGR)
image_open = cv2.resize(image_open, size)
sub_image = cv2.split(image_open)
# for i in sub_image:
# hist = []
# image_cvtcolor = cv2.cvtColor(numpy.asarray(i), cv2.COLOR_RGB2BGR)
# hist1 = cv2.calcHist([image_cvtcolor], [0], None, [256], [0.0, 255.0])
# hist.append(hist1)
model_temp.append(sub_image)
print('模板加载完毕。。。。。。')
return model_temp