Skip to content

Commit 7f48a1a

Browse files
committed
To finally have model running on device w/o opencl supporting.
1 parent 04a7446 commit 7f48a1a

File tree

4 files changed

+80
-8
lines changed

4 files changed

+80
-8
lines changed

src/embedding/FaceProcessing.py

+18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from scipy.misc import imread, imresize
2323
import sklearn.preprocessing
2424

25+
global mx
26+
mx = None
2527
global globGraph
2628
globGraph = None
2729
global mod
@@ -32,6 +34,7 @@
3234
DEBUG = False
3335

3436
DATA_RUNTIME_FOLDER = os.getenv('DATA_RUNTIME_FOLDER', '/data/runtime')
37+
HAS_OPENCL = os.getenv('HAS_OPENCL', 'true')
3538

3639
def load_graph(frozen_graph_filename):
3740
return None
@@ -108,6 +111,21 @@ def get_model(ctx, image_size, model_str, layer):
108111
def init_embedding_processor():
109112
global mod2
110113
global mod3
114+
115+
if HAS_OPENCL == 'false':
116+
global mx
117+
import mxnet as mx
118+
print('need init mxnet')
119+
120+
mod2 = None
121+
if os.path.isfile(DATA_RUNTIME_FOLDER+'/model-0000.params'):
122+
ctx = mx.cpu(0)
123+
mod3 = get_model(ctx, [112,112], DATA_RUNTIME_FOLDER+'/model,0', 'fc1')
124+
print('backup model loaded')
125+
return mod3
126+
else:
127+
print('has opencl supporting')
128+
111129
if os.path.isfile(DATA_RUNTIME_FOLDER+'/net2'):
112130
global __t
113131
global graph_runtime

src/embedding/embedding_client.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
import requests
3+
import json
4+
from faces import save_embedding
5+
6+
addr = 'http://localhost:6000/api/embedding'
7+
8+
def get_remote_embedding(embstr):
9+
# prepare headers for http request
10+
content_type = 'image/jpeg'
11+
headers = {'content-type': content_type}
12+
# send http request with image and receive response
13+
response = requests.post(addr, data=embstr, headers=headers)
14+
print(response)
15+
# decode response
16+
embedding = json.loads(response.text)['embedding']
17+
print(embedding)
18+
return save_embedding.convert_string_to_embedding(embedding)

src/embedding/embedding_server.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
import os
3+
from flask import Flask, request, jsonify
4+
import FaceProcessing
5+
from faces import save_embedding
6+
# Initialize the Flask application
7+
app = Flask(__name__)
8+
# route http posts to this method
9+
BASEDIR = os.getenv('RUNTIME_BASEDIR',os.path.abspath(os.path.dirname(__file__)))
10+
11+
@app.route('/api/embedding', methods=['POST'])
12+
def embedding():
13+
embedding = FaceProcessing.FaceProcessingBase64ImageData2(request.data)
14+
embedding_str = save_embedding.convert_embedding_to_string(embedding)
15+
print(embedding_str)
16+
return jsonify({'embedding':embedding_str}), 200
17+
if __name__ == '__main__':
18+
FaceProcessing.init_embedding_processor()
19+
print("start to warm up")
20+
embedding = FaceProcessing.FaceProcessingImageData2(os.path.join(BASEDIR,"image","Mike_Alden_0001_tmp.png"))
21+
print("warmed up")
22+
print(embedding)
23+
app.run(host="0.0.0.0", port=6000)

src/embedding/upload_api-v2.py

+21-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import division
44
from __future__ import print_function
55

6-
import os, json, time, sys, thread
6+
import os, json, time, sys, thread, base64
77
import argparse
88
import unicodedata
99
import shutil
@@ -130,6 +130,7 @@
130130
# run as worker only
131131
CLUSTER_WORKERONLY = os.getenv('CLUSTER_WORKERONLY', False)
132132

133+
HAS_OPENCL = os.getenv('HAS_OPENCL', 'true')
133134
SAVE_ORIGINAL_FACE = False
134135
original_face_img_path = os.path.join(BASEDIR, 'data', 'original_face_img')
135136
if not os.path.exists(original_face_img_path):
@@ -142,6 +143,10 @@
142143

143144
counter = 0
144145

146+
if HAS_OPENCL == 'false':
147+
from embedding_client import get_remote_embedding
148+
149+
145150
def featureCalculation(imgpath):
146151
img = misc.imread(os.path.expanduser(imgpath))
147152
prewhitened = facenet.prewhiten(img)
@@ -1623,7 +1628,12 @@ def getQueueName():
16231628

16241629
def featureCalculation2(imgpath):
16251630
embedding=None
1626-
embedding = FaceProcessing.FaceProcessingImageData2(imgpath)
1631+
if HAS_OPENCL == 'false':
1632+
with open(imgpath, "rb") as image_file:
1633+
encoded_string = base64.b64encode(image_file.read())
1634+
embedding = get_remote_embedding(encoded_string)
1635+
else:
1636+
embedding = FaceProcessing.FaceProcessingImageData2(imgpath)
16271637
return embedding
16281638

16291639
@worker_process_init.connect()
@@ -1638,11 +1648,11 @@ def setup(sender=None, **kwargs):
16381648
check_groupid_changed()
16391649
init_fs()
16401650

1641-
1642-
mod = FaceProcessing.init_embedding_processor()
1643-
print("start to warm up")
1644-
embedding = featureCalculation2(os.path.join(BASEDIR,"image","Mike_Alden_0001_tmp.png"))
1645-
print("warmed up")
1651+
if HAS_OPENCL == 'true':
1652+
mod = FaceProcessing.init_embedding_processor()
1653+
print("start to warm up")
1654+
embedding = featureCalculation2(os.path.join(BASEDIR,"image","Mike_Alden_0001_tmp.png"))
1655+
print("warmed up")
16461656
#if embedding is not None:
16471657
# print("worker embedding ready")
16481658

@@ -1672,7 +1682,10 @@ def extract_v2(image):
16721682

16731683
if current_groupid is None:
16741684
return json.dumps({"embedding_path":"","error":"please join group"})
1675-
embedding = FaceProcessing.FaceProcessingBase64ImageData2(imgstring)
1685+
if HAS_OPENCL == 'false':
1686+
embedding = get_remote_embedding(imgstring)
1687+
else:
1688+
embedding = FaceProcessing.FaceProcessingBase64ImageData2(imgstring)
16761689
embedding_path=''
16771690
embedding_str=''
16781691
if embedding is not None:

0 commit comments

Comments
 (0)