• As we can see that the simeple REST api like this

Client

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
def age_gender_predict(image_np):

data = {"success": False}

orig_height, orig_width, _ = image_np.shape
'''
# 1. Compose and send the request to db
'''
#image_np = preprocess_image_pd(image_np)
#image_np = image_np.copy(order="C")
image_id = str(uuid.uuid4())
input_data = {'id': image_id, 'image': helpers.base64_encode_image(image_np)}

db.rpush(settings.IMAGE_QUEUE, json.dumps(input_data))

'''
# 2. Loop the db to get the response
'''
while True:
output_data = db.get(image_id)
if output_data is not None:
output_data = output_data.decode("utf-8")
output_data = json.loads(output_data)
output_data['out_boxes'] = np.array(output_data['out_boxes']).astype('float32')
output_data['out_ages'] = np.array(output_data['out_ages']).astype('int64')
output_data['out_genders'] = np.array(output_data['out_genders'])
data["predictions"] = output_data

db.delete(image_id)
break

time.sleep(settings.CLIENT_SLEEP)

data["success"] = True

return data

Server

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
'''
### Par-2. 模型服务主要函数
'''
def detect_process():
'''
#1. 加载模型
'''
logfile.addinfolog("Loading model...")
model = FaceCV(depth=settings.NETWORK_DEPTH, width=settings.NETWORK_WIDTH)
logfile.addinfolog("Model Loaded")

'''
#2. 循环,从Redis获取请求数据,模型预测,返回结果到Redis
'''
while True:
'''
# 从Redis数据库,获取一张图片
'''
age_gender_request_list = db.lrange(settings.IMAGE_QUEUE, 0, 0)

imageID = None
imageNP = None
for age_gender_request in age_gender_request_list:
age_gender_request = json.loads(age_gender_request.decode("utf-8"))
imageID = age_gender_request['id']
imageNP = base64_decode_image(age_gender_request['image'], settings.IMAGE_DTYPE,
(settings.IMAGE_HEIGHT, settings.IMAGE_WIDTH,
settings.IMAGE_CHANS))
print(imageNP.shape)

'''
# 模型处理
'''
if imageNP is not None:
logfile.addinfolog("Start Process Request")
out_boxes, out_ages, out_genders = model.detect_face_one_image(imageNP)
logfile.addinfolog("End Process Request")
predicted_result = {
'out_boxes': np.array(out_boxes).astype(str).tolist(),
'out_ages': np.array(out_ages).astype(str).tolist(),
'out_genders': out_genders
}

db.set(imageID, json.dumps(predicted_result))
db.ltrim(settings.IMAGE_QUEUE, 1, -1)

# 睡眠
time.sleep(settings.SERVER_SLEEP)

Test

1
2
3
4
cd /path/age_gender_pred
python run_age_gender_rf_service.py

python age_gender_predict_client.py