threash = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)[1] cnts = cv2.findContours(threash.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:4] cnts = contours.sort_contours(cnts)[0] output = cv2.merge([gray] * 3) predictions = [] for c in cnts: x, y, w, h = cv2.boundingRect(c) roi = gray[y - 5:y + h + 5, x - 5:x + w + 5] roi = preprocess(roi, 28, 28) roi = np.expand_dims( img_to_array(roi), axis=0 ) / 255.0 # output (batch_num, width, height, channels), add dim batch_num pred = model.predict(roi).argmax( axis=1)[0] + 1 #axis =0 row =1 columns predictions.append(str(pred)) cv2.rectangle(output, (x - 2, y - 2), (x + w + 4, y + h + 4), (0, 255, 0), 1) cv2.putText(output, str(pred), (x - 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 255, 0), 2) print("[INFO] captcha: {}".format("".join(predictions))) cv2.imshow("output", output) cv2.waitKey()
'--dataset', required=True, help='Path to input dataset.') argument_parser.add_argument('-m', '--model', required=True, help='Path to output model.') arguments = vars(argument_parser.parse_args()) data = list() labels = list() for image_path in paths.list_images(arguments['dataset']): image = cv2.imread(image_path) image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) image = preprocess(image, 28, 28) image = img_to_array(image) data.append(image) label = image_path.split(os.path.sep)[-2] labels.append(label) data = np.array(data, dtype='float') / 255.0 labels = np.array(labels) X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=.25, random_state=42) label_binarizer = LabelBinarizer().fit(y_train)