|
| 1 | +import os |
| 2 | +import cv2 |
| 3 | +import time |
| 4 | +import argparse |
| 5 | + |
| 6 | +from detector import DetectorTF2 |
| 7 | + |
| 8 | + |
| 9 | +def DetectFromVideo(detector, Video_path, save_output=False, output_dir='output/'): |
| 10 | + |
| 11 | + cap = cv2.VideoCapture(Video_path) |
| 12 | + if save_output: |
| 13 | + output_path = os.path.join(output_dir, 'detection_'+ Video_path.split("/")[-1]) |
| 14 | + frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) |
| 15 | + frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
| 16 | + out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), 25, (frame_width, frame_height)) |
| 17 | + |
| 18 | + while (cap.isOpened()): |
| 19 | + ret, img = cap.read() |
| 20 | + if not ret: break |
| 21 | + |
| 22 | + timestamp1 = time.time() |
| 23 | + det_boxes = detector.DetectFromImage(img) |
| 24 | + elapsed_time = round((time.time() - timestamp1) * 1000) #ms |
| 25 | + img = detector.DisplayDetections(img, det_boxes, det_time=elapsed_time) |
| 26 | + |
| 27 | + cv2.imshow('TF2 Detection', img) |
| 28 | + if cv2.waitKey(1) == 27: break |
| 29 | + #& 0xFF == ord('q'): break |
| 30 | + |
| 31 | + if save_output: |
| 32 | + out.write(img) |
| 33 | + |
| 34 | + cap.release() |
| 35 | + if save_output: |
| 36 | + out.release() |
| 37 | + |
| 38 | + |
| 39 | +def DetectImagesFromFolder(detector, images_dir, save_output=False, output_dir='output/'): |
| 40 | + |
| 41 | + for file in os.scandir(images_dir): |
| 42 | + if file.is_file() and file.name.endswith(('.jpg', '.jpeg', '.png')) : |
| 43 | + image_path = os.path.join(images_dir, file.name) |
| 44 | + print(image_path) |
| 45 | + img = cv2.imread(image_path) |
| 46 | + det_boxes = detector.DetectFromImage(img) |
| 47 | + img = detector.DisplayDetections(img, det_boxes) |
| 48 | + |
| 49 | + cv2.imshow('TF2 Detection', img) |
| 50 | + cv2.waitKey(0) |
| 51 | + |
| 52 | + if save_output: |
| 53 | + img_out = os.path.join(output_dir, file.name) |
| 54 | + cv2.imwrite(img_out, img) |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + |
| 59 | + parser = argparse.ArgumentParser(description='Object Detection from Images or Video') |
| 60 | + parser.add_argument('--model_path', help='Path to frozen detection graph (model from Tensorflow)', |
| 61 | + default='models/efficientdet_d0_coco17_tpu-32/saved_model') |
| 62 | + parser.add_argument('--path_to_labelmap', help='Path to labelmap (.pbtxt) file', |
| 63 | + default='models/mscoco_label_map.pbtxt') |
| 64 | + parser.add_argument('--class_ids', help='id of classes to detect, expects string with ids delimited by ","', |
| 65 | + type=str, default=None) # example input "1,3" to detect person and car |
| 66 | + parser.add_argument('--threshold', help='Detection Threshold', type=float, default=0.4) |
| 67 | + parser.add_argument('--images_dir', help='Directory to input images)', default='data/samples/images/') |
| 68 | + parser.add_argument('--video_path', help='Path to input video)', default='data/samples/pedestrian_test.mp4') |
| 69 | + parser.add_argument('--output_directory', help='Path to output images and video)', default='data/output') |
| 70 | + parser.add_argument('--video_input', action='store_true') # default is false |
| 71 | + parser.add_argument('--save_output', action='store_true') # default is false |
| 72 | + args = parser.parse_args() |
| 73 | + |
| 74 | + id_list = None |
| 75 | + if args.class_ids is not None: |
| 76 | + id_list = [int(item) for item in args.class_ids.split(',')] |
| 77 | + |
| 78 | + if args.save_output: |
| 79 | + if not os.path.exists(args.output_directory): |
| 80 | + os.makedirs(args.output_directory) |
| 81 | + |
| 82 | + # instance of the class DetectorTF2 |
| 83 | + detector = DetectorTF2(args.model_path, args.path_to_labelmap, class_id=id_list, threshold=args.threshold) |
| 84 | + |
| 85 | + if args.video_input: |
| 86 | + DetectFromVideo(detector, args.video_path, save_output=args.save_output, output_dir=args.output_directory) |
| 87 | + else: |
| 88 | + DetectImagesFromFolder(detector, args.images_dir, save_output=args.save_output, output_dir=args.output_directory) |
| 89 | + |
| 90 | + print("Done ...") |
| 91 | + cv2.destroyAllWindows() |
0 commit comments