图片检索系统中Search.py 和 index.py实现读取中文文件命名文件的源代码

Search.py下的相应局部代码修改为:
cd = ColorDescriptor.ColorDescriptor((8,12,3))


# load the query image and describe it


temp_query=args["query"]


#编码使其可以读中文命名的文件,imread必须要为'gbk'码
temp_query=temp_query.encode('gbk')
#解码:temp_query.decode()
query = cv2.imread(temp_query.decode())

features = cd.describe(query)


#coding:utf-8
# -*- coding: utf-8 -*-
"CBIR(Content-Base Image Retrieval)--Search"
import argparse

import cv2

import ColorDescriptor
import Searcher

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 query image")
ap.add_argument("-r", "--result_path", required = True, help="Path to the result Path")
args = vars(ap.parse_args())
cd = ColorDescriptor.ColorDescriptor((8,12,3))

# load the query image and describe it

temp_query=args["query"]

#编码使其可以读中文命名的文件,imread必须要为'gbk'码
temp_query=temp_query.encode('gbk')
#解码:temp_query.decode()
query = cv2.imread(temp_query.decode())
features = cd.describe(query)

# perform the search
searcher = 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
    print(args["index"]+"/"+resultID)    
    # result = cv2.imread(args["result_path"]+"/"+resultID)  
    # cv2.imshow("Result",result)
    # cv2.waitKey(0)



-----------------------
index.py下的相应局部代码修改为:


    imageID = imagePath[imagePath.rfind("\\")+1:]


    imagePath=imagePath.encode('gbk')
    image = cv2.imread(imagePath.decode())


即可实现读取中文命名的文件名。

# -*- coding: UTF-8 -*-
"CBIR(Content-Base Image Retrieval)--Extract Features and Indexing"

import ColorDescriptor
import argparse
import glob
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True, help="Path to the directory that cntains 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.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 from the image

    imageID = imagePath[imagePath.rfind("\\")+1:]

    imagePath=imagePath.encode('gbk')
    image = cv2.imread(imagePath.decode())

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

    # write feature to file
    features = [str(f) for f in features]
    output.write("%s,%s\n" %(imageID,",".join(features)))
# close index file
output.close()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值