-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
main_2024.py detect target #156
Changes from 32 commits
2dbe3c9
10c489e
bfa1978
ad9ac08
f23f62d
dd6e5a1
3ee9905
d7ca078
8819c3c
d08e556
bd2a59d
a49c660
20e4018
0da5e01
37a37bc
cb186f4
724de57
5e6c8e7
0133d97
479a361
864bcaa
0b2f4b7
ba05393
108c7c6
ca549ba
0ab48c9
435992c
73a28a8
69be32a
5117781
dea615a
3080db0
6b00c7e
ef80b03
68fab82
f8359ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ def main() -> int: | |
parser = argparse.ArgumentParser() | ||
parser.add_argument("--cpu", action="store_true", help="option to force cpu") | ||
parser.add_argument("--full", action="store_true", help="option to force full precision") | ||
parser.add_argument("--show-annotated", action="store_true", help="option to show annotated image") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make multiline. |
||
args = parser.parse_args() | ||
|
||
# Set constants | ||
|
@@ -64,6 +65,7 @@ def main() -> int: | |
DETECT_TARGET_OVERRIDE_FULL_PRECISION = args.full | ||
DETECT_TARGET_SAVE_NAME_PREFIX = config["detect_target"]["save_prefix"] | ||
DETECT_TARGET_SAVE_PREFIX = f"{LOG_DIRECTORY_PATH}/{DETECT_TARGET_SAVE_NAME_PREFIX}" | ||
DETECT_TARGET_SHOW_ANNOTATED = args.show_annotated | ||
|
||
FLIGHT_INTERFACE_ADDRESS = config["flight_interface"]["address"] | ||
FLIGHT_INTERFACE_TIMEOUT = config["flight_interface"]["timeout"] | ||
|
@@ -113,6 +115,7 @@ def main() -> int: | |
DETECT_TARGET_MODEL_PATH, | ||
DETECT_TARGET_OVERRIDE_FULL_PRECISION, | ||
DETECT_TARGET_SAVE_PREFIX, | ||
DETECT_TARGET_SHOW_ANNOTATED, | ||
video_input_to_detect_target_queue, | ||
detect_target_to_main_queue, | ||
controller, | ||
|
@@ -139,9 +142,17 @@ def main() -> int: | |
|
||
while True: | ||
try: | ||
image = detect_target_to_main_queue.queue.get_nowait() | ||
detections = detect_target_to_main_queue.queue.get_nowait() | ||
except queue.Empty: | ||
image = None | ||
detections = None | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if detections is not None: | ||
print("timestamp: " + str(detections.timestamp)) | ||
print("detections: " + str(len(detections.detections))) | ||
for detection in detections.detections: | ||
print(" label: " + str(detection.label)) | ||
print(" confidence: " + str(detection.confidence)) | ||
print("") | ||
|
||
odometry_and_time_info: "odometry_and_time.OdometryAndTime | None" = \ | ||
flight_interface_to_main_queue.queue.get() | ||
|
@@ -160,12 +171,8 @@ def main() -> int: | |
print("pitch: " + str(orientation.pitch)) | ||
print("") | ||
|
||
if image is None: | ||
continue | ||
|
||
cv2.imshow("Landing Pad Detector", image) | ||
|
||
if cv2.waitKey(1) & 0xFF == ord('q'): | ||
if cv2.waitKey(1) == ord('q'): | ||
print("Exiting main loop") | ||
break | ||
|
||
# Teardown | ||
|
@@ -179,6 +186,8 @@ def main() -> int: | |
detect_target_manager.join_workers() | ||
flight_interface_manager.join_workers() | ||
|
||
cv2.destroyAllWindows() | ||
|
||
return 0 | ||
|
||
|
||
|
+0 −44 | .github/workflows/run-tests.yml | |
+0 −0 | camera/__init__.py | |
+3 −6 | camera/test_camera.py | |
+0 −0 | comms/__init__.py | |
+4 −6 | comms/receive.py | |
+6 −9 | comms/test_tx.py | |
+5 −7 | comms/transmit.py | |
+0 −0 | kml/__init__.py | |
+7 −10 | kml/modules/waypoints_to_kml.py | |
+14 −19 | kml/test_waypoints_to_kml.py | |
+0 −0 | mavlink/__init__.py | |
+1 −1 | mavlink/test_flight_controller.py | |
+0 −0 | qr/__init__.py | |
+8 −11 | qr/test_qr.py |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert to when you were displaying the image in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Amy mentioned above that this causes issues. Is there a work around? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it work on you end? If it does then maybe I missed something in the set up? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you try adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Oh this works! @Ethan118 you can do this instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove line 7 import (no longer being used). |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ class DetectTarget: | |
""" | ||
Contains the YOLOv8 model for prediction. | ||
""" | ||
def __init__(self, device: "str | int", model_path: str, override_full: bool, save_name: str = ""): | ||
def __init__(self, device: "str | int", model_path: str, override_full: bool, show_annotations: bool = False, save_name: str = ""): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make multiline. |
||
""" | ||
device: name of target device to run inference on (i.e. "cpu" or cuda device 0, 1, 2, 3). | ||
model_path: path to the YOLOv8 model. | ||
|
@@ -28,17 +28,18 @@ def __init__(self, device: "str | int", model_path: str, override_full: bool, sa | |
self.__model = ultralytics.YOLO(model_path) | ||
self.__counter = 0 | ||
self.__enable_half_precision = False if self.__device == "cpu" else True | ||
self.__show_annotations = show_annotations | ||
if override_full: | ||
self.__enable_half_precision = False | ||
self.__filename_prefix = "" | ||
if save_name != "": | ||
self.__filename_prefix = save_name + "_" + str(int(time.time())) + "_" | ||
|
||
def run(self, data: image_and_time.ImageAndTime) -> "tuple[bool, np.ndarray | None]": | ||
def run(self, data: image_and_time.ImageAndTime) -> "tuple[bool, detections_and_time.DetectionsAndTime | None]": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make multiline. |
||
""" | ||
Returns annotated image. | ||
TODO: Change to DetectionsAndTime | ||
""" | ||
print(self.__enable_half_precision) | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
image = data.image | ||
predictions = self.__model.predict( | ||
source=image, | ||
|
@@ -50,7 +51,6 @@ def run(self, data: image_and_time.ImageAndTime) -> "tuple[bool, np.ndarray | No | |
if len(predictions) == 0: | ||
return False, None | ||
|
||
# TODO: Change this to DetectionsAndTime for image and telemetry merge for 2024 | ||
image_annotated = predictions[0].plot(conf=True) | ||
|
||
# Processing object detection | ||
|
@@ -89,7 +89,9 @@ def run(self, data: image_and_time.ImageAndTime) -> "tuple[bool, np.ndarray | No | |
|
||
self.__counter += 1 | ||
|
||
# TODO: Change this to DetectionsAndTime | ||
return True, image_annotated | ||
if self.__show_annotations: | ||
cv2.imshow("Annotated", image_annotated) | ||
|
||
return True, detections | ||
|
||
# pylint: enable=too-few-public-methods |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ def detect_target_worker(device: "str | int", | |
model_path: str, | ||
override_full: bool, | ||
save_name: str, | ||
show_annotations: bool, | ||
input_queue: queue_proxy_wrapper.QueueProxyWrapper, | ||
output_queue: queue_proxy_wrapper.QueueProxyWrapper, | ||
controller: worker_controller.WorkerController): | ||
|
@@ -23,7 +24,7 @@ def detect_target_worker(device: "str | int", | |
input_queue and output_queue are data queues. | ||
controller is how the main process communicates to this worker process. | ||
""" | ||
detector = detect_target.DetectTarget(device, model_path, override_full, save_name) | ||
detector = detect_target.DetectTarget(device, model_path, override_full, show_annotations, save_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make multiline. |
||
|
||
while not controller.is_exit_requested(): | ||
controller.check_pause() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
9.213867187500000000e-01 5.000000000000000000e+00 1.518750000000000000e+01 2.286562500000000000e+02 8.083125000000000000e+02 7.484062500000000000e+02 | ||
8.930664062500000000e-01 0.000000000000000000e+00 6.674062500000000000e+02 3.898125000000000000e+02 8.091562500000000000e+02 8.783437500000000000e+02 | ||
8.867187500000000000e-01 0.000000000000000000e+00 4.978125000000000000e+01 4.016250000000000000e+02 2.444765625000000000e+02 9.019687500000000000e+02 | ||
8.779296875000000000e-01 0.000000000000000000e+00 2.223281250000000000e+02 4.083750000000000000e+02 3.455156250000000000e+02 8.606250000000000000e+02 | ||
6.113281250000000000e-01 0.000000000000000000e+00 1.582031250000000000e-01 5.509687500000000000e+02 6.517968750000000000e+01 8.690625000000000000e+02 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
8.896484375000000000e-01 0.000000000000000000e+00 7.480000000000000000e+02 4.150000000000000000e+01 1.140000000000000000e+03 7.130000000000000000e+02 | ||
8.847656250000000000e-01 0.000000000000000000e+00 1.450000000000000000e+02 2.000000000000000000e+02 1.108000000000000000e+03 7.130000000000000000e+02 | ||
7.167968750000000000e-01 2.700000000000000000e+01 4.375000000000000000e+02 4.347500000000000000e+02 5.300000000000000000e+02 7.170000000000000000e+02 |
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,19 +2,31 @@ | |
Generates expected output using pretrained default model and images. | ||
TODO: PointsAndTime | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
import pathlib | ||
|
||
import cv2 | ||
import numpy as np | ||
import ultralytics | ||
|
||
|
||
TEST_PATH = pathlib.Path("tests", "model_example") | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Downloaded from: https://github.com/ultralytics/assets/releases | ||
MODEL_PATH = "tests/model_example/yolov8s_ultralytics_pretrained_default.pt" | ||
MODEL_PATH = pathlib.Path(TEST_PATH, "yolov8s_ultralytics_pretrained_default.pt") | ||
|
||
BUS_IMAGE_PATH = pathlib.Path(TEST_PATH, "bus.jpg") | ||
ZIDANE_IMAGE_PATH = pathlib.Path(TEST_PATH, "zidane.jpg") | ||
|
||
BUS_IMAGE_ANNOTATED_PATH = pathlib.Path(TEST_PATH, "bus_annotated.png") | ||
ZIDANE_IMAGE_ANNOTATED_PATH = pathlib.Path(TEST_PATH, "zidane_annotated.png") | ||
BUS_BOUNDING_BOX_PATH = pathlib.Path(TEST_PATH, "bounding_box_bus.txt") | ||
ZIDANE_BOUNDING_BOX_PATH = pathlib.Path(TEST_PATH, "bounding_box_zidane.txt") | ||
|
||
|
||
if __name__ == "__main__": | ||
model = ultralytics.YOLO(MODEL_PATH) | ||
image_bus = cv2.imread("tests/model_example/bus.jpg") | ||
image_zidane = cv2.imread("tests/model_example/zidane.jpg") | ||
image_bus = cv2.imread(BUS_IMAGE_PATH) | ||
image_zidane = cv2.imread(ZIDANE_IMAGE_PATH) | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# ultralytics saves as .jpg , bad for testing reproducibility | ||
results_bus = model.predict( | ||
|
@@ -34,7 +46,25 @@ | |
image_zidane_annotated = results_zidane[0].plot(conf=True) | ||
|
||
# Save image | ||
cv2.imwrite("tests/model_example/bus_annotated.png", image_bus_annotated) | ||
cv2.imwrite("tests/model_example/zidane_annotated.png", image_zidane_annotated) | ||
cv2.imwrite(BUS_IMAGE_ANNOTATED_PATH, image_bus_annotated) | ||
cv2.imwrite(ZIDANE_IMAGE_ANNOTATED_PATH, image_zidane_annotated) | ||
|
||
# Generate expected | ||
bounding_box_bus = results_bus[0].boxes.xyxy.detach().cpu().numpy() | ||
bounding_box_zidane = results_zidane[0].boxes.xyxy.detach().cpu().numpy() | ||
|
||
conf_bus = results_bus[0].boxes.conf.detach().cpu().numpy() | ||
conf_zidane = results_zidane[0].boxes.conf.detach().cpu().numpy() | ||
|
||
labels_bus = results_bus[0].boxes.cls.detach().cpu().numpy() | ||
labels_zidane = results_zidane[0].boxes.cls.detach().cpu().numpy() | ||
|
||
predictions_bus = np.insert(bounding_box_bus, 0, [conf_bus, labels_bus], axis=1) | ||
predictions_zidane = np.insert(bounding_box_zidane, 0, [conf_zidane, labels_zidane], axis=1) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unnecessary space characters. |
||
# Save expected to text file | ||
# Format: [confidence, label, x1, y1, x2, y2] | ||
np.savetxt(BUS_BOUNDING_BOX_PATH, predictions_bus) | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
np.savetxt(ZIDANE_BOUNDING_BOX_PATH, predictions_zidane) | ||
|
||
print("Done!") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove line 12 import (no longer being used).