-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathold_application.py
143 lines (106 loc) · 4.1 KB
/
old_application.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
#!/usr/bin/env python3
import os
import sys
import datetime
import cv2
import socket
import shutil
import nmap
import netifaces as ni
import src.rotateImages as rotate
# Project is built using Python 3.5+, please comply
if sys.version_info[0] < 3:
sys.exit('''Project is built using Python 3.5+\n'''
'''Please comply or this won't work properly'''
)
# Imports specific to Pi or Not Pi
compsystem = os.uname()
OnPi = compsystem.nodename == 'raspberrypi'
if OnPi:
from src.piCode.streamwrite import pi_client as pi
WIFI = 'wlan0'
else:
WIFI = None # wifi function won't work if not on Pi
from src.alt_trials import mapfaceEncodings as facecomp
from src.piCode.streamwrite import computer_server as comp
def runStuff(wifiAddress=None, writeImagePath=None, rot=False):
compsystem = os.uname()
if OnPi:
if wifiAddress is None:
wifiAddress = findIPaddress()
runPi(ipaddress=wifiAddress)
else:
runComp(writeImagePath=writeImagePath, rot=rot)
return compsystem.nodename
def runComp(writeImagePath=None, rot=False):
known = facecomp.readFaceEncodings(encode_path="./src/alt_trials/known_faces/")
# Make a socket connection that can be written to
server_socket = socket.socket()
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('0.0.0.0', 8000))
server_socket.listen(0)
print("Computer server running")
# Accept a single connection and make a file-like object out of it
conn, addr = server_socket.accept()
connect = conn.makefile('rb')
# Run as a server and allow for face images to be downloaded
images = comp.getImages(connect=connect)
connect.close()
# For our orientation during testing, images need to be corrected
# The face_recognition library can't see faces that aren't upright
if rot:
images = rotate.rotList(images)
# If we desire to write images to a file location, this writes those files
if not writeImagePath is None:
if not os.path.exists(writeImagePath):
os.makedirs(writeImagePath)
now = datetime.datetime.today().strftime('%Y-%m-%d_%H-%M-%S')
for i in range(len(images)):
cv2.imwrite(os.path.join(writeImagePath, "frame{}_{}.jpg".format(i, now)), images[i])
# get image features (of the largest face)
img_feats = facecomp.turnImagesToFeats(images)
# find if it compares to any of the
res = facecomp.compareListToKnown(img_feats, compknown=known)
# Connect to the pi again
connect = conn.makefile('wb')
# Send results to Pi
comp.sendResult(res, connect=connect, ipaddress='0.0.0.0', port='8000')
connect.close()
# print(res)
conn.close()
server_socket.close()
def runPi(ipaddress='10.3.141.198', port=8000):
print("Pi Pie Phi guy running")
command = ''
while command != 'quit':
try:
client_socket = socket.socket()
client_socket.connect((ipaddress, port))
conn = client_socket.makefile('wb')
pi.runConnect(connect=conn, ipaddress=ipaddress, port=8000)
conn.close()
conn = client_socket.makefile('rb')
print(pi.runRead(connect=conn, ipaddress=ipaddress, port=8000))
conn.close()
except Exception as e:
print(e)
finally:
command = input("Type 'quit' to exit this process\n")
client_socket.close()
def findIPaddress():
if not WIFI is None:
ip = ni.ifaddresses(WIFI)[ni.AF_INET][0]['addr']
nm = nmap.PortScanner()
nm.scan(ip+"/24")
hosts = nm.all_hosts()
print(hosts)
while ip in hosts:
hosts.remove(ip)
print(hosts)
if len(hosts):
return hosts[0] # No good way to tell if > 1... cross fingers
# If here, no idea what to do, default to Ian's Ubuntu?
return '10.3.141.198'
if __name__ == "__main__":
# print(runStuff(wifiAddress='10.3.141.198', rot=True))
print(runStuff(wifiAddress='10.186.133.159', rot=True))