<jupyter_start><jupyter_text>Manual Preprocessing<jupyter_code>rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 3))
sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
# gray = cv2.cvtColor(image_data, cv2.COLOR_RGB2GRAY)
# sobel = cv2.Sobel(gray,ddepth=cv2.CV_32F, dx=dy=0)
# tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, rectKernel)
# plt.imshow(gray)
# print(image_data.shape)
# print(gray.shape)
# tophat=tophat.tobytes()
# # sobel = sobel.tobytes()


im_gray = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
(thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
thresh = 127
im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
plt.imshow(im_bw)
image = cv2.imencode('.jpg',im_bw)[1].tostring()<jupyter_output><empty_output><jupyter_text>~Creating Google Vision API Object for Image~<jupyter_code>im_object = vision.types.Image(content = content)<jupyter_output><empty_output><jupyter_text>~Fetching Response from the API<jupyter_code>response = client.text_detection(image=im_object)
texts = response.text_annotations
save_text = []
for text in texts:
        print('\n"{}"'.format(text.description))
        save_text.append(text.description)
        vertices = (['({},{})'.format(vertex.x, vertex.y)
                    for vertex in text.bounding_poly.vertices])

#         print('bounds: {}'.format(','.join(vertices)))
def count_alpha_n_digs(text):
    dig_count = 0
    alpha_count = 0
    for char in text:
        if char.isalpha():
            alpha_count = alpha_count+1
        if char.isdigit():
            dig_count = dig_count+1
    
    return dig_count,alpha_count<jupyter_output><empty_output><jupyter_text>~Since Images are too noisy and result from OCR may generate spaces between VIN, therefore using bruteforce instead of regex for detecting favourble VIN text and not going for only 17 char length alphanumeric word.~<jupyter_code>possibles_matches = []
for ix in range(1,len(save_text)):
    tmp = save_text[ix]
    digs,alps = count_alpha_n_digs(tmp)
    if len(tmp) >= 17:
        if digs >0 and alps >0:
            possibles_matches=[]
            possibles_matches.append({'Text':tmp,'digits':digs,'alphas':digs})
            print('VIN : '+tmp)
            break
    if digs+alps > 5 and digs!=0 and alps!=0:
        possibles_matches.append({'Text':tmp,'digits':digs,'alpha':alps})
    
    
print('Possible Matches ')
for match in possibles_matches:
    
    print(match['Text'])
len(possibles_matches)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13072669826224328, 'fraction_numerical': 0.02132701421800948, 'mean_word_length': 4.37791932059448, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '126058', 'license': 'MIT', 'n_tokens_mistral': 959, 'n_tokens_neox': 931, 'n_words': 168, 'path': 'Model.ipynb', 'repo_name': 'Arifkhan21/CV-OCR', 'size': '2532'}