Python 执行错误 SyntaxError : invalid syntax

各位大大

近期在执行以下程式码出现->SyntaxError: invalid syntax

小弟近日利用网路资源还找不出原因

大概能理解是语法上的问题,不确定括弧应存在的位置

附上程式码,是否有人能解救T^T

感激!!

http://img2.58codes.com/2024/20126820ord8xiYtDX.png

import cv2
import numpy as np
import scipy
from scipy.misc import imread
import _pickle as cPickle
import random
import os
import matplotlib.pyplot as plt

Feature extractor

def extract_features(image_path, vector_size=32):
image = imread(image_path, mode="RGB")
try:
# Using KAZE, cause SIFT, ORB and other was moved to additional module
# which is adding addtional pain during install
alg = cv2.KAZE_create()
# Dinding image keypoints
kps = alg.detect(image)
# Getting first 32 of them.
# Number of keypoints is varies depend on image size and color pallet
# Sorting them based on keypoint response value(bigger is better)
kps = sorted(kps, key=lambda x: -x.response)[:vector_size]
# computing descriptors vector
kps, dsc = alg.compute(image, kps)
# Flatten all of them in one big vector - our feature vector
dsc = dsc.flatten()
# Making descriptor of same size
# Descriptor vector size is 64
needed_size = (vector_size * 64)
if dsc.size < needed_size:
# if we have less the 32 descriptors then just adding zeros at the
# end of our feature vector
dsc = np.concatenate([dsc, np.zeros(needed_size - dsc.size)])
except cv2.error as e:
print ('Error: '), e
return None

return dsc

def batch_extractor(images_path, pickled_db_path="features.pck"):
files = [os.path.join(images_path, p) for p in sorted(os.listdir(images_path))]

result = {}for f in files:    print ('Extracting features from image %s') % f    name = f.split('/')[-1].lower()    result[name] = extract_features(f)# saving all our feature vectors in pickled filewith open(pickled_db_path, 'w') as fp:    cPickle.dump(result, fp)            

class Matcher(object):

def __init__(self, pickled_db_path="features.pck"):    with open(pickled_db_path) as fp:        self.data = cPickle.load(fp)    self.names = []    self.matrix = []    for k, v in self.data.iteritems():        self.names.append(k)        self.matrix.append(v)    self.matrix = np.array(self.matrix)    self.names = np.array(self.names)def cos_cdist(self, vector):    # getting cosine distance between search image and images database    v = vector.reshape(1, -1)    return scipy.spatial.distance.cdist(self.matrix, v, 'cosine').reshape(-1)def match(self, image_path, topn=5):    features = extract_features(image_path)    img_distances = self.cos_cdist(features)    # getting top 5 records    nearest_ids = np.argsort(img_distances)[:topn].tolist()    nearest_img_paths = self.names[nearest_ids].tolist()    return nearest_img_paths, img_distances[nearest_ids].tolist()def show_img(path):    img = imread(path, mode="RGB")plt.imshow(img)plt.show()

def run():
images_path = 'resources/images/'
files = [os.path.join(images_path, p) for p in sorted(os.listdir(images_path))]
# getting 3 random images
sample = random.sample(files, 3)

batch_extractor(images_path)ma = Matcher('features.pck')for s in sample:    print ('Query image ==========================================')    show_img(s)    names, match = ma.match(s, topn=3)    print ('Result images ========================================')    for i in range(3):        print 'Match %s' % (1-match[i])        show_img(os.path.join(images_path, names[i]))

run()


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章