When I run with less images, e.g 50-100 images, it work fine, but when I run with larger amount of images (200-700) it's throwing error. This is my code
from unittest import result
import cv2
import os
from glob import glob
import pickle
import numpy as np
import matplotlib as plt
import string
def get_train_and_test_img_features():
train_path = "/Users/Antonie/Project/Skripsi/myWork/Code/Source/DB/query/*/*.png"
#train_path = "/Users/Antonie/Project/Skripsi/myWork/Skripsi_Code/SIFTDocumentRetrieval/dataset3/test/*.png"
test_path = "/Users/Antonie/Project/Skripsi/myWork/Code/Source/DB/target/*/*.png"
def get_sift_features(_in_path,_debug_view = False):
img = cv2.imread(_in_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
sift = cv2.xfeatures2d.SIFT_create()
kp,desc = sift.detectAndCompute(gray, None)
img = cv2.drawKeypoints(gray, kp, img)
#cv2.imshow('sift_keypoints', img)
if _debug_view:
img = cv2.drawKeypoints(gray, kp, img)
cv2.imshow('sift_keypoints', img)
cv2.waitKey(0)
return kp,desc
def compare_features_flann(_kp1,_dsc1,_kp2,_dsc2,_thres=0):
FLANN_INDEX_KDTREE = 3
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(_dsc1, _dsc2, k=2)
matches_mask = [[0, 0,] for i in range(len(matches))]
good_points = []
for i, (m, n) in enumerate(matches):
if m.distance < 0.6 * n.distance:
#matches_mask[i] = [1, 0]
good_points.append(m)
number_keypoints = 0
if len(_kp1) <= len(_kp2):
number_keypoints = len(_kp1)
else:
number_keypoints = len(_kp2)
return good_points , len(good_points) / number_keypoints * 100
def compare_features_bf(_kp1,_dsc1,_kp2,_dsc2,_thres = 0):
bf = cv2.BFMatcher()
matches = bf.knnMatch(_dsc1, _dsc2, k=2)
good_points = []
for i, (m, n) in enumerate(matches):
if m.distance < 0.75 * n.distance:
#matches_mask[i] = [1, 0]
good_points.append(m)
number_keypoints = 0
if len(_kp1) <= len(_kp2):
number_keypoints = len(_kp1)
else:
number_keypoints = len(_kp2)
# print("KP 1: " + str(len(_kp1)))
# print("Kp 2: " + str(len(_kp2)))
# print("Match:", len(good_points))
# print("Matches: ", len(good_points) / number_keypoints * 100)
return good_points , len(good_points) / number_keypoints * 100
def create_query_database(_path):
img_db = {}
for file in glob(_path):
kp, desc = get_sift_features(file)
img_db[os.path.basename(file)] = {"keypoint": kp,
"descriptors": desc}
# # Database
# with open('queries.txt', 'wb') as file:
# file.write(pickle.dumps(img_db))
return img_db
def get_best_matches(_result_dict):
mean = np.mean([val for key,val in _result_dict.items()])
positive = {}
negative = {}
for key,val in _result_dict.items() :
res = (val - mean)
if res > mean:
positive[key] = val
else:
negative[key] = val
return positive
if __name__ == "__main__":
sift = cv2.xfeatures2d.SIFT_create()
#lokasi
target_path = "/Users/Antonie/Project/Skripsi/myWork/Code/Source/DB/target/*.*"
#query_path = "/Users/Antonie/Project/logo-det/Dataset/logo/img_logo/*/*.png"
#query_path = "/Users/Antonie/Project/Skripsi/myWork/Code/Source/DB/query/*/*.png"
query_path = "/Users/Antonie/Project/Skripsi/myWork/Code/dataset_img4/Process2/Process2/*.png"
query_db = create_query_database(query_path)
for files in glob(target_path, recursive=True):
results = {}
kb1, des1 = get_sift_features(files)
print(os.path.basename(files), "\n")
for keys, values in query_db.items():
kb2 = values["keypoint"]
des2 = values["descriptors"]
good, percentage = compare_features_flann(kb1, des1, kb2, des2)
results[keys] = percentage
#final_result = get_best_matches(results)
final_result = sorted(results.items(), key=lambda x: x[1], reverse=True)[:4]
sortdict = dict(final_result)
#print(sortdict)
out_list = []
for i in sortdict.keys():
out_list.append(i)
nama_file = out_list
print("ini daftar retrieve : ",out_list)
#print("ini hanya 1 : ", nama_file)
for i in range(0, len(nama_file)):
print(nama_file[i])
# document_path = "/Users/Antonie/Project/Skripsi/myWork/Code/dataset_img4/firstPage/"
# for root, dir, files in os.walk(query_path):
# if nama_file in files:
# print(os.path.join(root, nama_file))
# #img_path= os.path.join(document_path, str(a))
# img_path = document_path + str(out_list[1])
# #print(a)
# #bimg = cv2.imread(img_path)
# #b = cv2.resize(bimg, (450,600))
# #cv2.imshow("result", b)
# #cv2.waitKey(0)
#words = nama_file[0].split('_')
#print(words)
for j in nama_file:
wordss = j.split('_')
#wordss.append(j)
print(str(wordss[0]) + '.pdf')
#tugas next : split filename : PemkabBuleleng_1 (for logo)
#print(final_result)
#plt.figure()
#plt.imshow(final_result[1])
#plt.show()
print("-----------------")
Traceback (most recent call last): File
"/Users/Antonie/Project/Skripsi/myWork/Code/siftFindDescriptor.py",
line 127, in
good, percentage = compare_features_flann(kb1, des1, kb2, des2) File
"/Users/Antonie/Project/Skripsi/myWork/Code/siftFindDescriptor.py",
line 40, in compare_features_flann
matches = flann.knnMatch(_dsc1, dsc2, k=2) cv2.error: OpenCV(3.4.2)
/Users/travis/build/skvark/opencv-python/opencv/modules/flann/src/miniflann.cpp:315:
error: (-210:Unsupported format or combination of formats) in function
'buildIndex'
type=0
How can I fix it?
Related
I have a code that I learn from this link https://www.youtube.com/watch?v=nnH55-zD38I&t=1047s
but I got an error that said:
line 62, in <module>
cv2.putText(imgOriginal,classNames[id], (50,50),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),1)
IndexError: list index out of range
[ WARN:0] global C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-m8us58q4\opencv\modules\videoio\src\cap_msmf.cpp
(438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
Here is the code:
import cv2
import numpy as np
import os
path = '#2 Image descriptor\Assets\Query'
orb = cv2.ORB_create(nfeatures=1000)
#IMPORT IMAGES
images = []
classNames = []
myList = os.listdir(path)
print('Jumlah Classes = ', len(myList))
#print(myList)
for cl in myList:
imgCur = cv2.imread(f'{path}/{cl}', 0)
images.append(imgCur)
classNames.append(os.path.splitext(cl)[0])
print (classNames)
def findDes(images) :
desList = []
for img in images :
kp, des = orb.detectAndCompute(img, None)
desList.append(des)
return desList
def findID(img, desList, thres=15):
kp2, des2 = orb.detectAndCompute(img, None)
bf = cv2.BFMatcher()
matchList = []
finalVal = -1
try :
for des in desList:
matches = bf.knnMatch(des, des2, k=2)
good = []
for m, n in matches:
if m.distance < 0.7*n.distance:
good.append([m])
matchList.append(len(good))
except :
pass
#print(matchList)
if len(matchList) != 0:
if max(matchList) > thres:
finalVal = matchList.index(max(matchList))
return finalVal
desList = findDes(images)
print(len(desList))
cap = cv2.VideoCapture(0)
#cap = cv2.imread('#2 Image descriptor\Assets\Train\RE8.jpg')
while True:
success, img2 = cap.read()
imgOriginal = img2.copy()
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
id = findID(img2, desList)
if id != -1:
cv2.putText(imgOriginal,classNames[id], (50,50),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),1)
cv2.imshow('img2', imgOriginal)
if cv2.waitKey(1) == ord('q'):
break
cap.release() #to make sure disable the camera from the code
cv2.destroyAllWindows
I think the error at:
imgCur = cv2.imread(f'{path}/{cl}', 0)
The problem is at this part of your code:
if id != -1:
cv2.putText(imgOriginal,classNames[id], (50,50),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),1)
The error is basically telling you that you are trying to index id from the classNames list at an index that doesn't exist in the list. For example, if the classNames list is empty, doing classNames[0] will return the error. You can try debugging like so:
if id != -1:
if id < len(classNames):
cv2.putText(imgOriginal,classNames[id], (50,50),cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),1)
else:
print(classNames)
print(f"Error: Attempted to index {id} from list of length {len(classNames)}.")
This is the code:
import numpy as np
import cv2
import os
big_img_path = 'bloodredboy.jpg'
resolution = 0.05
small_img_path = 'Minecraft'
def LoadImages():
global small_img_path
imagelist = list(os.walk(small_img_path))[0][2]
images = {}
for im in imagelist:
img = cv2.imread(r'C:\Users\VVA\Desktop\\'+small_img_path+'\\'+im)
r = 0
g = 0
b = 0
count = 0
for y in img:
for x in y:
r += x[0]
g += x[1]
b += x[2]
count += 1
r /= count
b /= count
g /= count
images[r'C:\Users\VVA\Videos\Desktop\\'+small_img_path+'\\'+im] = [r, g, b]
return images
def change_resolution(img, resolution):
res = []
height = len(img)
width = len(img[0])
yy = 0
for y in range(0, height, round(1/resolution)):
yy += 1
xx = 0
res.append([])
for x in range(0, width, round(1/resolution)):
res[yy-1].append(img[y][x])
xx += 1
return np.array(res)
def ask_block(rgb):
global images
record_block = None
record_value = 99999999999999
for image in images.items():
r = abs(rgb[0] - image[1][0])
g = abs(rgb[1] - image[1][1])
b = abs(rgb[2] - image[1][2])
if r+g+b < record_value:
record_block = image[0]
record_value = r+g+b
return record_block
def make_img_out_of_blocks(img):
count = 0
total = len(img) * len(img[0])
rows = []
yy = 0
for y in img:
yy += 1
xx = 0
row = []
for x in y:
block = ask_block(x)
block = cv2.imread(block)
try:
row = np.concatenate((row, block), axis=1)
except:
row = block
xx += 1
count += 1
rows.append(row)
print(f'{round(count/total*100, 2)}% Completed')
for row in rows:
try:
res = np.concatenate((res, row), axis=0)
except:
res = row
return res
print('Loading Images...')
images = LoadImages()
img = cv2.imread(big_img_path)
print('Done')
if resolution != 1:
print('Changing resolution...')
img = change_resolution(img, resolution)
print('Done')
print('Transforming to Blocks...')
img = make_img_out_of_blocks(img)
print('Done')
cv2.imwrite('output.jpg', img)
cv2.imshow('Result', img)
cv2.waitKey(0)
and this is the error:
I tried to change the file path file name folder images
but no progress.
As you can see i am using numpy and cv2
Loading Images...
Traceback (most recent call last):
File "C:\Users\VVA\Desktop\Image_out_of_Minecraft_Blocks.py", line 94, in
images = LoadImages()
File "C:\Users\VVA\Desktop\Image_out_of_Minecraft_Blocks.py", line 20, in LoadImages
for y in img:
TypeError: 'NoneType' object is not iterable
When i run this code
import numpy as np
import cv2
from sklearn.datasets import fetch_mldata
from skimage.measure import label, regionprops
from sklearn.neighbors import KNeighborsClassifier
def train(data, target):
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(data, target)
return knn
def move(image, x, y):
img = np.zeros((28, 28))
img[:(28-x), :(28-y)] = image[x:, y:]
return img
def fill(image):
if np.shape(image)!=(28, 28):
img = np.zeros((28,28))
x = 28 - np.shape(image)[0]
y = 28 - np.shape(image)[1]
img[:-x,:-y] = image
return img
else:
return image
def my_rgb2gray(img_rgb):
img_gray = 0.5*img_rgb[:, :, 0] + 0*img_rgb[:, :, 1] + 0.5*img_rgb[:, :, 2]
img_gray = img_gray.astype('uint8')
return img_gray
def my_rgb2gray2(img_rgb):
frame = img_rgb
grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
ret, frame_bw = cv2.threshold(grey, 170, 255, 0)
frame_bw = cv2.morphologyEx(frame_bw, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)))
return frame_bw
def count_images(framecal):
regions = label(framecal)
labels = regionprops(regions)
images = []
for i in range(0, len(labels)):
if labels[i].centroid[0] < result[0] and labels[i].centroid[1] < result[1]:
images.append(labels[i].image)
count = 0
for img in images:
obrada = fill(np.array(img.astype('uint8')))
count += model.predict(obrada.reshape(1, -1))
return count
def check2(indices, i):
check = False
for el in indices:
if (el == i):
check = True
break
return check
def findPoints(lines):
Xmin = 1000
Ymin = 1000
Ymax = 0
Xmax = 0
for i in range(len(lines)):
for x1, y1, x2, y2 in lines[i]:
if x1 < Xmin:
Xmin = x1
Ymin = y1
if x2 > Xmax:
Ymax = y2
Xmax = x2
return Xmin, Ymin, Xmax, Ymax
def hough(frame, gray, min_line_len, max_line_gap):
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
cv2.imwrite('line.png', frame)
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 40, min_line_len, max_line_gap)
minx, miny, maxx, maxy = findPoints(lines)
cv2.line(frame, (minx, miny), (maxx, maxy), (233, 0, 0), 2)
return minx, miny, maxx, maxy
homepath = 'SoftVideoData/'
videopaths = ['video-0.avi',
'video-1.avi',
'video-2.avi',
'video-3.avi',
'video-4.avi',
'video-5.avi',
'video-6.avi',
'video-7.avi',
'video-8.avi',
'video-9.avi']
mnist = fetch_mldata('MNIST original')
data = mnist.data>0
data = data.astype('uint8')
target = mnist.target
fixed = np.empty_like(data)
for i in range(0, len(data)):
l = label(data[i].reshape(28, 28))
r = regionprops(l)
min_x = r[0].bbox[0]
min_y = r[0].bbox[1]
for j in range(1, len(r)):
if r[j].bbox[0] < min_x:
min_x = r[j].bbox[0]
if r[j].bbox[1] < min_y:
min_y = r[j].bbox[1]
img = move(data[i].reshape(28, 28), min_x, min_y)
fixed[i] = img.reshape(784, )
model = train(fixed, target)
for index in range(0,9):
total = 0
video = cv2.VideoCapture(homepath + videopaths[index])
flag, frame = video.read()
bw = my_rgb2gray(frame)
result = hough(frame, bw, 10, 50)
while 1:
flag1, frame1 = video.read()
last_count = total
if flag1 is True:
bwframe = my_rgb2gray2(frame1)
curr_count = count_images(bwframe)
if curr_count <= last_count:
last_count = curr_count
else:
total += curr_count - last_count
last_count = curr_count
print total
k = cv2.waitKey(15) & 0xff
if k == 27:
break
else:
break
with open('out.txt', 'a') as file:
file.write(homepath + videopaths[index] + '\t' + str(total))
i get this error:
Traceback (most recent call last):
File "C:\Users\Joe\Desktop\SOFT-master7o\SoftProject.py", line 147, in <module>
bw = my_rgb2gray(frame)
File "C:\Users\Joe\Desktop\SOFT-master7o\SoftProject.py", line 35, in my_rgb2gray
img_gray = 0.5*img_rgb[:, :, 0] + 0*img_rgb[:, :, 1] + 0.5*img_rgb[:, :, 2]
TypeError: 'NoneType' object has no attribute '__getitem__'
What's wrong? Thanks
When you call a name with the square brackets, Python calla 'getitem' under the hood.
So it means that img_rgb is not what you expect it to be. Instead of a numpy array it is None.
Check the portion of code where img_rgb is assigned to.
Answer to comment:
Check your inputs before you do operations on them.
I would use the VS inbuild debugger and set a breakpoint on this line:
bw = my_rgb2gray(frame)
and inspect each frame if it is None before entering the function.
How to handle it if its None? Depends - either skip that video-frame or, if all are None, something is amiss and you need to check why flag, frame = video.read() produces a frame that is None. Sometimes the documentation will help you out:
https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture
https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-read
I have trained cnn model and saved parameters in five files,but when I use these params to test photos ,I meet a question like this:enter image description here
the code of load_data is:
def load_data(pag_name):``
k = 0
for filename in os.listdir(pag_name):
if (filename != '.DS_Store'):
k = k + 1
num = k
# test_per = k*4
print k
i = 0
j = 0
label = 0
train_set = numpy.empty((num, 1, 56, 56))
while (j < 1):
for filename in os.listdir(pag_name):
if (filename != '.DS_Store'):
filename = pag_name+ '/' + filename
image = Image.open(filename)
#print image.size
#print image
img_ndarray = numpy.asarray(image, dtype='float64') / 256
img_ndarray = numpy.asarray([img_ndarray])
# train_set[i] = numpy.ndarray.flatten(img_ndarray)
train_set[i] = img_ndarray
#print train_set.shape
# print filename1
# print 'label:', label
# print 'i:',i
i = i + 1
j = j + 1
def shared_dataset(data_x, borrow=True):
shared_x = theano.shared(numpy.asarray(data_x,
dtype=theano.config.floatX),
borrow=borrow)
return shared_x
train_set = shared_dataset(train_set)
print train_set.get_value(borrow=True).shape
return train_set
and the code of use_CNN is :
def use_CNN(pag_name,nkerns=[20,40,60]):
data = load_data(pag_name)
data_num = data.get_value(borrow=True).shape[0]
layer0_params,layer01_params,layer1_params,layer2_params,layer3_params = load_params()
x = T.matrix('x')
layer0_input = x.reshape((data_num,1,56,56))
layer0 = LeNetConvPoolLayer(
input=layer0_input,
params_W = layer0_params[0],
params_b = layer0_params[1],
image_shape=(data_num, 1, 56, 56),
filter_shape=(nkerns[0], 1, 5,5),
poolsize=(2, 2)`
)
I haven't meet this problem ,and I don't know where and how I change my code.
the result of this error is the params are not 4D, the params I load is 3D, like my W and b is (20,1,5,5),but I load (1,5,5),so I meet this problem.
I am trying to compare one image with all images of another file , get the difference percentage and print file name of the least difference percentage .... if i try to append the output differences to a list ... i get an error saying " float values cannot be iterated".... this is what i have done so far ....
from itertools import izip
import os
import numpy as np
import cv2
from matplotlib import pyplot as plt
from PIL import Image
import math
res = 0
def take_and_save_picture(im_save):
'''Take a picture and save it
Args:
im_save: filepath where the image should be stored
'''
camera_port = 0
ramp_frames = 30
cap = cv2.VideoCapture(camera_port)
def get_image():
retval, im = cap.read()
return im
for i in xrange(ramp_frames):
temp = get_image()
print("Taking image...")
# Take the actual image we want to keep
camera_capture = get_image()
#im_save_tmp = im_save + '.jpg'
im_save_tmp = im_save
# A nice feature of the imwrite method is that it will automatically choose the
# correct format based on the file extension you provide. Convenient!
cv2.imwrite(im_save_tmp, camera_capture)
# You'll want to release the camera, otherwise you won't be able to create a new
# capture object until your script exits
# del(cap)
img1 = cv2.imread(im_save_tmp, 0)
edges = cv2.Canny(img1, 100, 200)
cv2.imwrite(im_save, edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
def re(path1,path2):
#path1 = raw_input("Enter the path1:")
#path2 = raw_input("Enter the path2:")
i2= Image.open(path2)
listing = os.listdir(path1)
for file in listing:
i1 = Image.open(path1 + file)
assert i1.mode == i2.mode, "Different kinds of images."
assert i1.size == i2.size, "Different sizes."
pairs = izip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
# for gray-scale jpegs
dif = sum(abs(p1-p2) for p1,p2 in pairs)
else:
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = i1.size[0] * i1.size[1] * 3
res = (dif / 255.0 * 100) / ncomponents
print "Difference (percentage):", res
def main():
capture_img = "/Users/Me/Documents/python programs/New/pro.png"
#img_to_compare = "/Users/Me/Documents/python programs/compare/img2.jpg"
take_and_save_picture(capture_img)
path1 = "/Users/Me/Documents/python programs/New/numbers1/"
path2 = "/Users/Me/Documents/python programs/New/pro.png"
re(path1,path2)
if __name__ == '__main__':
main()
the output is the difference
Difference (percentage): 2.52484809028
Difference (percentage): 2.64822048611
Difference (percentage): 2.64822048611
Difference (percentage): 3.55436197917
the values that i get in "res" have to be stored in a list and the minimum value should be found and printed.... please give me some code ... totally new to python ... thank you ...
You're code must be like this:
#######
list_dif = []
def re(path1,path2):
#path1 = raw_input("Enter the path1:")
#path2 = raw_input("Enter the path2:")
i2= Image.open(path2)
listing = os.listdir(path1)
for file in listing:
i1 = Image.open(path1 + file)
assert i1.mode == i2.mode, "Different kinds of images."
assert i1.size == i2.size, "Different sizes."
pairs = izip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
# for gray-scale jpegs
dif = sum(abs(p1-p2) for p1,p2 in pairs)
else:
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = i1.size[0] * i1.size[1] * 3
#######
for n in range(ncomponents):
res = (dif / 255.0 * 100) / (ncomponents + 1)
list_dif.append(res)
print "Difference (percentage):", list_dif
Something like this?
def re(path1,path2):
#path1 = raw_input("Enter the path1:")
#path2 = raw_input("Enter the path2:")
i2= Image.open(path2)
listing = os.listdir(path1)
res = []
for file in listing:
i1 = Image.open(path1 + file)
assert i1.mode == i2.mode, "Different kinds of images."
assert i1.size == i2.size, "Different sizes."
pairs = izip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
# for gray-scale jpegs
dif = sum(abs(p1-p2) for p1,p2 in pairs)
else:
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = i1.size[0] * i1.size[1] * 3
res.append((dif / 255.0 * 100) / ncomponents)
print "Difference (percentage):", res
minimum = min(res) # Find minimum value in res
print(minimum)