示例#1
0
def indexer():
    args={'index': 'index.csv', 'dataset': 'dataset'}
    cd = ColorDescriptor((8, 12, 3))
    output = open(args["index"], "w")
    i=0

    paths =[os.path.join(dirpath,f)
            for dirpath,dirnames,files in os.walk(args["dataset"])
            for f in files if (f.endswith('.jpg') | (f.endswith('.JPG')))]

    for imagePath in paths:
        if(imagePath.count('/')>1):
            choosenfile = imagePath.split('/')
            choosenfile = choosenfile[::-1]
            imageID = choosenfile[1]+'/'+choosenfile[0]
        else:
            imageID=imagePath[imagePath.rfind("/")+1:]

        image = cv2.imread(imagePath)
            # describe the image
        features = cd.describe(image)

            # write the features to file
        features = [str(f) for f in features]
        output.write("%s,%s\n" % (imageID, ",".join(features)))
        i+=1

	# close the index file

    output.close()
    return i
def Disease_Detection(image_path):
    # initialize the image descriptor
    cd = ColorDescriptor((8, 12, 3))
    # load the query image and describe it
    # query = cv2.imread(args["query"])
    filenames = listdir(image_path)
    a = [filename for filename in filenames if filename.endswith('.png')]
    path = image_path + '/' + a[0]
    print(path)
    query = cv2.imread(path)
    print("query==", query)
    features = cd.describe(query)

    # perform the search
    #searcher = Searcher(args["index"])
    searcher = Searcher("../index.csv")
    results = searcher.search(features)
    results.pop(0)
    print("==============", results)
    print()
    # disease_name = results[1][1].split("_")[0].split("\\")[1]   #.... extracting disease name

    # print("**********",disease_name)
    return (results[1][1].split("_")[0])


# Disease_Detection('/home/vishal/Dropbox/ML/Plant_Disease_Detection/queries')
示例#3
0
def find_similar(query):
    """
	query (str): path to the img
	ex : queries2\img_231.jpg

	return
	list_img (list) : 3 paths of img similar
	"""

    # initialize list
    list_img = []

    # initialize the image descriptor
    cd = ColorDescriptor((8, 12, 3))

    # load the query image and describe it
    query = cv2.imread(query)
    features = cd.describe(query)

    # perform the search
    searcher = Searcher("index2.csv")
    results = searcher.search(features)

    # display the query
    # cv2.imshow("Query", query)

    # loop over the results
    for (score, resultID) in results:

        # add img to list
        list_img.append(str(resultID).replace("\\", "/"))

    return list_img
示例#4
0
    def saveUrl(url, img_dir):
        try:
            file = urllib.URLopener()
            directory = ImgManagement.getTimeDir(img_dir)
            if not os.path.exists(directory):
                os.makedirs(directory)
            path = os.path.join(directory, str(randint(0, 100000000)))
            while os.path.isfile(path):
                path = os.path.join(directory, str(randint(0, 100000000)))

            file.retrieve(url, path)
            md5 = ImgManagement.getMD5(path)
            if ImageDB.getItem({"md5": md5}) is not None:
                ImgManagement.deleteFile(path)
            else:
                image = Image.open(path)
                cd = ColorDescriptor((8, 12, 3))
                features = cd.describe(image)
                ImageDB.insert(md5, features, path)
                ImageDB.getList(True)
        except:
            print("*** ImageManagement saveUrl takes error ***")
            print(sys.exc_info()[0])
            traceback.print_exc()
            print("*** ImageManagement saveUrl takes error ***")
示例#5
0
def check(limit):
    args = {"query": "queries/a.jpg", "index": "whpu.csv"}
    #初始化图像描述符
    cd = ColorDescriptor((8, 12, 3))

    #从磁盘读取待搜索图像
    query = cv2.imread(args["query"])
    #提取该图像的特征
    features = cd.describe(query)

    #执行搜索
    #使用提取到的特征进行搜索,返回经过排序后的结果列表
    searcher = Searcher(args["index"])
    results = searcher.search(features, limit)

    #显示出待搜索的图像
    #cv2.imshow("Query", query)
    result_files_data = []

    #遍历搜索结果,将相应的图像显示在屏幕上
    for (score, resultID) in results:
        result = "dataset/" + resultID
        result_files_data.append([score, result])

    return result_files_data
示例#6
0
def search():

    if request.method == "POST":

        RESULTS_ARRAY = []

        # get url
        image_url = request.form.get('img')

        try:

            # initialize the image descriptor
            cd = ColorDescriptor((8, 12, 3))

            # load the query image and describe it
            from skimage import io
            query = io.imread(image_url)
            features = cd.describe(query)

            # perform the search
            searcher = Searcher(INDEX)
            results = searcher.search(features)

            # loop over the results, displaying the score and image name
            for (score, resultID) in results:
                RESULTS_ARRAY.append(
                    {"image": str(resultID), "score": str(score)})

            # return success
            return jsonify(results=(RESULTS_ARRAY[::-1][:3]))

        except:

            # return error
            jsonify({"sorry": "Sorry, no results! Please try again."}), 500
