简体   繁体   English

我如何使用 Python 进行新加坡车牌识别的校验和

[英]How do i do the checksum for Singapore Car License Plate Recognition with Python

I am able to do recognition of car license plate and extract the plate values.我能够识别车牌并提取车牌值。 Sometimes, the results are inaccurate as i am using OCR to do the recognition.有时,结果不准确,因为我使用 OCR 进行识别。 I uses a checksum to ensure only the correct results are being printed and viewed.我使用校验和来确保只打印和查看正确的结果。 After the calculation for the checksum, I need to use another formula to get the last letter of the plate.在计算完校验和之后,我需要用另一个公式来得到盘子的最后一个字母。 match with these 19 letter, A=0, Z=1, Y=2, X=3, U=4, T=5, S=6, R=7, P=8, M=9, L=10, K=11, J=12, H=13, G=14, E=15, D=16, C=17, B=18.匹配这19个字母,A=0, Z=1, Y=2, X=3, U=4, T=5, S=6, R=7, P=8, M=9, L=10, K=11, J=12, H=13, G=14, E=15, D=16, C=17, B=18。 Is there any way i can use a loop to like declare the values of this letters instead of doing it one by one manually?有什么办法可以使用循环来声明这些字母的值,而不是手动一一进行? Please help out.请帮忙。 Thank you.谢谢你。

You can use a list and perform the lookups according to your needs.您可以使用列表并根据需要执行查找。

The list looks like this:该列表如下所示:

plate_letter_list = ['A', 'Z', 'Y', 'X', 'U', 'T', 'S', 'R', 'P', 'M', 'L', 'K', 'J', 'H', 'G', 'E', 'D', 'C', 'B']

Case 1: Lookup value from letter案例 1:从字母中查找值

If you need to find the numeric value associated with a letter, use the index method:如果需要查找与字母关联的数值,请使用index方法:

letter = 'T'
print(plate_letter_list.index(letter))
>> 5

Case 2: Lookup letter from value案例 2:从值中查找字母

If you need to find the letter associated with a numeric value, use it as index:如果您需要查找与数值关联的字母,请将其用作索引:

value = 13
print(plate_letter_list[value])
>> H

There are two ways to search for these values.有两种方法可以搜索这些值。 One of it is to use the List datatype like @sal provided.其中之一是使用提供的 @sal 之类的List数据类型。 The other way is to use Dictionary datatype另一种方法是使用Dictionary数据类型

** **

Solution using List Datatype使用列表数据类型的解决方案

** **

pl_vals_list = ['A', 'Z', 'Y', 'X', 'U', 'T', 'S', 'R', 'P', 'M', 'L', 'K', 'J', 'H', 'G', 'E', 'D', 'C', 'B']

You can then do a lookup either by position or by value然后,您可以通过position或按进行查找

To search by position , will provide you the value you assigned to the alphabet.通过position搜索,将为您提供分配给字母表的值。

print(pl_vals_list[0]) . print(pl_vals_list[0]) This will result in A .这将导致A

Alternatively, you can search by the alphabet itself.或者,您可以按字母本身进行搜索。 In this case, you have to use the index() function.在这种情况下,您必须使用index() function。

print(pl_vals_list.index('A')) . print(pl_vals_list.index('A')) This will result in the assigned number you gave to the alphabet.这将导致您为字母表分配的编号。 The result will be 0结果将是0

This provides you a means to look-up based on alphabet or value.这为您提供了一种基于字母或值进行查找的方法。

You can also check if the alphabet is inside the list using:您还可以使用以下方法检查字母表是否在列表中:

if 'A' in pl_vals_list:
    #then do something for value A
elif 'B' in pl_vals_list:
    #then do something for value B
else:
    #then do something

You can also iterate through the list using a for loop and enumerate.您还可以使用 for 循环遍历列表并进行枚举。 However, I don't know if that will be of value to you.但是,我不知道这对您是否有价值。

for i, v in enumerate(pl_vals_list):
    #do something with each value in the list
    #here i will store the index, and v will have the value - A, B, C, etc

You can get the value of each of these and determine what you want to do.您可以获得其中每一项的价值并确定您想要做什么。

** **

Solution using Dictionary Datatype使用字典数据类型的解决方案

** **

Similarly, you can do the same using a dictionary.同样,您可以使用字典来执行相同的操作。

pl_vals_dict = {'A':0, 'Z':1, 'Y':2, 'X':3, 'U':4, 'T':5, 'S':6, 'R':7, 'P':8, 'M':9, 'L':10, 'K':11, 'J':12, 'H':13, 'G':14, 'E':15, 'D':16, 'C':17, 'B':18}

To look for alphabets within a dictionary, you can use要在字典中查找字母,您可以使用

if 'A' in pl_vals_dict.keys():
    #then do something for value A
elif 'A' in pl_vals_dict.keys():
    #then do something for value B
else:
    #do something else

An alternate way to check for something would be:检查某事的另一种方法是:

x = True if 'F' in pl_vals_dict.keys() else False

In this case x will have a value of False在这种情况下, x的值为False

You can also use the get() function to get the value.您也可以使用 get() function 来获取值。

x = pl_vals_dict.get('A')  # OR

print (pl_vals_dict.get('A')

The simplest way to look up a dictionary value is:查找字典值的最简单方法是:

print (pl_vals_dict['A'])

which will result in 0这将导致0

However, you have to be careful if you try to print value of 'F', it will throw an error as 'F' is not part of the key value pair within the dictionary.但是,如果您尝试打印“F”的值,则必须小心,因为“F”不是字典中键值对的一部分,因此会引发错误。

print (pl_vals_dict['F'])

this will give you the following error:这会给你以下错误:

Traceback (most recent call last):
  File "<pyshell#48>", line 1, in <module>
    pl_vals_dict['F']
KeyError: 'F'

Similar to list, you can also iterate through the dictionary for keys and values.与列表类似,您也可以遍历字典以获取键和值。 Not sure if you will need to use this but here's an example for you.不确定您是否需要使用它,但这里有一个示例。

for k, v in pl_vals_dict.items():
    #do something with each pair of key and value
    #here k will have the keys A Z Y X ....
    #and v will have the values 1, 2, 3, 4, ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM