def main(): # load some VOC2007 images for test #image = Image.open("000001.jpg") #boxes = np.array([[48,240,195,371,11],[8,12,352,498,14]]) image = Image.open("000004.jpg") boxes = np.array([[13,311,84,362,6],[362,330,500,389,6],[235,328,334,375,6],[175,327,252,364,6],[139,320,189,359,6],[108,325,150,353,6],[84,323,121,350,6]]) #image = Image.open("000010.jpg") #boxes = np.array([[87,97,258,427,12],[133,72,245,284,14]]) classes = boxes[:, -1] scores = [1.0]*len(classes) class_names = get_classes('../../configs/voc_classes.txt') colors = get_colors(class_names) image_origin = draw_boxes(np.array(image, dtype='uint8'), boxes[:, :4], classes, scores, class_names, colors) image, boxes = random_rotate(image, boxes, prob=1.0) image = draw_boxes(np.array(image, dtype='uint8'), boxes[:, :4], classes, scores, class_names, colors) Image.fromarray(image_origin).show() Image.fromarray(image).show()
def __init__(self): self.log = logging.getLogger('EMSfIIoT') # Load TFLite model and allocate tensors. try: self.interpreter = tflite.Interpreter( model_path="weights/emsfiiot_lite.h5") except: self.interpreter = tf.lite.Interpreter( model_path="weights/emsfiiot_lite.h5") self.interpreter.allocate_tensors() # Get input and output tensors. self.input_details = self.interpreter.get_input_details() self.output_details = self.interpreter.get_output_details() # check the type of the input tensor self.floating_model = self.input_details[0]['dtype'] == np.float32 height = self.input_details[0]['shape'][1] width = self.input_details[0]['shape'][2] self.model_image_size = (width, height) self.anchors = get_anchors("configs/yolo3_anchors.txt") self.class_names = get_classes("configs/emsfiiot_classes.txt") self.colors = get_colors(self.class_names)
def handle_prediction(prediction, image_file, image, image_shape, anchors, class_names, model_image_size): start = time.time() if len(anchors) == 5: # YOLOv2 use 5 anchors and have only 1 prediction assert len(prediction) == 1, 'invalid YOLOv2 prediction number.' boxes, classes, scores = yolo2_postprocess_np(prediction[0], image_shape, anchors, len(class_names), model_image_size) else: boxes, classes, scores = yolo3_postprocess_np(prediction, image_shape, anchors, len(class_names), model_image_size) end = time.time() print("PostProcess time: {:.8f}ms".format((end - start) * 1000)) print('Found {} boxes for {}'.format(len(boxes), image_file)) for box, cls, score in zip(boxes, classes, scores): xmin, ymin, xmax, ymax = box print("Class: {}, Score: {}, Box: {},{}".format( class_names[cls], score, (xmin, ymin), (xmax, ymax))) colors = get_colors(class_names) image = draw_boxes(image, boxes, classes, scores, class_names, colors) Image.fromarray(image).show() return
def handle_prediction(prediction, image_file, image, image_shape, anchors, class_names, model_input_shape, elim_grid_sense, v5_decode, output_path): start = time.time() if len(anchors) == 5: # YOLOv2 use 5 anchors and have only 1 prediction assert len(prediction) == 1, 'invalid YOLOv2 prediction number.' boxes, classes, scores = yolo2_postprocess_np(prediction[0], image_shape, anchors, len(class_names), model_input_shape, elim_grid_sense=elim_grid_sense) else: if v5_decode: boxes, classes, scores = yolo5_postprocess_np(prediction, image_shape, anchors, len(class_names), model_input_shape, elim_grid_sense=True) #enable "elim_grid_sense" by default else: boxes, classes, scores = yolo3_postprocess_np(prediction, image_shape, anchors, len(class_names), model_input_shape, elim_grid_sense=elim_grid_sense) end = time.time() print("PostProcess time: {:.8f}ms".format((end - start) * 1000)) print('Found {} boxes for {}'.format(len(boxes), image_file)) for box, cls, score in zip(boxes, classes, scores): xmin, ymin, xmax, ymax = box print("Class: {}, Score: {}, Box: {},{}".format(class_names[cls], score, (xmin, ymin), (xmax, ymax))) colors = get_colors(len(class_names)) image = draw_boxes(image, boxes, classes, scores, class_names, colors) # save or show result if output_path: os.makedirs(output_path, exist_ok=True) output_file = os.path.join(output_path, os.path.basename(image_file)) Image.fromarray(image).save(output_file) else: Image.fromarray(image).show() return
def __init__(self, **kwargs): super(YOLO_np, self).__init__() self.__dict__.update(self._defaults) # set up default values self.__dict__.update(kwargs) # and update with user overrides self.class_names = get_classes(self.classes_path) self.anchors = get_anchors(self.anchors_path) self.colors = get_colors(self.class_names) K.set_learning_phase(0) self.yolo_model = self._generate_model()
def dataset_visualize(annotation_file, classes_path): annotation_lines = get_dataset(annotation_file, shuffle=False) # get class names and count class item number class_names = get_classes(classes_path) colors = get_colors(len(class_names)) pbar = tqdm(total=len(annotation_lines), desc='Visualize dataset') for i, annotation_line in enumerate(annotation_lines): pbar.update(1) line = annotation_line.split() image = Image.open(line[0]).convert('RGB') image = np.array(image, dtype='uint8') boxes = np.array( [np.array(list(map(int, box.split(',')))) for box in line[1:]]) classes = boxes[:, -1] boxes = boxes[:, :-1] scores = np.array([1.0] * len(classes)) image = draw_boxes(image, boxes, classes, scores, class_names, colors, show_score=False) # show image file info image_file_name = os.path.basename(line[0]) cv2.putText(image, image_file_name + '({}/{})'.format(i + 1, len(annotation_lines)), (3, 15), cv2.FONT_HERSHEY_PLAIN, fontScale=1, color=(255, 0, 0), thickness=1, lineType=cv2.LINE_AA) # convert to BGR for cv2.imshow image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) cv2.namedWindow("Image", 0) cv2.imshow("Image", image) keycode = cv2.waitKey(0) & 0xFF if keycode == ord('q') or keycode == 27: # 27 is keycode for Esc break pbar.close()
def get_prediction_class_records(model, model_format, annotation_records, anchors, class_names, model_image_size, conf_threshold, save_result): ''' Do the predict with YOLO model on annotation images to get predict class dict predict class dict would contain image_name, coordinary and score, and sorted by score: pred_classes_records = { 'car': [ ['000001.jpg','94,115,203,232',0.98], ['000002.jpg','82,64,154,128',0.93], ... ], ... } ''' if model_format == 'MNN': #MNN inference engine need create session session = model.createSession() # create txt file to save prediction result, with # save format as annotation file but adding score, like: # # path/to/img1.jpg 50,100,150,200,0,0.86 30,50,200,120,3,0.95 # os.makedirs('result', exist_ok=True) result_file = open(os.path.join('result','detection_result.txt'), 'w') pred_classes_records = OrderedDict() pbar = tqdm(total=len(annotation_records), desc='Eval model') for (image_name, gt_records) in annotation_records.items(): image = Image.open(image_name) if image.mode != 'RGB': image = image.convert('RGB') image_array = np.array(image, dtype='uint8') # support of tflite model if model_format == 'TFLITE': pred_boxes, pred_classes, pred_scores = yolo_predict_tflite(model, image, anchors, len(class_names), conf_threshold) # support of MNN model elif model_format == 'MNN': pred_boxes, pred_classes, pred_scores = yolo_predict_mnn(model, session, image, anchors, len(class_names), conf_threshold) # support of TF 1.x frozen pb model elif model_format == 'PB': pred_boxes, pred_classes, pred_scores = yolo_predict_pb(model, image, anchors, len(class_names), model_image_size, conf_threshold) # support of ONNX model elif model_format == 'ONNX': pred_boxes, pred_classes, pred_scores = yolo_predict_onnx(model, image, anchors, len(class_names), conf_threshold) # normal keras h5 model elif model_format == 'H5': pred_boxes, pred_classes, pred_scores = yolo_predict_keras(model, image, anchors, len(class_names), model_image_size, conf_threshold) else: raise ValueError('invalid model format') #print('Found {} boxes for {}'.format(len(pred_boxes), image_name)) pbar.update(1) # save prediction result to txt result_file.write(image_name) for box, cls, score in zip(pred_boxes, pred_classes, pred_scores): xmin, ymin, xmax, ymax = box box_annotation = " %d,%d,%d,%d,%d,%f" % ( xmin, ymin, xmax, ymax, cls, score) result_file.write(box_annotation) result_file.write('\n') result_file.flush() if save_result: gt_boxes, gt_classes, gt_scores = transform_gt_record(gt_records, class_names) result_dir=os.path.join('result','detection') os.makedirs(result_dir, exist_ok=True) colors = get_colors(class_names) image_array = draw_boxes(image_array, gt_boxes, gt_classes, gt_scores, class_names, colors=None, show_score=False) image_array = draw_boxes(image_array, pred_boxes, pred_classes, pred_scores, class_names, colors) image = Image.fromarray(image_array) # here we handle the RGBA image if(len(image.split()) == 4): r, g, b, a = image.split() image = Image.merge("RGB", (r, g, b)) image.save(os.path.join(result_dir, image_name.split(os.path.sep)[-1])) # Nothing detected if pred_boxes is None or len(pred_boxes) == 0: continue for box, cls, score in zip(pred_boxes, pred_classes, pred_scores): pred_class_name = class_names[cls] xmin, ymin, xmax, ymax = box coordinate = "{},{},{},{}".format(xmin, ymin, xmax, ymax) #append or add predict class item record = [os.path.basename(image_name), coordinate, score] if pred_class_name in pred_classes_records: pred_classes_records[pred_class_name].append(record) else: pred_classes_records[pred_class_name] = list([record]) # sort pred_classes_records for each class according to score for pred_class_list in pred_classes_records.values(): pred_class_list.sort(key=lambda ele: ele[2], reverse=True) pbar.close() result_file.close() return pred_classes_records
def get_prediction_class_records(model, model_format, annotation_records, anchors, class_names, model_image_size, conf_threshold, save_result): ''' Do the predict with YOLO model on annotation images to get predict class dict predict class dict would contain image_name, coordinary and score, and sorted by score: pred_classes_records = { 'car': [ ['00001.jpg','94,115,203,232',0.98], ['00002.jpg','82,64,154,128',0.93], ... ], ... } ''' if model_format == 'MNN': #MNN inference engine need create session session = model.createSession() pred_classes_records = {} pbar = tqdm(total=len(annotation_records), desc='Eval model') for (image_name, gt_records) in annotation_records.items(): image = Image.open(image_name) image_array = np.array(image, dtype='uint8') # support of tflite model if model_format == 'TFLITE': pred_boxes, pred_classes, pred_scores = yolo_predict_tflite( model, image, anchors, len(class_names), conf_threshold) # support of MNN model elif model_format == 'MNN': pred_boxes, pred_classes, pred_scores = yolo_predict_mnn( model, session, image, anchors, len(class_names), conf_threshold) # support of TF 1.x frozen pb model elif model_format == 'PB': pred_boxes, pred_classes, pred_scores = yolo_predict_pb( model, image, anchors, len(class_names), model_image_size, conf_threshold) # normal keras h5 model elif model_format == 'H5': pred_boxes, pred_classes, pred_scores = yolo_predict_keras( model, image, anchors, len(class_names), model_image_size, conf_threshold) else: raise ValueError('invalid model format') #print('Found {} boxes for {}'.format(len(pred_boxes), image_name)) pbar.update(1) if save_result: gt_boxes, gt_classes, gt_scores = transform_gt_record( gt_records, class_names) result_dir = os.path.join('result', 'detection') touchdir(result_dir) colors = get_colors(class_names) image_array = draw_boxes(image_array, gt_boxes, gt_classes, gt_scores, class_names, colors=None, show_score=False) image_array = draw_boxes(image_array, pred_boxes, pred_classes, pred_scores, class_names, colors) image = Image.fromarray(image_array) # here we handle the RGBA image if (len(image.split()) == 4): r, g, b, a = image.split() image = Image.merge("RGB", (r, g, b)) image.save( os.path.join(result_dir, image_name.split(os.path.sep)[-1])) # Nothing detected if pred_boxes is None or len(pred_boxes) == 0: continue for box, cls, score in zip(pred_boxes, pred_classes, pred_scores): pred_class_name = class_names[cls] xmin, ymin, xmax, ymax = box coordinate = "{},{},{},{}".format(xmin, ymin, xmax, ymax) #append or add predict class item if pred_class_name in pred_classes_records: pred_classes_records[pred_class_name].append( [image_name, coordinate, score]) else: pred_classes_records[pred_class_name] = list( [[image_name, coordinate, score]]) # sort pred_classes_records for each class according to score for pred_class_list in pred_classes_records.values(): pred_class_list.sort(key=lambda ele: ele[2], reverse=True) pbar.close() return pred_classes_records
if not args.disable_ovtf: # Print list of available backends print('Available Backends:') backends_list = ovtf.list_backends() for backend in backends_list: print(backend) ovtf.set_backend(backend_name) else: ovtf.disable() cap = None images = [] if label_file: labels = load_labels(label_file) colors = get_colors(labels) input_mode = get_input_mode(input_file) if input_mode == "video": cap = cv2.VideoCapture(input_file) elif input_mode == "camera": cap = cv2.VideoCapture(0) elif input_mode == 'image': images = [input_file] elif input_mode == 'directory': if not os.path.isdir(input_file): raise AssertionError("Path doesn't exist {0}".format(input_file)) images = [ os.path.join(input_file, fname) for fname in os.listdir(input_file) if fname.lower().endswith(('.png', '.jpg', '.jpeg', '.tif', '.tiff', '.bmp')) ]
input_mean = args.input_mean if args.input_std: input_std = args.input_std if args.backend: backend_name = args.backend if args.conf_threshold: conf_threshold = args.conf_threshold if args.iou_threshold: iou_threshold = args.iou_threshold # Load graph and process input image graph = load_graph(model_file) # Load the labels if label_file: classes = load_coco_names(label_file) colors = get_colors(classes) anchors = get_anchors(anchor_file) input_name = "import/" + input_layer input_operation = graph.get_operation_by_name(input_name) output_operation = [ graph.get_operation_by_name("import/" + output_name) for output_name in output_layer ] if not args.disable_ovtf: # Print list of available backends print('Available Backends:') backends_list = ovtf.list_backends() for backend in backends_list: print(backend) ovtf.set_backend(backend_name) else: