def resize_pad(): srcdir0 = '/home/kevin/PycharmProjects/autograde/dataset/svm/data-0810' # srcdir0 = '/home/kevin/PycharmProjects/autograde/dataset/svm/data-0810-aug' dstdir0 = hp.mkdir( '/home/kevin/PycharmProjects/autograde/dataset/svm/data-0810-28') # for cls in range(22): for cls in [22]: srcdir = os.path.join(srcdir0, str(cls)) dstdir = hp.mkdir(os.path.join(dstdir0, str(cls))) file_list = os.listdir(srcdir) print(len(file_list)) for file in file_list: if file.endswith('.jpg') or file.endswith('.png'): src_full = os.path.join(srcdir, file) dst_full = os.path.join(dstdir, file[:-4] + '.jpg') print(dst_full) gray = cv2.imread(src_full, 0) gray = imu.strip_white_boder(gray) result = adjust_img(gray) cv2.imwrite(dst_full, result)
def predict_with_proba(self, gray, var_thres=0.01): im = imu.strip_white_boder(gray) im = adjust_img(im) sample = im.ravel() / 255.0 var = np.var(self.model.predict_proba(sample.reshape(1, -1))) if var > var_thres: label = self.model.predict(sample.reshape(1, -1)) else: label = [22] return label
def check_position(img, p1, p2): """Check two positions of characters. Args: img: A gray image object. p1: The position of number character. p2: The position of down or dot character. Returns: A boolean indicates if the positions of characters is reasonable. """ img1 = img[p1[1]:p1[3], p1[0]:p1[2]] img2 = img[p2[1]:p2[3], p2[0]:p2[2]] im = imu.strip_white_boder(img1) t1, b1 = get_vertical_white_edge_dis(img1) t2, b2 = get_vertical_white_edge_dis(img2) if b2 - b1 > im.shape[0] / 4 or t2 - t1 < im.shape[0] / 2 or p2[0] - p1[ 2] > im.shape[1]: return False return True
def all_strip_white(): categories = range(22) #不考虑其他类 for c in categories: srcdir = os.path.join('../dataset/svm/data-0807', str(c)) outdir = os.path.join('../dataset/svm/data-0810', str(c)) if not os.path.exists(outdir): os.mkdir(outdir) papers = os.listdir(srcdir) # papers = ['120190703153331060.jpg'] for paper in papers: if paper.endswith('.jpg') or paper.endswith('.png'): imgfile = os.path.join(srcdir, paper) outfile = os.path.join(outdir, paper) im = cv2.imread(imgfile, 0) imout = imu.strip_white_boder(im) cv2.imwrite(outfile, imout)
def augment(gray, shift_low=-10): angle = (2 * np.random.random() - 1) * 5.0 # print(angle) rotated = rotate_image(gray, angle) # imu.imshow_(rotated) rotated = imu.strip_white_boder(rotated) # imu.imshow_(rotated) shift = np.random.randint(low=shift_low, high=50) gray = rotated if shift >= 0: gray = cv2.add(gray, shift) else: bw = imu.preprocess_bw_inv(gray) gray[bw > 0] = gray[bw > 0] + shift # print(shift) # if shift>0: # gray = cv2.add(gray, shift) # else: # gray = cv2.subtract(gray, shift) # imu.imshow_(gray) h, w = gray.shape hcut = min(2, h // 5) wcut = min(2, w // 5) top = np.random.randint(hcut + 1) bottom = h - np.random.randint(hcut + 1) left = np.random.randint(wcut + 1) right = w - np.random.randint(wcut + 1) return gray[top:bottom, left:right]
def predict_proba(self, gray): im = imu.strip_white_boder(gray) im = adjust_img(im) sample = im.ravel() / 255.0 return self.model.predict_proba(sample.reshape(1, -1))