0

recently I've been working on a project using the raspberry pi camera OpenCV and Python to count people passing by a specific area, live, since for my usage will be easier than processing a recorded video.

Overall the code works and all, but I've been experiencing a problem with the counting part of it, that:

1 - If an object stays in the reference line, it keeps adding to the counts;

2 - Sometimes depending on the speed of the object, it is counted multiple times;

I am not an expert on python, and may be lacking the words in english to look for the proper solution, so I thought maybe someone could tell me what would be better here to solve this problem. To illustrate, here it is a gif sample:

enter image description here

Even tough it looks like there are more than one reference box crossing the line, it happens when only one box crosses it, as well as when the object stays on the line.

This is the code that checks if the object is crossing the line:

if (TestaInterseccaoEntrada(CoordenadaYCentroContorno,CoordenadaYLinhaEntrada,CoordenadaYLinhaSaida)):
    ContadorEntradas += 1

if (TestaInterseccaoSaida(CoordenadaYCentroContorno,CoordenadaYLinhaEntrada,CoordenadaYLinhaSaida)):  
    ContadorSaidas += 1

I thought of using some kind of delay with time.sleep(x) on the loop, but that does not solve it obviously, and also looks bad =D.

If needed, I may post the rest of the code here, but it's here, to keep things here tidy: Code Paste

Don't mind any bad syntax or errors, part of it is not mine and the part that is, looks terrible! XD

Thanks in advance.

3
  • 1
    Haven't checked the code, but could it be that you add 1 for every frame executed? You might need to distinguish if an object stays in this zone. E.g only add 1 if there_is_an_object_now AND there_was_no_object_last_frame. Commented Feb 6, 2019 at 21:36
  • 1
    I see what you mean, but if I compare , was the object was on last frame, since the program runs at around 20-30fps maybe checking the last 1 frame wouldn't give me the same result? Just wondering here before trying, hope I make sense. Commented Feb 7, 2019 at 12:38
  • 1
    I'm not sure I got you right. It's hard to read. If I get you right you have concerns about my plans. Well implemented right, this is about 3 lines of code and you can test it quite easily. You just have to check in your if-statement (the object detector) detected an object one frame ago. If so do NOT count up. Only count up if there was no object recognized one sample ago. A problem could be that in some frames nothing is detected (lags despite present object). You might check the last five frames. I assume with 30fps no object passes in 5 frames. Commented Feb 7, 2019 at 19:19

1 Answer 1

1

Cool project! It's quite a challenge to count the amount of bounding boxes that pass each line if you don't track them. It's even worse if you want to count them going both ways.

Because of this difficulty usually people prefer to track the object and then look at the trajectory to determine if the object passed the line or not.

This link can help you understand the difference. It also provides code to do the detection (but you got that part working already) and tracking (which you will need) https://www.pyimagesearch.com/2018/08/13/opencv-people-counter/

Next to that the easiest way to track is by linking the boxes with the highest iou. A good and easy implementation can be found here:

https://github.com/bochinski/iou-tracker

Good luck!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Victor! What an amazing project this one on github, so informative. I've been sitting here reading it for the last 1 hour hahaha! And, the project is based on Adrian's works already, but I missed that page... It will surely help me better make the code here. Again, thank you and I will be sure to post back with my results. About the bounding boxes, in fact it's there just to mark the shape, the count is done by the black dot in it's center, passing the lines. If that's what you meant of course, I may have misunderstood this part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.