Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
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.
 
 
 
 
 
 

127 lines
2.7 KiB

  1. import cv2
  2. import numpy as np
  3. import sys
  4. winname = 'Diff'
  5. def tovalues(values, histo):
  6. return [ 0 if x < histo[1][1] else 2 if x > histo[1][2] else 1 for x in values ]
  7. def frameiter(fname):
  8. #vc = cv2.VideoCapture('output.mp4')
  9. #vc = cv2.VideoCapture('PXL_20220806_231420638.mp4')
  10. #vc = cv2.VideoCapture('PXL_20220806_231644224.mp4')
  11. #vc = cv2.VideoCapture('PXL_20220808_233318199.mp4')
  12. vc = cv2.VideoCapture('PXL_20220809_162724414.mp4')
  13. while True:
  14. r, frame = vc.read()
  15. if not r:
  16. break
  17. # base image processing
  18. frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY, 1)
  19. yield frame
  20. def pixtot(frame):
  21. colsum = np.sum(frame, 0, np.float64)
  22. colsum = np.sum(colsum, 0, np.float64)
  23. return colsum
  24. def numpixels(frame):
  25. return pixtot(frame) / 255
  26. sumhist = []
  27. cont = False
  28. #cont = True
  29. key = 0
  30. valuehist = []
  31. basefr = None
  32. for frame in frameiter('output.mp4'):
  33. if basefr is None:
  34. basefr = frame
  35. continue
  36. frdiff = cv2.absdiff(basefr, frame)
  37. #hist = np.histogram(frdiff, bins=5)
  38. #print(repr(hist))
  39. #frdiff = frame - basefr
  40. #frdiffbin = cv2.threshold(frdiff, 50, 100, cv2.THRESH_BINARY)
  41. frdiffbin = cv2.compare(frdiff, 50, cv2.CMP_GE)
  42. kernel = np.ones((4,4), np.uint8)
  43. frdiffbin = cv2.erode(frdiffbin, kernel, iterations = 2)
  44. numpix = numpixels(frdiffbin)
  45. print('np:', repr(numpix))
  46. if numpix:
  47. # detected change
  48. # get average pixel value
  49. selected = cv2.bitwise_and(frame, frdiffbin)
  50. avg = pixtot(selected) / numpix
  51. print('avg:', avg)
  52. valuehist.append(avg)
  53. cv2.imshow('selected', selected)
  54. cv2.moveWindow('Base Frame', 1200, 300)
  55. if len(valuehist) > 6:
  56. print(repr(sorted(valuehist)))
  57. hist = np.histogram(valuehist, bins=3)
  58. print('hist:', repr(hist))
  59. #print(repr(frdiffbin))
  60. #moments = cv2.moments(frdiffbin)
  61. #((x, y), radius) = cv2.minEnclosingCircle(conts[0][0])
  62. #x = int(x)
  63. #y = int(y)
  64. #radius = int(radius)
  65. #circ = ((x, y), radius)
  66. #white = (255,)
  67. #print(repr(moments))
  68. #frdiff = cv2.circle(frdiff, circ[0], circ[1], white)
  69. cv2.imshow('Base Frame', basefr)
  70. cv2.moveWindow('Base Frame', 300, 300)
  71. cv2.imshow('Diff', frdiffbin)
  72. cv2.moveWindow('Diff', 600, 300)
  73. cv2.imshow('Next Frame', frame)
  74. cv2.moveWindow('Next Frame', 900, 300)
  75. # sum up the diffs
  76. colsum = np.sum(frdiff, 0, np.float64)
  77. colsum = np.sum(colsum, 0, np.float64)
  78. colsum = np.sum(colsum, 0, np.float64)
  79. sumhist.append(colsum)
  80. print(repr(colsum))
  81. if not cont:
  82. key = cv2.waitKey(0)
  83. if key == ord('q'):
  84. break
  85. elif key == ord('c'):
  86. cont = True
  87. basefr = frame
  88. print(len(sumhist))
  89. hist = np.histogram(sumhist, bins=5)
  90. print(repr(hist))
  91. print(len(hist))
  92. print(len(hist[1]))
  93. print(repr(valuehist))
  94. hist = np.histogram(valuehist, bins=3)
  95. print(repr(hist))
  96. print(repr(tovalues(valuehist, hist)))