Reputation: 1
I am trying to modify YOLO code for my lane detection project but it does not work as I expected.
I modified darknet_images.py to not draw the bounding box but rather use the coordinates to draw the center of each bounding boxes and line fit them by adding -lane_detection flag. But when I start the detection, it does not work and somehow just keep outputting the same results with the bounding boxes. I also have already tried to rebuilt it with CMAKE GUI and MSVC. Here is the modification that I made:
import utils
def parser():
parser = argparse.ArgumentParser(description="YOLO Object Detection")
parser.add_argument("--input", type=str, default="",
help="image source. It can be a single image, a"
"txt with paths to them, or a folder. Image valid"
" formats are jpg, jpeg or png."
"If no input is given, ")
parser.add_argument("--batch_size", default=1, type=int,
help="number of images to be processed at the same time")
parser.add_argument("--weights", default="yolov4.weights",
help="yolo weights path")
parser.add_argument("--dont_show", action='store_true',
help="windown inference display. For headless systems")
parser.add_argument("--ext_output", action='store_true',
help="display bbox coordinates of detected objects")
parser.add_argument("--save_labels", action='store_true',
help="save detections bbox for each image in yolo format")
parser.add_argument("--config_file", default="./cfg/yolov4.cfg",
help="path to config file")
parser.add_argument("--data_file", default="./cfg/coco.data",
help="path to data file")
parser.add_argument("--thresh", type=float, default=.25,
help="remove detections with lower confidence")
parser.add_argument("--lane_detection", help="activate lane detection utility")
return parser.parse_args()
def image_detection(image_or_path, network, class_names, class_colors, thresh, is_lane_detection = False):
# Darknet doesn't accept numpy images.
# Create one with image we reuse for each detect
width = darknet.network_width(network)
height = darknet.network_height(network)
darknet_image = darknet.make_image(width, height, 3)
if isinstance(image_or_path, str):
image = cv2.imread(image_or_path)
else:
image = image_or_path
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_resized = cv2.resize(image_rgb, (width, height),
interpolation=cv2.INTER_LINEAR)
darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes())
detections = darknet.detect_image(network, class_names, darknet_image, thresh=thresh)
darknet.free_image(darknet_image)
if not is_lane_detection:
image = darknet.draw_boxes(detections, image_resized, class_colors)
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections
return image_resized, detections
def main():
args = parser()
check_arguments_errors(args)
random.seed(3) # deterministic bbox colors
network, class_names, class_colors = darknet.load_network(
args.config_file,
args.data_file,
args.weights,
batch_size=args.batch_size
)
images = load_images(args.input)
index = 0
while True:
# loop asking for new image paths if no list is given
if args.input:
if index >= len(images):
break
image_name = images[index]
else:
image_name = input("Enter Image Path: ")
prev_time = time.time()
# Start lane detection if requested
if args.lane_detection:
image, detections = image_detection(
image_name, network, class_names, class_colors, args.thresh, lane_detection=True
)
for label, confidence, bbox in detections:
# Draw bounding box centers
centers, img = utils.draw_centers(img, bbox, class_colors[label])
# Run hough transform
lines, img = utils.probabilistic_hough_transform(img, 10, 5, 100, 75, 105, 60)
if not args.lane_detection:
image, detections = image_detection(
image_name, network, class_names, class_colors, args.thresh
)
if args.save_labels:
save_annotations(image_name, image, detections, class_names)
darknet.print_detections(detections, args.ext_output)
fps = int(1/(time.time() - prev_time))
print("FPS: {}".format(fps))
if not args.dont_show:
cv2.imshow('Inference', image)
if cv2.waitKey() & 0xFF == ord('q'):
break
index += 1
if __name__ == "__main__":
# unconmment next line for an example of batch processing
# batch_detection_example()
main()
Am I missing something or modifying the wrong file?
Upvotes: 0
Views: 336
Reputation: 20380
Make sure you are using the right repo. The old Darknet one also had a file called darknet_images.py. The one you should be using is this one from Hank.ai: https://github.com/hank-ai/darknet
There is no need to re-build and re-install Darknet when you change a Python .py file. The build steps with CMake is only required if you modify one of the C/C++ files.
You mention "Here is the modification that I made" but then it looks like you pasted a large part of the file instead of a diff. Either way, what you pasted does not match what is currently in the repo, so I assume you are using one of the old abandoned Darknet repos.
With the new repo, the line you'd want to comment out or replace is #208: https://github.com/hank-ai/darknet/blob/master/src-python/darknet_images.py#L208
Upvotes: 0