OpenCV Python Tutorial For Beginners 24 - Motion Detection and Tracking Using Opencv Contours

Поділитися
Вставка
  • Опубліковано 28 лис 2024

КОМЕНТАРІ • 215

  • @myetis1990
    @myetis1990 5 років тому +102

    if you watch previous tutorials you will understand what is happening behind syntax
    briefly,
    first step, movement is difference between two frames
    second, difference has noises because of details and light on video so gaussian blurring is eliminating the noises,
    third, obtaining threshold from clean difference
    fourth, dilating for eliminating district small weak threshold lines which corrupt healthy threshold detection
    fifth, finding contours from clean threshold
    sixth, eliminating small contours which can not be a human by filtering contour area
    seventh, drawing rectangles for each detected contour on the frame, rectangle dimensions obtained from cv2.boundingRect(contour)
    that is it!

    • @omgleowtf
      @omgleowtf 5 років тому

      Cool, nice quick explanation, thanks!

    • @kunalgurtatta1813
      @kunalgurtatta1813 5 років тому

      Thanks for the simple explanation !

    • @emnaguermazi9406
      @emnaguermazi9406 5 років тому

      Great explanation

    • @jayasreegubbala3514
      @jayasreegubbala3514 4 роки тому

      can you send the previous videos link

    • @kumarabhishek5652
      @kumarabhishek5652 4 роки тому

      plz explain 6th point. where to look for. my code is still recognizing contour over ropes.
      ps: i am also getting this error:
      error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\core\src\arithm.cpp:669: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'cv::arithm_op'

  • @myetis1990
    @myetis1990 5 років тому +23

    By evaluating the aspect ratio of each rectangle , we can determine whether it is a person or not.
    if Height/Width >1 then it may be a person
    elif Height/Width

    • @ayy4703
      @ayy4703 5 років тому

      Thanks :D

  • @efekanefe442
    @efekanefe442 5 років тому +16

    You gave me hope dude.You are the man!Thank you so much for helping us about the topics that we can't easily learn from anybody.

    • @gyghfhyh7439
      @gyghfhyh7439 Рік тому

      abi merhaba. Bir projeniz mi vardı? Neden ihtiyacınız oldu acaba bu tutoriallara? Şuanda bir bitirme projem var da, bende bu videoların içersinde kaybolmuş durumdayım.

  • @sauravsutradhar1395
    @sauravsutradhar1395 4 роки тому +4

    You can change the thresh value in threshold function which eradicates the noise generated by the rope movement behind. I found a value of thresh=50 which worked fine for this input.

  • @philspaghet
    @philspaghet 4 роки тому +43

    Top tip: replace 'vtest.avi' with '0' to use your native webcam!

    • @brainibruno1796
      @brainibruno1796 4 роки тому +8

      yes but please without those ' ' , so just write cap = cv2.VideoCapture(0)

  • @umarqureshi2650
    @umarqureshi2650 4 роки тому

    I added edited this line with w>h which makes sure no box with a width greater then its height will be detected (which helps with the rope not being detected):
    if cv2.contourArea(contour) < 900 or w > h:

  • @Anon-atg
    @Anon-atg 3 роки тому +1

    You my friend are a legend

    • @Anon-atg
      @Anon-atg 3 роки тому

      Super helpful guide, was looking for an input into my KcF tracker that wasn’t the traditional selectROI so used this and outputted the bounding boxes once they fitted my parameters

  • @peacecyebukayire5316
    @peacecyebukayire5316 3 роки тому +1

    I've loved the video already before I even watch it a second:)

  • @dhruuvvvv
    @dhruuvvvv 5 років тому +12

    Pls upload tutorial on creating own haar cascade classifier

  • @MarcosGarcia-mc8oj
    @MarcosGarcia-mc8oj Рік тому

    Thanks for making these videos so easy to follow.

  • @jonatapaulino
    @jonatapaulino 2 роки тому +2

    Very good explanation. Congratulations. Do you have a video that counts these people after detection? Thanks for the video.

  • @survivaldude29
    @survivaldude29 3 роки тому +1

    After several hours I finally found the correct link to download opencv
    Edit: never mind ,it was still the wrong link ,it’s now been 3 hours.I’m just gonna waste my money on an actual security camera, even though it’s not necessary
    Because I can use codes.

  • @ravenfly2739
    @ravenfly2739 2 роки тому

    Thanks for that very helpful Video.

  • @mujeebsonlineacademy7962
    @mujeebsonlineacademy7962 6 місяців тому

    which camera and components u have used for implementing as a hardware

  • @anirudhkumar7477
    @anirudhkumar7477 Рік тому

    That was amazing! Thank you so much

  • @muhammaddawoodarsalan5416
    @muhammaddawoodarsalan5416 2 роки тому

    thanks dude appreciate it

  • @ayushagarwal69
    @ayushagarwal69 3 роки тому +1

    Awesome tutorial sir , althought harr cascade would be better

  • @jhonjaguer1488
    @jhonjaguer1488 4 роки тому

    yhank you god bless you. you are the best teacher. you helpt me a lot

  • @darshitjain5965
    @darshitjain5965 4 роки тому +2

    Great Video!!
    Is there a way to use the concept in the video to count the number of people in the test-video??

  • @sidranasreen7932
    @sidranasreen7932 Рік тому

    Can you provide the video which you have used in your code. Also can you please explain why have you used frame1

  • @antoninsoulier6391
    @antoninsoulier6391 Рік тому

    Thank you !

  • @berberlim
    @berberlim 2 роки тому

    Thank you so much bro your really helped me, you made my day

    • @justanothergoogleuser
      @justanothergoogleuser 2 роки тому

      Hi! How to download/get the video mentioned in this video

    • @berberlim
      @berberlim 2 роки тому

      @@justanothergoogleuser
      I used my own video

  • @pedro_pinheiro
    @pedro_pinheiro 3 роки тому

    Great video! Thank you for sharing!

  • @shaikhjunaid5577
    @shaikhjunaid5577 2 роки тому

    I want to change my rectangle frame as im using live webcam and the previous code has (frame,(x,y),(x+w,y+h),(0,225,225),2)
    What values should i pick tor my live wecam so that full body can be detected

  • @menakakonara7957
    @menakakonara7957 4 роки тому

    Really helpful,Thanks

  • @riyushachannel4761
    @riyushachannel4761 3 роки тому

    Work gan 👍🏻 ,thanks 🙏🙏
    (Auto subscribe)

  • @paterfamiliasgeminusiv4623
    @paterfamiliasgeminusiv4623 3 роки тому +1

    Interesting. How could we differentiate between actual movement and camera movement?

  • @mirzaabdulrehman428
    @mirzaabdulrehman428 4 роки тому

    its worked thanks..

  • @X31-n7f
    @X31-n7f 2 роки тому

    Can you use this to create data points (X,Y) for that movement every second for example?

  • @aacasd
    @aacasd 4 роки тому

    Which database would you prefer to store the data about a person, trace and time they were present in this video?

  • @Kpsr
    @Kpsr 5 років тому +2

    i translated it all to C++ and it is compiling but unfortunately there is no contours drew
    edit: after debugging i realized that contours are shown only at first frame
    edit2: okay so after my investigation i found out that in C++ there might be a problem with "=" operator for Mat class and frame1=frame2 isnt working propely. i had to replace it for frame1=frame2.clone()

  • @vigneshkathirkamar3113
    @vigneshkathirkamar3113 4 роки тому +1

    how to select the "thresh" argument for cv2.threshold(). In this video it is mentioned as 20. Is there any criteria for arriving at a value?

  • @umarqureshi2650
    @umarqureshi2650 4 роки тому

    If you guys want realtime with a webcam use the following code:
    cap = cv2.VideoCapture(0)
    cap.set(3, 640)
    cap.set(4, 480)
    If you want to use some external camera I think u need to change the number 0 to 1 or another number, I would google it
    I would also google cap.set so u can make sure tht it works for u

    • @joecomets8654
      @joecomets8654 2 роки тому

      That's cool, but I don't know how to add a toolbar to control the video progress, can you teach us how to do it? thanks

  • @DecodingComputers
    @DecodingComputers 5 років тому +1

    Good tutorial

  • @TuanNguyen-wo2zq
    @TuanNguyen-wo2zq 4 роки тому +3

    Can I ask if this is real-time or it has some delay? If delay, how much time does it behind from the real-time? Thank you

    • @theaveragepineapple8967
      @theaveragepineapple8967 3 роки тому

      If you still want to know very minimal delay if any on my code from my web cam don’t notice any

  • @redzalmahdin3477
    @redzalmahdin3477 3 роки тому

    Thank you for the tutorial, I am curious, is it possible to use a stream video as the source video? if yes, how is it can be achieve?

  • @kieno5754
    @kieno5754 4 роки тому

    Thanks for this great tutuorial!

  • @rohanverma321
    @rohanverma321 4 роки тому +1

    Hey ! Where can I get a similar video?

  • @mvp2372
    @mvp2372 2 роки тому

    Hello. Can it be applied when we are watching any video online?

  • @diegoruza3732
    @diegoruza3732 4 роки тому +1

    Hi. Thanks so much for this tutorial. It helps me a lot. Could you please show us how to take a screenshot of the frame when capture motion?

    • @guseechka
      @guseechka 4 роки тому

      Did u resolve this question?

  • @jadavpur4
    @jadavpur4 3 роки тому

    Hi...
    Thank you for the tutorial.. It's impressive. I have one question.
    Can we make the animation of moving objects a function of time? the program will run and continuous animation will run???
    Thank you

  • @MadeInFIFA10
    @MadeInFIFA10 3 роки тому +1

    File "C:/Users/tasme/PycharmProjects/pythonProject1/main.py", line 15, in
    print(frame1.shape)
    AttributeError: 'NoneType' object has no attribute 'shape'
    Process finished with exit code 1
    im having this problem will u pls help

  • @HM-cw8im
    @HM-cw8im 4 роки тому

    Thank you so much for this awesome tutorial!!

    • @HM-cw8im
      @HM-cw8im 3 роки тому

      @Karaoke By K Thank you!

  • @gustavodslara
    @gustavodslara 3 роки тому

    is it possible to do with real time video streamming? example: from my security cam

  • @rakshitharakshi5968
    @rakshitharakshi5968 5 років тому +1

    can u tell me which algorithm ur using

  • @naveenbm3729
    @naveenbm3729 2 роки тому

    Can you please let me know what is the unit for the area of rectangle you r using, for example 700…. Is it 700 mm^2 ? How can I get to know?

  • @prasanthjavvaji
    @prasanthjavvaji 4 роки тому +1

    How can I get the video file that have been given as input

  • @Tushar-481
    @Tushar-481 4 роки тому

    The approach is very excellent but the algorithm won't work if the people are not moving cuz when they will stop moving the will be no difference between two consecutive frames hence it won't detect the person. CAN U FIX THIS PROBLEM?

  • @mtkavak
    @mtkavak 3 роки тому

    Thank you, how about consecutive images?

  • @chiral4055
    @chiral4055 5 років тому +1

    Thanks for this great tutuorial.

  • @logicawe
    @logicawe 5 років тому

    This is very inspiring content. Thank you for sharing.

  • @adrien123_
    @adrien123_ 4 роки тому

    Hi, I'm developing a python speed tracking project and your video taught me a lot... Could I copy a part of your script to implement it in my project ? that would be cool...

  • @umutcoskun4247
    @umutcoskun4247 5 років тому +1

    Thank you for this video.
    Question: Why are you showing frame1 and not frame2? Because frame2 should be closer to realtime picture

    • @Ghkfdvbj
      @Ghkfdvbj 5 років тому +1

      Hi,
      drawing contours ( or rectangles ) on frame is nothing but changing the frame itself. if we draw contours on frame2, in the next iteration frame2 will be frame1 and which not the original frame of video, its modified ( contours written frame) .

  • @shashanksharmadon4295
    @shashanksharmadon4295 4 роки тому

    thanks bro........

  • @akshaypendyala
    @akshaypendyala 3 роки тому +1

    What if there are several moving objects other than just persons and ropes, let's say cars, birds, etc. How will you track only persons? It could have been awesome if you explained this Yolo and multi-threading for faster and accurate video processing.

    • @Satchi017
      @Satchi017 2 роки тому

      Have you found the solution????

    • @akshaypendyala
      @akshaypendyala 2 роки тому +1

      @@Satchi017 Nah, after doing this project, I was busy with other tasks.

  • @anishbhurke847
    @anishbhurke847 4 роки тому

    can we raise an alarm through code if any person comes near the lamppost ?? can u guide on same ??

  • @SadatSafawi
    @SadatSafawi 3 роки тому +1

    how to see the accuracy of this code ?? Please help me with this

  • @sarathram3312
    @sarathram3312 3 роки тому

    Why are we assigning frame2 to frame1 while drawing contours and again reading a new frame in frame2??

  • @nvdhairi
    @nvdhairi 5 років тому

    Can i know what is the difference between motion detection and object detection? Does it have differences?

  • @abhijeetmandal434
    @abhijeetmandal434 5 років тому

    Super

  • @mekukun4406
    @mekukun4406 3 роки тому

    Hi! Where can I get the sample video shown in this video ?

  • @arendelle_ok
    @arendelle_ok 2 роки тому

    can i use vlc window panel as a motion capture ?. cuz i want be able to change the video source

  • @serignediewrine7908
    @serignediewrine7908 4 роки тому

    great

  • @rajarshimandal3235
    @rajarshimandal3235 4 роки тому +1

    Sir in the very starting of the mp4, why didn't opencv detect the person who was walking along the left side of the window on the narrow footpath?

    • @erikanderson6076
      @erikanderson6076 4 роки тому +1

      The person is far away from the camera, and because of this the number of pixels that person takes up is below the area threshold set in the code. You see as that person comes into the foreground, they occupy more pixels and they start getting detected

    • @rajarshimandal3235
      @rajarshimandal3235 4 роки тому

      @@erikanderson6076 yes I realized it later thanks😊

  • @sametakbeyaz5021
    @sametakbeyaz5021 3 роки тому

    Is it possible to do it in the game?

  • @yunusyavuz6147
    @yunusyavuz6147 3 роки тому

    thx

  • @saadaldossri5348
    @saadaldossri5348 4 роки тому

    Good work, but could you put the code and the testing movie.

  • @franklinjoe52
    @franklinjoe52 4 роки тому

    Really appreciate your vids but please use dark mode. You'll thank me later

  • @elizabethsalas830
    @elizabethsalas830 4 роки тому

    Hi, would this work to track multiple markers on a segmented object?

  • @Ricarrr
    @Ricarrr 4 роки тому

    Congratulations on the great job.
    I would like to know how to put an ID on these people?

    • @franklinjoe52
      @franklinjoe52 4 роки тому

      You should look more into face detection and recognition using the Haars Cascade algorithm. This script is for simple motion detection

    • @ayushagarwal69
      @ayushagarwal69 3 роки тому

      umm , ig you could declare an int variable and then puttext that number on the coordinates on each rectangle and then when the loop is over , you could puttext at the top of screen :" j moving objects detected ", and then you could store that j somewhere (idk much about that ) . But nice idea , that way we can find the no of objects

  • @osmanhussain6715
    @osmanhussain6715 4 роки тому +1

    how can i know the area limit of small objects(here it was 700 or 900)

  • @ojoj335
    @ojoj335 4 роки тому +1

    Thanks, great tutorial!!! Im trying to capture a window or part of the screen, is it possible to put this through cv2.videoCapture() ?

  • @ajinzrathod
    @ajinzrathod 4 роки тому

    Vid starts at 2:34

  • @abdremo
    @abdremo 5 років тому

    Nice nice

  • @magnetpen_1
    @magnetpen_1 Рік тому

    how do you use your webcam

  • @kunalgurtatta1813
    @kunalgurtatta1813 5 років тому +2

    can this comment get a heart ?👀

  • @videoguy402
    @videoguy402 4 роки тому

    I ran your code with a new video. In the video, along with the people, camera is also moving and the camera is just placed behind a possession. In that case, I am getting multiple rectangles for the same moving objects instead of one. Is there a way to solve this issue?

    • @pratikshamanave9709
      @pratikshamanave9709 3 роки тому

      DO YOU HAVE MORE RECORDINGS TO TEST? CAN YOU MAIL THEM TO ME AT pratiksha.manave99@gmail.com?

  • @loektenbosch8110
    @loektenbosch8110 4 роки тому

    After line 15, I get the following error: "ValueError: too many values to unpack (expected 2).
    The same as in the previous video where this technique is also used.
    Who can help me / point me in the right direction (yes, I already used Google).

  • @harshvirtomar6552
    @harshvirtomar6552 4 роки тому +1

    Can I run it on collab?

  • @kraskagm
    @kraskagm 5 років тому

    When it is taking difference between two frames, shouldn't it be 0 because the same video input is used(captured)?

    • @anirudhpai5617
      @anirudhpai5617 5 років тому

      I do believe it is taking two frames directly one after the other.

  • @FaridCenreng-
    @FaridCenreng- 3 роки тому

    How to enable auto suggest opencv on vscode?

  • @favourchianumba5823
    @favourchianumba5823 3 роки тому

    i copied the code shown at the start into a google colab notebook and the video isnt showing up, anyone know why?

  • @guseechka
    @guseechka 4 роки тому

    How can i crop frames rectangle from video?

  • @indiangamersg2585
    @indiangamersg2585 2 роки тому

    Helloji radhe radhe

  • @hadjerBrioua
    @hadjerBrioua Рік тому

    I am getting this error:
    Traceback (most recent call last):
    File "c:\Users\orange\Bureau\Learnings\openCv\basic_motion_detection.py", line 19, in
    diff = cv.absdiff(frame1, frame2)
    cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:650: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'cv::arithm_op'

  • @anuragaher2400
    @anuragaher2400 4 роки тому

    Can I get the video link? Could anyone help me with the pedestrians walking video?

  • @tinokor
    @tinokor 3 роки тому

    how to add a line that comes from the back?

  • @youssfinada3410
    @youssfinada3410 4 роки тому

    mille merciiiiiiiiiiiii

  • @udaykiran5602
    @udaykiran5602 2 роки тому

    How to add multiple videos bro

  • @antislit_4938
    @antislit_4938 4 роки тому

    Do I need to use a avi can this work with mp4 i tried it but it doesn't work why is this

  • @hs34-t9s
    @hs34-t9s 4 роки тому

    how can I find video that you used

  • @nicoestay6351
    @nicoestay6351 5 років тому

    Hi, great video!
    i have a question, how can i know how much people are in the input video?

    • @utmostcreator
      @utmostcreator 5 років тому +1

      you can extract contours.length or so if I am not mistaken
      if a countours is a list which should have its length

    • @superminecraft902
      @superminecraft902 5 років тому +1

      count++ in the for contour in contours

    • @Ghkfdvbj
      @Ghkfdvbj 5 років тому

      Hi, As we are working on frames, so we can find number of people equals to number of contours in each frame by adjusting contours area size ( one contour for one person)

  • @JuanRojas956
    @JuanRojas956 4 роки тому

    Is there a way to do motion tracking with the boxes around the people with live video?

    • @Nanda-ho5eh
      @Nanda-ho5eh 4 роки тому

      Do you have a idea. If you have please drop a hint

    • @ayushagarwal69
      @ayushagarwal69 3 роки тому

      use cap.VideoCapture(0) for live webcam

  • @fadhelalsuwaidi1221
    @fadhelalsuwaidi1221 5 років тому +1

    Hi
    Its wonderful video, can u make the camera record when the motion start, I mean record every motion, when camera detect someone moving start to record the video, it will be greate if u teach is how to do it.

    • @Sski85
      @Sski85 4 роки тому

      Not sure if this is helpful. But "Blue Iris" software is capable of doing what you are asking. I have a pc running 6 cameras with it and works very well

  • @nagashishsv843
    @nagashishsv843 5 років тому

    What's the use of taking difference between 2 frames??why it is needed..??
    Please reply..

    • @blz1rorudon
      @blz1rorudon 5 років тому

      By taking the difference between the two frames, all intensity for parts of the video where things don't move will be reduced to zero (since their values are the same in both frames). The parts of the video where things DO move, however, will have different values in the two frames, and so the difference will result in a non-zero value. The resulting image -diff- will be low-intensity (0) wherever the two frames are the same(no movement) and it will have some intensity for those parts that are different (movement). In this way, diff only preserves the movement of the original video.

    • @dojomats
      @dojomats 4 роки тому

      @@blz1rorudon does this mean that if there is any vibration on the webcam/camera, the whole data cant be used reliably to detect motion?

  • @shiwamjaiswal9653
    @shiwamjaiswal9653 5 років тому

    How do we decide the threshold value? Isn't it better to use OTSU's Binarization to calculate the threshold value

  • @sulmanyousaf6545
    @sulmanyousaf6545 5 років тому

    this error is occurring what i should do now ?
    diff = cv2.absdiff(frame1, frame2)
    TypeError: Expected Ptr for argument '%s'

    • @nikhilrana8800
      @nikhilrana8800 5 років тому

      I also got error in this line that shape of 2 frames is not same. If u come up with any solution please inform me.

    • @sulmanyousaf6545
      @sulmanyousaf6545 5 років тому

      @@nikhilrana8800 if u get the same error then u must changed some thing that not follow tutorial I did the same thing

    • @nikhilrana8800
      @nikhilrana8800 5 років тому

      @@sulmanyousaf6545 What did u change in your code??

    • @sulmanyousaf6545
      @sulmanyousaf6545 5 років тому +1

      @@nikhilrana8800 before the loop end we are getting the frame2=cv2.read() I just placed ret, before frame2 as
      ret, frame2= cv2.read()

    • @aakashaundhkar3159
      @aakashaundhkar3159 4 роки тому

      @@sulmanyousaf6545 thanks for the help

  • @willhawkins4112
    @willhawkins4112 4 роки тому

    Any idea's on how to download a nice video to run this code on? I'm a nube to CV.

    • @jeff._.6262
      @jeff._.6262 4 роки тому

      I assume you have already figured it out but If your just testing you don’t really need to download a video you can simply go to ex shutterstock/video and get the url to video ( using f11 or whatever button that gets you into the delvoper tab in your browser ) then just write that in cv.videoCapture(“the url”)

  • @rishabhkothari7901
    @rishabhkothari7901 4 роки тому +1

    Can someone expalin me the use of line 17 and 18?
    frame1= frame2
    ret, frame= cap.read()
    like what is their use exactly?

    • @noushadkhan360
      @noushadkhan360 4 роки тому

      I want also brother

    • @vigneshkathirkamar3113
      @vigneshkathirkamar3113 4 роки тому

      once your frame2 is assigned to frame1, now both frame1 and frame 2 will have same value, which will make 'diff' variable 0.
      Hence frame2 is set to new value using cap.read()

    • @dojomats
      @dojomats 4 роки тому

      @@vigneshkathirkamar3113 hi vignesh, what is the importance of the "ret, " part?

  • @anupamadhikari6043
    @anupamadhikari6043 4 роки тому

    Why we have None in dilute function