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.

111 lines
3.3 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. from imutils.video import VideoStream
  2. from pyzbar import pyzbar
  3. import imutils
  4. import time
  5. import cv2
  6. import database_read
  7. import datetime
  8. import generate_moves
  9. import serial
  10. arduino = serial.Serial('/dev/ttyACM0', 115200, timeout=.1)
  11. def move(locations):
  12. reps = 0
  13. for i in range(0,len(locations),2):
  14. print "Sending " + locations[i]
  15. while(arduino.read() != "1"):
  16. arduino.write(locations[i])
  17. time.sleep(0.25)
  18. arduino.flushInput()
  19. arduino.flushOutput()
  20. print "Sending " + locations[i+1]
  21. while(arduino.read() != '1'):
  22. arduino.write(locations[i+1])
  23. time.sleep(0.25)
  24. arduino.flushInput()
  25. arduino.flushOutput()
  26. arduino.write('-')
  27. print("[INFO] starting video stream...")
  28. def dateStr():
  29. date_cur = ""
  30. if len(str(datetime.date.today().day)) == 1:
  31. date_cur = date_cur + "0" + str(datetime.date.today().day) + "."
  32. else:
  33. date_cur = date_cur + str(datetime.date.today().day) + "."
  34. if len(str(datetime.date.today().month)) == 1:
  35. date_cur = date_cur + "0" + str(datetime.date.today().month) + "."
  36. else:
  37. date_cur = date_cur + str(datetime.date.today().month) + "."
  38. date_cur += str(datetime.date.today().year)
  39. return date_cur
  40. vs = VideoStream().start()
  41. time.sleep(2.0)
  42. dates = []
  43. reps = 0
  44. barcodes = None
  45. prevcode = None
  46. exp_date = []
  47. current_date = dateStr()
  48. def main():
  49. global vs
  50. global dates
  51. global reps
  52. global barcodes
  53. global prevcode
  54. global exp_date
  55. global current_date
  56. while True:
  57. try:
  58. while (barcodes == None or barcodes == []):
  59. frame = vs.read()
  60. frame = imutils.resize(frame, width=400)
  61. barcodes = pyzbar.decode(frame)
  62. cv2.imshow("Image", frame)
  63. if cv2.waitKey(1) & 0xFF == ord('q'):
  64. break
  65. try:
  66. a = arduino.read() == "1"
  67. if(a):
  68. return
  69. except:
  70. continue
  71. barcodes = pyzbar.decode(frame)
  72. # loop over the detected barcodes
  73. for barcode in barcodes:
  74. (x, y, w, h) = barcode.rect
  75. cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
  76. barcodeData = barcode.data.decode("utf-8")
  77. barcodeType = barcode.type
  78. if (barcodeData != prevcode):
  79. text = "{} ({})".format(barcodeData, barcodeType)
  80. cv2.putText(frame, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
  81. 0.5, (0, 0, 255), 2)
  82. print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))
  83. dates.append(database_read.getDate(barcodeData))
  84. reps += 1
  85. arduino.write("1")
  86. prevcode = barcodeData
  87. barcodes = None
  88. cv2.imshow("Image", frame)
  89. if cv2.waitKey(1) & 0xFF == ord('q'):
  90. break
  91. except KeyboardInterrupt:
  92. break
  93. main()
  94. print("[INFO] cleaning up...")
  95. arduino.write("1")
  96. moves = generate_moves.generate(dates,current_date)
  97. move(moves)
  98. cv2.destroyAllWindows()
  99. vs.stop()