Skip to content

Commit

Permalink
remote working
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-cranfill committed Sep 26, 2017
1 parent 1c2d680 commit 7d051c5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
3 changes: 2 additions & 1 deletion cena/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@
RYAN_FILE_NAME = 'dun_dun_dun.mp3'
RYAN_SONG_PATH = os.path.join(SONGS_DIR, RYAN_FILE_NAME)

SERVER_URL = 'http://localhost:5000/recognize'
# SERVER_URL = 'http://localhost:5000/recognize'
SERVER_URL = 'http://52.207.182.58:5000/recognize'
12 changes: 12 additions & 0 deletions cena/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import numpy as np
import base64


def encode_image(image):
image = image.astype(np.uint8)
return base64.b64encode(image.tobytes()).decode('utf-8')


def decode_image(encoded_str, shape):
decoded_arr = np.fromstring(base64.b64decode(encoded_str), dtype=np.uint8)
return decoded_arr.reshape(shape)
25 changes: 18 additions & 7 deletions face_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from cena.recognition import FaceRecognizer
from cena.settings import RYAN_SONG_PATH, DEV, CASCADE_FILE_PATH, SERVER_URL
from cena.utils import encode_image, decode_image


def play_mp3(path):
Expand All @@ -19,8 +20,19 @@ def listen_for_quit():


def get_server_response(frame, list_o_faces):
response = post(SERVER_URL, json={'list_o_faces': list_o_faces, 'frame': frame.tolist()}) # , files=files)
return response.json()['frame'], response.json()['people_list'], response.json()['time']
# response = post(SERVER_URL, json={'list_o_faces': list_o_faces, 'frame': frame.tolist()}) # , files=files)
shape = frame.shape
request_json = {
'list_o_faces': list_o_faces,
'frame': encode_image(frame),
'shape': shape
}
response = post(SERVER_URL, json=request_json)
frame = decode_image(response.json()['frame'], shape)
people_list = response.json()['people_list']
time = response.json()['time']
return frame, people_list, time
# return response.json()['frame'], response.json()['people_list'], response.json()['time']


def process_frame(video_capture, face_recognizer=None):
Expand All @@ -37,7 +49,6 @@ def process_frame(video_capture, face_recognizer=None):
minSize=(80, 80),
flags=cv2.CASCADE_SCALE_IMAGE
)

if len(faces) > 0:
# RYAN_PLAYED = play_mp3(RYAN_SONG_PATH, RYAN_PLAYED)
list_o_faces = []
Expand All @@ -46,12 +57,12 @@ def process_frame(video_capture, face_recognizer=None):
if DEV:
# frame, people_list, time = face_recognizer.recognize_faces(frame, list_o_faces)
frame, people_list, time = get_server_response(frame, list_o_faces)
frame = np.array(frame)
frame = frame.astype('uint8')
# frame = np.array(frame)
# frame = frame.astype('uint8')
else:
frame, people_list, time = get_server_response(frame, list_o_faces)
frame = np.array(frame)
frame = frame.astype('uint8')
# frame = np.array(frame)
# frame = frame.astype('uint8')
# play_mp3(RYAN_SONG_PATH)
print(people_list, datetime.now() - now)
else:
Expand Down
20 changes: 16 additions & 4 deletions feature_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from scipy.misc import toimage

from cena.recognition import FaceRecognizer
from cena.utils import decode_image, encode_image

RECOGNIZER = FaceRecognizer()
app = Flask(__name__)
Expand All @@ -17,23 +18,34 @@ def recognize():
abort(400)
if 'list_o_faces' not in request.json:
abort(400)
if 'shape' not in request.json:
abort(400)

# frame = cv2.imdecode(np.array(request.json['frame']), None)
frame = np.array(request.json['frame'])
frame = frame.astype('uint8')
# frame = np.array(request.json['frame'])
# frame = frame.astype('uint8')
# frame = toimage(np.array(request.json['frame']))
# frame = cv2.(np.array(request.json['frame']))
# print(frame[0][0].dtype)
encoded_frame = request.json['frame']
shape = request.json['shape']
frame = decode_image(encoded_frame, shape)
list_o_faces = request.json['list_o_faces']

# print(type(list_o_faces[0][0]))

frame, people_list, time = RECOGNIZER.recognize_faces(frame, list_o_faces)
# response = {
# 'people_list': people_list,
# 'frame': frame.tolist(),
# 'time': time
# }
response = {
'people_list': people_list,
'frame': frame.tolist(),
'frame': encode_image(frame),
'time': time
}
return jsonify(response)

if __name__ == '__main__':
app.run(debug=True)
app.run(host='0.0.0.0', debug=True)

0 comments on commit 7d051c5

Please sign in to comment.