def infer(image: str, phi: int = 0, saved_model: str = './savedmodel', classes: dict = None, score_threshold: float = 0.3, nms_threshold: float = 0.5, device: str = 'gpu'): if device != 'gpu': os.environ['CUDA_VISIBLE_DEVICES'] = '-1' #Using CPU else: os.environ['CUDA_VISIBLE_DEVICES'] = '0' #Using GPU #For COCO dataset if classes == None: classes = { value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values() } #select resolution according to architecture image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] #To get different color for each class num_classes = len(classes.values()) colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] #load the model model = load_model(saved_model) #load and preprocess image img = cv2.imread(image) src_image = img.copy() img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) h, w = img.shape[:2] img, scale = preprocess_image(img, image_size=image_size) #detect and post process start = time.time() boxes, scores, labels = model.predict_on_batch( [np.expand_dims(img, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze( labels) end = time.time() boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # print(f'infer time: {end-start}, fps: {1/(end-start)}') # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # indices = tf.image.non_max_suppression(boxes,scores,max_output_size=[100],iou_threshold = nms_threshold,score_threshold = score_threshold) boxes = boxes[indices] labels = labels[indices] #draw boxes on the original image draw_boxes(src_image, boxes, scores, labels, colors, classes) return src_image
def main(): os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 0 weighted_bifpn = False model_path = 'checkpoints/flir_59_0.1409_0.6001.h5' image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes classes = { value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values() } num_classes = 3 score_threshold = 0.01 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] _, model = efficientdet(phi=phi, weighted_bifpn=weighted_bifpn, num_classes=num_classes, score_threshold=score_threshold) model.load_weights(model_path, by_name=True) video_path = r'G:\datasets\iray\cap\20200515_100016.avi' video_path = r'G:\datasets\iray\cap\20200515_100016.avi' video_path = r'G:\projects\20200513_102922.avi' cap = cv2.VideoCapture(video_path) while True: ret, image = cap.read() if not ret: break src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', src_image) cv2.waitKey(0)
def main(): os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 0 # Architecture number D# weighted_bifpn = True model_path = 'efficientdet-d0.h5' image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes classes = { value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values() } num_classes = 90 score_threshold = 0.3 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] # _, model = efficientdet(phi=phi, # weighted_bifpn=weighted_bifpn, # num_classes=num_classes, # score_threshold=score_threshold) # model.load_weights(model_path, by_name=True) # tf.saved_model.save(model,'savedmodel') #Convert .h5 weights to .pb model model = load_model('savedmodel/') for image_path in glob.glob('test/2.jpg'): image = cv2.imread(image_path) src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', src_image) cv2.waitKey(0) cv2.imwrite('out.jpg', src_image)
def main(): os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 0 weighted_bifpn = False model_path = 'checkpoints/deepfashion.h5' image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes classes = { value['id'] - 1: value['name'] for value in json.load(open('deepfashion_13.json', 'r')).values() } num_classes = 13 score_threshold = 0.3 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] #_, model = efficientdet(phi=phi, # weighted_bifpn=weighted_bifpn, # num_classes=num_classes, # score_threshold=score_threshold) models = EfficientDetModel(0) model = models.p_model model.load_weights(model_path, by_name=True) # 'datasets/VOC2007/JPEGImages/*.jpg' for image_path in glob.glob('data/sample_val/image/000002.jpg'): image = cv2.imread(image_path) src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels, masks = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) #cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imwrite('results/image.jpg', src_image) #cv2.imread('results/image.jpg') '''
def main(image_path): os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 1 weighted_bifpn = True model_path = 'efficientdet-d1.h5' image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes classes = { value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values() } num_classes = 90 score_threshold = 0.3 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] _, model = efficientdet(phi=phi, weighted_bifpn=weighted_bifpn, num_classes=num_classes, score_threshold=score_threshold) model.load_weights(model_path, by_name=True) #for image_path in glob.glob('datasets/VOC2007/JPEGImages/*.jpg'): try: image = cv2.imread(image_path) src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', src_image) cv2.waitKey(0) except Exception as ex: print(ex)
def main(): os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 1 weighted_bifpn = False model_path = 'checkpoints/2019-12-03/pascal_05_0.6283_1.1975_0.8029.h5' image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] classes = [ 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor', ] num_classes = len(classes) score_threshold = 0.5 colors = [np.random.randint(0, 256, 3).tolist() for i in range(num_classes)] model, prediction_model = efficientdet(phi=phi, weighted_bifpn=weighted_bifpn, num_classes=num_classes, score_threshold=score_threshold) prediction_model.load_weights(model_path, by_name=True) video_path = 'datasets/video.mp4' cap = cv2.VideoCapture(video_path) while True: ret, frame = cap.read() if not ret: break h, w = frame.shape[:2] image, scale, offset_h, offset_w = preprocess_image(frame, image_size=image_size) anchors = anchors_for_shape((image_size, image_size)) boxes_batch, scores_batch, labels_batch = prediction_model.predict_on_batch([np.expand_dims(image, axis=0), np.expand_dims(anchors, axis=0)]) for i, (boxes, scores, labels) in enumerate(zip(boxes_batch, scores_batch, labels_batch)): boxes = post_process_boxes(boxes=boxes, scale=scale, offset_h=offset_h, offset_w=offset_w, height=h, width=w) indices = np.where(scores[:] > score_threshold)[0] boxes = boxes[indices] labels = labels[indices] draw_boxes(frame, boxes, scores, labels, colors, classes) cv2.imshow('image', frame) cv2.waitKey(1)
def main(): os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 0 weighted_bifpn = True # model_path = 'efficientdet-d1.h5' # model_path = r"checkpoints\2020-06-23\csv_50_0.0431_0.9146.h5" model_path = r"checkpoints\2020-06-23\1\csv_44_0.0448_1.0142.h5" # class_path = 'coco_90.json' class_path = r"train_car_object\classes.json" image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes classes = {value['id'] - 1: value['name'] for value in json.load(open(class_path, 'r')).values()} num_classes = 4 score_threshold = 0.5 colors = [np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)] _, model = efficientdet(phi=phi, weighted_bifpn=weighted_bifpn, num_classes=num_classes, score_threshold=score_threshold) model.load_weights(model_path, by_name=True) # for image_path in glob.glob('datasets/VOC2007/JPEGImages/*.jpg'): for image_path in glob.glob(r"E:\DATA\@car\car_photo\carplate_test\*.jpg"): image = cv2.imread(image_path) src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels = model.predict_on_batch([np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze(labels) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', src_image) cv2.waitKey(0)
def draw(self, model_body, class_names, anchors, image_data, out_path="output_images", out_crop="output_crop", save_all=True): # load best weight model_body.load_weights(self.config.yoloWeights) yolo_outputs = yolo_head(model_body.output, anchors, len(class_names)) input_image_shape = K.placeholder(shape=(2, )) boxes, scores, classes = yolo_eval(yolo_outputs, input_image_shape, score_threshold=0.80, iou_threshold=0.0) sess = K.get_session( ) # TODO: Remove dependence on Tensorflow session. if not os.path.exists(out_path): os.makedirs(out_path) for i in range(len(image_data)): out_boxes, out_scores, out_classes = sess.run( [boxes, scores, classes], feed_dict={ model_body.input: image_data[i], input_image_shape: [image_data.shape[2], image_data.shape[3]], K.learning_phase(): 0 }) print('Found {} boxes for image.'.format(len(out_boxes))) print(out_boxes) # Plot image with predicted boxes. image_with_boxes, origin_image, crop_boxs = draw_boxes( image_data[i][0], out_boxes, out_classes, class_names, out_scores) for ib, crop_box in enumerate(crop_boxs): origin_image.crop(crop_box).save( os.path.join(out_crop, str(i) + '-' + str(ib) + '.png')) # Save the image: if save_all or (len(out_boxes) > 0): cv.imwrite(os.path.join(out_path, str(i) + '.jpg'), image_with_boxes)
def main(args=None): if len(sys.argv) is 3: model_path = str(sys.argv[1]) image_data = os.path.join(str(sys.argv[2]), "*.jpg") else: print( "Pass model path and image data path in respectively as command line argument" ) exit() os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 4 weighted_bifpn = False model_path = model_path image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes coco_classes = { value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values() } coco_num_classes = 90 # Dhaka-ai classes dhaka_ai_classes = { 0: 'ambulance', 1: 'auto rickshaw', 2: 'bicycle', 3: 'bus', 4: 'car', 5: 'garbagevan', 6: 'human hauler', 7: 'minibus', 8: 'minivan', 9: 'motorbike', 10: 'pickup', 11: 'army vehicle', 12: 'policecar', 13: 'rickshaw', 14: 'scooter', 15: 'suv', 16: 'taxi', 17: 'three wheelers (CNG)', 18: 'truck', 19: 'van', 20: 'wheelbarrow' } dhaka_ai_num_classes = 21 score_threshold = 0.05 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(dhaka_ai_num_classes) ] _, model = efficientdet(phi=phi, weighted_bifpn=weighted_bifpn, num_classes=dhaka_ai_num_classes, score_threshold=score_threshold) model.load_weights(model_path, by_name=True) for image_path in glob.glob(image_data): image = cv2.imread(image_path) src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold # indices = np.where(scores[:] > score_threshold)[0] # select those detections # boxes = boxes[indices] # labels = labels[indices] selected_indices = tf.image.non_max_suppression(boxes, scores, 80, iou_threshold=0.25, score_threshold=0.30) selected_boxes = tf.gather(boxes, selected_indices) selected_labels = tf.gather(labels, selected_indices) selected_boxes = tf.Session().run(selected_boxes) selected_labels = tf.Session().run(selected_labels) # boxes = boxes[selected_indices] # labels = labels[selected_indices] draw_boxes(src_image, selected_boxes, scores, selected_labels, colors, dhaka_ai_classes) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', src_image) cv2.waitKey(0)
def main(): os.environ['CUDA_VISIBLE_DEVICES'] = '1' phi = 0 model_path = 'checkpoints/2020-03-13/pascal_64_0.2799_0.4923_0.7896_w.h5' image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] classes = [ 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor', ] num_classes = len(classes) score_threshold = 0.3 colors = [ np.random.randint(0, 256, 3).tolist() for i in range(num_classes) ] model, prediction_model = sapd( phi=phi, num_classes=num_classes, score_threshold=score_threshold, ) prediction_model.load_weights(model_path, by_name=True) for image_path in glob.glob('datasets/VOC2007/JPEGImages/*.jpg'): image = cv2.imread(image_path) src_image = image.copy() image = image[:, :, ::-1] h, w = image.shape[:2] image, scale, offset_h, offset_w = preprocess_image( image, image_size=image_size) # run network start = time.time() boxes, scores, labels = prediction_model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) print(time.time() - start) boxes = post_process_boxes(boxes=boxes, scale=scale, offset_h=offset_h, offset_w=offset_w, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', src_image) key = cv2.waitKey(0)
def main(): os.environ['CUDA_VISIBLE_DEVICES'] = '-1' phi = 0 weighted_bifpn = True model_path = 'efficientdet-d0.h5' image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes classes = { value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values() } num_classes = 90 score_threshold = 0.5 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] # _, model = efficientdet(phi=phi, # weighted_bifpn=weighted_bifpn, # num_classes=num_classes, # score_threshold=score_threshold) # model.load_weights(model_path, by_name=True) model = load_model('savedmodel/') video_path = 'test/video.mp4' cap = cv2.VideoCapture(video_path) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = int(cap.get(cv2.CAP_PROP_FPS)) codec = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('./out.mp4', codec, fps, (width, height)) times = [] while True: ret, image = cap.read() if not ret: break src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) end = time.time() print(end - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) #calculating fps times.append(end - start) times = times[-20:] ms = sum(times) / len(times) * 1000 fps = 1000 / ms # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) src_image = cv2.putText(src_image, "Time: {:.1f}FPS".format(fps), (0, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 2) out.write(src_image)
def main(): parser = argparse.ArgumentParser() model_path = parser.add_argument('--model_path', required=True) GPU = parser.add_argument('--GPU', default='0') image_path = parser.add_argument('--image_path', required=True) save_path = parser.add_argument('--save_path', required=True) json_path = parser.add_argument('--json_path', default='default_path_json.json') image_size = parser.add_argument('--image_size') file_format = parser.add_argument('--file_format', default='jpeg') score_threshold = parser.add_argument('--score_threshold', default=0.3) phi = parser.add_argument('--phi', default=0, type=int) args = parser.parse_args() json_dict = {} json_dict["boundingbox"] = [] os.environ['CUDA_VISIBLE_DEVICES'] = args.GPU print(args.image_path + '/*.' + args.file_format) file_lists = glob.glob(args.image_path + '/*.' + args.file_format) length = len(file_lists) iteration = 0 print(length) print(args) weighted_bifpn = True model_path = args.model_path image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[args.phi] # coco classes classes = { value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values() } num_classes = 90 score_threshold = 0.3 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] _, model = efficientdet(phi=args.phi, weighted_bifpn=weighted_bifpn, num_classes=num_classes, score_threshold=score_threshold) model.load_weights(model_path, by_name=True) print(model_path) try: os.makedirs(args.save_path) except: print(args.save_path, "is aleardy exist") pass start = time.time() for image_path in tqdm(file_lists): # iteration += 1 # if iteration % 20 == 19: # break image = cv2.imread(image_path) src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] file_name = image_path.split('/')[-1] temp_dict = {} temp_dict["box"] = boxes[indices].tolist() temp_dict["label"] = labels[indices].tolist() temp_dict["label_name"] = [classes[int(i)] for i in labels] temp_dict["image_name"] = file_name json_dict["boundingbox"].append(copy.deepcopy(temp_dict)) draw_boxes(src_image, boxes, scores, labels, colors, classes) cv2.imwrite(args.save_path + '/' + file_name, src_image) # cv2.waitKey(0) print("time : ", time.time() - start) with open(args.json_path, 'w') as json_data: json.dump(json_dict, json_data, indent=4)
def main(): parser = argparse.ArgumentParser() model_path = parser.add_argument('--model_path', required = True) GPU = parser.add_argument('--GPU', default = '0') # image_path = parser.add_argument('--image_path', required = True) save_path = parser.add_argument('--save_path', required = True) json_path = parser.add_argument('--json_path', default = 'default_path_json.json') image_size = parser.add_argument('--image_size') # file_format = parser.add_argument('--file_format', default = 'jpeg') score_threshold = parser.add_argument('--score_threshold', default = 0.3) phi = parser.add_argument('--phi', default = 0, type = int) args = parser.parse_args() json_dict = {} json_dict["boundingbox"] = [] os.environ['CUDA_VISIBLE_DEVICES'] = args.GPU # print(args.image_path + '/*.' + args.file_format) # file_lists = glob.glob(args.image_path + '/*.' + args.file_format) # length = len(file_lists) iteration = 0 # print(length) print(args) weighted_bifpn = True model_path = args.model_path image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[args.phi] # coco classes classes = {value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values()} num_classes = 90 score_threshold = 0.3 colors = [np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)] _, model = efficientdet(phi=args.phi, weighted_bifpn=weighted_bifpn, num_classes=num_classes, score_threshold=score_threshold) model.load_weights(model_path, by_name=True) print(model_path) try: os.makedirs(args.save_path) except: print(args.save_path, "is aleardy exist") pass start = time.time() mon = {'top': 87, 'left': 224, 'width': 590, 'height': 950} # full screen # mon = {'top': 297, 'left': 224, 'width': 590, 'height': 450} sct = mss() while 1: sct.get_pixels(mon) image = np.array(Image.frombytes('RGB', (sct.width, sct.height), sct.image)) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if cv2.waitKey(25) & 0xFF == ord('q'): cv2.destroyAllWindows() break src_image = image.copy() image = image[:, :, :] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network boxes, scores, labels = model.predict_on_batch([np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze(labels) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] # file_name = image_path.split('/')[-1] # cv2.imwrite(args.save_path +'/' + file_name, src_image) temp_dict = {} temp_dict["box"] = boxes[indices].tolist() temp_dict["label"] = labels[indices].tolist() temp_dict["label_name"] = [classes[int(i)] for i in labels] # temp_dict["image_name"] = file_name json_dict["boundingbox"].append(copy.deepcopy(temp_dict)) draw_boxes(src_image, boxes, scores, labels, colors, classes) cv2.imshow('test', src_image) print("time : ", time.time()-start) with open(args.json_path ,'w') as json_data: json.dump(json_dict, json_data, indent = 4)
def main(): phi = 1 model_path = 'checkpoints/2019-12-03/pascal_05.pb' image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] classes = [ 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor', ] num_classes = len(classes) score_threshold = 0.5 colors = [ np.random.randint(0, 256, 3).tolist() for i in range(num_classes) ] output_names = { 'output_boxes': 'filtered_detections/map/TensorArrayStack/TensorArrayGatherV3:0', 'output_scores': 'filtered_detections/map/TensorArrayStack_1/TensorArrayGatherV3:0', 'output_labels': 'filtered_detections/map/TensorArrayStack_2/TensorArrayGatherV3:0' } graph = tf.Graph() graph.as_default() sess = tf.Session() graph = get_frozen_graph(model_path) tf.import_graph_def(graph, name='') output_boxes = sess.graph.get_tensor_by_name(output_names["output_boxes"]) output_scores = sess.graph.get_tensor_by_name( output_names['output_scores']) output_labels = sess.graph.get_tensor_by_name( output_names['output_labels']) image_path = 'datasets/VOC2007/JPEGImages/000002.jpg' image = cv2.imread(image_path) src_image = image.copy() image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) anchors = anchors_for_shape((image_size, image_size)) # run network start = time.time() image_batch = np.expand_dims(image, axis=0) anchors_batch = np.expand_dims(anchors, axis=0) feed_dict = {"input_1:0": image_batch, "input_4:0": anchors_batch} boxes, scores, labels = sess.run( [output_boxes, output_scores, output_labels], feed_dict) boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze( labels) print(time.time() - start) boxes = post_process_boxes(boxes=boxes, scale=scale, offset_h=offset_h, offset_w=offset_w, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', src_image) cv2.waitKey(0)
def capture(app, mail): save_path = "RealTimeTest/" GPU = '0' Model_path = 'efficientdet-d0.h5' phi = 0 json_dict = {} json_dict["boundingbox"] = [] os.environ['CUDA_VISIBLE_DEVICES'] = GPU weighted_bifpn = True model_path = Model_path image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes classes = {value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values()} num_classes = 90 score_threshold = 0.3 colors = [np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)] _, model = efficientdet(phi=phi, weighted_bifpn=weighted_bifpn, num_classes=num_classes, score_threshold=score_threshold) model.load_weights(model_path, by_name=True) try: os.makedirs(save_path) except: print(save_path, "is aleardy exist") pass start = time.time() mon = {'top': 297, 'left': 224, 'width': 590, 'height': 450} sct = mss() image_num = 0 before_object_num = 0 while True: with mss() as sct: sct.get_pixels(mon) image = np.array(Image.frombuffer('RGB', (sct.width, sct.height), sct.image)) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # # TODO: np_img 에다가 prediction 추가할 것. src_image = image.copy() h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network boxes, scores, labels = model.predict_on_batch([np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze(labels) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] # file_name = image_path.split('/')[-1] # cv2.imwrite('prediction.png', src_image) temp_dict = {} temp_dict["box"] = boxes[indices].tolist() temp_dict["label"] = labels[indices].tolist() temp_dict["label_name"] = [classes[int(i)] for i in labels] # temp_dict["image_name"] = file_name json_dict["boundingbox"].append(copy.deepcopy(temp_dict)) draw_boxes(src_image, boxes, scores, labels, colors, classes) np_img = np.array(src_image) encode_return_code, image_buffer = cv2.imencode('.jpg', np_img) io_buf = io.BytesIO(image_buffer) yield (b'--frame\r\n' b'Content-Type: image/png\r\n\r\n' + io_buf.read() + b'\r\n')
break image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze( labels) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) indices = np.where(scores[:] > 0.5)[0] boxes = boxes[indices] labels = labels[indices] for b, lab in zip(boxes, labels): xmin, ymin, xmax, ymax = b[0], b[1], b[2], b[3] centroid = ((xmin + xmax) / 2, (ymin + ymax) / 2) if (centroid[0] >= x1 and centroid[0] <= x2) and (centroid[1] >= y1 and centroid[1] <= y2 + 10): count = count + 1 fr = fr + 5 cap.set(1, fr) draw_boxes(img, boxes, scores, labels, colors, classes) cv2.line(img, (x1, y1), (x2, y2 + 10), (0, 255, 0), 9) cv2.putText(img, 'count : {}'.format(count), (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 4, (255, 0, 0), 4) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', img) if cv2.waitKey(1) & 0xFF == ord('q'): break #cv2.waitKey(0)
def main(): os.environ['CUDA_VISIBLE_DEVICES'] = '0' phi = 0 weighted_bifpn = False model_path = 'checkpoints/flir_27_0.1779.h5' image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # coco classes classes = { value['id'] - 1: value['name'] for value in json.load(open('coco_90.json', 'r')).values() } num_classes = 3 score_threshold = 0.8 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] _, model = efficientdet(phi=phi, weighted_bifpn=weighted_bifpn, num_classes=num_classes, score_threshold=score_threshold) model.load_weights(model_path, by_name=True) mean = [0.485, 0.456, 0.406] std = [0.229, 0.224, 0.225] path = r'G:\datasets\iray_infrad_384\JPEGImages' path = r'G:\datasets\iray\200m_404man' imgs = [os.path.join(path, x) for x in os.listdir(path)] for file in imgs: if file.find("Raw") > -1: img = np.fromfile(file, np.uint16).reshape((512, 640)) image = cv2.normalize(img, dst=None, alpha=0, beta=65535, norm_type=cv2.NORM_MINMAX) image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) elif file.split('.')[-1] in ['jpg', 'jpeg', 'bmp', 'png']: image = cv2.imread(file) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) else: continue src_image = image.copy() image = image.astype(np.float32) image[..., 0] *= std[0] image[..., 1] *= std[1] image[..., 2] *= std[2] image[..., 0] += mean[0] image[..., 1] += mean[1] image[..., 2] += mean[2] image *= 255. # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = np.squeeze(boxes), np.squeeze( scores), np.squeeze(labels) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', src_image) cv2.waitKey(0)
def eveluate_testset(self, ckp_path: str = "checkpoints/"): os.environ["CUDA_VISIBLE_DEVICES"] = "0" phi = 1 weighted_bifpn_flag = True model_path = ckp_path + self.ckp_model_dir + "/" + self.ckp_model_file image_sizes = (512, 640, 768, 896, 1024, 1280, 1408) image_size = image_sizes[phi] # my dataset classes classes = { value["id"] - 1: value["name"] for value in json.load(open("mine_classes_eveluation_ds.json", "r")).values() } num_classes = 13 score_threshold = 0.50 colors = [ np.random.randint(0, 256, 3).tolist() for _ in range(num_classes) ] _, model = efficientdet( phi=phi, num_classes=num_classes, weighted_bifpn=weighted_bifpn_flag, score_threshold=score_threshold, ) model.load_weights(model_path, by_name=True) counter = 0 for image_path in glob.glob(self.base_path + "testset_images/*.jpg"): image = cv2.imread(image_path) src_image = image.copy() # BGR -> RGB image = image[:, :, ::-1] h, w = image.shape[:2] image, scale = preprocess_image(image, image_size=image_size) # run network start = time.time() boxes, scores, labels = model.predict_on_batch( [np.expand_dims(image, axis=0)]) boxes, scores, labels = ( np.squeeze(boxes), np.squeeze(scores), np.squeeze(labels), ) print(time.time() - start) boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w) # select indices which have a score above the threshold indices = np.where(scores[:] > score_threshold)[0] # select those detections boxes = boxes[indices] labels = labels[indices] draw_boxes(src_image, boxes, scores, labels, colors, classes) # cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imwrite( "evaluated_images/evaluated_image_" + str(counter) + ".jpg", src_image) counter += 1 # cv2.imshow('image', src_image) # cv2.waitKey(0) if counter == 500: break