python - POST request results in "internal server error", most of the times but not all (!?) -
edit:
tl;dr:
how post image using python request flask app can use opencv manipulate it?
long version of question , stuff i've tried out:
i trying post image 1 python flask app another. problem facing seems work sometimes, not.
app receives data:
@app.route("/post", methods=['get', 'post']) def post(): print "in post" req = request.files['file'] # req = open('pug.jpg', 'r+b') arr = np.asarray(bytearray(req.read()), dtype=np.uint8) img = cv2.imdecode(arr,-1) # 'load is' print img cv2.imwrite('result.png',img) return "hello"
app sends data:
def hello(): url = 'http://ip_address/post' #this image not work image = open('path/to/image/pug.jpg', 'r+b') #but image work!? #image = open('path/to/image/res_tile.png', 'r+b') files = {'file': image} r = requests.post(url, files=files) image.close() return 'hello world!'
what have tried:
- first, thought there wrong specific image have tried bunch.
- i have eliminated post , put in 1 module. works.
so, there has wrong in way doing post; works other image. strange.
any appreciated!
edit: ive tried couple of additional ways, both in receiving , sending of post request still no luck. ive found more images work , more don't work. puzzling , irritating. see below more code.
app receives data:
def post(): print "in post" photo = request.files['file'] in_memory_file = io.bytesio() photo.save(in_memory_file) data = np.fromstring(in_memory_file.getvalue(), dtype=np.uint8) color_image_flag = 1 img = cv2.imdecode(data, color_image_flag) print img cv2.imwrite('result.png',img) return "hello"
app sends data:
def hello(): url = 'http://ipaddress/post' image = open('../path/to/image/pug2.png', 'r+b') files = {'file': ('hej.png', image, 'image/png')} r = requests.post(url, files=files, stream=true) image.close() print r.content return 'hello world!'
still no luck.
edit:
now i've tried "multipartencoder" requests_toolbelt , im still facing same issue. on images works. on others not. out there have idea im doing wrong?
app sends request:
def hello(): m = multipartencoder( fields={'field0': 'value', 'field1': 'value', 'field2': ('filename', open('../path/to/img', 'rb'), 'image/png')} ) print m.fields['field0'] r = requests.post('http://192.168.0.3/post', data=m, headers={'content-type': m.content_type})
app receives request same before, changed access correct field file (with "req = request.files['field2']")
any tips/hits/ideas welcome!
its "solved". or rather, found workaround.
both apps (both receiving , sending image) running locally on machine. pushing receiving app aws , keeping sending app on machine seams have solved issue (at least can see in initial tests). wanted have both locally easier develop seams need have 1 app running non-locally.
if of have idea why works...im ears.
Comments
Post a Comment