def license_plate_recognition(self): training_successful = DetectChars.load_and_train() if not training_successful: print("\nerror: KNN training was not successful\n") return file = ''.join(self.plate_image_file[0]) img_original_scene = cv2.imread(file) if img_original_scene is None: msg = QtWidgets.QErrorMessage() msg.showMessage("Can't open Image from file") exit(-1) return list_of_possible_plates = DetectPlates.detect_plates_in_scene( img_original_scene) list_of_possible_plates = DetectChars.detect_chars_in_plates( list_of_possible_plates) if len(list_of_possible_plates) == 0: msg = QtWidgets.QErrorMessage() msg.showMessage("No license plate were detected") else: list_of_possible_plates.sort( key=lambda possible_plate: len(possible_plate.strChars), reverse=True) lic_plate = list_of_possible_plates[0] if len(lic_plate.strChars) == 0: msg = QtWidgets.QErrorMessage() msg.showMessage("No characters were detected") return draw_rectangle_around_plate(img_original_scene, lic_plate) f = open('Plates.txt', 'a') f.write(lic_plate.strChars) f.close() write_license_plate_chars_on_image(img_original_scene, lic_plate) cv2.imwrite("img_original_scene.png", img_original_scene) pixmap = QtGui.QPixmap("img_original_scene.png") self.label.setPixmap(pixmap)
def main(image_url): # # loading image resp = urllib.request.urlopen(image_url) image_url = np.asarray(bytearray(resp.read()), dtype="uint8") im_orig = cv2.imdecode(image_url, cv2.IMREAD_COLOR) # im_orig = cv2.imread(image_url) if im_orig is None: return 'AA00AAA' # croping useful area and resizing h, w = im_orig.shape[:2] im_orig = im_orig[int(h / 100 * 35):h - 20, 40:w - 40] im_orig = cv2.resize(im_orig, (0, 0), fx=1.4, fy=1.4, interpolation=cv2.INTER_CUBIC) psb_plates = DetectPlates.detect_plates_in_scene(im_orig) # detect plates if len(psb_plates) > 1: psb_plates = [choose_plate_to_handle(psb_plates) ] # we don't need more than one plate psb_plates = DetectChars.detect_chars_in_plates( psb_plates) # detect chars in plates # plates with more than 8 symbols untruth psb_plates = [x for x in psb_plates if len(x.strChars) < 8] if len(psb_plates) == 0: # if no plates were found return 'AA00AAA' else: # plates with little number of symbols untruth psb_plates.sort(key=lambda possiblePlate: len(possiblePlate.strChars), reverse=True) licPlate = psb_plates[0] if len(licPlate.strChars) == 0: return 'AA00AAA' licPlate.strChars = validate_for_britain(licPlate.strChars) return licPlate.strChars