rebootnet
Posts: 7
Joined: Tue Nov 17, 2015 8:55 am

Vehicle Counting with IR Sensor

Tue Nov 17, 2015 9:04 am

Hi,

I have an IR sensor connected to a Raspberry Pi 2 Model B with the PiFace board.

I am trying to count the number of vehicles passing (specifically trucks) but it should only count one vehicle after 10 seconds for example (vehicle on weighbridge) so as to not count people who may be walking by.

At the moment I've got

Code: Select all

time.sleep()
but it doesn't work for obvious reasons. I can't seem to find a way of determining how long the input is triggered.

Has anyone got an idea for me? I'd be very thankful.

Code: Select all

#!/usr/bin/env python
import pifacedigitalio as pio
import time

pfd = pio.PiFaceDigital()
count = 0

pio.init()
while(True):
	if pfd.switches[0].value == 1:
		start = time.time()
		pio.digital_write(2,1)
		time.sleep(5)
		end = time.time()
		elapsed = end - start
		print 'Elapsed trigger time is', elapsed
		if elapsed >= 5:
			count += 1
			fname = open(time.strftime('%d%m%Y.log'), 'w')
			fname.write(str(count))
			fname.close()
			print count
	else:
		pio.digital_write(2,0)

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Vehicle Counting with IR Sensor

Tue Nov 17, 2015 10:29 am

Not sure how the PIR reacts to vehicles in real world but I would guess the vehicle stops on the weigthbridge for some minimum time and the PIR shows 'detected' for that period - the passing people only trigger the PIR for a shorter time?

So you would do something like:

Code: Select all

    ...
    # wait until something detected
    while not PIR_is_detecting():
        time.sleep(0.001)

    # start timing
    start = time.time()
    while PIR_is_detecting():
        time.sleep(0.001)
    end = time.time()

    if end - start > x:
        # something detected for long enough
        count += 1

    ...
...just need to find the suitable value for 'x' (and implement the PIR_is_detecting method).
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

rebootnet
Posts: 7
Joined: Tue Nov 17, 2015 8:55 am

Re: Vehicle Counting with IR Sensor

Tue Nov 17, 2015 1:51 pm

-rst- wrote:Not sure how the PIR reacts to vehicles in real world but I would guess the vehicle stops on the weigthbridge for some minimum time and the PIR shows 'detected' for that period - the passing people only trigger the PIR for a shorter time?

So you would do something like:

Code: Select all

    ...
    # wait until something detected
    while not PIR_is_detecting():
        time.sleep(0.001)

    # start timing
    start = time.time()
    while PIR_is_detecting():
        time.sleep(0.001)
    end = time.time()

    if end - start > x:
        # something detected for long enough
        count += 1

    ...
...just need to find the suitable value for 'x' (and implement the PIR_is_detecting method).
Hi,

Thanks for the response. Yes, your logic makes sense, the problem I've been having is on how to properly detect the input on the PiFace. Since it appears as though it is detecting thousands of times a minute rather than being constantly on (when triggered).

So for now I have come up with this for a solution, I know in my gut there's a better and much simpler way to do this but I just don't know how.

Code: Select all

#!/usr/bin/env python
import pifacedigitalio as pio
import time
pfd = pio.PiFaceDigital()
pio.init()
count = 0
trucks = 0

def pioread():
    while(True):
	if pfd.switches[0].value == 1:
        global count
		count = count + 1
		pio.digital_write(2,1)
		time.sleep(0.01)
		if count == 1000:
			global trucks
			trucks = trucks + 1
			truckfile = open(time.strftime('trucks_%d%m%Y.log'), 'w')
			truckfile.write(str(trucks))
			truckfile.close()
			print 'Amount of trucks is', trucks
    	else:
		pio.digital_write(2,0)
		count = 0
print('PiFace Input/Output Process Started on ' + time.strftime('%c') + '.')
pioread()

bulletmark
Posts: 121
Joined: Wed Oct 17, 2012 10:10 pm
Location: Brisbane Australia

Re: Vehicle Counting with IR Sensor

Tue Nov 17, 2015 11:33 pm

@rebootnet, I'll help you but please fix up the indentation in your posted code first so I know exactly what you are trying to do.

rebootnet
Posts: 7
Joined: Tue Nov 17, 2015 8:55 am

Re: Vehicle Counting with IR Sensor

Wed Nov 18, 2015 8:21 am

bulletmark wrote:@rebootnet, I'll help you but please fix up the indentation in your posted code first so I know exactly what you are trying to do.
Hi, thanks.

Not sure why the indentation is doing that when pasting here.

