how to draw a shape on top of a playing video by clicking a mouse button in opencv python

Viewed 938

Well, to begin with, I should admit that it is a pretty long question and I failed to find possible solutions through googling

I have a video in which an intruder tries to intrude into the other side of the fence.

enter image description here

I can track the intruder, but when he is in the other side, I should be able to save the intrusion duration into a file. The intrusion area would be something like this

enter image description here

I thought these steps:

I. Reading a video file;
II. Getting the very first frame displayed,
  1. Pausing the video playback;
  2. Manually drawing intrusion area on that frame with a mouse; (making draw and reset buttons as events maybe)
  3. Replaying the video again
III. Waiting for the intruder to appear, etc. (III part is not important)

So far, I've done I and II (silly, I know) and should accomplish 1,2,3 subparts of step II.

import cv2

file  = "intrusion.mp4"
capture = cv2.VideoCapture(file)

ret, firstFrame= capture.read()

while True:
    cv2.imshow("First Frame", firstFrame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()

I hope you can give your advice and instructions!

PS: refer to any related posts, blogs or links, I am excited to find out

1 Answers

adding cv2.waitKey(0) will pause your while loop indefinitely! It will resume only after any key press.

I think what you're trying to achieve is object tracking using Background Subtraction. Refer Here and see if it fits your requirements.

EDIT:

I guess you want to draw a freehand shape for intrusion area! This Link will guide you to do it. I hope this helps

Related