def bonne_taille(filename): repo = "separations/"+filename+"/" liste_images = os.listdir(repo) a_conserver = [] for img in liste_images : a_conserver, repo = crop.main(repo+img, repo, a_conserver) for i in os.listdir(repo): if i not in a_conserver: try : os.remove(repo+"/"+i) except : ... else : resize.main(repo + i) bw.binarize(repo + i)
def trainingSetFromImage(trainImage, target): grayImage = binarize.toGrayScale(trainImage) binarizedImage = binarize.binarize(grayImage) tmpBinImage = copy.deepcopy(binarizedImage) height, width = binarizedImage.shape imageRegionSet = segmentation.segmentation(binarizedImage) i = 1 font = cv2.FONT_HERSHEY_SIMPLEX for imageRegion in imageRegionSet: dim = imageRegion[2] (x, y, w, h) = dim cv2.rectangle(tmpBinImage, (x, height - y - h), (x + w, height - y), (255, 255, 255), 2) cv2.putText(tmpBinImage, str(i), (x, height - y - h - 20), font, 0.4, (255, 255, 255), 1, cv2.LINE_AA) i = i + 1 cv2.imshow("Training Image", tmpBinImage) k = cv2.waitKey(0) cv2.destroyAllWindows() print 'Number of Segments : ' + str(len(imageRegionSet)) while True: print "(i)Include Training Set (r)Retry (q)Quit" option = str(raw_input("Option : ")) if option == 'q': break elif option == 'i': includeTrainingSet(imageRegionSet, target) break elif option == 'r': break return option
def upload_multipart(): if 'uploadFile' not in request.files: make_response(jsonify({'result': 'uploadFile is required.'})) file = request.files['uploadFile'] filename = file.filename if '' == filename: make_response(jsonify({'result': 'filename must not empty.'})) filename = werkzeug.utils.secure_filename(filename) file_path = os.path.join(UPLOAD_DIR, filename) file.save(file_path) img_rows, img_cols = 400, 400 img_channels = 1 img = cv2.imread(file_path, cv2.IMREAD_COLOR) os.remove(file_path) binary = cv2.resize(binarize(img), (img_rows, img_cols)) img2 = np.zeros((img_rows, img_cols, img_channels)) img2[:, :, 0] = binary img2 = np.expand_dims(img2, axis=0) with graph.as_default(): pred = model.predict(img2) pred = pred.flatten() # predicted_class_indices = np.argmax(pred) labels = ['FBMessanger', 'Instagram', 'Invalid', 'LINE', 'Twitter'] pred_per = list(map(lambda x: x*100, pred)) return make_response(jsonify(dict(zip(labels, pred_per))))
def test(imageFileName, image=None, flag=0): if flag == 0: image = getImage(imageFileName) grayImage = binarize.toGrayScale(image) binarizedImage = binarize.binarize(grayImage) cv2.imshow('image', binarizedImage) regions = [] res, contours, hierarchy = cv2.findContours(binarizedImage, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) for contour in contours: [x, y, w, h] = cv2.boundingRect(contour) if h < 30 and w < 30: continue else: regions.append((binarizedImage[y:y + h, x:x + w], (x, y, w, h))) classifier = learning.getClassifier(svm_nn) for region in regions: testData = learning.getResizedImage(region[0]) (x, y, w, h) = region[1] result = classifier.predict([testData]) cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 0), 2) cv2.putText(image, str(result[0]), (x, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 0), 1, cv2.LINE_AA) print result cv2.imshow('test', image) cv2.waitKey(0) cv2.destroyAllWindows()
def test_booleans(self): self.assertEqual(b'\x00', binarize(False, endian=Endian.little, width=1)) self.assertEqual(b'\x01', binarize(True, endian=Endian.little, width=1)) self.assertEqual(b'\x00', binarize(False, endian=Endian.big, width=1)) self.assertEqual(b'\x01', binarize(True, endian=Endian.big, width=1)) self.assertEqual(b'\x00\x00', binarize(False, endian=Endian.little, width=2)) self.assertEqual(b'\x01\x00', binarize(True, endian=Endian.little, width=2)) self.assertEqual(b'\x00\x00', binarize(False, endian=Endian.big, width=2)) self.assertEqual(b'\x00\x01', binarize(True, endian=Endian.big, width=2))
def test(models_dir, image_path): im = cv2.imread(image_path, cv2.IMREAD_UNCHANGED) bw = binarize.binarize(im, algorithm=binarize.ntirogiannis2014) AH = algorithm.dominant_char_height(bw) print('AH =', AH) possible_AHs = np.array([int(d) for d in os.listdir(models_dir) if d.isdigit()]) size = possible_AHs[np.abs(possible_AHs - AH).argmin()] model_dir = os.path.join(models_dir, str(size)) P_l_T, P_h_T, pca, A_l_T, Q_T, A_h_T = load_model(model_dir)
def open_pdf(link, cnum): """ This module opens the link in selenium Takes a screenshot and saves it Crops the captcha part Binarizes the captcha SOlves the captcha to get the text Fills in the text and clicks submit If captcha is correct the PDF opens for preview. If captcha is wrong it tries again until the pdf opens. """ ndir = getCurrentDirectory(cnum) if not os.path.exists(ndir): os.makedirs(ndir) driver = captcha_download.download_captcha(link, ndir) crop_captcha.crop_captcha() binarize.binarize() key = captcha_solver.resolve("modified.png") key = simplify(key) rval = captcha_download.fill_captcha(driver, key) try: p = driver.find_element_by_id("lblCaptchaMessage") if p.text == 'Please enter correct captcha !': print "Wrong captcha" driver.close() return 0 else: # time.sleep(40) while not os.path.isfile(fname): pass driver.close() return 1 except: while not os.path.isfile(fname): pass driver.close() return 1
def main(): import sys classifier = cv2.SVM() classifier.load(sys.argv[1]) im = cv2.imread(sys.argv[2]) im = cv2.GaussianBlur(im, (3, 3), 0) imshow_large(__file__, im) cv2.waitKey() unrotated = unrotate(im) # # Check if image is right way up, correct otherwise # imshow_large(__file__, unrotated) key = cv2.waitKey() if key & 0xff == ord("r"): unrotated = cv2.flip(cv2.flip(unrotated, 0), 1) imshow_large(__file__, unrotated) cv2.waitKey() binarized = binarize(unrotated) rois = get_rois(binarized) results = {} grayscale = cv2.cvtColor(unrotated, cv.CV_BGR2GRAY) for (x, y, width, height) in rois: roi = grayscale[y:y + height, x:x + width] vec = extract_features(roi) label = classifier.predict(vec) results[(x, y, width, height)] = "01234567890X"[int(label)] scale = SCREEN_HEIGHT / unrotated.shape[0] unrotated = cv2.cvtColor(grayscale, cv.CV_GRAY2BGR) scaled = cv2.resize(unrotated, (0, 0), fx=scale, fy=scale) for roi in rois: x = int(roi[0] * scale) y = int(roi[1] * scale) width = int(roi[2] * scale) height = int(roi[3] * scale) cv2.rectangle(scaled, (x, y), (x + width, y + height), (0, 255, 0, 0), 1) if results[roi] == "X": continue cv2.putText(scaled, results[roi], (x, y), cv.CV_FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0, 0)) cv2.imshow(__file__, scaled) cv2.waitKey()
def main(): import sys classifier = cv2.SVM() classifier.load(sys.argv[1]) im = cv2.imread(sys.argv[2]) im = cv2.GaussianBlur(im, (3,3), 0) imshow_large(__file__, im) cv2.waitKey() unrotated = unrotate(im) # # Check if image is right way up, correct otherwise # imshow_large(__file__, unrotated) key = cv2.waitKey() if key & 0xff == ord("r"): unrotated = cv2.flip(cv2.flip(unrotated, 0), 1) imshow_large(__file__, unrotated) cv2.waitKey() binarized = binarize(unrotated) rois = get_rois(binarized) results = {} grayscale = cv2.cvtColor(unrotated, cv.CV_BGR2GRAY) for (x,y,width,height) in rois: roi = grayscale[y:y+height,x:x+width] vec = extract_features(roi) label = classifier.predict(vec) results[(x,y,width,height)] = "01234567890X"[int(label)] scale = SCREEN_HEIGHT/unrotated.shape[0] unrotated = cv2.cvtColor(grayscale, cv.CV_GRAY2BGR) scaled = cv2.resize(unrotated, (0,0), fx=scale, fy=scale) for roi in rois: x = int(roi[0]*scale) y = int(roi[1]*scale) width = int(roi[2]*scale) height = int(roi[3]*scale) cv2.rectangle(scaled, (x,y), (x+width, y+height), (0,255,0,0), 1) if results[roi] == "X": continue cv2.putText(scaled, results[roi], (x, y), cv.CV_FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0, 0)) cv2.imshow(__file__, scaled) cv2.waitKey()
def extractBlobs(img, doFilter=True, forOCR=False): #img=cv.cvtColor(cImg, cv.COLOR_BGR2GRAY) bImg=binarize(img, 0.5) if forOCR: mode=cv.RETR_EXTERNAL else: mode=cv.RETR_LIST contours,hierarchy = cv.findContours(bImg, mode, cv.CHAIN_APPROX_NONE) print(len(contours)) H, W = bImg.shape area=H*W bxs=[] for cnt in contours: if((not doFilter) or (cv.contourArea(cnt)>area/100 and cv.contourArea(cnt)<area/20)): x,y,w,h = cv.boundingRect(cnt) bxs.append((y,x,h,w)) return bxs
def main(): import sys in_dir = sys.argv[1] images = map(lambda x: P.join(in_dir, x), os.listdir(in_dir)) out_dir = sys.argv[2] counters = {} for subdir in "0123456789X": path = P.join(out_dir, subdir) if not P.isdir(path): os.makedirs(path) files = os.listdir(path) indices = map(lambda x: int(P.splitext(x)[0]), files) try: counters[subdir] = max(indices) + 1 except ValueError: counters[subdir] = 0 for fpath in images: print fpath im = cv2.imread(fpath) im = cv2.GaussianBlur(im, (3, 3), 0) imshow_large(__file__, im) key = cv2.waitKey() if key & 0xff == ord("q"): sys.exit(0) elif key & 0xff == ord("n"): continue unrotated = unrotate(im) # # Check if image is right way up, correct otherwise # imshow_large(__file__, unrotated) key = cv2.waitKey() if key & 0xff == ord("r"): unrotated = cv2.flip(cv2.flip(unrotated, 0), 1) imshow_large(__file__, unrotated) cv2.waitKey() elif key & 0xff == ord("n"): continue binarized = binarize(unrotated) scale = SCREEN_HEIGHT / unrotated.shape[0] colorbin = copy.deepcopy(unrotated) colorbin = cv2.resize(colorbin, (0, 0), fx=scale, fy=scale) rois = get_rois(binarized) for (x, y, width, height) in rois: x = int(x * scale) y = int(y * scale) width = int(width * scale) height = int(height * scale) cv2.rectangle(colorbin, (x, y), (x + width, y + height), (255, 0, 0, 0), 1) cv2.imshow(__file__, colorbin) key = cv2.waitKey() if key & 0xff == ord("q"): break elif key & 0xff == ord("n"): continue for (x, y, width, height) in rois: colorbin2 = copy.deepcopy(colorbin) x_ = int(x * scale) y_ = int(y * scale) width_ = int(width * scale) height_ = int(height * scale) cv2.rectangle(colorbin2, (x_, y_), (x_ + width_, y_ + height_), (0, 0, 255, 0), 1) sub = unrotated[y:y + height, x:x + width] supers = cv2.resize(sub, (192, 192), interpolation=cv.CV_INTER_NN) cv2.imshow(__file__, colorbin2) cv2.imshow("supers", supers) key = cv2.waitKey() if key & 0xff == ord("q"): sys.exit(0) elif key & 0xff == ord("n"): # move on to the next image break elif (key & 0xff) in map(ord, "0123456789"): digit = chr(key & 0xff) subdir = P.join(out_dir, digit) out_file = P.join(subdir, str(counters[digit]) + ".png") cv2.imwrite(out_file, sub) counters[digit] += 1 else: subdir = P.join(out_dir, "X") out_file = P.join(subdir, str(counters["X"]) + ".png") cv2.imwrite(out_file, sub) counters["X"] += 1
def kim2014(orig, O=None, split=False, n_points_w=None, n_tries=30): lib.debug_imwrite('gray.png', binarize.grayscale(orig)) im = binarize.binarize(orig, algorithm=lambda im: binarize.sauvola_noisy(im, k=0.1)) global bw bw = im im_h, im_w = im.shape AH, lines, _ = get_AH_lines(im) if O is None: O = np.array((im_w / 2.0, im_h / 2.0)) if split: # Test if line start distribution is bimodal. line_xs = np.array([line.left() for line in lines]) bimodal = line_xs.std() / im_w > 0.10 dual = bimodal and im_w > im_h else: dual = False if dual: print('Bimodal! Splitting page!') pages = crop.split_lines(lines) n_points_w = 1.2 * np.percentile(np.array([line.width() for line in lines]), 90) n_points_w = max(n_points_w, 1800) if lib.debug: debug = cv2.cvtColor(bw, cv2.COLOR_GRAY2BGR) for page in pages: page_crop = Crop.from_lines(page).expand(0.005) # print(page_crop) page_crop.draw(debug) lib.debug_imwrite('split.png', debug) page_crops = [Crop.from_lines(page) for page in pages] if len(page_crops) == 2: [c0, c1] = page_crops split_x = (c0.x1 + c1.x0) / 2 page_crops = [ c0.union(Crop(0, 0, split_x, im_h)), c1.union(Crop(split_x, 0, im_w, im_h)) ] result = [] for i, (page, page_crop) in enumerate(zip(pages, page_crops)): print('==== PAGE {} ===='.format(i)) lib.debug_prefix.append('page{}'.format(i)) page_image = page_crop.apply(orig) page_bw = page_crop.apply(im) page_AH, page_lines, _ = get_AH_lines(page_bw) new_O = O - np.array((page_crop.x0, page_crop.y0)) lib.debug_imwrite('precrop.png', im) lib.debug_imwrite('page.png', page_image) bw = page_bw dewarper = Kim2014(page_image, page_bw, page_lines, [page_lines], new_O, page_AH, n_points_w) result.append(dewarper.run_retry()[0]) lib.debug_prefix.pop() return result else: lib.debug_prefix.append('page0') dewarper = Kim2014(orig, im, lines, [lines], O, AH, n_points_w) lib.debug_prefix.pop() return dewarper.run_retry(n_tries=n_tries)
def test_negative_integers(self): self.assertEqual(b'\x80', binarize(-128, endian=Endian.little, width=1)) self.assertEqual(b'\x7f', binarize(-129, endian=Endian.little, width=1)) self.assertEqual(b'\xff\xff', binarize(-1, endian=Endian.little, width=2)) self.assertEqual(b'\xfe\xff', binarize(-2, endian=Endian.little, width=2))
def test_positive_big_endian_integers(self): # some "normal" integers self.assertEqual(b'\x00\x00\x00\x00', binarize(0, endian=Endian.big, width=4)) self.assertEqual(b'\x00\x00\x00\x01', binarize(1, endian=Endian.big, width=4)) self.assertEqual(b'\x00\x00\x04\x00', binarize(1024, endian=Endian.big, width=4)) # 1->2 byte boundary self.assertEqual(b'\x00\x00\x00\xff', binarize(255, endian=Endian.big, width=4)) self.assertEqual(b'\x00\x00\x01\x00', binarize(256, endian=Endian.big, width=4)) # 2->3 byte boundary self.assertEqual(b'\x00\x00\xff\xff', binarize(65535, endian=Endian.big, width=4)) self.assertEqual(b'\x00\x01\x00\x00', binarize(65536, endian=Endian.big, width=4)) # 3->4 byte boundary self.assertEqual(b'\x00\xff\xff\xff', binarize(16777215, endian=Endian.big, width=4)) self.assertEqual(b'\x01\x00\x00\x00', binarize(16777216, endian=Endian.big, width=4)) # 4->5 byte boundary self.assertEqual(b'\xff\xff\xff\xff', binarize(4294967295, endian=Endian.big, width=4)) self.assertRaises(TypeError, binarize, 4294967296, endian=Endian.big, width=4) # large width: 8->9 byte boundary self.assertEqual(b'\xff\xff\xff\xff\xff\xff\xff\xff', binarize((2**64)-1, endian=Endian.big, width=8)) self.assertRaises(TypeError, binarize, 2**64, endian=Endian.big, width=8) # "weird" width self.assertEqual(b'\x00\x10\xbc', binarize(4284, endian=Endian.big, width=3)) self.assertEqual(b'\xff\xff\xff', binarize(16777215, endian=Endian.big, width=3)) self.assertRaises(TypeError, binarize, 16777216, endian=Endian.big, width=3)
def upscale(path, data_dir, factor): assert factor == 2 im = cv2.imread(path, cv2.IMREAD_UNCHANGED) im_h, im_w = im.shape bw = binarize.binarize(im, algorithm=binarize.ntirogiannis2014) AH = dominant_char_height(bw) print('AH =', AH) possible_AHs = np.array( [int(d) for d in os.listdir(data_dir) if d.isdigit()]) size = possible_AHs[np.abs(possible_AHs - AH).argmin()] D_T_coupled = np.load(os.path.join(data_dir, str(size), 'dict.npy')) W_l = int(size / 3) | 1 W_h = 2 * W_l step = 3 assert D_T_coupled.shape[1] == W_l * W_l + W_h * W_h D_T = D_T_coupled[:, :W_l * W_l] K = D_T.shape[0] lam = 0.2 # weight of sparsity. TODO: confirm same as training data. lo_patches = patches(im, W_l, step) struct = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) gradient = cv2.morphologyEx(im, cv2.MORPH_GRADIENT, struct) gradient_means, _ = binarize.mean_std(gradient, W_l) patch_gradient = gradient_means[W_l // 2:-W_l // 2 + 1:step, W_l // 2:-W_l // 2 + 1:step] assert patch_gradient.shape == (lo_patches.shape[0], lo_patches.shape[1]) patch_mask = patch_gradient > np.percentile(patch_gradient, 50) good_patches = np.nonzero(patch_mask) cv2.imwrite('spp.png', -patch_mask.astype(np.uint8)) lo_patches_vec = lo_patches[good_patches].reshape(-1, W_l * W_l).astype( np.float64) means = lo_patches_vec.mean(axis=1) X_T = lo_patches_vec - means[:, newaxis] t = X_T.shape[0] Z_T = np.zeros((t, K), dtype=np.float64) last_objective = None while True: training.feature_sign_search_vec(X_T, Z_T, D_T, lam) objective = np.square(X_T - Z_T.dot(D_T)).sum() print('\ncurrent objective:', objective) if last_objective is not None: relative_err = abs(last_objective - objective) / last_objective print('relative error:', relative_err) if relative_err < 1e-4: break last_objective = objective hi_patches = Z_T.dot(D_T_coupled)[:, W_l * W_l:] + means[:, newaxis] hi_float = np.zeros((factor * im_h, factor * im_w), dtype=np.float64) dest_patches = patches(hi_float, W_h, 2 * step) patch_count = np.zeros((factor * im_h, factor * im_w), dtype=int) count_patches = patches(patch_count, W_h, 2 * step) for i, patch in enumerate(hi_patches): dest_patches[good_patches[i]] += patch count_patches[good_patches[i]] += 1 np.divide(hi_float, patch_count, hi_float, where=patch_count > 0) hi_lanczos = cv2.resize(im, (0, 0), None, 2., 2., interpolation=cv2.INTER_LANCZOS4) return np.where(patch_count > 0, hi_float, hi_lanczos).clip(0, 255).astype(np.uint8)
def process_image(original, dpi=None): original_rot90 = original for i in range(args.rotate / 90): original_rot90 = np.rot90(original_rot90) # original_rot90 = cv2.resize(original_rot90, (0, 0), None, 1.5, 1.5) im_h, im_w = original_rot90.shape[:2] # image height should be about 10 inches. round to 100 if not dpi: dpi = int(round(im_h / 1100.0) * 100) print('detected dpi:', dpi) split = im_w > im_h # two pages cropped_images = [] if args.dewarp: lib.debug_prefix.append('dewarp') dewarped_images = dewarp.kim2014(original_rot90) for im in dewarped_images: bw = binarize.binarize(im, algorithm=binarize.sauvola, resize=1.0) lib.debug_prefix.append('crop') _, [lines] = crop(im, bw, split=False) lib.debug_prefix.pop() c = Crop.from_lines(lines) if c.nonempty(): cropped_images.append(Crop.from_whitespace(bw).apply(im)) lib.debug_prefix.pop() else: bw = binarize.binarize(original_rot90, algorithm=binarize.adaptive_otsu, resize=1.0) debug_imwrite('thresholded.png', bw) AH, line_sets = crop(original_rot90, bw, split=split) for lines in line_sets: c = Crop.from_lines(lines) if c.nonempty(): lib.debug = False bw_cropped = c.apply(bw) orig_cropped = c.apply(original_rot90) angle = algorithm.skew_angle(bw_cropped, original_rot90, AH, lines) if not np.isfinite(angle): angle = 0. rotated = algorithm.safe_rotate(orig_cropped, angle) rotated_bw = binarize.binarize(rotated, algorithm=binarize.adaptive_otsu) _, [new_lines] = crop(rotated, rotated_bw, split=False) # dewarped = algorithm.fine_dewarp(rotated, new_lines) # _, [new_lines] = crop(rotated, rotated_bw, split=False) new_crop = Crop.union_all([line.crop() for line in new_lines]) if new_crop.nonempty(): # cropped = new_crop.apply(dewarped) cropped = new_crop.apply(rotated) cropped_images.append(cropped) out_images = [] lib.debug_prefix.append('binarize') for i, cropped in enumerate(cropped_images): lib.debug_prefix.append('page{}'.format(i)) if lib.is_bw(original_rot90): out_images.append(binarize.otsu(cropped)) else: out_images.append( binarize.ng2014_fallback(binarize.grayscale(cropped)) ) lib.debug_prefix.pop() lib.debug_prefix.pop() return dpi, out_images
def main(): import sys in_dir = sys.argv[1] images = map(lambda x: P.join(in_dir, x), os.listdir(in_dir)) out_dir = sys.argv[2] counters = {} for subdir in "0123456789X": path = P.join(out_dir, subdir) if not P.isdir(path): os.makedirs(path) files = os.listdir(path) indices = map(lambda x: int(P.splitext(x)[0]), files) try: counters[subdir] = max(indices) + 1 except ValueError: counters[subdir] = 0 for fpath in images: print fpath im = cv2.imread(fpath) im = cv2.GaussianBlur(im, (3,3), 0) imshow_large(__file__, im) key = cv2.waitKey() if key & 0xff == ord("q"): sys.exit(0) elif key & 0xff == ord("n"): continue unrotated = unrotate(im) # # Check if image is right way up, correct otherwise # imshow_large(__file__, unrotated) key = cv2.waitKey() if key & 0xff == ord("r"): unrotated = cv2.flip(cv2.flip(unrotated, 0), 1) imshow_large(__file__, unrotated) cv2.waitKey() elif key & 0xff == ord("n"): continue binarized = binarize(unrotated) scale = SCREEN_HEIGHT/unrotated.shape[0] colorbin = copy.deepcopy(unrotated) colorbin = cv2.resize(colorbin, (0,0), fx=scale, fy=scale) rois = get_rois(binarized) for (x,y,width,height) in rois: x = int(x*scale) y = int(y*scale) width = int(width*scale) height = int(height*scale) cv2.rectangle(colorbin, (x,y), (x+width, y+height), (255,0,0,0), 1) cv2.imshow(__file__, colorbin) key = cv2.waitKey() if key & 0xff == ord("q"): break elif key & 0xff == ord("n"): continue for (x,y,width,height) in rois: colorbin2 = copy.deepcopy(colorbin) x_ = int(x*scale) y_ = int(y*scale) width_ = int(width*scale) height_ = int(height*scale) cv2.rectangle(colorbin2, (x_,y_), (x_+width_, y_+height_), (0,0,255,0), 1) sub = unrotated[y:y+height, x:x+width] supers = cv2.resize(sub, (192, 192), interpolation=cv.CV_INTER_NN) cv2.imshow(__file__, colorbin2) cv2.imshow("supers", supers) key = cv2.waitKey() if key & 0xff == ord("q"): sys.exit(0) elif key & 0xff == ord("n"): # move on to the next image break elif (key & 0xff) in map(ord, "0123456789"): digit = chr(key & 0xff) subdir = P.join(out_dir, digit) out_file = P.join(subdir, str(counters[digit]) + ".png") cv2.imwrite(out_file, sub) counters[digit] += 1 else: subdir = P.join(out_dir, "X") out_file = P.join(subdir, str(counters["X"]) + ".png") cv2.imwrite(out_file, sub) counters["X"] += 1
def preprocess(image): height, width, channels = image.shape grayImage = binarize.toGrayScale(image) binarizedImage = binarize.binarize(grayImage) segmentList = segmentation.segmentation(binarizedImage) return segmentList
assert np.all(segments[:, 0] < segments[:, 1]) theta_p = np.zeros((len(ellipses), )) s_p = np.full((len(ellipses), ), 5) def V_pq_sites(p1, p2, l1, l2): s_1, theta_1 = unpack_label(l1) s_2, theta_2 = unpack_label(l2) centroid_1, centroid_2 = centroids[(p1, p2), :] d_pq_sq = np.square(centroid_1 - centroid_2).sum() scale = np.exp(-k * d_pq_sq / (s_values[s_1]**2 + s_values[s_2]**2)) f_diff = abs(s_1 - s_2) + abs(theta_1 - theta_2) mu = 0 if l1 == l2 else (lam_1 if f_diff <= 3 else lam_2) return mu * scale # graph.set_smooth_cost_function(V_pq_sites) # graph.expansion() if __name__ == '__main__': lib.debug = True orig = cv2.imread(sys.argv[1]) bw = binarize.binarize(orig, algorithm=binarize.sauvola) lib.debug_imwrite('retinex.png', bw) AH = algorithm.dominant_char_height(bw) koo2010(bw, AH)
with open('bbox.txt', 'rb') as f: bxs=pickle.load(f) fname='cars_markus/image_0011.jpg' # trImg = cv.imread('train_img.png',cv.IMREAD_GRAYSCALE) cImg = cv.imread(fname) img=cv.cvtColor(cImg, cv.COLOR_BGR2GRAY) # bx = [226, 381, 75, 32] # trImg = trImg[bx[0]:bx[0]+bx[2],bx[1]:bx[1]+bx[3]] bx=bxs[0][1] cimgD = cImg[:][bx[0]:bx[0]+bx[2],bx[1]:bx[1]+bx[3]] # print(bxs) imgD = img[bx[0]:bx[0]+bx[2],bx[1]:bx[1]+bx[3]]; bimgD=binarize(imgD, 0.5) bxs=extractBlobs(imgD) model = getModel(); # clbp = findLBP(trImg); # chist = lbpHist(clbp); # chist = chist.reshape((1, 69)).astype(np.float32); # retval, results, neigh_resp, dists = model.find_nearest(chist, k = 1) # string = chr(int((results[0][0]))) # print(string) i=0 for cbx in bxs: y,x,h,w=cbx print(cbx)