def postprocess(self, inputs: Any, **postprocess_parameters: Dict) -> Any: model_outputs, images, orig_images = unzip(inputs) orig_img_shapes = [img.shape for img in orig_images] output = self.detector.postprocessing(model_outputs, images, orig_img_shapes, **postprocess_parameters) return unzip([output, orig_images])
def postprocess(self, inputs: Any, **postprocess_parameters: Dict) -> Any: confidences, region_ids, count_lines = self.detector.unzip_predicted( unzip(inputs)) count_lines = self.detector.custom_count_lines_id_to_all_count_lines( count_lines) region_names = self.detector.get_region_labels(region_ids) return unzip( [region_ids, region_names, count_lines, confidences, inputs])
def forward(self, inputs: Any, **forward_parameters: Dict) -> Any: images_bboxs, images = unzip(self.number_plate_localization(inputs, **forward_parameters)) images_points, _ = unzip(self.number_plate_key_points_detection(unzip([images, images_bboxs]), **forward_parameters)) filled_images = [] for key_ponts, image in zip(images_points, images): image = image.astype(np.uint8) for cntr in key_ponts: cntr = np.array(cntr, dtype=np.int32) cv2.drawContours(image, [cntr], -1, (0, 0, 0), -1) filled_images.append(image) return filled_images
def forward_detection_np(self, images: Any, **forward_parameters: Dict): orig_img_shapes = [img.shape for img in images] model_inputs = self.localization_detector.normalize_imgs( images, **forward_parameters) localization_detector_output = self.localization_detector.model( model_inputs)[0] shape = localization_detector_output.shape localization_detector_output = localization_detector_output.reshape( (shape[0], 1, shape[1], shape[2])) images_target_boxes = self.localization_detector.postprocessing( localization_detector_output, model_inputs, orig_img_shapes, **forward_parameters) images_points, images_mline_boxes = self.key_points_detector.detect( unzip([images, images_target_boxes]), **forward_parameters) zones, image_ids = crop_number_plate_zones_from_images( images, images_points) zones_model_input = self.option_detector.preprocess(zones) options_output = self.option_detector.forward(zones_model_input) options_output = [p.cpu().numpy() for p in options_output] confidences, region_ids, count_lines = self.option_detector.unzip_predicted( options_output) count_lines = self.option_detector.custom_count_lines_id_to_all_count_lines( count_lines) region_names = self.option_detector.get_region_labels(region_ids) return (region_ids, region_names, count_lines, confidences, options_output, zones, image_ids, images_target_boxes, images, images_points, images_mline_boxes)
def forward(self, inputs: Any, **forward_parameters: Dict) -> Any: images_bboxs, images = unzip(self.number_plate_localization(inputs, **forward_parameters)) zones, image_ids = crop_number_plate_rect_zones_from_images(images, images_bboxs) region_names = [self.text_reader_name for _ in zones] lines = [self.default_lines_count for _ in zones] number_plate_text_reading_res = unzip( self.number_plate_text_reading(unzip([zones, region_names, lines]), **forward_parameters)) if len(number_plate_text_reading_res): texts, _ = number_plate_text_reading_res else: texts = [] (texts, zones) = group_by_image_ids(image_ids, (texts, zones)) return unzip([images, images_bboxs, zones, texts])
def forward_recognition_np(self, region_ids, region_names, count_lines, confidences, zones, image_ids, images_bboxs, images, images_points, **forward_parameters): number_plate_text_reading_res = unzip( self.number_plate_text_reading( unzip([zones, region_names, count_lines]), **forward_parameters)) if len(number_plate_text_reading_res): texts, _ = number_plate_text_reading_res else: texts = [] (region_ids, region_names, count_lines, confidences, texts, zones) = \ group_by_image_ids(image_ids, (region_ids, region_names, count_lines, confidences, texts, zones)) return unzip([ images, images_bboxs, images_points, zones, region_ids, region_names, count_lines, confidences, texts ])
def forward(self, images: Any, **forward_parameters: Dict) -> Any: model_inputs = self.detector.normalize_imgs(images, **forward_parameters) model_outputs = self.detector.model(model_inputs)[0] shape = model_outputs.shape model_outputs = model_outputs.reshape( (shape[0], 1, shape[1], shape[2])) return unzip([model_outputs, model_inputs, images])
def detect(data: Dict): img_path = data['path'] try: result = number_plate_detection_and_reading([img_path]) (images, images_bboxs, images_points, images_zones, region_ids, region_names, count_lines, confidences, texts) = unzip(result) return ujson.dumps(dict(res=texts, img_path=img_path)) except Exception as e: exc_type, exc_value, exc_tb = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_tb) return ujson.dumps(dict(error=str(e), img_path=img_path))
def forward_detection_np(self, inputs: Any, **forward_parameters: Dict): images_bboxs, images = unzip( self.number_plate_localization(inputs, **forward_parameters)) images_points, images_mline_boxes = unzip( self.number_plate_key_points_detection( unzip([images, images_bboxs]), **forward_parameters)) zones, image_ids = crop_number_plate_zones_from_images( images, images_points) if self.number_plate_classification is None: region_ids = [-1 for _ in zones] region_names = [self.default_label for _ in zones] count_lines = [self.default_lines_count for _ in zones] confidences = [-1 for _ in zones] predicted = [-1 for _ in zones] else: (region_ids, region_names, count_lines, confidences, predicted) = unzip( self.number_plate_classification(zones, **forward_parameters)) return (region_ids, region_names, count_lines, confidences, predicted, zones, image_ids, images_bboxs, images, images_points, images_mline_boxes)
def postprocess(self, inputs: Any, **postprocess_parameters: Dict) -> Any: images_target_boxes, images = unzip( NumberPlateLocalization.postprocess(self, inputs, **postprocess_parameters)) filled_number_plate = [] for image_target_boxes, img in zip(images_target_boxes, images): img = img.astype(np.uint8) for target_box in image_target_boxes: cv2.rectangle(img, (int(target_box[0]), int(target_box[1])), (int(target_box[2]), int(target_box[3])), (0, 0, 0), -1) filled_number_plate.append(img) return filled_number_plate
def read_number_plates(url): global number_plate_detection_and_reading with tempfile.NamedTemporaryFile() as fp: with urlopen(url) as response: fp.write(response.read()) result = number_plate_detection_and_reading([fp.name]) (images, images_bboxs, images_points, images_zones, region_ids, region_names, count_lines, confidences, texts) = unzip(result) return texts[0], region_names[0]
def post(self): data = tornado.escape.json_decode(self.request.body) img_path = data['path'] try: result = number_plate_detection_and_reading([img_path]) (images, images_bboxs, images_points, images_zones, region_ids, region_names, count_lines, confidences, texts) = unzip(result) res = ujson.dumps(dict(res=texts, img_path=img_path)) self.write(res) except Exception as e: exc_type, exc_value, exc_tb = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_tb) res = ujson.dumps(dict(error=str(e), img_path=img_path)) self.write(res)
def forward(self, inputs: Any, **forward_parameters: Dict) -> Any: images, labels, lines = unzip(inputs) model_inputs = self.detector.preprocess(images, labels, lines) model_outputs = self.detector.forward(model_inputs) model_outputs = self.detector.postprocess(model_outputs) return unzip([images, model_outputs, labels])
def postprocess(self, inputs: Any, **postprocess_parameters: Dict) -> Any: return unzip( self.detector.postprocess(inputs, **postprocess_parameters))
def forward(self, images: Any, **forward_parameters: Dict) -> Any: model_inputs = self.detector.normalize_imgs(images, **forward_parameters) model_outputs = self.detector.forward(model_inputs) return unzip([model_outputs, model_inputs, images])
import os from glob import glob from _paths import nomeroff_net_dir from nomeroff_net import pipeline from nomeroff_net.tools import unzip if __name__ == '__main__': multiline_number_plate_detection_and_reading = pipeline( "multiline_number_plate_detection_and_reading", image_loader="opencv") result = multiline_number_plate_detection_and_reading( glob( os.path.join(nomeroff_net_dir, './data/examples/multiline_images/*'))) (images, images_bboxs, images_points, images_zones, region_ids, region_names, count_lines, confidences, texts) = unzip(result) print(texts)
import os from _paths import nomeroff_net_dir from nomeroff_net import pipeline from nomeroff_net.tools import unzip if __name__ == '__main__': number_plate_detection_and_reading = pipeline("number_plate_detection_and_reading", image_loader="opencv") result = number_plate_detection_and_reading([ os.path.join(nomeroff_net_dir, './data/examples/oneline_images/example1.jpeg'), ]) (images, images_bboxs, images_points, images_zones, region_ids, region_names, count_lines, confidences, texts) = unzip(result) # (['AC4921CB'], ['RP70012', 'JJF509']) print(texts)
def postprocess(self, inputs: Any, **postprocess_parameters: Dict) -> Any: images, model_outputs, labels = unzip(inputs) outputs = text_postprocessing(model_outputs, labels) return unzip([outputs, images])
def preprocess(self, inputs: Any, **preprocess_parameters: Dict) -> Any: images, labels, lines = unzip(inputs) images = [self.image_loader.load(item) for item in images] return unzip([images, labels, lines])
def preprocess(self, inputs: Any, **preprocess_parameters: Dict) -> Any: images = [self.image_loader.load(item) for item in inputs] batch_input_image, batch_origin_h, batch_origin_w = self.detector.yolov5_wrapper.prepare_batch_input_image(images) return unzip([images, batch_input_image, batch_origin_h, batch_origin_w])
def forward(self, inputs: Any, **forward_parameters: Dict) -> Any: model_output = self.detector.forward(inputs) return unzip([p.cpu().numpy() for p in model_output])
def postprocess(self, inputs: Any, **postprocess_parameters: Dict) -> Any: images, detected_images_bboxs = unzip(inputs) detected_images_bboxs = self.detector.postprocessing(detected_images_bboxs, **postprocess_parameters) return unzip([detected_images_bboxs, images])
def forward(self, inputs: Any, **forward_parameters: Dict) -> Any: images, batch_input_image, batch_origin_h, batch_origin_w = unzip(inputs) detected_images_bboxs = self.detector.yolov5_wrapper.infer(batch_input_image, batch_origin_h, batch_origin_w) return unzip([images, detected_images_bboxs])