img_path.rstrip('\n') ] + single_prediction + [x_size, y_size]], columns=[ 'image', 'image_path', 'xmin', 'ymin', 'xmax', 'ymax', 'label', 'confidence', 'x_size', 'y_size' ])) end = timer() print('Processed {} images in {:.1f}sec - {:.1f}FPS'.format( len(input_image_paths), end - start, len(input_image_paths) / (end - start))) out_df.to_csv(FLAGS.box, index=False) # This is for videos if input_video_paths: print('Found {} input videos: {} ...'.format( len(input_video_paths), [os.path.basename(f) for f in input_video_paths[:5]])) start = timer() for i, vid_path in enumerate(input_video_paths): output_path = os.path.join( FLAGS.output, os.path.basename(vid_path).replace('.', FLAGS.postfix + '.')) detect_video(yolo, vid_path, output_path=output_path) end = timer() print('Processed {} videos in {:.1f}sec'.format( len(input_video_paths), end - start)) # Close the current yolo session yolo.close_session()
class YoloModel: def __init__(self): min_confidence = 0.25 is_tiny = False if is_tiny and anchors_path: anchors_path = os.path.join( os.path.dirname(anchors_path), "yolo-tiny_anchors.txt" ) anchors_path = os.path.join(src_path, "keras_yolo3", "model_data", "yolo_anchors.txt") anchors = get_anchors(anchors_path) # define YOLO detector self.yolo = YOLO( **{ "model_path": model_weights, "anchors_path": anchors_path, "classes_path": model_classes, "score": min_confidence, "gpu_num": 0, "model_image_size": (416, 416), } ) # labels to draw on images class_file = open(model_classes, "r") self.input_labels = [line.rstrip("\n") for line in class_file.readlines()] def __del__(self): # Close the current yolo session self.yolo.close_session() def detect(self, img, show_stats=True): start = timer() prediction, detected_img = self.yolo.detect_image(img, show_stats=show_stats) detected_img = np.asarray(detected_img) y_size, x_size, _ = detected_img.shape # Make a dataframe for the prediction outputs out_df = pd.DataFrame( columns=[ "xmin", "ymin", "xmax", "ymax", "label", "confidence", "x_size", "y_size", ] ) for single_prediction in prediction: out_df = out_df.append( pd.DataFrame( [ single_prediction + [x_size, y_size] ], columns=[ "xmin", "ymin", "xmax", "ymax", "label", "confidence", "x_size", "y_size", ], ) ) end = timer() if show_stats: print(f"Yolo v3 detection took {end-start:.2f} s") return out_df, detected_img
def detect(): global FLAGS, anchors_path, output_path # Delete all default flags parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS) """ Command line options """ parser.add_argument( "--multiple_inputs_filepath", type=str, default=None, help= "Path to file with multiple input paths, defaults to None for non use") parser.add_argument( "--input_path", type=str, default=image_test_folder, help= "Path to image/video directory. All subdirectories will be included. Default is " + image_test_folder, ) parser.add_argument( "--output", type=str, default=detection_results_folder, help="Output path for detection results. Default is " + detection_results_folder, ) parser.add_argument( "--no_save_img", default=False, action="store_true", help= "Only save bounding box coordinates but do not save output check with annotated boxes. Default is False.", ) parser.add_argument( "--yolo_model", type=str, dest="model_path", default=model_weights, help="Path to pre-trained weight files. Default is " + model_weights, ) parser.add_argument( "--anchors", type=str, dest="anchors_path", default=anchors_path, help="Path to YOLO anchors. Default is " + anchors_path, ) parser.add_argument( "--classes", type=str, dest="classes_path", default=model_classes, help="Path to YOLO class specifications. Default is " + model_classes, ) parser.add_argument("--gpu_num", type=int, default=1, help="Number of GPU to use. Default is 1") parser.add_argument( "--confidence", type=float, dest="score", default=0.25, help= "Threshold for YOLO object confidence score to show predictions. Default is 0.25.", ) parser.add_argument( "--box_file", type=str, dest="box", default=detection_results_file, help="File to save bounding box results to. Default is " + detection_results_file, ) parser.add_argument( "--postfix", type=str, dest="postfix", default="_catface", help= 'Specify the postfix for images with bounding boxes. Default is "_catface"', ) parser.add_argument( "--is_tiny", default=False, action="store_true", help= "Use the tiny Yolo version for better performance and less accuracy. Default is False.", ) FLAGS = parser.parse_args() inputs_filepath = FLAGS.multiple_inputs_filepath if FLAGS.is_tiny and FLAGS.anchors_path == anchors_path: anchors_path = os.path.join(os.path.dirname(FLAGS.anchors_path), "yolo-tiny_anchors.txt") anchors = get_anchors(anchors_path) # replace FLAGS model_path = FLAGS.model_path classes_path = FLAGS.classes_path score = FLAGS.score gpu_num = FLAGS.gpu_num no_save_img = FLAGS.no_save_img postfix = FLAGS.postfix box = FLAGS.box yolo = YOLO( **{ "model_path": model_path, "anchors_path": anchors_path, "classes_path": classes_path, "score": score, "gpu_num": gpu_num, "model_image_size": (256, 256), }) # predict from single directory or multiple inputs if inputs_filepath: # get a list of all input directories with open(inputs_filepath, 'r') as file: input_dirs = [line.rstrip('\n') for line in file.readlines()] # predict separately for every directory for dir in input_dirs: output_path = dir + '/detected' input_path = dir + '/raw' predict_input_dir(yolo, classes_path, no_save_img, postfix, box, input_path, output_path) else: predict_input_dir(yolo, classes_path, no_save_img, postfix, box, FLAGS.input_path, FLAGS.output) # Close the current yolo session yolo.close_session()
class Receiver(object):#I python 2 burde man skrive klassen som dette her, det gir flere fordeler som @classmethods, @staticmethods og mer. i Python3 trenger man ikke aa skrive den slik etter som det er standard def __init__(self, args): self.colorImage = None self.img = None self.pilImg = None self.yolo = YOLO(**vars(args)) self.publishers = [] self.callback_list = [] self.detection_info_msg = detection_info() self.pub_detection = rospy.Publisher("/detection_info_publisher", detection_info) self.pub_color_image = rospy.Publisher("/detection_color", Image) self.pub_color_info = rospy.Publisher("/detection_color_info", CameraInfo) self.pub_depth_image = rospy.Publisher("/detection_depth", Image) self.pub_depth_info = rospy.Publisher("/detection_depth_info", CameraInfo) #self.publishers[0] = rospy.Publisher("/detection_info_publisher", detection_info) #self.publishers[1] = rospy.Publisher("/detection_color", Image) #self.publishers[2] = rospy.Publisher("/detection_color_info", CameraInfo) #self.publishers[3] = rospy.Publisher("/detection_depth", Image) #self.publishers[4] = rospy.Publisher("/detection_depth_info", CameraInfo) self.bridge = CvBridge() #self.image_sub = rospy.Subscriber("/kinect2/qhd/image_color_rect", Image, self.callback) self.fourcc = cv2.VideoWriter_fourcc(*'XVID') self.video = cv2.VideoWriter("/home/erlendb/Programmering/PCL/kinect_ws/test.avi", self.fourcc, 30, (960, 540)) self.running = None #FPS variables self.accum_time = 0 self.curr_fps = 0 self.prev_time = timer() self.fps = "FPS: ??" def start(self): print("Start()") #Subscribers, using TimeSynchronization image_color_sub = message_filters.Subscriber('/kinect2/qhd/image_color_rect', Image) image_info_sub = message_filters.Subscriber('/kinect2/qhd/camera_info', CameraInfo) image_depth_sub = message_filters.Subscriber('/kinect2/qhd/image_depth_rect', Image) image_depth_info_sub = message_filters.Subscriber('/kinect2/qhd/camera_info', CameraInfo) ts = message_filters.TimeSynchronizer([image_color_sub, image_info_sub, image_depth_sub, image_depth_info_sub], 10) ts.registerCallback(self.callback) self.running = True self.startTime = time.time() rospy.spin() def callback(self, color_image, color_info, depth_image, depth_info): #print("Neine") self.colorImage = color_image try: self.img = self.bridge.imgmsg_to_cv2(self.colorImage, "bgr8") self.pilImg = PILImage.fromarray(self.img) except CvBridgeError as e: print(e) self.detection() #print(self.detection_info_msg) #self.viewer(self.img) self.detection_info_msg.header.stamp = color_image.header.stamp self.pub_detection.publish(self.detection_info_msg) self.pub_color_image.publish(color_image) self.pub_color_info.publish(color_info) self.pub_depth_image.publish(depth_image) self.pub_depth_info.publish(depth_info) def close(self): self.yolo.close_session() self.video.release() cv2.destroyAllWindows() rospy.signal_shutdown('Quitting') def viewer(self, img): if self.running: if not self.video.isOpened(): print("error with writer") else: self.video.write(img) key = cv2.waitKey(1) if key == 113: self.close() cv2.imshow("get_image", img) def detection(self): image = self.pilImg #boxes har verdiene top, left, bottom, right (image, boxes, scores, classes) = self.yolo.detect_image(image) print(boxes) result = np.asarray(image) #Konverterer bildet slik at den kan brukes i openCV #Assign to message if len(classes) > 0: self.detection_info_msg.class_type = classes[0] self.detection_info_msg.score = scores[0] self.detection_info_msg.y1 = boxes[0][0] self.detection_info_msg.x1 = boxes[0][1] self.detection_info_msg.y2 = boxes[0][2] self.detection_info_msg.x2 = boxes[0][3] print("element 0: %d element 1: %d element 2: %d element 3: %d" % (boxes[0][0], boxes[0][1], boxes[0][2] ,boxes[0][3]) ) else: self.detection_info_msg.class_type = -1 #print(self.detection_info_msg.class_type) """" curr_time = timer() exec_time = curr_time - self.prev_time self.prev_time = curr_time self.accum_time = self.accum_time + exec_time self.curr_fps = self.curr_fps + 1 if self.accum_time > 1: self.accum_time = self.accum_time -1 self.fps = "FPS: " + str(self.curr_fps) self.curr_fps = 0 """ #cv2.putText(result, text=self.fps, org=(3,15), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.50, color=(255,255,255), thickness=2) #cv2.namedWindow("result", cv2.WINDOW_NORMAL) cv2.imshow("result", result) key = cv2.waitKey(1) if key == 113: self.close()
def detect(image_test_folder): # Delete all default flags parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS) """ Command line options """ parser.add_argument( "--input_path", type=str, default=image_test_folder, help= "Path to image/video directory. All subdirectories will be included. Default is " + image_test_folder, ) parser.add_argument( "--output", type=str, default=fp.detection_results_folder, help="Output path for detection results. Default is " + fp.detection_results_folder, ) parser.add_argument( "--no_save_img", default=False, action="store_true", help= "Only save bounding box coordinates but do not save output images with annotated boxes. Default is False.", ) parser.add_argument( "--file_types", "--names-list", nargs="*", default=[], help= "Specify list of file types to include. Default is --file_types .jpg .jpeg .png .mp4", ) parser.add_argument( "--yolo_model", type=str, dest="model_path", default=fp.YOLO_weights, help="Path to pre-trained weight files. Default is " + fp.YOLO_weights, ) parser.add_argument( "--anchors", type=str, dest="anchors_path", default=fp.anchors_path, help="Path to YOLO anchors. Default is " + fp.anchors_path, ) parser.add_argument( "--classes", type=str, dest="classes_path", default=fp.YOLO_classname, help="Path to YOLO class specifications. Default is " + fp.YOLO_classname, ) parser.add_argument("--gpu_num", type=int, default=1, help="Number of GPU to use. Default is 1") parser.add_argument( "--confidence", type=float, dest="score", default=0.25, help= "Threshold for YOLO object confidence score to show predictions. Default is 0.25.", ) parser.add_argument( "--box_file", type=str, dest="box", default=fp.detection_results_file, help="File to save bounding box results to. Default is " + fp.detection_results_file, ) parser.add_argument( "--postfix", type=str, dest="postfix", default="_fish", help= 'Specify the postfix for images with bounding boxes. Default is "_fish"', ) FLAGS = parser.parse_args() save_img = not FLAGS.no_save_img file_types = FLAGS.file_types if file_types: input_paths = GetFileList(FLAGS.input_path, endings=file_types) else: input_paths = GetFileList(FLAGS.input_path) # Split images and videos img_endings = (".jpg", ".jpg", ".png") vid_endings = (".mp4", ".mpeg", ".mpg", ".avi") input_image_paths = [] input_video_paths = [] for item in input_paths: if item.endswith(img_endings): input_image_paths.append(item) elif item.endswith(vid_endings): input_video_paths.append(item) output_path = FLAGS.output if not os.path.exists(output_path): os.makedirs(output_path) # define YOLO detector yolo = YOLO( **{ "model_path": FLAGS.model_path, "anchors_path": FLAGS.anchors_path, "classes_path": FLAGS.classes_path, "score": FLAGS.score, "gpu_num": FLAGS.gpu_num, "model_image_size": (416, 416), }) # Make a dataframe for the prediction outputs out_df = pd.DataFrame(columns=[ "image", "image_path", "xmin", "ymin", "xmax", "ymax", "label", "confidence", "x_size", "y_size", ]) # labels to draw on images class_file = open(FLAGS.classes_path, "r") input_labels = [line.rstrip("\n") for line in class_file.readlines()] print("Found {} input labels: {} ...".format(len(input_labels), input_labels)) if input_image_paths: print("Found {} input images: {} ...".format( len(input_image_paths), [os.path.basename(f) for f in input_image_paths[:5]], )) start = timer() text_out = "" # This is for images for i, img_path in enumerate(input_image_paths): print(img_path) prediction, image = detect_object( yolo, img_path, save_img=save_img, save_img_path=FLAGS.output, postfix=FLAGS.postfix, ) y_size, x_size, _ = np.array(image).shape for single_prediction in prediction: out_df = out_df.append( pd.DataFrame( [[ os.path.basename(img_path.rstrip("\n")), img_path.rstrip("\n"), ] + single_prediction + [x_size, y_size]], columns=[ "image", "image_path", "xmin", "ymin", "xmax", "ymax", "label", "confidence", "x_size", "y_size", ], )) end = timer() print("Processed {} images in {:.1f}sec - {:.1f}FPS".format( len(input_image_paths), end - start, len(input_image_paths) / (end - start), )) out_df.to_csv(FLAGS.box, index=False) # This is for videos if input_video_paths: print("Found {} input videos: {} ...".format( len(input_video_paths), [os.path.basename(f) for f in input_video_paths[:5]], )) start = timer() for i, vid_path in enumerate(input_video_paths): output_path = os.path.join( FLAGS.output, os.path.basename(vid_path).replace(".", FLAGS.postfix + "."), ) detect_video(yolo, vid_path, output_path=output_path) end = timer() print("Processed {} videos in {:.1f}sec".format( len(input_video_paths), end - start)) # Close the current yolo session yolo.close_session() return fp.detection_results_folder