示例#7
0
    def browse_query_img(self):

        self.query_img_frame = Frame(self.master)
        self.query_img_frame.pack()
        from tkFileDialog import askopenfilename
        self.filename = tkFileDialog.askopenfile(
            title='Choose an Image File').name

        # process query image to feature vector
        # initialize the image descriptor
        cd = ColorDescriptor((8, 12, 3))
        classifierFilePath = './pyimagesearch/VWClassifier.dat'
        fe = FeatureEvaluator(classifierFilePath)

        # load the query image and describe it
        query = cv.imread(self.filename)
        self.queryfeatures = cd.describe(query)
        # print len(self.queryfeatures) = 1440
        self.vmfeatures = fe.predictFromImage(query, k=1469)
        self.trfeatures = TRS.TRSolver(self.filename).result
        self.sffeatures = TFSFS.SemanticFeatureSolver(self.filename).result

        # show query image
        image_file = Image.open(self.filename)
        resized = image_file.resize((100, 100), Image.ANTIALIAS)
        im = ImageTk.PhotoImage(resized)
        image_label = Label(self.query_img_frame, image=im)
        image_label.pack()

        self.query_img_frame.mainloop()
示例#8
0
    def preprocess_image(self):
        # process query image to feature vector
        # initialize the image descriptor
        #cd = ColorDescriptor((8, 12, 3))
        cd = ColorDescriptor((3, 3, 2))
        sr = SemanticsReader()

        # load the query image and describe it
        query = cv2.imread(self.filename)
        self.queryfeatures = cd.describe(query)

        # convert query image to list of texts (if any)
        reader = csv.reader(open("dataset\\dataset\\combined_text_tags.txt"),
                            delimiter=" ")
        # find the line belonging to query image, req_line = None if no tags found
        img_name = self.filename.split("/")[-1]
        req_line = None
        for line in reader:
            if (line[0] == img_name):
                req_line = line[6:]
                break
        self.querytext = req_line

        # process query image to semantics vector
        # generate temp.txt for exe to run on it.
        tempfile = open("semanticFeature\\temp.txt", "w")
        tempfile.write(self.filename)
        tempfile.close()
        # generate the txt file with 1000D for query
        FNULL = open(os.devnull, 'w')  #suppress output to stdout
        os.chdir("semanticFeature")
        # Check if txt file with 1000D already exists.
        reqfile = self.filename
        base, ext = os.path.splitext(reqfile)
        req_text_file = base + ".txt"
        if (not os.path.isfile(req_text_file)):
            args = "./image_classification.exe temp.txt"
            #subprocess.call(args, stdout=FNULL, stderr=FNULL)
            subprocess.call(args)
        os.chdir("../")
        # read 1000D vector for semantics
        self.querysemantics = sr.read(base + ".txt")

        # convert query image to grayscale
        convertQueryToGray(self.filename)
        # generate key file for query
        #with open("sift/temp/query.pgm","rb") as ip, open("sift/temp/query.key","wb") as op:
        #   subprocess.call("sift/siftWin32.exe",stdin=ip,stdout=op)

        # Do deep learning
        # self.querycategory = deepSearch(self.filename)

        # show query image
        '''image_file = Image.open(self.filename)
示例#9
0
def search():
 
    if request.method == "POST":
 
        '''file = request.files['file']
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        RESULTS_ARRAY = []
        print(filename)'''
        RESULTS_ARRAY = []

        # get url
        image_url = request.form.get('img')
        print(image_url)
 
        try:
 
            # initialize the image descriptor
            cd = ColorDescriptor((8, 12, 3))
 
            # load the query image and describe it
            from skimage import io
            import cv2
            #query = io.imread("/home/kene/Documents/PyImageSearch/3D Histogram Descriptor Method With Web Interface/app/"+image_url)
            query = cv2.imread("/home/kene/Documents/Projects/Image-Search-Engine/app/static/queries/"+image_url);
            #query = io.imread(image_url)
            #query = (query * 255).astype("uint8")
            #(r, g, b) = cv2.split(query)
            #query = cv2.merge([b, g, r])
            #query = cv2.cvtColor(query, cv2.COLOR_BGR2RGB)
            features = cd.describe(query)
 
            # perform the search
            searcher = Searcher(INDEX)
            results = searcher.search(features)
 
            # loop over the results, displaying the score and image name
            for (score, resultID) in results:
                RESULTS_ARRAY.append(
                    {"image": str(resultID), "score": str(score)})
            # return success
            return jsonify(results=(RESULTS_ARRAY[::-1][:101]), preview="queries/"+image_url)
            #resultSet=jsonify(results=(RESULTS_ARRAY[::-1][:5]))
            #return render_template('index.html', preview = "static/queries/"+filename, resultSet = jsonify(results=(RESULTS_ARRAY[::-1][:5])))
 
        except Exception, e:
            print(str(e))
            # return error
            return jsonify({"sorry": "Sorry, no results! Please try again."}), 500
示例#10
0
def search():

    if request.method == "POST":

        RESULTS_ARRAY = []

        # get url
        image_url = request.form.get('img')

        try:

            # initialize the image descriptor
            cd = ColorDescriptor((8, 12, 3))

            # load the query image and describe it
            from skimage import io
            import cv2
            # query = io.imread(image_url)
            # query = (query * 255).astype("uint8")
            # (r, g, b) = cv2.split(query)
            # query = cv2.merge([b, g, r])

            image_url = "app/" + image_url[1:]
            # print "图像url路径:", image_url
            # print os.getcwd()
            # print sys.path[0]
            query = cv2.imread(image_url)
            # print "读取成功!"
            features = cd.describe(query)
            # print "描述子生成成功"

            # perform the search
            searcher = Searcher(INDEX)
            results = searcher.search(features)

            # loop over the results, displaying the score and image name
            for (score, resultID) in results:
                RESULTS_ARRAY.append({
                    "image": str(resultID),
                    "score": str(score)
                })

            # return success
            return jsonify(results=(RESULTS_ARRAY[:5]))

        except:

            # return error
            jsonify({"sorry": "Sorry, no results! Please try again."}), 500
示例#11
0
def search():
 
    if request.method == "POST":
 
        RESULTS_ARRAY = []
 
        # get url
        image_url = request.form.get('img')
        

        try:
 
            # initialize the image descriptor
            cd = ColorDescriptor((8, 12, 3))
 
            # load the query image and describe it
            from skimage import io
            import cv2
            # query = io.imread(image_url)
            # query = (query * 255).astype("uint8")
            # (r, g, b) = cv2.split(query)
            # query = cv2.merge([b, g, r])

            image_url = "app/" + image_url[1:]
            # print "图像url路径:", image_url
            # print os.getcwd()
            # print sys.path[0]
            query = cv2.imread(image_url)
            # print "读取成功!"
            features = cd.describe(query)
            # print "描述子生成成功"
 
            # perform the search
            searcher = Searcher(INDEX)
            results = searcher.search(features)
 
            # loop over the results, displaying the score and image name
            for (score, resultID) in results:
                RESULTS_ARRAY.append(
                    {"image": str(resultID), "score": str(score)})
 
            # return success
            return jsonify(results=(RESULTS_ARRAY[:5]))
 
        except:
 
            # return error
            jsonify({"sorry": "Sorry, no results! Please try again."}), 500
    def predict(self,location):
        # initialize the image descriptor
        cd = ColorDescriptor((8, 12, 3))
        print(location)
        query = cv2.imread(location)
        features = cd.describe(query)


        searcher = Searcher("index.csv")
        results = searcher.search(features)

        final_res = []
        for (score, resultID) in results:
            final_res.append(resultID)
            print(score,resultID)

        return final_res[0]
示例#13
0
def upload():
    file = request.files['file']
    print('upload', file)
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
    if request.method == "POST":

        RESULTS_ARRAY = []

        # get url

        try:

            # initialize the image descriptor
            cd = ColorDescriptor((8, 12, 3))

            # load the query image and describe it
            from skimage import io
            filename = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            print '###' + filename
            query = io.imread(filename)
            print 'shape'
            features = cd.describe(query)

            # perform the search
            searcher = Searcher(INDEX)
            results = searcher.search(features)

            # loop over the results, displaying the score and image name
            for (score, resultID) in results:
                RESULTS_ARRAY.append({
                    "image": str(resultID),
                    "score": str(score)
                })

            # return success
            return jsonify(results=(RESULTS_ARRAY[::-1][:3]),
                           file=file.filename)

        except:

            # return error
            return jsonify({"sorry":
                            "Sorry, no results! Please try again."}), 500
示例#14
0
def search():
    if request.method == "POST":

        RESULTS_ARRAY = []

        # get url
        image_url = request.form.get('img')

        try:

            # initialize the image descriptor
            cd = ColorDescriptor((8, 12, 3))
            # load the query image and describe it
            from skimage import io
            import cv2

            query = io.imread(image_url)
            # query = (query * 255).astype("uint8")
            # (r, g, b) = cv2.split(query)
            # query = cv2.merge([b, g, r])
            # cv2.imshow("Image", query)
            query = cv2.cvtColor(query, cv2.COLOR_BGR2RGB)
            features = cd.describe(query)
            # perform the search
            searcher = Searcher(INDEX)
            results = searcher.search(features)

            # loop over the results, displaying the score and image name
            for (score, resultID) in results:
                RESULTS_ARRAY.append({
                    "image": str(resultID),
                    "score": str(score)
                })

            # return success
            return jsonify(results=(RESULTS_ARRAY[::-1]))
            # return 'ok'

        except Exception as ex:
            template = "An exception of type {0} occurred. Arguments:\n{1!r}"
            message = template.format(type(ex).__name__, ex.args)
            print(message)
            return jsonify({"sorry":
                            "Sorry, no results! Please try again."}), 500
示例#15
0
def search():

    if request.method == "POST":

        RESULTS_ARRAY = []

        # get url
        image_url = request.form.get('img')

        try:

            # initialize the image descriptor
            cd = ColorDescriptor((8, 12, 3))

            # load the query image and describe it
            from skimage import io
            import cv2
            print("image_url ", image_url)
            path_img = os.path.join(os.getcwd(), image_url[1:])
            query = cv2.imread(path_img, cv2.COLOR_BGR2RGB)
            print("describe")
            features = cd.describe(query)

            print("Searcher")
            # perform the search
            searcher = Searcher(INDEX)
            print("search")
            results = searcher.search(features)
            print(INDEX)
            print("image")
            # loop over the results, displaying the score and image name
            for (score, resultID) in results:
                RESULTS_ARRAY.append({
                    "image": str(resultID),
                    "score": str(score)
                })

            print("Before success")
            # return success
            return jsonify(results=(RESULTS_ARRAY[::-1][:3]))

        except:
            # return error
            jsonify({"sorry": "Sorry, no results! Please try again."}), 500
示例#16
0
def search():

    if request.method == "POST":

        RESULTS_ARRAY = []

        # get url
        image_url = request.form.get('img')

        try:

            # initialize the image descriptor
            cd = ColorDescriptor((8, 12, 3))

            # load the query image and describe it
            from skimage import io
            import cv2

            query = cv2.imread(
                os.path.join(os.path.dirname(__file__),
                             'static/images/' + image_url))
            features = cd.describe(query)

            # perform the search
            searcher = Searcher(INDEX)
            results = searcher.search(features)

            # loop over the results, displaying the score and image name
            for (score, resultID) in results:
                RESULTS_ARRAY.append({
                    "image": str(resultID),
                    "score": str(score)
                })
            # return success
            return jsonify(results=(RESULTS_ARRAY[:101]),
                           preview="images/" + image_url)

        except Exception as e:
            print(str(e))
            # return error
            return jsonify({"sorry":
                            "Sorry, no results! Please try again."}), 500
示例#17
0
def func2(command):
    folder = 'target'
    command = os.path.join(folder, command)
    # initialize the image descriptor
    cd = ColorDescriptor((8, 12, 3))

    # load the query image and describe it
    query = cv2.imread(command)
    features = cd.describe(query)

    # perform the search
    searcher = Searcher("index.csv")
    results = searcher.search(features)

    # loop over the results
    result = []
    for (score, resultp) in results:
        url, imgname, imgurl = resultp
        doct = {"url": url, 'name': imgname, 'imgurl': imgurl}
        result.append(doct)
    return result
def Disease_Detection(image_path):
    # initialize the image descriptor
    cd = ColorDescriptor((8, 12, 3))
    # load the query image and describe it
    # query = cv2.imread(args["query"])
    filelist = [
        file for file in os.listdir(image_path) if file.endswith('.png')
    ]
    query = cv2.imread(image_path + filelist[0])
    # query = cv2.imread( 'D:\\projects\\Plant_Disease_Detection\\dataset\\CollarRot_1.png' )
    features = cd.describe(query)

    # perform the search
    #searcher = Searcher(args["index"])
    searcher = Searcher(image_path + "/index.csv")
    results = searcher.search(features)
    results.pop(0)

    disease_name = results[1][1].split("_")[0].split("\\")[
        1]  #.... extracting disease name

    return (disease_name)
示例#19
0
文件: handle.py 项目: keysona/project
def handle_upload_file(f):
    #存储文件
    fobj = open('test.file','w')
    for chunk in f.chunks():
	   fobj.write(chunk)
    fobj.close()
    #初始化工作
    conn = db.connect(host='localhost',user='******',passwd='dg123321',db='project')
    cur = conn.cursor()
    cd = ColorDescriptor((8, 12, 3))
    rows = cur.execute('select url,width,height,time,type,hist from image') 
    dataset = cur.fetchall()
    img1 = cv2.imread('test.file')
    feature = cd.describe(img1) 
    #多进程
    start,end,step= 0,0,100
    processes = rows/step + 1 if rows%step else 0
    pool = Pool(processes)
    operand = []
    for i in range(processes):
        end = (end + step) if (rows - end > step) else (rows + 1)
	operand.append([dataset[start:end],feature[:]])
        start = end
    result = []
    for item in pool.imap(comp,operand):
        result.extend(item)

    result = sorted(result)
    final = {}
    for i in range(processes):
        temp = {}
        temp['name'] = 'item'+str(i)
        temp['url'] = result[i][1][0]
        temp['width'] = result[i][1][1]
        temp['height'] = result[i][1][2]
        temp['time'] = result[i][1][3]
        temp['type'] = result[i][1][4]
        final[str(i)] = temp
    return final
示例#20
0
文件: search.py 项目: eglxiang/sql
from pyimagesearch.searcher import Searcher
import argparse
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--index", required=True,
	help="Path to where the computed index will be stored")
ap.add_argument("-q", "--query", required=True,
	help="Path to the query image")
ap.add_argument("-r", "--result-path", required=True,
	help="path to the result path")
args = vars(ap.parse_args())

# initialize the image descriptor
cd = ColorDescriptor((8,12,3))

# load the query image and describe it
query = cv2.imread(args["query"])
features = cd.describe(query)

# perform the search
searcher = Searcher(args["index"])
results = Searcher.search(features)

# display the query
cv2.imshow("Query",query)

# loop over the results
for (score, resultID) in results:
	# load the result image and display it
示例#21
0
import os
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument(
    "-d",
    "--dataset",
    required=True,
    help="Path to the directory that contains the images to be indexed")
ap.add_argument("-i",
                "--index",
                required=True,
                help="Path to where the computed index will be stored")
args = vars(ap.parse_args())

# initialize the color descriptor
cd = ColorDescriptor()

# open the output index file for writing
output = open(args["index"], "w")
count = 0
for imagePath in glob.glob(args["dataset"] + "/*.jpg"):
    # extract the image ID (i.e. the unique filename) from the image
    # path and load the image itself
    imageID = imagePath[imagePath.rfind("/") + 1:]
    image = cv2.imread(os.path.abspath(imagePath))
    # describe the image
    features = cd.describe(image)
    #ignore image if empty features
    if features == [] or features is None: continue
    features = features.ravel().tolist()
    # write the features to file
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument(
    "-d",
    "--dataset",
    required=True,
    help="Path to the directory that contains the images to be indexed")
ap.add_argument("-i",
                "--index",
                required=True,
                help="Path to where the computed index will be stored")
args = vars(ap.parse_args())

# initialize the color descriptor
cd = ColorDescriptor((4, 8, 6))

# open the output index file for writing
output = open(args["index"], "w")

# use glob to grab the image paths and loop over them
for imagePath in glob.glob(args["dataset"] + "/*.png"):
    # extract the image ID (i.e. the unique filename) from the image
    # path and load the image itself
    imageID = imagePath[imagePath.rfind("/") + 1:]
    print(imageID)
    image = cv2.imread(imagePath)

    # describe the image
    features = cd.describe(image)
示例#23
0
import urllib2
import requests
from requests.auth import HTTPBasicAuth
import json
import ast
import os
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required = True,
	help = "Path to the directory that contains the images to be indexed")
ap.add_argument("-i", "--index", required = True,
	help = "Path to where the computed index will be stored")
args = vars(ap.parse_args())

# initialize the color descriptor
cd = ColorDescriptor()

# open the output index file for writing
output = open(args["index"], "w")
count = 0
for imagePath in glob.glob(args["dataset"] + "/*.jpg"):
	# extract the image ID (i.e. the unique filename) from the image
	# path and load the image itself
	imageID = imagePath[imagePath.rfind("/") + 1:]
	image = cv2.imread(os.path.abspath(imagePath))
	# describe the image
	features = cd.describe(image)
	#ignore image if empty features
	if features ==[] or features is None: continue
	features = features.ravel().tolist()
	# write the features to file
示例#24
0
ft = tkFont.Font(family='Fixdsys', size=20, weight=tkFont.BOLD)

text = tk.Label(
    root,
    text=
    "This is a CBIR app, Please click the following image to search others",
    font=ft)
text.grid(row=0, column=0, columnspan=5, sticky='e')

status = tk.IntVar()

i = 1

# initialize the image descriptor
cd = ColorDescriptor((8, 12, 3))

# use tk_im as a global list to store described image and avoid garbage collection
tk_im = []

# use this to store button and destroy them
tk_button = []

# use this to count the number of button (to destroy them)
# button_num = 0


# imageOutput = tk.Button(root, width=w_box, height=h_box)
def call_back(imagepath):
    print(imagepath)
    # if status.get() == 1:
from pyimagesearch.colordescriptor import ColorDescriptor
import argparse
import glob
import cv2
import numpy
from sklearn import datasets, svm, metrics
# construct the argument parser and parse the arguments from the input
ap = argparse.ArgumentParser()
ap.add_argument(
    "-d",
    "--dataset",
    required=True,
    help="Path to the directory that contains the images to be indexed")
ap.add_argument("-i",
                "--index",
                required=True,
                help="Path to where the computed index will be stored")
args = vars(ap.parse_args())

cd = ColorDescriptor((8, 12, 3))  # initialize

output = open(args["index"], "w")

for imagePath in glob.glob(args["dataset"] + "/*.png"):
    imageID = imagePath[imagePath.rfind("/") + 1:]
    image = cv2.imread(imagePath)
    features = cd.describe(image)  # describe the image using describe function
    features = [str(f) for f in features]  # write the features to file
    output.write("%s,%s\n" % (imageID, ",".join(features)))
output.close()
示例#26
0
ap.add_argument(
    "-d",
    "--dataset",
    required=False,
    default='dataset\\dataset',
    help="Path to the directory that contains the images to be indexed")
ap.add_argument("-i",
                "--index",
                required=False,
                default='index.csv',
                help="Path to where the computed index will be stored")
args = vars(ap.parse_args())

# initialize the color descriptor
#cd = ColorDescriptor((8, 12, 3))
cd = ColorDescriptor((3, 3, 2))

# open the output index file for writing
output = open(args["index"], "w")

# use glob to grab the image paths and loop over them
for imagePath in glob.glob(args["dataset"] + "/*.jpg"):
    # extract the image ID (i.e. the unique filename) from the image
    # path and load the image itself
    imageID = imagePath[imagePath.rfind("/") + 1:]
    image = cv2.imread(imagePath)

    # describe the image
    features = cd.describe(image)

    # write the features to file
示例#27
0
def searchVideo(query):

    index = "index.csv"

    # initialize the image descriptor
    cd = ColorDescriptor((8, 12, 3))

    # load the query video and describe it
    temp = "/home/ankita/btp7/CBVR/temp"
    i = 0
    # print path_video
    cap = cv2.VideoCapture("uploadedFiles/" + query)
    if cap.isOpened():
        rval, frame = cap.read()
    else:
        rval = False
    # print rval
    # print query
    while rval:

        rval, frame = cap.read()
        if not rval: break
        name = str(i) + '.png'
        cv2.imwrite(os.path.join(temp, name), frame)
        i = i + 1
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cap.release()
    avg_int = []
    no_frames = i
    # print no_frames

    for img in glob.glob(temp + '/*png'):

        image = io.imread(img)

        im = rgb2gray(image)
        avg_int.append(np.mean(im))

        # print(np.mean(im))

    # for i in avg_int:
    #     print i

    diff = [avg_int[p + 1] - avg_int[p] for p in range(len(avg_int) - 1)]

    # y = diff
    # plt.plot(y)
    # plt.show()
    # print diff.index(min(diff))
    name1 = temp + '/' + str(diff.index(min(diff))) + '.png'

    # print diff.index(max(diff))
    name2 = temp + '/' + str(diff.index(max(diff))) + '.png'

    # image1 = cv2.imread(name1)
    # save_name = fold +"#"+ video.split('.')[0]+'_1.png'
    # cv2.imwrite(os.path.join(data ,save_name ), image1)
    # image2 = cv2.imread(name2)
    # save_name = fold +"#"+ video.split('.')[0]+'_2.png'
    # cv2.imwrite(os.path.join(data ,save_name ), image2)

    query_image = cv2.imread(name2)

    for the_file in os.listdir(temp):
        file_path = os.path.join(temp, the_file)
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
            #elif os.path.isdir(file_path): shutil.rmtree(file_path)
        except Exception as e:
            print(e)

    # query = cv2.imread(args["query"])
    features = cd.describe(query_image)

    # perform the search
    searcher = Searcher(index)
    results = searcher.search(features)

    result_images = []
    # display the query
    # cv2.imshow("Query", query_image)

    # loop over the results
    path_video = []
    # path_video.append("uploadedFiles/"+query)
    for (score, resultID) in results:
        # load the result image and display it
        result = cv2.imread("data/" + resultID)
        result_images.append(result)
        fold = resultID.split('#')[0]
        x = resultID.split('#')[1]

        name = x.rsplit('_', 1)[0]
        # print fold

        arr = os.listdir('data/' + fold)
        ext = arr[0].split('.')[1]

        video = 'data/' + fold + '/' + name + '.' + ext
        path_video.append(video)

        # print video
        # cv2.imshow("Result", result)
        # cv2.waitKey(0)
    return path_video
示例#28
0
    hash_md5 = hashlib.md5()
    with open(fname, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            hash_md5.update(chunk)
    return hash_md5.hexdigest()


# initialize mongodb client
client = MongoClient("127.0.0.1:5988")

# Content-based image retrieval database
db = client.CIBR
collection = db.ImageFeature
imgList = list(collection.find())

cd = ColorDescriptor(feature=Feature.HSV)
count = 0
start_time = time.time()
for imgItem in imgList:
    image = None
    if "ImageUrl" in imgItem:
        try:
            image = io.imread(imgItem["ImageUrl"])
        except:
            print("unable to fetch image:%s", imgItem["ImageUrl"])

    if image is None and "Path" in imgItem:
        try:
            image = cv2.imread("." + imgItem["Path"])
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        except:
示例#29
0
import cv2
import numpy as np

e1 = cv2.getTickCount()
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--index", required = True,
	help = "Path to where the computed index will be stored")
ap.add_argument("-q", "--query", required = True,
	help = "Path to the query image")
ap.add_argument("-r", "--result-path", required = True,
	help = "Path to the result path")
args = vars(ap.parse_args())

# initialize the image descriptor
cd = ColorDescriptor()

# load the query image and describe it
query = cv2.imread(args["query"])

features = cd.describe(query)


searcher = Searcher(args["index"])
results = searcher.search(features)

# display the query
cv2.imshow("Query", query)
e2 = cv2.getTickCount()
time = (e2-e1)/cv2.getTickFrequency()
示例#30
0
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True,
                help="Path to the directory that contains the images to be indexed")
ap.add_argument("-u", "--url", required=False,
                help="Path to the url directory that contains the images to be indexed")
args = vars(ap.parse_args())

# initialize mongodb client
client = MongoClient()

# Content-based image retrieval database
db = client.CIBR
collection = db.ImageFeature
# initialize the color descriptor
hsv_cd = ColorDescriptor((8, 12, 3), feature=Feature.HSV)
luv_cd = ColorDescriptor(feature=Feature.LUV)
top_n_classes = 5
clf = CNNClassifier(top_n_classes=top_n_classes)
start_time = time.time()
count = 0

all_imgs = []
for path, subdirs, files in os.walk(args["dataset"]):
    for name in files:
        all_imgs.append(os.path.join(path, name))

# use glob to grab the image paths and loop over them
for imagePath in all_imgs:
    # extract the image ID (i.e. the unique filename) from the image
    print(imagePath)
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i",
                "--index",
                required=True,
                help="Path to where the computed index will be stored")
ap.add_argument("-q", "--query", required=True, help="Path to the query image")
ap.add_argument("-r",
                "--result-path",
                required=True,
                help="Path to the result path")
args = vars(ap.parse_args())

# initialize the image descriptor
cd = ColorDescriptor((4, 8, 6))

# load the query image and describe it
query = cv2.imread(args["query"])
features = cd.describe(query)

# perform the search
searcher = Searcher(args["index"])
results = searcher.search(features)

# display the query
# cv2.imshow("Query", query)

# loop over the results
# for (score, resultID) in results:
# 	# load the result image and display it
示例#32
0
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i",
                "--index",
                required=True,
                help="Path to where the computed index will be stored")
ap.add_argument("-q", "--query", required=True, help="Path to the query image")
ap.add_argument("-r",
                "--result-path",
                required=True,
                help="Path to the result path")
args = vars(ap.parse_args())

# initialize the image descriptor
cd = ColorDescriptor((8, 12, 3))

# load the query image and describe it
query = cv2.imread(args["query"])
features = cd.describe(query)

# perform the search
searcher = Searcher(args["index"])
results = searcher.search(features)
# display the query
cv2.imshow("Query", query)
c = 0
r = []
# loop over the results
for (score, resultID) in results:
    # load the result image and display it
示例#33
0
# ====================================================================================================
# This file is reponsible for extracting features from the training images and write them to index.csv
# ====================================================================================================
from pyimagesearch.colordescriptor import ColorDescriptor
import glob
import cv2
from keras.datasets import cifar10

(X_train, y_train), (X_test, y_test) = cifar10.load_data()

cd = ColorDescriptor((8, 12, 3))

# Write feature info of each training image to index.csv
output = open("index.csv", "w")
for image in X_test:
    features = cd.describe(image)  # extract features from each image
    features = [str(f) for f in features]
    output.write(",".join(features) + "\n")
output.close()
示例#34
0
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument(
    "-d",
    "--dataset",
    required=True,
    help="Path to the directory that contains the images to be indexed")
ap.add_argument("-i",
                "--index",
                required=True,
                help="Path to where the computed index will be stored")
args = vars(ap.parse_args())

# initialize the color descriptor
cd = ColorDescriptor((8, 12, 3))

# open the output index file for writing
output = open(args["index"], "w")

# use glob to grab the image paths and loop over them
for imagePath in glob.glob(args["dataset"] + "/*.jpg"):
    # extract the image ID (i.e. the unique filename) from the image
    # path and load the image itself
    imageID = imagePath[imagePath.rfind("\\") + 1:]
    split = imageID.split("_")
    image = cv2.imread(imagePath)

    # describe the image
    features = cd.describe(image)
示例#35
0
    def createimagepage(self):

        status = tk.IntVar()
        global i
        i = 0

        # initialize the image descriptor
        global cd
        cd = ColorDescriptor((8, 12, 3))

        # use tk_im as a global list to store described image and avoid garbage collection
        global tk_im
        tk_im = []

        # use this to store button and destroy them
        global button_list
        button_list = []


        # if self.root.queryfolder == '':
        #     path = r"C:\Users\DYL18\Desktop\Graduation_design\vacation-image-search-engine\queries"
        # else:
        #     path = self.root.queryfolder
        #
        # if self.root.datafolder == '':
        #     datapath = r"C:\Users\DYL18\Desktop\Graduation_design\vacation-image-search-engine\dataset"
        # else:
        #     datapath = self.root.datafolder
        path = self.root.queryfolder

        self.textframe = tk.Frame(self.root, width=1380, height=200)
        self.textframe.grid(row=0, column=0, columnspan=10)
        ft = tkFont.Font(family='Fixdsys', size=20, weight=tkFont.BOLD)
        text = tk.Label(self.textframe,
                        text="This is a CBIR app, Please click the following image to search others\n"
                             "这是基于内容的图像检索系统,请点击下面的图片以查询内容相关联的其他图片", font=ft)
        text.grid(row=0, column=0, columnspan=5, sticky='e')
        self.textframe.grid_rowconfigure(0, weight=1)

        self.frame = tk.Frame(self.root, width=1580, height=400)
        self.frame.grid(row=1, column=0)
        self.canvas = tk.Canvas(self.frame, bg='red', width=1500, height=300, scrollregion=(0, 0, 2600, 1000))
        self.canvas.create_line(0, 0, 200, 100)

        global tk_image
        tk_image = []
        global tk_image_path
        tk_image_path = []

        globpath = []
        globpath = globpath + glob.glob(path + "/*.png")
        globpath = globpath + glob.glob(path + "/*.jpg")

        print("test if its right")
        print(globpath)
        # for imagepath in glob.glob(path + "/*.png"):
        for imagepath in globpath:
            pil_image = Image.open(imagepath)

            tk_image_path.append(imagepath)
            print(tk_image_path[i])

            # pil_image_resized = self.resize(self.root.w_box, self.root.h_box, pil_image)
            pil_image_resized = self.resize(w_box, h_box, pil_image)
            tk_image.append(ImageTk.PhotoImage(pil_image_resized))

            cb = (lambda p: lambda: self.call_back(p))(imagepath)
            button = tk.Button(self.frame, image=tk_image[i], width=w_box, height=h_box, command=cb)

            # global i
            i += 1
            print(i)
            self.canvas.create_window(150+250*(i-1), 100, window=button)
            # button.grid(row=1, column=i - 2, rowspan=6, sticky='w')

        hbar = tk.Scrollbar(self.frame, orient='horizontal')
        hbar.grid(row=1, column=0, sticky='we')
        hbar.config(command=self.canvas.xview)
        vbar = tk.Scrollbar(self.frame, orient="vertical")
        vbar.grid(row=0, column=1, sticky='ns')
        vbar.config(command=self.canvas.yview)

        self.canvas.config(width=1500, height=300)
        self.canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
        self.canvas.grid(row=0, column=0)

        # self.canvas.create_window(0, 0, window=self.frame)



        self.frame2 = tk.Frame(self.root, width=1580, height=400)
        self.frame2.grid(row=2, column=0)
        self.canvas2 = tk.Canvas(self.frame2, bg='green', width=1500, height=400, scrollregion=(0, 0, 2600, 1000))

        hbar2 = tk.Scrollbar(self.frame2, orient='horizontal')
        hbar2.grid(row=1, column=0, sticky='we')
        hbar2.config(command=self.canvas2.xview)
        vbar2 = tk.Scrollbar(self.frame2, orient="vertical")
        vbar2.grid(row=0, column=1, sticky='ns')
        vbar2.config(command=self.canvas2.yview)

        self.canvas2.config(width=1500, height=400)
        self.canvas2.config(xscrollcommand=hbar2.set, yscrollcommand=vbar2.set)
        self.canvas2.grid(row=0, column=0)
示例#36
0
    def search(self, fileName):

        query = cv.imread(fileName)

        #Added check for file exist
        if query is None:
            return []

        cd = ColorDescriptor((8, 12, 3))
        classifierFilePath = './pyimagesearch/VWClassifier.dat'
        fe = FeatureEvaluator(classifierFilePath)

        self.queryfeatures = cd.describe(query)
        self.vmfeatures = fe.predictFromImage(query, k=-1)
        self.trfeatures = TRS.TRSolver(fileName).result
        self.sffeatures = TFSFS.SemanticFeatureSolver(fileName).result

        colorSearcher = colorHistogramSearcher("index.csv")
        colorResults = colorSearcher.search(self.queryfeatures)
        color_max = max(score for (score, resultID) in colorResults)
        chResults = [(1 - t[0] * (1 / color_max), t[1]) for t in colorResults]

        # perform VW search
        vm_max = max(score for (resultID, score) in self.vmfeatures)
        vmResults = [(1 - t[1] * (1 / vm_max), t[0]) for t in self.vmfeatures]

        # perform TR search
        tr_max = max(score for (resultID, score) in self.trfeatures)
        trResults = [(t[1], t[0]) for t in self.trfeatures]

        # perform SF search
        sfResults = [(t[1], t[0]) for t in self.sffeatures]

        #TODO: join results (top 16)

        image_dict = {}
        sf_weight = 0.6
        vm_weight = 0.2
        ch_weight = 0.1
        tr_weight = 0.1
        for (score, resultID) in sfResults:
            image_dict[resultID] = score * sf_weight

        for (score, resultID) in vmResults:
            if resultID in image_dict:
                image_dict[resultID] = image_dict[resultID] + score * vm_weight
            else:
                image_dict[resultID] = score * vm_weight

        for (score, resultID) in chResults:
            if resultID in image_dict:
                image_dict[resultID] = image_dict[resultID] + score * ch_weight
            else:
                image_dict[resultID] = score * ch_weight

        for (score, resultID) in trResults:
            if resultID in image_dict:
                image_dict[resultID] = image_dict[resultID] + score * tr_weight
            else:
                image_dict[resultID] = score * tr_weight

        k = 50
        image_list = sorted(image_dict.items(),
                            key=operator.itemgetter(1),
                            reverse=True)[:k]
        result = [resultID for (resultID, score) in image_list]
        return result
示例#37
0
def callback(file):
	args={'result_path': 'dataset', 'index': 'index.csv', 'query': ''}
	args['query']=file
	# # initialize the image descriptor
	cd = ColorDescriptor((8, 12, 3))
    #
	# # load the query image and describe it
	query = cv2.imread(args["query"])
	features = cd.describe(query)
    #
	# # perform the search
	searcher = Searcher(args["index"])
	results = searcher.search(features)
    #
	return results
# display the query
#cv2.imshow("Query", query)
#loop over the results
# count=0
# print "Note: lower % of matching shows maximum similarity." 
# for (score, resultID) in results:
# 	# load the result image and display it
# 	i=0
# 	total=100
	
# 	perc=int(score)
# 	while(i<=perc):
# 		total-=5+i;
# 		i+=1
		
	
# 	print "Image "+resultID +" got "+str(total+5)+"% similarity and "+'%2.3f'%score+" score.."
# 	result = cv2.imread(args["result_path"] + "/" + resultID)
# 	cv2.imwrite("/home/venkat/Review/output/"+str(score)+" "+resultID, result)
# 	count+=1
	

# print "Got "+str(count)+" similer results...."

# root = Tk()
# root.geometry('{}x{}'.format(1330, 760))
# root.title("Content Based Image Retrieval")	
# label=Label(root,text = 'Resultant similer images..')
# label.bind()
# c=0
# r=0
# path='/home/venkat/CBIR/dataset/'
# output='/home/venkat/CBIR/'
# print "Note: lower % of matching shows maximum similarity." 
# for (score, resultID) in results:
# 	# load the result image and display it
# 	i=0
# 	total=100
	
# 	perc=int(score)
# 	while(i<=perc):
# 		total-=5;
# 		i+=1
		
	
# 	print "Image "+resultID +" got "+str(total)+"% similarity and "+'%2.3f'%score+" score.."
# 	path1 = Image.open("/home/venkat/Review/dataset/"+resultID)
# 	path1=path1.resize((250, 250),Image.ANTIALIAS)
# 	img1 = ImageTk.PhotoImage(path1)
# 	myvar=Label(root,image = img1)
# 	myvar.image = img1
# 	myvar.grid(row=r,column=c)
# 	c+=250
# 	if(c>1000):
# 		r+=250
# 		c=0
	
	
# root.mainloop()

# USAGE
# python search.py --index index.csv --query queries/103100.png --result-path dataset