<jupyter_start><jupyter_text>Recognition is fine when we cut all trash blobs. But it is a little bit riskyWe must use `remove_small_holes(img_as_ubyte(opened), 50)` or something like.But not like this `remove_small_holes(img_as_ubyte(opened), 300)`<jupyter_code>def numberprepare(image):

    img = img_as_float(image)
    logarithmic_corrected = adjust_log(img, 1)
    img_log_clah = equalize_adapthist(logarithmic_corrected, clip_limit=0.03)
    gray = rgb2gray(img_log_clah)

    seed = np.copy(gray)
    seed[1:-1, 1:-1] = gray.max()
    mask = gray

    filled = reconstruction(seed, mask, method='erosion')
    # holes = denoise_wavelet(rank.enhance_contrast(gray-filled+1, disk(6)))
    # niblack = holes > threshold_niblack(holes, window_size=71, k=0.9)
    holes = rank.enhance_contrast(gray-filled+1, disk(6))
    
    thresh_min = threshold_mean(holes)
    binary_min = holes > thresh_min
    
    filtered_smallest_noise = remove_small_holes(img_as_ubyte(binary_min), 10)
    
    opened = binary_opening(filtered_smallest_noise, disk(2))
    
    filtered_larger_noise = remove_small_holes(img_as_ubyte(opened), 50)

    return filtered_larger_noise

src = Image.open('test_data/final720/photo1.jpg')

# First source
# rotated = src.rotate(-90, expand=True)

serial = src.crop((1043, 218, 1043+80, 218+108))
filtered_serial = serialprepare(serial)

number = src.crop((89, 218, 89+399, 218+99)).rotate(180, expand=True)
%time filtered_number = numberprepare(number)
%time numberCandidate = pytesseract.image_to_string(Image.fromarray(filtered_number, "L"), config="--tessdata-dir ./tessdata -psm 8 -oem 3 -l eng -c tessedit_char_whitelist=1234567890 nobatch digits")
thresholded = img_as_ubyte(filtered_number)

edged = cv2.Canny(thresholded, 30, 200)
width, height = thresholded.shape[:2]

refCnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
# refCnts = refCnts[0] if imutils.is_cv2() else refCnts[1]
# refCnts = contours.sort_contours(refCnts, method="left-to-right")[0]
 
# create a clone of the original image so we can draw on it
clone = np.dstack([thresholded.copy()] * 3)
 
# loop over the (sorted) contours
for c in refCnts:
    # compute the bounding box of the contour and draw it on our
    # image
    (x, y, w, h) = cv2.boundingRect(c)
    cv2.rectangle(clone, (x, y), (x + w, y + h), (0, 255, 0), 2)

fig, ax = plt.subplots(1, 3, figsize=(40, 5), sharex=True, sharey=True)
ax = ax.ravel()

ax[0].imshow(number)
ax[0].set_title('Original image')
ax[0].axis('off')

ax[1].imshow(filtered_number, cmap='gray')
ax[1].set_title('Filtered Number')
ax[1].axis('off')

ax[2].imshow(cv2.cvtColor(clone, cv2.COLOR_BGR2RGB))
ax[2].set_title('Contours')
ax[2].axis('off')

print(numberCandidate)<jupyter_output>/media/levabd/ScienceProjects/Code/Science-Repos/scikit-image/skimage/util/dtype.py:123: UserWarning: Possible precision loss when converting from float64 to uint16
  .format(dtypeobj_in, dtypeobj_out))
/media/levabd/ScienceProjects/Code/Science-Repos/scikit-image/skimage/util/dtype.py:123: UserWarning: Possible precision loss when converting from float64 to uint8
  .format(dtypeobj_in, dtypeobj_out))
/media/levabd/ScienceProjects/Code/Science-Repos/scikit-image/skimage/util/dtype.py:123: UserWarning: Possible precision loss when converting from float64 to uint16
  .format(dtypeobj_in, dtypeobj_out))
/media/levabd/ScienceProjects/Code/Science-Repos/scikit-image/skimage/util/dtype.py:123: UserWarning: Possible precision loss when converting from float64 to uint8
  .format(dtypeobj_in, dtypeobj_out))
/media/levabd/ScienceProjects/Code/Science-Repos/scikit-image/skimage/morphology/misc.py:203: UserWarning: Any labeled images will be returned as a boolean array. Did you mean to use a boole[...]
{'fraction_non_alphanumeric': 0.12772172065852364, 'fraction_numerical': 0.0403611258629846, 'mean_word_length': 5.363175675675675, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '495539', 'license': 'MIT', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1383, 'n_words': 286, 'path': '22-MobileSerialNumberSegmentation.ipynb', 'repo_name': 'levabd/excise-digit-recogniser', 'size': '3766'}