def solve_captcha(image_file, model_data): (model, graph, lb) = model_data #letter_images = preprocess_image(image_file) letter_images = segment_image(image_file) if not letter_images: return "" # Create an output image and a list to hold our predicted letters predictions = [] # loop over the letters for letter_image in letter_images : # Re-size the letter image to 20x20 pixels to match training data # letter_image = resize_to_fit(letter_image, 20, 20) letter_image = resize_to_fit(letter_image, 28, 28) # Turn the single image into a 4d list of images to make Keras happy letter_image = np.expand_dims(letter_image, axis=2) letter_image = np.expand_dims(letter_image, axis=0) with graph.as_default(): # Ask the neural network to make a prediction prediction = model.predict(letter_image) # Convert the one-hot-encoded prediction back to a normal letter letter = lb.inverse_transform(prediction)[0] predictions.append(letter) # Print the captcha's text captcha_text = "".join(predictions) return captcha_text
def generateSubsquares(path): img, segments = segment_image(path) yuv = retrieveYUV(img) n_segments = segments.max() + 1 # code.InteractiveConsole(locals=locals()).interact() # Compute the centroids/average U and V of each of the superpixels point_count = np.zeros(n_segments) centroids = np.zeros((n_segments, 2)) U = np.zeros(n_segments) V = np.zeros(n_segments) for (i, j), value in np.ndenumerate(segments): point_count[value] += 1 centroids[value][0] += i centroids[value][1] += j U[value] += yuv[i][j][1] V[value] += yuv[i][j][2] for k in range(n_segments): centroids[k] /= point_count[k] U[k] /= point_count[k] V[k] /= point_count[k] # Generate the array of squares subsquares = np.zeros((n_segments, SQUARE_SIZE * SQUARE_SIZE)) for k in range(n_segments): # Check that the square lies completely within the image top = max(int(centroids[k][0]), 0) if top + SQUARE_SIZE >= img.shape[0]: top = img.shape[0] - 1 - SQUARE_SIZE left = max(int(centroids[k][1]), 0) if left + SQUARE_SIZE >= img.shape[1]: left = img.shape[1] - 1 - SQUARE_SIZE for i in range(0, SQUARE_SIZE): for j in range(0, SQUARE_SIZE): subsquares[k][i * SQUARE_SIZE + j] = yuv[i + top][j + left][0] subsquares[k] = np.fft.fft2(subsquares[k].reshape(SQUARE_SIZE, SQUARE_SIZE)).reshape(SQUARE_SIZE * SQUARE_SIZE) return subsquares, U, V
def generateSubsquares(path): img, segments = segment_image(path) yuv = retrieveYUV(img) n_segments = segments.max() + 1 # code.InteractiveConsole(locals=locals()).interact() # Compute the centroids/average U and V of each of the superpixels point_count = np.zeros(n_segments) centroids = np.zeros((n_segments, 2)) U = np.zeros(n_segments) V = np.zeros(n_segments) for (i,j), value in np.ndenumerate(segments): point_count[value] += 1 centroids[value][0] += i centroids[value][1] += j U[value] += yuv[i][j][1] V[value] += yuv[i][j][2] for k in range(n_segments): centroids[k] /= point_count[k] U[k] /= point_count[k] V[k] /= point_count[k] # Generate the array of squares subsquares = np.zeros((n_segments, SQUARE_SIZE * SQUARE_SIZE)) for k in range(n_segments): # Check that the square lies completely within the image top = max(int(centroids[k][0]), 0) if top + SQUARE_SIZE >= img.shape[0]: top = img.shape[0] - 1 - SQUARE_SIZE left = max(int(centroids[k][1]), 0) if left + SQUARE_SIZE >= img.shape[1]: left = img.shape[1] - 1 - SQUARE_SIZE for i in range(0, SQUARE_SIZE): for j in range(0, SQUARE_SIZE): subsquares[k][i*SQUARE_SIZE + j] = yuv[i + top][j + left][0] subsquares[k] = np.fft.fft2(subsquares[k].reshape(SQUARE_SIZE, SQUARE_SIZE)).reshape(SQUARE_SIZE * SQUARE_SIZE) return subsquares, U, V
def predict_image(u_svr, v_svr, path, verbose, output_file = None): img, segments = segment_image(path) yuv = retrieveYUV(img) # Use first component of yuv to obtain black and white n_segments = segments.max() + 1 # Construct the centroids of the image point_count = np.zeros(n_segments) centroids = np.zeros((n_segments, 2)) luminance = np.zeros(n_segments) for (i,j), value in np.ndenumerate(segments): point_count[value] += 1 centroids[value][0] += i centroids[value][1] += j luminance[value] += yuv[i][j][0] for k in range(n_segments): centroids[k] /= point_count[k] luminance[k] /= point_count[k] # Generate the subsquares subsquares = np.zeros((n_segments, SQUARE_SIZE * SQUARE_SIZE)) for k in range(n_segments): # Check that the square lies completely within the image top = max(int(centroids[k][0]), 0) if top + SQUARE_SIZE >= img.shape[0]: top = img.shape[0] - 1 - SQUARE_SIZE left = max(int(centroids[k][1]), 0) if left + SQUARE_SIZE >= img.shape[1]: left = img.shape[1] - 1 - SQUARE_SIZE for i in range(0, SQUARE_SIZE): for j in range(0, SQUARE_SIZE): subsquares[k][i*SQUARE_SIZE + j] = yuv[i + top][j + left][0] subsquares[k] = np.fft.fft2(subsquares[k].reshape(SQUARE_SIZE, SQUARE_SIZE)).reshape(SQUARE_SIZE * SQUARE_SIZE) # Predict using SVR predicted_u = np.zeros(n_segments) predicted_v = np.zeros(n_segments) for k in range(n_segments): predicted_u[k] = clampU(u_svr.predict(subsquares[k])*2) predicted_v[k] = clampU(v_svr.predict(subsquares[k])*2) # Apply MRF to smooth out colorings predicted_u, predicted_v = apply_mrf(predicted_u, predicted_v, segments, n_segments, img, subsquares) # Reconstruct images for (i,j), value in np.ndenumerate(segments): yuv[i][j][1] = predicted_u[value] yuv[i][j][2] = predicted_v[value] rgb = retrieveRGB(yuv) # Compute the norm error. Note that it will be wildly incorrect if the img is b/w. error = 1000 * np.linalg.norm(rgb - img) / (img.shape[0] * img.shape[1]) if verbose: print 'Norm error:', error # Draw the actual figure fig = plt.figure(frameon=False) ax = plt.Axes(fig, [0., 0., 1., 1.]) ax.set_axis_off() fig.add_axes(ax) ax.imshow(rgb) if output_file: imsave(output_file, rgb) plt.show() return error