get_video_info.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import os
  2. import sys
  3. import cv2
  4. import numpy as np
  5. import argparse
  6. def argsparser():
  7. parser = argparse.ArgumentParser(description=__doc__)
  8. parser.add_argument(
  9. "--video_file",
  10. type=str,
  11. default=None,
  12. help="Path of video file, `video_file` or `camera_id` has a highest priority."
  13. )
  14. parser.add_argument(
  15. '--region_polygon',
  16. nargs='+',
  17. type=int,
  18. default=[],
  19. help="Clockwise point coords (x0,y0,x1,y1...) of polygon of area when "
  20. "do_break_in_counting. Note that only support single-class MOT and "
  21. "the video should be taken by a static camera.")
  22. return parser
  23. def get_video_info(video_file, region_polygon):
  24. entrance = []
  25. assert len(region_polygon
  26. ) % 2 == 0, "region_polygon should be pairs of coords points."
  27. for i in range(0, len(region_polygon), 2):
  28. entrance.append([region_polygon[i], region_polygon[i + 1]])
  29. if not os.path.exists(video_file):
  30. print("video path '{}' not exists".format(video_file))
  31. sys.exit(-1)
  32. capture = cv2.VideoCapture(video_file)
  33. width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
  34. height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
  35. print("video width: %d, height: %d" % (width, height))
  36. np_masks = np.zeros((height, width, 1), np.uint8)
  37. entrance = np.array(entrance)
  38. cv2.fillPoly(np_masks, [entrance], 255)
  39. fps = int(capture.get(cv2.CAP_PROP_FPS))
  40. frame_count = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
  41. print("video fps: %d, frame_count: %d" % (fps, frame_count))
  42. cnt = 0
  43. while (1):
  44. ret, frame = capture.read()
  45. cnt += 1
  46. if cnt == 3: break
  47. alpha = 0.3
  48. img = np.array(frame).astype('float32')
  49. mask = np_masks[:, :, 0]
  50. color_mask = [0, 0, 255]
  51. idx = np.nonzero(mask)
  52. color_mask = np.array(color_mask)
  53. img[idx[0], idx[1], :] *= 1.0 - alpha
  54. img[idx[0], idx[1], :] += alpha * color_mask
  55. cv2.imwrite('region_vis.jpg', img)
  56. if __name__ == "__main__":
  57. parser = argsparser()
  58. FLAGS = parser.parse_args()
  59. get_video_info(FLAGS.video_file, FLAGS.region_polygon)
  60. # python get_video_info.py --video_file=demo.mp4 --region_polygon 200 200 400 200 300 400 100 400