def get_images(url='../static/img', file_extension='.jpg'): with open(f"{url}/README.txt") as f: descr = f.read() filenames = [ f"{url}/{filename}" for filename in sorted(os.listdir(url)) if filename.endswith(file_extension) ] images = [imread(filename) for filename in filenames] return Bunch(images=images, filenames=filenames, DESC=descr)
def read_file(filename) -> np.array: image = np.array(imread(filename)) assert image.shape == (26, 40, 3) image = image[:, :, 0] integral_img = integral_image(image) assert integral_img.shape == (26, 40) images = [image] # change images to get more samples flipped_image = np.fliplr(image) images.append(integral_image(flipped_image)) flipped_image_tr = np.flipud(image) images.append(integral_image(flipped_image_tr)) return images
def read_file(fname): image = imread(fname) assert image.shape == (26, 40, 3) return image[:, :, 0]
images_and_predictions = list(zip(digits.images[n_samples // 2:], predictedY)) for index, [image, prediction] in enumerate(images_and_predictions[:5]): plt.subplot(2, 5, index + 6) plt.axis('on') plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest') plt.title('Prediction: %i' % prediction) print("Original Values: ", digits.target[n_samples // 2:(n_samples // 2) + 5]) plt.show() #Install Pillow library #from PIL #from scipy.misc import imread , imresize, bytescale from sklearn.externals._pilutil import imresize, imread, bytescale img = imread("three.jpeg") img = imresize(img, (8, 8)) classifier = svm.SVC(gamma=0.001) classifier.fit(imageData[:], digits.target[:]) img = img.astype(digits.images.dtype) img = bytescale(img, high=16.0, low=0) print("img.shape : ", img.shape) print("\n", img) x_testData = [] for row in img: for col in row: x_testData.append(sum(col) / 3.0)
for index, [image, prediction] in enumerate(images_and_predictions[:5]): plt.subplot( 2, 5, index + 6 ) # dividing plotting space into 2 rows and 5 column, and plotting graph in 2nd row plt.axis('on') # ticks plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest') plt.title('Prediction : %i' % prediction) # original target values of images plotted in graph print("Original Values: ", digits.target[n_samples // 2:(n_samples // 2) + 5]) # plt.show() # from scipy.misc import imread, imresize, bytescale # from matplotlib.pyplot import imread img = imread("FourC.jpeg") # resizing images to required dimension img = imresize(img, (8, 8)) classifier = svm.SVC(gamma=0.001) classifier.fit(imageData[:], digits.target[:]) # making type of images same as earlier(data set of sklearn) img = img.astype(digits.images.dtype) img = bytescale(img, high=16.0, low=0) print("img.shape : ", img.shape) print("\n", img) x_testData = []
img_dim = (350, 350) # show original image orig_img = cv2.imread("image1.jpg") cv2.imshow("Original image", orig_img) # Convert to HSV hsv_img = cv2.cvtColor(orig_img, cv2.COLOR_BGR2HSV) cv2.imshow("HSV image", hsv_img) cv2.waitKey(0) cv2.destroyAllWindows() # Gray Scale image gray_img = cv2.cvtColor(orig_img, cv2.COLOR_RGB2GRAY) cv2.imwrite("gray.jpg", gray_img) # Static Threshold images th_gray = imread("gray.jpg", 0) ret, th1 = cv2.threshold(th_gray, img_dim[0], img_dim[1], cv2.THRESH_BINARY) ret, th2 = cv2.threshold(th_gray, img_dim[0], img_dim[1], cv2.THRESH_BINARY_INV) ret, th3 = cv2.threshold(th_gray, img_dim[0], img_dim[1], cv2.THRESH_TRUNC) ret, th4 = cv2.threshold(th_gray, img_dim[0], img_dim[1], cv2.THRESH_TOZERO) ret, th5 = cv2.threshold(th_gray, img_dim[0], img_dim[1], cv2.THRESH_TOZERO_INV) th_names = [ "original image", "Binary", "Binary invert", "Truncate", "ToZero", "ToZero Invert" ] img_ths = [th_gray, th1, th2, th3, th4, th5] labels = [1, 2, 3, 4, 5, 6] for i in range(6): plt.pyplot.subplot(2, 3, i + 1),