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.

145 lines
3.9 KiB

  1. #!/usr/bin/python3
  2. import cv2
  3. import sys
  4. STATIC_THRESHOLD_MSECS = 3000
  5. UI = False
  6. cap = cv2.VideoCapture(sys.argv[1])
  7. if len(sys.argv) > 5:
  8. if len(sys.argv) > 6:
  9. STATIC_THRESHOLD_MSECS = int(sys.argv[6])
  10. (x, y, w, h) = map(int, sys.argv[2:6])
  11. def crop(frame):
  12. global x, y, w, h
  13. # from https://stackoverflow.com/a/40296783
  14. return frame[y:y+h, x:x+w]
  15. else:
  16. def crop(frame):
  17. return frame
  18. frame = None
  19. gray = None
  20. anchor_frame = None
  21. anchor_gray = None
  22. anchor_frame_num = None
  23. anchor_time = None
  24. def msec_to_human_readable(time):
  25. sec = time / 1000
  26. return '%d:%02d' % (sec / 60, sec % 60)
  27. while(True):
  28. ret, frame = cap.read()
  29. if frame is None:
  30. break
  31. frame = crop(frame)
  32. if UI:
  33. cv2.imshow('window', frame)
  34. elif (cap.get(cv2.CAP_PROP_POS_FRAMES) % 2000) == 0:
  35. time_str = msec_to_human_readable(cap.get(cv2.CAP_PROP_POS_MSEC))
  36. print("Current time: %s" % time_str)
  37. gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  38. gray = cv2.GaussianBlur(gray, (21, 21), 0)
  39. def set_anchor():
  40. global anchor_frame, anchor_gray, anchor_frame_num, anchor_time
  41. anchor_frame = frame
  42. anchor_gray = gray
  43. anchor_frame_num = cap.get(cv2.CAP_PROP_POS_FRAMES)
  44. anchor_time = cap.get(cv2.CAP_PROP_POS_MSEC)
  45. if UI:
  46. cv2.imshow('anchor', frame)
  47. if anchor_frame is None:
  48. set_anchor()
  49. deltaframe = cv2.absdiff(anchor_gray, gray)
  50. if UI:
  51. cv2.imshow('delta', deltaframe)
  52. threshold = cv2.threshold(deltaframe, 10, 255, cv2.THRESH_BINARY)[1]
  53. threshold = cv2.dilate(threshold, None)
  54. if UI:
  55. cv2.imshow('threshold', threshold)
  56. if threshold.any():
  57. time = cap.get(cv2.CAP_PROP_POS_MSEC)
  58. if time - anchor_time > STATIC_THRESHOLD_MSECS:
  59. time_str = msec_to_human_readable(anchor_time)
  60. print("Static frame at time %s for %f seconds"
  61. % (time_str, (time-anchor_time) / 1000.0))
  62. if UI:
  63. cv2.imshow('static', anchor_frame)
  64. cv2.imwrite('static_at_%s.jpg' % time_str, anchor_frame)
  65. set_anchor()
  66. if UI:
  67. key = cv2.waitKey(20)
  68. if key == ord('q'):
  69. break
  70. # Move in video
  71. elif key == ord('z'):
  72. cap.set(cv2.CAP_PROP_POS_FRAMES,
  73. cap.get(cv2.CAP_PROP_POS_FRAMES) - 100)
  74. anchor_frame = None
  75. elif key == ord('x'):
  76. cap.set(cv2.CAP_PROP_POS_FRAMES,
  77. cap.get(cv2.CAP_PROP_POS_FRAMES) + 100)
  78. anchor_frame = None
  79. # Adjust crop
  80. elif key == ord('w'):
  81. y -= 10
  82. print('y=%d' % y)
  83. elif key == ord('W'):
  84. y -= 1
  85. print('y=%d' % y)
  86. elif key == ord('a'):
  87. x -= 10
  88. print('x=%d' % x)
  89. elif key == ord('A'):
  90. x -= 1
  91. print('x=%d' % x)
  92. elif key == ord('s'):
  93. y += 10
  94. print('y=%d' % y)
  95. elif key == ord('S'):
  96. y += 1
  97. print('y=%d' % y)
  98. elif key == ord('d'):
  99. x += 10
  100. print('x=%d' % x)
  101. elif key == ord('D'):
  102. x += 1
  103. print('x=%d' % x)
  104. elif key == ord('i'):
  105. h -= 10
  106. print('h=%d' % h)
  107. elif key == ord('I'):
  108. h -= 1
  109. print('h=%d' % h)
  110. elif key == ord('j'):
  111. w -= 10
  112. print('w=%d' % w)
  113. elif key == ord('J'):
  114. w -= 1
  115. print('w=%d' % w)
  116. elif key == ord('k'):
  117. h += 10
  118. print('h=%d' % h)
  119. elif key == ord('K'):
  120. h += 1
  121. print('h=%d' % h)
  122. elif key == ord('l'):
  123. w += 10
  124. print('w=%d' % w)
  125. elif key == ord('L'):
  126. w += 1
  127. print('w=%d' % w)
  128. cap.release()
  129. cv2.destroyAllWindows()