Last active
October 31, 2019 17:07
-
-
Save SheatNoisette/4d9e4bf8da6b2cf5e13941ef95d647d8 to your computer and use it in GitHub Desktop.
Naive characters detection in a 2D array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
# Find characters in a 2D array and return their position in the grid | |
# Basis for a simple optical recognition software | |
# MIT License | |
char_array = [ | |
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,1,1,0,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,1,1,0,0,0,1,1,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,1,1,0,0,0,1,1,1,1,1,0,0,0,0,1,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0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,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] | |
] | |
char_array_i = [ | |
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0], | |
[0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0] | |
] | |
#Utils | |
#Count number of '1' in a list | |
def sum_line(list): | |
counter = 0 | |
for element in list: | |
if element == 1: | |
counter += 1 | |
return counter | |
#Count number '1' vertically in an array | |
def sum_vertical_array(array, x1, y1, y2): | |
counter = 0 | |
currentY = y1 | |
while currentY < y2: | |
if array[currentY][x1] == 1: | |
counter += 1 | |
currentY += 1 | |
return counter | |
# Output y: [1,6,9,14,16,21] | |
def find_char_y_pos(array): | |
yArray = len(array) | |
yPos = 0 | |
detectY = [] | |
lineRecognised = False | |
currentSum = 0 | |
while yPos < yArray: | |
currentSum = sum_line(array[yPos]) | |
if currentSum > 0 and not lineRecognised: | |
lineRecognised = True | |
detectY.append(yPos) | |
elif currentSum == 0 and lineRecognised: | |
detectY.append(yPos - 1) | |
lineRecognised = False | |
#If we are at the end of the array and the line isn't finished | |
elif yPos == yArray - 1 and lineRecognised: | |
detectY.append(yArray - 1) | |
yPos += 1 | |
return detectY | |
def find_char_x_pos(array, yPosList): | |
if len(yPosList)%2 != 0: | |
print("Y List length error in find_char_xpos: ", str(yPosList)) | |
return -1 | |
xArray = len(array[0]) | |
yPosLeft = len(yPosList) // 2 | |
# Keep track of the position | |
currentYPos = 0 | |
currentXPos = 0 | |
# Current Horizontal sum | |
currentSum = 0 | |
# Found a char? | |
charRecognised = False | |
# Current X char position | |
currentCharDetected = [] | |
finalCharDetected = [] | |
while yPosLeft > 0: | |
# Reset x cursor | |
currentXPos = 0 | |
# Reset X Sum | |
currentSum = 0 | |
#Reset current position detected | |
currentCharDetected = [] | |
# Read horizontally | |
while currentXPos < xArray: | |
# Get vertical sum | |
currentSum = sum_vertical_array(array, currentXPos, yPosList[currentYPos], yPosList[currentYPos + 1]) | |
if currentSum > 0 and not charRecognised: | |
# Beginning of the char | |
charRecognised = True | |
# Add X1 and Y1 | |
currentCharDetected.append(currentXPos) | |
currentCharDetected.append(yPosList[currentYPos]) | |
elif currentSum == 0 and charRecognised: | |
# End of the Char | |
charRecognised = False | |
# Add X2 and Y2 | |
currentCharDetected.append(currentXPos - 1) | |
currentCharDetected.append(yPosList[currentYPos + 1]) | |
# Add the char position to final list | |
finalCharDetected.append(currentCharDetected) | |
# Reset for the next char | |
currentCharDetected = [] | |
currentXPos += 1 | |
# Go to the next line | |
currentYPos += 2 | |
# -1 Y couple remaining | |
yPosLeft -= 1 | |
return finalCharDetected | |
def find_char_array(array): | |
return find_char_x_pos(array, find_char_y_pos(array)) | |
# Output example: [Char 1: [x1, y1, x2, y2], ...] | |
print(find_char_array(char_array)) | |
print(find_char_array(char_array_i)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment