0

I'm working on my first Python project for a device that reads a string from OCR and outputs braille. The braille device can only output 6 letters at a time. I'm stuck trying to scan each character in the 6 character long multi-string array.

For simplicity's sake, for now I only want to print "this is (insert character)" for every character in the multi-string array. In reality, the output would be to run the code that tells the first two motors to create the character in braille, and then do it for the remaining 5 characters with the 10 remaining motors with a short delay between each 6 character long string. How do I scan each 6 character long string and loop it for the rest of the strings in the array?

Here's where I'm at so far:

from PIL import Image
import pytesseract


img = Image.open('img file path')
text = [item for item in (pytesseract.image_to_string(img, lang='eng', config='--psm 6')).split('\n')]
oneLineStr = ' '.join(text)
# displays: The quick brown fox jumps over the lazy dog.
print(oneLineStr)

arr6elem = []
for idx in range(0, len(oneLineStr), 6):
    arr6elem.append(oneLineStr[idx:idx + 6])
# displays: ['The qu', 'ick br', 'own fo', 'x jump', 's over', ' the l', 'azy do', 'g.']
print(arr6elem)

# Don't know what to do from this point
# Want to scan each 6-element string in list and for each string, see which elements it consists of
# (capital/lower case characters, numbers, spaces, commas, apostrophes, periods, etc.)
# Then, print "this is a" for letter a, or "this is a colon" for :, etc.
# So that output looks like:
# ["'this is T', 'this is h', 'this is e', this is a space', 'this is q', 'this is u'", "'this is i', 'this is c'...]

1 Answer 1

1

A dictionary should do the trick:

punctuation = {
    ' ': 'a space',
    ',': 'a comma',
    "'": 'an apostrophes',
    '.': 'a period'
}

for word in arr6elem:
    for char in word:
        print('This is {}'.format(punctuation.get(char, char)))

Once you've built your punctuation dict with all the items you need, the loop will either get the corresponding value from it, or default to the actual char.

Output:
# This is T
# This is h
# This is e
# This is a space
# This is q
# This is u
# This is i
# This is c
# This is k
# This is a space
# This is b
# This is r
# This is o
# This is w
# This is n
# This is a space
# This is f
# ...
Sign up to request clarification or add additional context in comments.

5 Comments

I knew I needed a nested for loop, just didn't know what else so thanks!
If I wanted to add a delay between each 6 element string so that the person has time to read the braille, how would I go about that? I'm guessing there's a way to make the for loop pause in between each string?
Never mind! I imported time and added "time.sleep(1)" above the "for char in word:" and that works
@Philip you can either use time.sleep(x), with x being the amount of seconds you want the loop to stop for, or input() to pause the loop until enter is pressed
I posted my reply at the same time you replied but I used exactly that and it works, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.