From 8ffeeb17f5fbbd4f4f273dcea3e63df6554745f1 Mon Sep 17 00:00:00 2001 From: Bigsk Date: Sat, 21 Jan 2023 22:46:29 +0800 Subject: [PATCH] added near block merge function and fixed some bugs --- main.py | 110 ++++++++++++++++++++++++++++++++++++++++-------- out/1.jpg.json | 1 + out/10.jpg.json | 1 + out/2.jpg.json | 1 + out/3.jpg.json | 1 + out/4.jpg.json | 1 + out/5.jpg.json | 1 + out/6.jpg.json | 1 + out/7.jpg.json | 1 + out/8.jpg.json | 1 + out/9.jpg.json | 1 + 11 files changed, 103 insertions(+), 17 deletions(-) create mode 100644 out/1.jpg.json create mode 100644 out/10.jpg.json create mode 100644 out/2.jpg.json create mode 100644 out/3.jpg.json create mode 100644 out/4.jpg.json create mode 100644 out/5.jpg.json create mode 100644 out/6.jpg.json create mode 100644 out/7.jpg.json create mode 100644 out/8.jpg.json create mode 100644 out/9.jpg.json diff --git a/main.py b/main.py index 1b2b7d3..ac114fb 100644 --- a/main.py +++ b/main.py @@ -78,6 +78,37 @@ class number(object): i += 1 break + def __distance(self, p1, p2): + return ((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2) ** 0.5 + + def __closest_pair(self, X, Y): + if len(X) <= 3: + return min([self.__distance(X[i], X[j]) for i in range(len(X)) for j in range(i+1, len(X))]) + + mid = len(X)//2 + XL, XR = X[:mid], X[mid:] + YL, YR = [p for p in Y if p in XL], [p for p in Y if p in XR] + + d = min(self.__closest_pair(XL, YL), self.__closest_pair(XR, YR)) + + line = (X[mid][0] + X[mid-1][0]) / 2 + YS = [p for p in Y if abs(p[0]-line) < d] + + return min(d, self.__closest_split_pair(YS, d)) + + def __closest_split_pair(self, Y, d): + n = len(Y) + for i in range(n-1): + for j in range(i+1, min(i+8, n)): + if self.__distance(Y[i], Y[j]) < d: + d = self.__distance(Y[i], Y[j]) + return d + + def __dis(self, p1, p2): + X = p1 + p2 + Y = sorted(X, key=lambda p: (p[0], p[1])) + return self.__closest_pair(X, Y) + def search(self, keyword): ''' Search a plane by it registration number @@ -131,30 +162,73 @@ class number(object): ocr_filter = [] for _ in range(self.times): - eocr_result = self.__eocr.readtext(img, detail=1) - if self.debug: - print(eocr_result) - print("------------------------------") + # OCR recognize pocr_result = self.__pocr.ocr(img, cls=True) if self.debug: print(pocr_result) - print("------------------------------") - for e in eocr_result: - if e[2] > self.filter and e[1] not in ocr_filter: - ocr_result.append( - (tuple([tuple(i) for i in e[0]]), e[1], e[2]) - ) - ocr_filter.append(e[1]) + print("------------------------------B-") + eocr_result = self.__eocr.readtext(img, detail=1) + if self.debug: + print(eocr_result) + print("------------------------------A-") + + # OCR results tidy up + for i in range(len(pocr_result[0])): + for j in range(len(pocr_result[0])): + if self.debug: + print("------------------------------D-") + print(pocr_result[0][i][0], pocr_result[0][j][0]) + if i != j and len(pocr_result[0][i][0]) == 4 and len(pocr_result[0][j][0]) == 4 and self.__dis(pocr_result[0][i][0], pocr_result[0][j][0]) < 5: + if self.debug: + print("D Appended") + pocr_result.append(((pocr_result[0][i][0], pocr_result[0][j][0]), pocr_result[0][i][1][1] + pocr_result[0][j][1][1], (pocr_result[0][i][1][2] + pocr_result[0][j][1][2]) / 2)) + pocr_result.append(((pocr_result[0][j][0], pocr_result[0][i][0]), pocr_result[0][j][1][1] + pocr_result[0][i][1][1], (pocr_result[0][j][1][2] + pocr_result[0][i][1][2]) / 2)) + else: + if self.debug: + disout = 0 + if len(eocr_result[i][0]) == 4 and len(eocr_result[j][0]) == 4: + disout = self.__dis(pocr_result[0][i][0], pocr_result[0][j][0]) + print(i != j, len(pocr_result[0][i][0]) == 4, len(pocr_result[0][j][0]) == 4, disout) + print("------------------------------D-") + + for i in range(len(eocr_result)): + for j in range(len(eocr_result)): + if self.debug: + print("------------------------------C-") + print(eocr_result[i][0], eocr_result[j][0]) + if i != j and len(eocr_result[i][0]) == 4 and len(eocr_result[j][0]) == 4 and self.__dis(eocr_result[i][0], eocr_result[j][0]) < 5: + if self.debug: + print("C Appended") + eocr_result.append(((eocr_result[i][0], eocr_result[j][0]), eocr_result[i][1] + eocr_result[j][1], (eocr_result[i][2] + eocr_result[j][2]) / 2)) + eocr_result.append(((eocr_result[j][0], eocr_result[i][0]), eocr_result[j][1] + eocr_result[i][1], (eocr_result[j][2] + eocr_result[i][2]) / 2)) + else: + if self.debug: + disout = 0 + if len(eocr_result[i][0]) == 4 and len(eocr_result[j][0]) == 4: + disout = self.__dis(eocr_result[i][0], eocr_result[j][0]) + print(i != j, len(eocr_result[i][0]) == 4, len(eocr_result[j][0]) == 4, disout) + print("------------------------------C-") + + pocr_result = [sorted(pocr_result[0], key=lambda x: len(x[1][0]), reverse=True)] + eocr_result = sorted(eocr_result, key=lambda x: len(x[1]), reverse=True) + + if self.debug: + print(pocr_result) + print(eocr_result) + + # OCR results sum up for p in pocr_result[0]: if p[1][1] > self.filter and p[1][0] not in ocr_filter: ocr_result.append( (tuple([tuple(i) for i in p[0]]), p[1][0], p[1][1]) ) ocr_filter.append(p[1][0]) - - # OCR result tidy up - - + for e in eocr_result: + if e[2] > self.filter and e[1] not in ocr_filter: + ocr_result.append( + (tuple([tuple(i) for i in e[0]]), e[1], e[2]) + ) + ocr_filter.append(e[1]) # Read database for i in ocr_result: @@ -167,7 +241,9 @@ class number(object): return None if __name__ == "__main__": + import json num = number() + os.makedirs("out", exist_ok=True) for pic in os.listdir("test"): - print(pic, ":") - print(num.recognize(os.path.join("test", pic))) \ No newline at end of file + with open(os.path.join("out", f"{pic}.json"), "w+") as fb: + fb.write(json.dumps(num.recognize(os.path.join("test", pic)))) \ No newline at end of file diff --git a/out/1.jpg.json b/out/1.jpg.json new file mode 100644 index 0000000..5927879 --- /dev/null +++ b/out/1.jpg.json @@ -0,0 +1 @@ +{"icao24": "780ecb", "registration": "B-6995", "manufacturericao": "BOEING", "manufacturername": "Boeing", "model": "737NG 84P/W", "typecode": "B738", "serialnumber": "61323", "linenumber": "", "icaoaircrafttype": "L2J", "operator": "", "operatorcallsign": "", "operatoricao": "", "operatoriata": "", "owner": "Urumqi Airlines", "testreg": "", "registered": "", "reguntil": "", "status": "", "built": "", "firstflightdate": "", "seatconfiguration": "", "engines": "", "modes": "false", "adsb": "false", "acars": "false", "notes": "", "categoryDescription": "No ADS-B Emitter Category Information"} \ No newline at end of file diff --git a/out/10.jpg.json b/out/10.jpg.json new file mode 100644 index 0000000..345e300 --- /dev/null +++ b/out/10.jpg.json @@ -0,0 +1 @@ +{"icao24": "70c0a6", "registration": "A4O-DC", "manufacturericao": "AIRBUS", "manufacturername": "Airbus", "model": "A330 243", "typecode": "A332", "serialnumber": "1049", "linenumber": "", "icaoaircrafttype": "L2J", "operator": "", "operatorcallsign": "OMAN AIR", "operatoricao": "OMA", "operatoriata": "", "owner": "Oman Air", "testreg": "", "registered": "", "reguntil": "", "status": "", "built": "", "firstflightdate": "", "seatconfiguration": "", "engines": "", "modes": "false", "adsb": "false", "acars": "false", "notes": "", "categoryDescription": "No ADS-B Emitter Category Information"} \ No newline at end of file diff --git a/out/2.jpg.json b/out/2.jpg.json new file mode 100644 index 0000000..9fc3302 --- /dev/null +++ b/out/2.jpg.json @@ -0,0 +1 @@ +{"icao24": "780d77", "registration": "B-2762", "manufacturericao": "BOEING", "manufacturername": "Boeing", "model": "Boeing 787-8", "typecode": "B788", "serialnumber": "41542", "linenumber": "", "icaoaircrafttype": "L2J", "operator": "", "operatorcallsign": "XIAMEN AIR", "operatoricao": "CXA", "operatoriata": "", "owner": "Xiamen Airlines", "testreg": "", "registered": "", "reguntil": "", "status": "", "built": "", "firstflightdate": "", "seatconfiguration": "", "engines": "", "modes": "false", "adsb": "false", "acars": "false", "notes": "", "categoryDescription": "Heavy (> 300000 lbs)"} \ No newline at end of file diff --git a/out/3.jpg.json b/out/3.jpg.json new file mode 100644 index 0000000..532d5a7 --- /dev/null +++ b/out/3.jpg.json @@ -0,0 +1 @@ +{"icao24": "78060b", "registration": "B-6645", "manufacturericao": "AIRBUS", "manufacturername": "Airbus", "model": "A320 214", "typecode": "A320", "serialnumber": "4168", "linenumber": "", "icaoaircrafttype": "L2J", "operator": "", "operatorcallsign": "AIR SPRING", "operatoricao": "CQH", "operatoriata": "", "owner": "Spring Airlines", "testreg": "", "registered": "", "reguntil": "", "status": "", "built": "", "firstflightdate": "", "seatconfiguration": "", "engines": "", "modes": "false", "adsb": "false", "acars": "false", "notes": "", "categoryDescription": "No ADS-B Emitter Category Information"} \ No newline at end of file diff --git a/out/4.jpg.json b/out/4.jpg.json new file mode 100644 index 0000000..f2feb8f --- /dev/null +++ b/out/4.jpg.json @@ -0,0 +1 @@ +{"icao24": "7804cc", "registration": "B-5198", "manufacturericao": "BOEING", "manufacturername": "Boeing", "model": "737NG 89L/W", "typecode": "B738", "serialnumber": "36491", "linenumber": "", "icaoaircrafttype": "L2J", "operator": "", "operatorcallsign": "AIR CHINA", "operatoricao": "CCA", "operatoriata": "", "owner": "Air China", "testreg": "", "registered": "", "reguntil": "", "status": "", "built": "", "firstflightdate": "", "seatconfiguration": "", "engines": "", "modes": "false", "adsb": "false", "acars": "false", "notes": "", "categoryDescription": "No ADS-B Emitter Category Information"} \ No newline at end of file diff --git a/out/5.jpg.json b/out/5.jpg.json new file mode 100644 index 0000000..056e39d --- /dev/null +++ b/out/5.jpg.json @@ -0,0 +1 @@ +{"icao24": "780570", "registration": "B-6570", "manufacturericao": "AIRBUS", "manufacturername": "Airbus", "model": "A320 214", "typecode": "A320", "serialnumber": "4010", "linenumber": "", "icaoaircrafttype": "L2J", "operator": "", "operatorcallsign": "SHENZHEN AIR", "operatoricao": "CSZ", "operatoriata": "", "owner": "Shenzhen Airlines", "testreg": "", "registered": "", "reguntil": "", "status": "", "built": "", "firstflightdate": "", "seatconfiguration": "", "engines": "", "modes": "false", "adsb": "false", "acars": "false", "notes": "", "categoryDescription": "No ADS-B Emitter Category Information"} \ No newline at end of file diff --git a/out/6.jpg.json b/out/6.jpg.json new file mode 100644 index 0000000..182e08a --- /dev/null +++ b/out/6.jpg.json @@ -0,0 +1 @@ +{"icao24": "89616f", "registration": "A6-EFI", "manufacturericao": "BOEING", "manufacturername": "Boeing", "model": "777 F1H", "typecode": "B77L", "serialnumber": "35609", "linenumber": "", "icaoaircrafttype": "L2J", "operator": "", "operatorcallsign": "EMIRATES", "operatoricao": "UAE", "operatoriata": "", "owner": "Emirates Airline", "testreg": "", "registered": "", "reguntil": "", "status": "", "built": "", "firstflightdate": "", "seatconfiguration": "", "engines": "", "modes": "false", "adsb": "false", "acars": "false", "notes": "", "categoryDescription": "No ADS-B Emitter Category Information"} \ No newline at end of file diff --git a/out/7.jpg.json b/out/7.jpg.json new file mode 100644 index 0000000..9e78cb0 --- /dev/null +++ b/out/7.jpg.json @@ -0,0 +1 @@ +{"icao24": "780b5c", "registration": "B-5850", "manufacturericao": "BOEING", "manufacturername": "Boeing", "model": "737NG 89L/W", "typecode": "B738", "serialnumber": "41311", "linenumber": "", "icaoaircrafttype": "L2J", "operator": "", "operatorcallsign": "", "operatoricao": "", "operatoriata": "", "owner": "Dalian Airlines", "testreg": "", "registered": "", "reguntil": "", "status": "", "built": "", "firstflightdate": "", "seatconfiguration": "", "engines": "", "modes": "false", "adsb": "false", "acars": "false", "notes": "", "categoryDescription": "No ADS-B Emitter Category Information"} \ No newline at end of file diff --git a/out/8.jpg.json b/out/8.jpg.json new file mode 100644 index 0000000..fc736c0 --- /dev/null +++ b/out/8.jpg.json @@ -0,0 +1 @@ +{"icao24": "8963a1", "registration": "A6-EFN", "manufacturericao": "BOEING", "manufacturername": "Boeing", "model": "777 F1H", "typecode": "B77L", "serialnumber": "42232", "linenumber": "", "icaoaircrafttype": "L2J", "operator": "", "operatorcallsign": "EMIRATES", "operatoricao": "UAE", "operatoriata": "", "owner": "Emirates Airline", "testreg": "", "registered": "", "reguntil": "", "status": "", "built": "", "firstflightdate": "", "seatconfiguration": "", "engines": "", "modes": "false", "adsb": "false", "acars": "false", "notes": "", "categoryDescription": "No ADS-B Emitter Category Information"} \ No newline at end of file diff --git a/out/9.jpg.json b/out/9.jpg.json new file mode 100644 index 0000000..ef24291 --- /dev/null +++ b/out/9.jpg.json @@ -0,0 +1 @@ +{"icao24": "06a13b", "registration": "A7-BAY", "manufacturericao": "BOEING", "manufacturername": "Boeing", "model": "777 3DZER", "typecode": "B77W", "serialnumber": "41778", "linenumber": "1078", "icaoaircrafttype": "L2J", "operator": "Qatar Airways", "operatorcallsign": "QATARI", "operatoricao": "QTR", "operatoriata": "QR", "owner": "Qatar Airways", "testreg": "", "registered": "2013-03-01", "reguntil": "", "status": "", "built": "2013-01-01", "firstflightdate": "", "seatconfiguration": "", "engines": "", "modes": "false", "adsb": "false", "acars": "false", "notes": "", "categoryDescription": "No ADS-B Emitter Category Information"} \ No newline at end of file