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

1

2

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

Carey answered 10/1, 2019 at 9:22 Comment(0)
A
0

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

Arnitaarno answered 10/1, 2019 at 9:38 Comment(4)
well thanks for the link, I reviewed it. Currently I have no problem with tracking, all I want is to draw a shape on the video frame to hence be able to preserve that drawn area as intrusion area. I already possess a tracking module in my program. Anyway, thanks a lot!Carey
Hi, check out my latest answer! What you're looking for is already asked and answered(Partially). I have given the link to the answerArnitaarno
Hi @voo_doo, I have updated my answer. Have a look at itArnitaarno
yes I saw it, but I couldn't generalize it for my case, in the link, it is an image, mine is video frame, I could draw a shape, but then it should continue playing, I failed at this.Carey

© 2022 - 2024 — McMap. All rights reserved.