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.

56 lines
1.8 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. vs = VideoStream().start()
  7. dates = []
  8. reps = 0
  9. barcodes = None
  10. prevcode = None
  11. def main(host):
  12. global vs
  13. global dates
  14. global reps
  15. global barcodes
  16. global prevcode
  17. while True:
  18. try:
  19. while (barcodes == None or barcodes == []):
  20. frame = vs.read()
  21. frame = imutils.resize(frame, width=400)
  22. barcodes = pyzbar.decode(frame)
  23. cv2.imshow("Image", frame)
  24. if cv2.waitKey(1) & 0xFF == ord('q'):
  25. break
  26. barcodes = pyzbar.decode(frame)
  27. # loop over the detected barcodes
  28. for barcode in barcodes:
  29. (x, y, w, h) = barcode.rect
  30. cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
  31. barcodeData = barcode.data.decode("utf-8")
  32. barcodeType = barcode.type
  33. if (barcodeData != prevcode):
  34. text = "{} ({})".format(barcodeData, barcodeType)
  35. cv2.putText(frame, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
  36. 0.5, (0, 0, 255), 2)
  37. print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))
  38. requests.post('https://' + host + '/reduce', data={'id': barcodeData, 'reduce': 2.5})
  39. reps += 1
  40. prevcode = barcodeData
  41. barcodes = None
  42. cv2.imshow("Image", frame)
  43. if cv2.waitKey(1) & 0xFF == ord('q'):
  44. break
  45. except KeyboardInterrupt:
  46. break
  47. main(input('Host << '))
  48. print("[INFO] cleaning up...")
  49. cv2.destroyAllWindows()
  50. vs.stop()