类似图片查找器脚本的错误“列表索引必须是整数或切片,而不是str”

时间:2019-04-01 20:20:39

标签: hash cv2

我正在跟踪一个脚本,用于在数据库中查找相似的图像并标识由Moondra创建的图像:https://www.youtube.com/watch?v=8rcVcH4OTdo

我一字接一字地复制它,然后遇到一个问题,即“列表索引必须是整数或切片,而不是str”。

在运行该行时发生:

重复,ds_dict,hash_ds = difference_score_dict_hash(图像文件)

在绘制图像时我也遇到了问题,因此我可以看到哪些图像是重复的。如果有一种简单的方法来显示哪些图像是重复图像,那也将不胜感激。

(P.S主要用于Moondra)

这是代码(仅用于所需的上下文):

import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import imread, imresize, imshow
import numpy as np
import cv2
import os
import time
from hashlib import md5

IMAGE_DIR = 'c:\\users\\JeffO\\desktop\\Image_Test'
os.chdir(IMAGE_DIR)
os.getcwd()

image_files = os.listdir()
print(len(image_files))

#Gets name of image
image_files[0]

#Gets shape of image
imread(image_files[0]).shape


##Helper functions

#Makes sure image is 3 dimensional

def filter_images(images):
    image_list = []
    for image in images:
        try:
            assert imread(image).shape[2] == 3
            image_list.append(image)
        except AssertionError as e:
            print(e)
        return image_list

#Turning the image into gray scale

def img_gray(image):
    image = imread(image)
    return np.average(image, weights=[0.229, 0.587, 0.114], axis=2)

#Resize the images and flatten
def resize (image, height=30, width= 30):
   row_res= cv2.resize(image, (height, width), interpolation = cv2.INTER_AREA).flatten()
   col_res= cv2.resize(image, (height, width), interpolation = cv2.INTER_AREA).flatten('F')
    return row_res, col_res

#Gradient direction based on intesity

def intensity_diff(row_res, col_res):
    difference_row = np.diff(row_res)
    difference_col = np.diff(col_res)
    difference_row = difference_row > 0
    difference_col = difference_col > 0
     return np.vstack((difference_row, difference_col)).flatten()

##Helper functions end

# Hashes the images 
def file_hash(array):
    return md5(array).hexdigest()

#Takes all the functions and puts it into one big function

def difference_score(image, height = 30, width = 30):
    gray = img_gray(image)
    row_res, col_res = resize(gray, height, width)
    difference = intensity_diff(row_res, col_res)

    return difference

#Creating a dictionary of all the hash values
def difference_score_dict_hash(image_list):
    ds_dict = [] #Holds the keys
    duplicates = [] #Holds the duplicates
    hash_ds = []
    for image in image_list:
        ds = difference_score(image)
        hash_ds.append(ds)
        filehash = md5(ds).hexdigest() #Creates a hash out of dict store
        if filehash not in ds_dict:
        ds_dict[filehash] = image
    else:
        duplicates.append((image, ds_dict[filehash]))

return duplicates, ds_dict, hash_ds

#Finds the duplicates
image_files = filter_images(image_files)
duplicates, ds_dict, hash_ds = difference_score_dict_hash(image_files)

!!!!!!!

这是我得到的确切错误:

文件“”,第1行,在     重复项,ds_dict,hash_ds = Difference_score_dict_hash(image_files)

文件“”,第96行,在difference_score_dict_hash中     ds_dict [filehash] =图片

TypeError:列表索引必须是整数或切片,而不是str

0 个答案:

没有答案