robmarkcole / HASS-plate-recognizer

Read number plates with https://platerecognizer.com/
135 stars 26 forks source link

Add watched_plate arg #27

Closed robmarkcole closed 3 years ago

robmarkcole commented 3 years ago

Parallels https://github.com/robmarkcole/HASS-Sighthound/issues/22

Add a watched_plate arg that is a list of plates to watch for, allowing fuzzy matching. I.e. a matched plate will be detected when there is a good match of characters using regex. For each plate an attribute is exposed which is a binary sensor indicating if the plate was in the last scanned imaged or not. Add a watched_plate event as well?

robmarkcole commented 3 years ago

The api returns the most likely plate, but also other candidate plates, and these can be matched against also:

[ 'plate': 'kfa8725',
  'region': {'code': 'us-pa', 'score': 0.979},
  'score': 0.899,
  'candidates': [{'score': 0.899, 'plate': 'kfa8725'},
   {'score': 0.779, 'plate': 'kfab725'}],

The following function returns all candidate plates, could just match on this:

from typing import List, Dict

def get_plates(results : List[Dict]) -> List[str]:
    """
    Return the list of candidate plates. 
    If no plates empty list returned.
    """
    plates = []
    candidates = [result['candidates'] for result in results]
    for candidate in candidates:
        cand_plates = [cand['plate'] for cand in candidate]
        for plate in cand_plates:
            plates.append(plate)
    return list(set(plates))

get_plates(response.json()['results'])

... ['kfa8726', 'kfa8725', 'kfab725', 'kfab726']

Could also count the number of letters that must exist in any detected plate, this could also be an arg? Too complicated

robmarkcole commented 3 years ago

done