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
2.1 KiB

6 years ago
  1. from imutils.video import VideoStream
  2. from pyzbar import pyzbar
  3. import requests
  4. import imutils
  5. import cv2
  6. import urllib3
  7. import socket
  8. import struct
  9. import zlib
  10. import pickle
  11. vs = VideoStream().start()
  12. dates = []
  13. reps = 0
  14. barcodes = None
  15. prevcode = None
  16. host = '10.10.26.141'
  17. client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  18. client_socket.connect((host, 8485))
  19. connection = client_socket.makefile('wb')
  20. def main():
  21. global vs
  22. global dates
  23. global reps
  24. global barcodes
  25. global prevcode
  26. while True:
  27. try:
  28. while (barcodes == None or barcodes == []):
  29. frame = vs.read()
  30. frame = imutils.resize(frame, width=400)
  31. barcodes = pyzbar.decode(frame)
  32. res, frm = cv2.imencode('.jpg', frame, [int(cv2.IMWRITE_JPEG_QUALITY), 90])
  33. send_data = zlib.compress(pickle.dumps(frm, 0))
  34. size = len(send_data)
  35. client_socket.sendall(struct.pack(">L", size) + data)
  36. barcodes = pyzbar.decode(frame)
  37. # loop over the detected barcodes
  38. for barcode in barcodes:
  39. (x, y, w, h) = barcode.rect
  40. cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
  41. barcodeData = barcode.data.decode("utf-8")
  42. barcodeType = barcode.type
  43. if (barcodeData != prevcode):
  44. text = "{} ({})".format(barcodeData, barcodeType)
  45. cv2.putText(frame, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
  46. 0.5, (0, 0, 255), 2)
  47. print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))
  48. requests.post('https://' + host + ':5000/reduce', data={'id': barcodeData, 'reduce': 5}, verify=False)
  49. reps += 1
  50. prevcode = barcodeData
  51. barcodes = None
  52. except KeyboardInterrupt:
  53. break
  54. urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
  55. main()
  56. print("[INFO] cleaning up...")
  57. cv2.destroyAllWindows()
  58. vs.stop()