app.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import cv2
  2. import gradio as gr
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. # input_video = 'car.mp4'
  6. # video Inference
  7. def vid_inf(vid_path):
  8. # Create a VideoCapture object
  9. cap = cv2.VideoCapture(vid_path)
  10. # get the video frames' width and height for proper saving of videos
  11. frame_width = int(cap.get(3))
  12. frame_height = int(cap.get(4))
  13. fps = int(cap.get(cv2.CAP_PROP_FPS))
  14. frame_size = (frame_width, frame_height)
  15. fourcc = cv2.VideoWriter_fourcc(*'mp4v')
  16. output_video = "output_recorded.mp4"
  17. # create the `VideoWriter()` object
  18. out = cv2.VideoWriter(output_video, fourcc, fps, frame_size)
  19. # Create Background Subtractor MOG2 object
  20. backSub = cv2.createBackgroundSubtractorMOG2()
  21. # Check if camera opened successfully
  22. if not cap.isOpened():
  23. print("Error opening video file")
  24. count = 0
  25. # Read until video is completed
  26. while cap.isOpened():
  27. # Capture frame-by-frame
  28. ret, frame = cap.read()
  29. # print(frame.shape)
  30. if ret:
  31. # Apply background subtraction
  32. fg_mask = backSub.apply(frame)
  33. # print(fg_mask.shape)
  34. # cv2.imshow('Frame_bg', fg_mask)
  35. # apply global threshol to remove shadows
  36. retval, mask_thresh = cv2.threshold(
  37. fg_mask, 180, 255, cv2.THRESH_BINARY)
  38. # cv2.imshow('frame_thresh', mask_thresh)
  39. # set the kernal
  40. kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
  41. # Apply erosion
  42. mask_eroded = cv2.morphologyEx(mask_thresh, cv2.MORPH_OPEN, kernel)
  43. # cv2.imshow('frame_erode', mask_eroded)
  44. # Find contours
  45. contours, hierarchy = cv2.findContours(
  46. mask_eroded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  47. # print(contours)
  48. min_contour_area = 2000 # Define your minimum area threshold
  49. large_contours = [
  50. cnt for cnt in contours if cv2.contourArea(cnt) > min_contour_area]
  51. # frame_ct = cv2.drawContours(frame, large_contours, -1, (0, 255, 0), 2)
  52. frame_out = frame.copy()
  53. for cnt in large_contours:
  54. # print(cnt.shape)
  55. x, y, w, h = cv2.boundingRect(cnt)
  56. frame_out = cv2.rectangle(
  57. frame, (x, y), (x+w, y+h), (0, 0, 200), 3)
  58. frame_out_display = cv2.cvtColor(frame_out, cv2.COLOR_BGR2RGB)
  59. vid = out.write(frame_out)
  60. # Display the resulting frame
  61. # cv2.imshow('Frame_final', frame_out)
  62. # update the count every frame and display every 12th frame
  63. if not count % 12:
  64. yield frame_out_display, None
  65. count += 1
  66. # Press Q on keyboard to exit
  67. if cv2.waitKey(25) & 0xFF == ord('q'):
  68. break
  69. else:
  70. break
  71. # When everything done, release the video capture and writer object
  72. cap.release()
  73. out.release()
  74. # Closes all the frames
  75. cv2.destroyAllWindows()
  76. yield None, output_video
  77. # vid_inf(input_video)
  78. # gradio interface
  79. input_video = gr.Video(label="Input Video")
  80. output_frames = gr.Image(label="Output Frames")
  81. output_video_file = gr.Video(label="Output video")
  82. # sample_video=r'sample/car.mp4'
  83. app = gr.Interface(
  84. fn=vid_inf,
  85. inputs=[input_video],
  86. outputs=[output_frames, output_video_file],
  87. title=f"MotionScope",
  88. description=f'A gradio app for dynamic video analysis tool that leverages advanced background subtraction and contour detection techniques to identify and track moving objects in real-time.',
  89. allow_flagging="never",
  90. examples=[["sample/car.mp4"]],
  91. )
  92. app.queue().launch()