-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSightFullServerFinal.py
156 lines (127 loc) · 4.11 KB
/
SightFullServerFinal.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# -*- coding: utf-8 -*-
#"""keras-ocr-complet.ipynb
#Automatically generated by Colaboratory.
#Original file is located at
# https://colab.research.google.com/drive/1R53Bba2YGKWf26t7kpYFUc6fXOGHJ3wq
#"""
#!pip install gtts
#!pip install keras_ocr
from gtts import gTTS
import keras_ocr
import keras_ocr.tools
import socket
import io
import PIL.Image as Image
import struct
AROUND=60
def gTTS_call(vocalText):
########### GTTS ############
tts = gTTS(text=vocalText, lang="it")
tts.save("/Users/wollo/Desktop/SightFull/vocal.mp3")
def DL_recognition(buff):
pipeline = keras_ocr.pipeline.Pipeline()
#image_for_Keras = keras_ocr.tools.read(path_to_image)
images = [keras_ocr.tools.read(buff)]
prediction_groups = pipeline.recognize(images);
return prediction_groups;
######WORD SORTING ALGORITHM########
#- lista di appoggio che restituisce la lista ordinata delle parole
#- itero sugli elementi della lista passata come parametro
#- individuo un set di elementi che hanno la y minore --> guardo al primo valore di coordinate nell'array
#- tra quegli elementi scelgo l'elemento con x minore
#- appendo quell'elemento alla lista di appoggio ed elimino quell'elemento dalla lista dei parametri
#- itero il procedimento finchè la lista non è vuota
def isAround(y, record):
res = record-y
if res < AROUND and res > (AROUND*(-1)): #test more values
return True
else:
return False
def findTopLeftValue(lista):
if len(lista) == 0:
return None
recordTopMin = lista[0][1][0][1]
topList = []
for element in lista:
y=element[1][0][1]
if y < recordTopMin:
recordTopMin = y
if isAround(y,recordTopMin):
topList.append(element)
recordLeft = topList[0]
for topElement in topList:
x=topElement[1][0][0]
if x<recordLeft[1][0][0]:
recordLeft = topElement
for element in lista:
try:
if element == recordLeft:
lista.remove(element)
break
except ValueError:
return ["0",None]
return recordLeft
def sortElements(lista):
if len(lista)>0:
sortedList = []
element = lista[0]
while element!=None:
if len(lista) == 0:
break
element = findTopLeftValue(lista)
if element[0]=="0" and element[1]==None:
return [["Eccessivo","0"],["testo","0"],["presente","0"],["nella","0"],["foto","0"]]
sortedList.append(element)
return sortedList
else:
return [["Nessun","0"],["testo","0"],["presente","0"],["nella","0"],["foto","0"]]
def recv_msg(sock):
# Read message length and unpack it into an integer
raw_msglen = recvall(sock, 4)
if not raw_msglen:
return None
msglen = struct.unpack('>I', raw_msglen)[0]
# Read the message data
return recvall(sock, msglen)
def recvall(sock, n):
# Helper function to recv n bytes or return None if EOF is hit
data = bytearray()
while len(data) < n:
packet = sock.recv(n - len(data))
if not packet:
return None
data.extend(packet)
return data
savepath="/Users/wollo/Desktop/SightFull/img.jpeg" #change when needed
def main():
HOST = "192.168.1.142" #My eth0 address -> CHANGE IF SERVER MACHINE IS CHANGED
#HOST = "localhost"
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen()
print(f"server is running on {HOST}........")
while True:
conn, addr = s.accept()
with conn:
print(f"Connected by {addr}")
data = recv_msg(conn)
print('data received')
if not data:
break
image = Image.open(io.BytesIO(data))
image.save(savepath)
prediction_groups=DL_recognition(io.BytesIO(data))
pg = prediction_groups[0].copy()
sortedElements = sortElements(pg)
vocalText =""
for text in sortedElements[:]:
vocalText += text[0] +" "
#vocalText += "\n"
bytes_to_send=vocalText.encode()
conn.sendall(bytes_to_send)
conn.close()
print(f"operation complete...{vocalText}")
gTTS_call(vocalText)
if __name__ == "__main__":
main()