/
php2python.py
56 lines (45 loc) · 1.85 KB
/
php2python.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
import sys
from jaweson import json
from PIL import Image
from pylab import *
import pika
import uuid
class RpcSender(object):
def __init__(self):
credentials = pika.PlainCredentials('guest', 'guest')
self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', credentials))
self.channel = self.connection.channel()
result = self.channel.queue_declare(exclusive=True)
self.callback_queue = result.method.queue
self.channel.basic_consume(self.on_response, no_ack=True,
queue=self.callback_queue)
def on_response(self, ch, method, props, body):
if self.corr_id == props.correlation_id:
self.response = body
def call(self, n):
self.response = None
self.corr_id = str(uuid.uuid4())
self.channel.basic_publish(exchange='',
routing_key='_argus_queue',
properties=pika.BasicProperties(
reply_to = self.callback_queue,
correlation_id = self.corr_id,
),
body=n)
while self.response is None:
self.connection.process_data_events()
return self.response
def init():
#print sys.argv[1]
inputFromPhp = json.loads(sys.argv[1])
file_path = inputFromPhp['image_file_path']
operation = inputFromPhp['operation']
data = inputFromPhp['data']
sender = RpcSender()
im = array(Image.open(file_path))
msg = {"image_path" : file_path, "image" : im, "operation" : operation, "model_parameters" : data}
json_msg = json.dumps(msg)
response = sender.call(json_msg)
print response
if __name__ == "__main__":
init()