def main(): sess = K.get_session() image_size = 384 image_input = Input(shape=(image_size, image_size, 3)) dataname = 'Piman' class_names = [dataname] YOLO_ANCHORS = np.array( ((0.57273, 0.677385), (1.87446, 2.06253), (3.33843, 5.47434), (7.88282, 3.52778), (9.77052, 9.16828))) anchors = YOLO_ANCHORS yolo = tiny_yolo_body(image_input, len(anchors), len(class_names)) yolo.load_weights('tiny_weights.h5') image_shape = (1080., 1920.) yolo_outputs = yolo_head(yolo.output, anchors, len(class_names)) boxes, scores, classes = yolo_eval(yolo_outputs, image_shape) cap = cv2.VideoCapture(GST_STR, cv2.CAP_GSTREAMER) count = 176 if True: fourcc = cv2.VideoWriter_fourcc(*'DIVX') fps = cap.get(cv2.CAP_PROP_FPS) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) writer = cv2.VideoWriter('pimanDetect2.avi', fourcc, fps, (width, height)) while True: ret, img = cap.read() img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = Image.fromarray(img) if ret != True: break resized_image = img.resize(tuple(reversed((384, 384))), Image.BICUBIC) image_data = np.array(resized_image, dtype='float32') image_data /= 255. image_data = np.expand_dims(image_data, 0) # Add batch dimension. out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo.input: image_data, K.learning_phase(): 0 }) print('Found {} boxes for {}'.format(len(out_boxes), "hoge")) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file draw_boxes(img, out_scores, out_boxes, out_classes, class_names, colors) cv2.imshow(WINDOW_NAME, np.asarray(img)[..., ::-1]) writer.write(np.asarray(img)[..., ::-1]) key = cv2.waitKey(10) if key == 27: # ESC break writer.release() cap.release()
def predict(sess, image_file, yolo_model, anchors, class_names): """ Runs the graph stored in "sess" to predict boxes for "image_file". Prints and plots the predictions. Arguments: sess -- your tensorflow/Keras session containing the YOLO graph image_file -- name of an image stored in the "images" folder. Returns: out_scores -- tensor of shape (None, ), scores of the predicted boxes out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes out_classes -- tensor of shape (None, ), class index of the predicted boxes Note: "None" actually represents the number of predicted boxes, it varies between 0 and max_boxes. """ # Preprocess your image image, image_data = preprocess_image("images/" + image_file, model_image_size=(608, 608)) # Run the session with the correct tensors and choose the correct placeholders in the feed_dict. yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) scores, boxes, classes = yolo_eval(yolo_outputs, image_shape=(720., 1280.)) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) # Print predictions info print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Save the predicted bounding box on the image image.save(os.path.join("out", image_file), quality=90) # Display the results in the notebook output_image = scipy.misc.imread(os.path.join("out", image_file)) imshow(output_image) return out_scores, out_boxes, out_classes
def detect_image(sess, image_input, yolo_model, class_names, anchors): """ Detect an image Parameters ---------- sess: tensor the tensorflow/Keras session containing the YOLO graph. image_input: a array OpenCV's format in RGB color space. yolo_model: the model returned by load_model function. class_name: str the name lists. anchors: str the anchor lists. Returns ------- output_image: str still the direction of image. class_names: str the name lists read from file. anchors: str the anchor lists read from file. """ image_shape = float(image_input.shape[0]), float(image_input.shape[1]) # Convert output of the model to usable bounding box tensors yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) # Filtering boxes scores, boxes, classes = yolo_eval(yolo_outputs, image_shape) output_image, out_scores, out_boxes, out_classes = predict( sess, image_input, yolo_model, class_names, scores, boxes, classes) return output_image, out_scores, out_boxes, out_classes
·每个19*19的单元格拥有425个数字 ·425=5*85,即每个单元格拥有5个锚框,每个锚框由5个基本信息+80个分类预测构成 ·85=5+80,其中5个基本信息是(Pc,Px,Py,Ph,Pw),剩下的80个就是80个分类预测 4、然后我们会根据一下规则选择锚框: ·预测分数阈值:丢弃分数低于阈值的分类的锚框 ·非最大值抑制:计算交并比,并避免选择重叠的框 5、最后给出YOLO的输出 """ sess = K.get_session() class_names = read_classes('F:\\吴恩达DL作业\课后作业\\代码作业\\第四课第三周编程作业\\Car detection for Autonomous Driving\\model_data\\coco_classes.txt') anchors = read_anchors('F:\\吴恩达DL作业\课后作业\\代码作业\\第四课第三周编程作业\\Car detection for Autonomous Driving\\model_data\\yolo_anchors.txt') image_shape = (720., 1280.) yolo_model = load_model('F:\\吴恩达DL作业\课后作业\\代码作业\\第四课第三周编程作业\\Car detection for Autonomous Driving\\model_data\\yolo.h5') yolo_model.summary() yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) scores, boxes, classes = yolo_eval(yolo_outputs, image_shape) def predict(sess, image_file, is_show_info=True, is_plot=True): """ 运行存储在sess的计算图以预测image_file的边界框,打印出预测图与信息 :param sess: 包含了YOLO计算图的TensorFlow/keras的会话 :param imagefile: 存储images文件下的图片名称 :param is_show_info: :param is_plot: :return: out_scores:tensor, (None, ),锚框的预测的可能值 out_boxes:tensor, (None,4),包含了锚框位置信息 out_classes:tensor, (None, ),锚框的预测的分类索引 """ image, image_data = preprocess_image(image_file, model_image_size =(608, 608))###预处理图像