-
Notifications
You must be signed in to change notification settings - Fork 0
/
vin_scanner.py
111 lines (95 loc) · 5 KB
/
vin_scanner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import sys
from dynamsoft_capture_vision_bundle import *
import os
class VINResult:
def __init__(self, item: ParsedResultItem):
self.vin_string = None
self.wmi = None
self.region = None
self.vds = None
self.vis = None
self.model_year = None
self.plant_code = None
self.code_type = item.get_code_type()
if self.code_type != "VIN":
return
if item.get_field_value("vinString") != None:
self.vin_string = item.get_field_value("vinString")
if(item.get_field_validation_status("vinString")==EnumValidationStatus.VS_FAILED):
self.vin_string += ", Validation Failed"
if item.get_field_value("WMI") != None and item.get_field_validation_status("WMI") != EnumValidationStatus.VS_FAILED:
self.wmi = item.get_field_value("WMI")
if item.get_field_value("region") != None and item.get_field_validation_status("region") != EnumValidationStatus.VS_FAILED:
self.region = item.get_field_value("region")
if item.get_field_value("VDS") != None and item.get_field_validation_status("VDS") != EnumValidationStatus.VS_FAILED:
self.vds = item.get_field_value("VDS")
if item.get_field_value("VIS") != None and item.get_field_validation_status("VIS") != EnumValidationStatus.VS_FAILED:
self.vis = item.get_field_value("VIS")
if item.get_field_value("modelYear") != None and item.get_field_validation_status("modelYear") != EnumValidationStatus.VS_FAILED:
self.model_year = item.get_field_value("modelYear")
if item.get_field_value("plantCode") != None and item.get_field_validation_status("plantCode") != EnumValidationStatus.VS_FAILED:
self.plant_code = item.get_field_value("plantCode")
def to_string(self):
return (f"VIN String: {self.vin_string}\n"
f"Parsed Information:\n"
f"\tWMI: {self.wmi or ''}\n"
f"\tRegion: {self.region or ''}\n"
f"\tVDS: {self.vds or ''}\n"
f"\tVIS: {self.vis or ''}\n"
f"\tModelYear: {self.model_year or ''}\n"
f"\tPlantCode: {self.plant_code or ''}\n")
def print_results(result: ParsedResult) -> None:
tag = result.get_original_image_tag()
if isinstance(tag, FileImageTag):
print("File:", tag.get_file_path())
if result.get_error_code() != EnumErrorCode.EC_OK:
print("Error:", result.get_error_string())
else:
items = result.get_items()
print("Parsed", len(items), "VIN codes.")
for item in items:
vin_result = VINResult(item)
print(vin_result.to_string())
if __name__ == '__main__':
print("**********************************************************")
print("Welcome to Dynamsoft Capture Vision - VIN Sample")
print("**********************************************************")
# Initialize license.
# You can request and extend a trial license from https://www.dynamsoft.com/customer/license/trialLicense?product=dcv&utm_source=samples&package=python
# The string 'DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9' here is a free public trial license. Note that network connection is required for this license to work.
error_code, error_message = LicenseManager.init_license("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9")
if error_code != EnumErrorCode.EC_OK and error_code != EnumErrorCode.EC_LICENSE_CACHE_USED:
print("License initialization failed: ErrorCode:", error_code, ", ErrorString:", error_message)
else:
cvr_instance = CaptureVisionRouter()
while (True):
image_path = input(">> Enter the image path (or 'Q'/'q' to quit):").strip('\'"')
if image_path == "":
print("Invalid path.")
continue
if image_path.lower() == "q":
sys.exit(0)
if not os.path.exists(image_path):
print("The image path does not exist.")
continue
while(True):
print(">> Step 2: Choose a Mode Number:")
print("1. Read VIN from Barcode")
print("2. Read VIN from Text")
try:
mode = int(input())
if mode == 1 or mode == 2:
template_name = "ReadVINBarcode" if mode == 1 else "ReadVINText"
break
except ValueError:
continue
result = cvr_instance.capture(image_path, template_name)
if result.get_error_code() != EnumErrorCode.EC_OK:
print("Error:", result.get_error_code(), result.get_error_string())
else:
parsed_result = result.get_parsed_result()
if parsed_result is None or len(parsed_result.get_items()) == 0:
print("No parsed results.")
else:
print_results(parsed_result)
input("Press Enter to quit...")