-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
executable file
·151 lines (115 loc) · 3.99 KB
/
run.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 15 19:46:46 2020
@author: aarav
"""
# Reference: https://stackoverflow.com/a/23312964/5370202
import socket
import struct
import sys
import netifaces as ni
import os
import numpy as np
import cv2
import imutils
import matplotlib.pyplot as plt
import pickle
from keras.preprocessing import image
dir_path = os.path.dirname(os.path.realpath(__file__))
print('dir path is \n ', dir_path)
sys.path.insert(0, dir_path+"/Modules")
import utils
import FaceEliminator
from tensorflow import keras
# loading the model
model = keras.models.load_model('/home/aarav/Desktop/MajorProject/Models/m2.h5')
def predictSign(img):
hand=img
hand= cv2.resize(hand, (64,64))
img = image.img_to_array(hand)
img = np.expand_dims(img, axis = 0)
img.astype('float32')
img=img/255.0
pred= model.predict(img)
maxProb= max(pred[0])
if maxProb>0.8:
p=np.argmax(pred, axis=1)
return utils.getSign(p)
return -1
socketTimeOutEnable = False
displayWindows = True
recognitionMode="SIGN"
port = int(input("Enter port no: "))
# Reference: https://stackoverflow.com/a/24196955/5370202
print(ni.interfaces())
ni.ifaddresses('wlp1s0')
ipAddr = ni.ifaddresses('wlp1s0')[ni.AF_INET][0]['addr']
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("TCP Socket successfully created")
s.bind(('', port))
print("TCP Socket binded to %s: %s" %(ipAddr,port))
s.listen(1)
print("Socket is listening")
client, addr = s.accept()
print('Got TCP connection from', addr)
while True:
buf = client.recv(4)
# print(buf)
size = struct.unpack('!i', buf)[0]
#Reference: https://stackoverflow.com/a/37601966/5370202, https://docs.python.org/3/library/struct.html
# print(size)
print("receiving image of size: %s bytes" % size)
data = client.recv(size,socket.MSG_WAITALL) #Reference: https://www.binarytides.com/receive-full-data-with-the-recv-socket-function-in-python/
# Instead of storing the image as mentioned in the 1st reference: https://stackoverflow.com/a/23312964/5370202
# we can directly convert it to Opencv Mat format
# Reference: https://stackoverflow.com/a/17170855/5370202
nparr = np.fromstring(data, np.uint8)
img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
img_np = imutils.rotate_bound(img_np,90)
img_np = cv2.resize(img_np,(0,0), fx=0.7, fy=0.7)
mask1 = utils.segment(img_np)
gray = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
# detect faces in the grayscale image
haar_cascade_face = cv2.CascadeClassifier('/home/aarav/Desktop/MajorProject/Models/haarcascade_frontalface_default.xml')
rects = haar_cascade_face.detectMultiScale(gray, scaleFactor = 1.2, minNeighbors = 5);
maxArea1 = 0
faceRect = -1
foundFace = False
for (x,y,w,h) in rects:
if w*h > maxArea1:
maxArea1 = w*h
faceRect = (x,y,w,h)
foundFace = True
mask1 = FaceEliminator.eliminateFace(mask1, foundFace, faceRect)
if displayWindows:
cv2.imshow("Mask12",mask1)
if displayWindows:
cv2.imshow("Originl Img",img_np)
# contour of hand is useless delete krna h isko
handFound, hand, contours_of_hand = utils.get_my_hand(mask1)
if recognitionMode == "SIGN":
if handFound:
if displayWindows:
cv2.imshow("Your hand",hand)
pred = predictSign(hand)
else:
pred = -1
utils.addToQueue(pred)
pred = utils.getConsistentSign(displayWindows)
print("Stable Sign:",pred)
if pred == -1:
op1 = "--"+"\r\n"
else:
if pred == "2":
pred = "2 / v"
op1 = pred+"\r\n"
if recognitionMode =="SIGN":
client.send(op1.encode('ascii'))
k = cv2.waitKey(10)
if k == 'q':
break
print('Stopped TCP server of port: '+str(port))
print(recognitionMode+" recognition stopped")
s.close()
cv2.destroyAllWindows()