Skip to content

Instantly share code, notes, and snippets.

@kylehounslow
Last active April 23, 2024 10:58
Show Gist options
  • Save kylehounslow/767fb72fde2ebdd010a0bf4242371594 to your computer and use it in GitHub Desktop.
Save kylehounslow/767fb72fde2ebdd010a0bf4242371594 to your computer and use it in GitHub Desktop.
Send and receive images using Flask, Numpy and OpenCV
from __future__ import print_function
import requests
import json
import cv2
addr = 'http://localhost:5000'
test_url = addr + '/api/test'
# prepare headers for http request
content_type = 'image/jpeg'
headers = {'content-type': content_type}
img = cv2.imread('lena.jpg')
# encode image as jpeg
_, img_encoded = cv2.imencode('.jpg', img)
# send http request with image and receive response
response = requests.post(test_url, data=img_encoded.tostring(), headers=headers)
# decode response
print(json.loads(response.text))
# expected output: {u'message': u'image received. size=124x124'}
from flask import Flask, request, Response
import jsonpickle
import numpy as np
import cv2
# Initialize the Flask application
app = Flask(__name__)
# route http posts to this method
@app.route('/api/test', methods=['POST'])
def test():
r = request
# convert string of image data to uint8
nparr = np.fromstring(r.data, np.uint8)
# decode image
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
# do some fancy processing here....
# build a response dict to send back to client
response = {'message': 'image received. size={}x{}'.format(img.shape[1], img.shape[0])
}
# encode response using jsonpickle
response_pickled = jsonpickle.encode(response)
return Response(response=response_pickled, status=200, mimetype="application/json")
# start flask app
app.run(host="0.0.0.0", port=5000)
@Arnold-git
Copy link

Arnold-git commented Mar 15, 2022

Anyone that wants to get image from client and store in buffer can use this snippet below for flask

       ```
        original_image = request.files.get('original image')
        compare_image = request.files.get('compare image')
        original_img = cv2.imdecode(np.frombuffer(original_image.read(), np.uint8), cv2.IMREAD_COLOR)
        compare_img = cv2.imdecode(np.frombuffer(compare_image.read(), np.uint8), cv2.IMREAD_COLOR)
        known_encoding = face_recognition.face_encodings(original_img)[0]
        unknown_encoding = face_recognition.face_encodings(compare_img)[0]

        results = face_recognition.compare_faces([known_encoding], unknown_encoding)

@kulkarnikeerti
Copy link

@kylehounslow Thanks for the this understandable scripts.
I would like to pass some more additional information along with image to server. Something like below

meta = {'camera': 'camera', 'distance': 'distance'}
response = requests.post(test_url, data=img_encoded.tostring(), headers=headers, json=meta)

And I access this meta information on the server as input_json = r.get_json(force=True)

But this results in error in client side as below.

Traceback (most recent call last):
  File "/home/keerti/PycharmProjects/Flask_Api/client.py", line 21, in <module>
    print(json.loads(response.text))
  File "/usr/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.10/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Could you please help me with this? If this is the right way to pass additional information

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment