You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.8 KiB

6 years ago
  1. from flask import Flask, jsonify, request, abort,Response
  2. from multiprocessing import Process
  3. from PIL import Image
  4. from io import BytesIO
  5. from imutils.video import VideoStream
  6. import requests
  7. import cv2
  8. import pickle
  9. import base64
  10. import json
  11. from pyzbar import pyzbar
  12. import subprocess
  13. import urllib3
  14. import imutils
  15. urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
  16. app = Flask(__name__)
  17. user = {}
  18. image = None
  19. def im2str(im):
  20. crop_img = Image.fromarray(im, "RGB")
  21. buffered = BytesIO()
  22. crop_img.save(buffered, format="JPEG")
  23. img = base64.b64encode(buffered.getvalue()).decode("ascii")
  24. return img
  25. vs=VideoStream().start()
  26. barcodeData_prev = 0
  27. @app.route('/get')
  28. def get_qr():
  29. global user
  30. global image
  31. global barcodeData_prev
  32. frame = vs.read()
  33. img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  34. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  35. barcode = pyzbar.decode(gray)
  36. if len(barcode) > 0:
  37. barcode = barcode[0]
  38. (x, y, w, h) = barcode.rect
  39. barcodeData = barcode.data.decode("utf-8")
  40. barcodeType = barcode.type
  41. if barcodeData_prev == 0 or barcodeData_prev != barcodeData:
  42. barcodeData_prev = barcodeData
  43. r = requests.get('https://192.168.2.203:5000/users/{}'.format(barcodeData), verify=False)
  44. user = json.loads(r.text)
  45. text = "{} ({})".format(barcodeData, barcodeType)
  46. cv2.putText(img, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
  47. 0.5, (255, 0, 0), 2)
  48. cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
  49. else:
  50. user = {}
  51. result = subprocess.check_output(['vcgencmd', 'measure_temp'])
  52. temp = result.split("=")[1][:-2] +"C"
  53. image = im2str(img)
  54. resp = Response(json.dumps({"user":user,"img":image,"temp":temp}))
  55. resp.headers['Access-Control-Allow-Origin'] = '*'
  56. return resp
  57. app.run(host='0.0.0.0', port=3000)