def predict(): input_size = (416, 416) tf.app.flags.DEFINE_string('image_file', './images/person.jpg', 'image_path') FLAGS = tf.app.flags.FLAGS image_file = FLAGS.image_file image = cv2.imread(image_file) image_shape = image.shape[:2] image_cp = preprocess_image(image, input_size) images = tf.placeholder(tf.float32, [1, input_size[0], input_size[1], 3]) detection_feat = darknet(images) feat_sizes = input_size[0] // 32, input_size[1] // 32 detection_results = decode(detection_feat, feat_sizes, len(class_names), anchors) checkpoint_path = "./checkpoint_dir/yolo2_coco.ckpt" #checkpoint_path = "/Users/xiang/Downloads/DeepLearning_tutorials-master/ObjectDetections/yolo2/checkpoint_dir/yolo2_coco.ckpt" saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, checkpoint_path) bboxes, obj_probs, class_probs = sess.run(detection_results, feed_dict={images: image_cp}) bboxes, scores, class_inds = postprocess(bboxes, obj_probs, class_probs, image_shape=image_shape) img_detection = draw_detection(image, bboxes, scores, class_inds, class_names) cv2.imwrite("./res/detection.jpg", img_detection) cv2.imshow("detection results", img_detection) print('*****Click the window,and press any key to close!*****') cv2.waitKey(0) cv2.destroyAllWindows()
def main(): input_size = (416, 416) # image_file = "/home/zdq/darknet/data/1.jpg" # image = cv2.imread(image_file) image_shape = image.shape[:2] image_cp = preprocess_image(image, input_size) images = tf.placeholder(tf.float32, [1, input_size[0], input_size[1], 3]) detection_feat = darknet(images) feat_sizes = input_size[0] // 32, input_size[1] // 32 detection_results = decode(detection_feat, feat_sizes, len(class_names), anchors) checkpoint_path = "/home/zdq/YOLO/checkpoint_dir/yolo2_coco.ckpt" saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, checkpoint_path) bboxes, obj_probs, class_probs = sess.run(detection_results, feed_dict={images: image_cp}) bboxes, scores, class_inds = postprocess(bboxes, obj_probs, class_probs, image_shape=image_shape) img_detection = draw_detection(image, bboxes, scores, class_inds, class_names) #回归框,得到矩阵框的X左右,Y下像素坐标 print('\n') # #检测车道线的像素,并放入字典中 # lane_cor = {} # #手动选取ROI,待修改 # vertices = np.array([[(110, 194), (110, 0), (150, 0), (150, 194)]], dtype=np.int32) # roi = region_of_interest(line_img, vertices) # # cv2.imshow("roi", roi) # for i in range(0, (roi.shape)[0]): # for j in range(0, (roi.shape)[1]): # if roi[i, j, 2] == 255: #roi[i,j,num]这里num代表着BGR第几通道 # lane_cor[i] = j # print("The coodinate of the detected_lane y:x") # # print(lane_cor) # # global box # if (utils.box[0] + m * (utils.box[2] - utils.box[0])) <= lane_cor[utils.box[3]] <= (utils.box[2] - m * (utils.box[2] - utils.box[0])): # print("The car is on the solid line!!!") # else: # print("The car is permitted~") # mix1 = weight_add(img_detection, line_img, alpha=0.7, belta=1, gamma=0.) # mixed = weight_add(img_detection,roi , alpha=0.7, belta=1, gamma=0.) # cv2.imshow("mix1", mix1) # cv2.imshow("mixed",mixed) # cv2.imshow("detection results", img_detection) cv2.imwrite("/home/zdq/PycharmProjects/YOLOv2/detection.jpg", img_detection) cv2.waitKey(0) return img_detection
def camera_detect(): tf.app.flags.DEFINE_string('video', False, 'Whether to output video file') FLAGS = tf.app.flags.FLAGS input_size = (416, 416) cv2.namedWindow("camera") capture = cv2.VideoCapture(0) #开启摄像头 success, image = capture.read() images = tf.placeholder(tf.float32, [1, input_size[0], input_size[1], 3]) detection_feat = darknet(images) if FLAGS.video: fps = 1 size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) videoWriter = cv2.VideoWriter('./test/result.avi', cv2.VideoWriter_fourcc('M','J', 'P', 'G'), fps, size) num = 1 while success and cv2.waitKey(1) == -1: cv2.imshow('camera', image) image_shape = image.shape[:2] image_cp = preprocess_image(image, input_size) feat_sizes = input_size[0] // 32, input_size[1] // 32 detection_results = decode(detection_feat, feat_sizes, len(class_names), anchors) checkpoint_path = "./checkpoint_dir/yolo2_coco.ckpt" #checkpoint_path = "/Users/xiang/Downloads/DeepLearning_tutorials-master/ObjectDetections/yolo2/checkpoint_dir/yolo2_coco.ckpt" saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, checkpoint_path) bboxes, obj_probs, class_probs = sess.run(detection_results, feed_dict={images: image_cp}) bboxes, scores, class_inds = postprocess(bboxes, obj_probs, class_probs, image_shape=image_shape) img_detection = draw_detection(image, bboxes, scores, class_inds, class_names) if FLAGS.video: videoWriter.write(img_detection) else: cv2.imwrite("./test/"+str(num)+"test.jpg", img_detection) success, image = capture.read() num += 1 cv2.destroyWindow("camera") capture.release()
image = cv2.imread(image_file) image_shape = image.shape[:2] image_cp = preprocess_image(image, input_size) """ image = Image.open(image_file) image_cp = image.resize(input_size, Image.BICUBIC) image_cp = np.array(image_cp, dtype=np.float32)/255.0 image_cp = np.expand_dims(image_cp, 0) #print(image_cp) """ images = tf.placeholder(tf.float32, [1, input_size[0], input_size[1], 3]) detection_feat = darknet(images) feat_sizes = input_size[0] // 32, input_size[1] // 32 start = time.clock() detection_results = decode(detection_feat, feat_sizes, len(class_names), anchors) checkpoint_path = "./checkpoint_dir/yolo2_coco.ckpt" saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, checkpoint_path) bboxes, obj_probs, class_probs = sess.run(detection_results, feed_dict={images: image_cp}) bboxes, scores, class_inds = postprocess(bboxes, obj_probs, class_probs, image_shape=image_shape) #plt_bboxes(image,class_inds,scores,bboxes) img_detection = draw_detection(image, bboxes, scores, class_inds, class_names) end = time.clock()
image = cv2.imread(image_file) image_shape = image.shape[:2] image_cp = preprocess_image(image, input_size) """ image = Image.open(image_file) image_cp = image.resize(input_size, Image.BICUBIC) image_cp = np.array(image_cp, dtype=np.float32)/255.0 image_cp = np.expand_dims(image_cp, 0) #print(image_cp) """ images = tf.placeholder(tf.float32, [1, input_size[0], input_size[1], 3]) detection_feat = darknet(images) feat_sizes = input_size[0] // 32, input_size[1] // 32 detection_results = decode(detection_feat, feat_sizes, len(class_names), anchors) checkpoint_path = "./checkpoint_dir/yolo2_coco.ckpt" saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, checkpoint_path) bboxes, obj_probs, class_probs = sess.run(detection_results, feed_dict={images: image_cp}) bboxes, scores, class_inds = postprocess(bboxes, obj_probs, class_probs, image_shape=image_shape) img_detection = draw_detection(image, bboxes, scores, class_inds, class_names) cv2.imwrite("detection.jpg", img_detection) cv2.imshow("detection results", img_detection) cv2.waitKey(0)