-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogram.py
102 lines (87 loc) · 3.1 KB
/
program.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import cv2
from PIL import Image
import numpy as np
import sys
import glob
import os
from subprocess import call
from urllib.request import urlopen
from collections import Counter
import json
from pathlib import Path
import re
cf = 0
ResultStr = ['']
def LogicOpStrings():
l = len(ResultStr)
newArr = []
for i in range(0, l):
if not ResultStr[i]=="":
newArr.append(ResultStr[i].replace("\n","").strip())
newArrSet = set(newArr)
url = ("https://raw.githubusercontent.com/dwyl/english-words/master/words_dictionary.json")
print("Loading english dictionary from internet...")
wordDic = get_jsonparsed_data(url)
print("English word vocabulary has been successfully loaded!")
validWordCountMap = []
l = len(newArr)
for i in range(0, l):
count = 0
wordChunks = newArr[i].split(' ')
for j in range(0,len(wordChunks)):
if wordChunks[j].lower() in wordDic:
count = count + 1
else:
print(wordChunks[j]," is not a valid word!")
validWordCountMap.append(count)
print("-----Overall result:-----")
print(newArr[validWordCountMap.index(most_Common(validWordCountMap))])
print("-------------------------")
def get_jsonparsed_data(url):
response = urlopen(url)
data = response.read().decode("utf-8")
return json.loads(data)
def most_Common(lst):
data = Counter(lst)
return data.most_common(1)[0][0]
def tessfunc():
os.system("tesseract file.png data")
clearTesseractExecNote()
with open('data.txt', 'r') as mf:
try:
content = mf.read()
if(not content.isspace()):
ResultStr.append(content)
print("\nResult:\n********************************************\n", re.sub('[^a-zA-Z\r\n]+', ' ', content),"\n********************************************")
except UnicodeDecodeError:
print("Unicode character found which cant be decoded")
def removechunk():
for i in glob.glob('file*.png'):
print("Unlinked %s" % i)
os.unlink(i)
def clearTesseractExecNote():
print ("\033[A \033[A")
print ("\033[A \033[A")
print("Accessing device's camera...")
cap = cv2.VideoCapture(0)
print ("\033[A \033[A")
print("Camera ready!")
print("Activated command mode!\nPress 's' to start text recognition")
count = 0
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('Frame', gray)
ret,thresh=cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imwrite('file.png', thresh)
if cf == 1:
tessfunc()
if cv2.waitKey(1) & 0xFF == ord('q'):
cf = 0
if cv2.waitKey(1) & 0xFF == ord('s'):
cf = 1
if cv2.waitKey(1) & 0xFF == ord('e'):
print("Terminating the program")
break
cap.release()
exit()