Code: Select all

#!/usr/bin/env python
import pifacedigitalio as pio
import time
pfd = pio.PiFaceDigital()
pio.init()
count = 0
trucks = 0

def piotrigger():
	print('... Started on ' + time.strftime('%c') + '.')
	while(True):
		if pfd.switches[0].value == 1:
			global count
			count +=1
			pio.digital_write(2,1)
			time.sleep(0.01)
			if count == 1000:
				global trucks
				trucks +=1
				truckfile = open(time.strftime('trucks_%d%m%Y.log'), 'w')
				truckfile.write(str(trucks))
				truckfile.close()
				print 'Number of trucks counted:', trucks
		else:
			pio.digital_write(2,0)
			count = 0
piotrigger()

bulletmark
Posts: 121
Joined: Wed Oct 17, 2012 10:10 pm
Location: Brisbane Australia

Re: Vehicle Counting with IR Sensor

Wed Nov 18, 2015 12:52 pm

OK, I have put a modified version of your code at http://pastebin.com/GeZCmcN2.

Comments and changes are:

You were mixing up the classes and static function pifacedigitalio interfaces. See https://piface.github.io/pifacedigitalio/example.html. So you don't want that pfd = pio.PiFaceDigital() line at all and just replace the pdf.switches[0]... with a digital_read() as I have done.

You should only write the digital outputs when they actually change to on or off (i.e. count goes 0 or not). No need to pointlessly hammer the hardware.

You were driving output 2 on when an input registered but I also drive output 3 on when an input is deemed to be a truck, it makes diagnosing faults etc easier. (I realise you are not driving outputs 0 and 1 here which are also the PiFace relays).

In modern python (i.e. 2.6+) you should always use a with statement instead of explicitly closing the file.

Your sleep() should be done at the bottom of the loop, not just when the input is on, else you are flat-lining the CPU.

Always avoid global statements. No need for them here anyhow.

It is more efficient and cleaner to construct the file name string once at startup, not every time you write the contents.

Should use python string formatting instead of appending bits and pieces.

Should always use 4 chars to ident python code (edict from PEP8). I've also changed the spacing and formatting to be consistent throughout.

rebootnet
Posts: 7
Joined: Tue Nov 17, 2015 8:55 am

Re: Vehicle Counting with IR Sensor

Wed Nov 18, 2015 5:19 pm

bulletmark wrote:OK, I have put a modified version of your code at http://pastebin.com/GeZCmcN2.

Comments and changes are:

You were mixing up the classes and static function pifacedigitalio interfaces. See https://piface.github.io/pifacedigitalio/example.html. So you don't want that pfd = pio.PiFaceDigital() line at all and just replace the pdf.switches[0]... with a digital_read() as I have done.

You should only write the digital outputs when they actually change to on or off (i.e. count goes 0 or not). No need to pointlessly hammer the hardware.

You were driving output 2 on when an input registered but I also drive output 3 on when an input is deemed to be a truck, it makes diagnosing faults etc easier. (I realise you are not driving outputs 0 and 1 here which are also the PiFace relays).

In modern python (i.e. 2.6+) you should always use a with statement instead of explicitly closing the file.

Your sleep() should be done at the bottom of the loop, not just when the input is on, else you are flat-lining the CPU.

Always avoid global statements. No need for them here anyhow.

It is more efficient and cleaner to construct the file name string once at startup, not every time you write the contents.

Should use python string formatting instead of appending bits and pieces.

Should always use 4 chars to ident python code (edict from PEP8). I've also changed the spacing and formatting to be consistent throughout.
Hi bulletmark,

Thank you very much! I appreciate it a lot.

I'll try your script tomorrow. I'm only learning Python (and programming for that matter) now so this was the way I could make it work (sort of at least) but always knew I was doing it wrong.

I'll test the results.

rebootnet
Posts: 7
Joined: Tue Nov 17, 2015 8:55 am

Re: Vehicle Counting with IR Sensor

Thu Nov 19, 2015 7:44 am

Hi bulletmark,

Tested. Works perfectly!

Thank you so much for your help!

bulletmark
Posts: 121
Joined: Wed Oct 17, 2012 10:10 pm
Location: Brisbane Australia

Re: Vehicle Counting with IR Sensor

Thu Nov 19, 2015 7:51 am

I could not install pifacedigitalio on my Rpi2 probably because I use Arch, not Raspbian. I am interested to know however how much CPU it uses at that .01 sec poll rate so would you mind installing htop package, then run it in another terminal while your app is running and let me know the CPU % it uses at idle? Also, are you running Raspbian jessie?

Return to “Python”