Skip to content

Commit

Permalink
#54 Added sensible target identifier and dump more metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
rland93 committed May 26, 2022
1 parent 400bd60 commit 9e9360f
Showing 1 changed file with 42 additions and 30 deletions.
72 changes: 42 additions & 30 deletions odcl_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def get_next_in_dir(datadir: Path, suffix: str, k=5):
Returns
-------
str
string with enumeration length k plus suffix
tuple(int, str)
integer, and len(k) string representation
"""
# find length k collection of digits
digit_re = k * [r"\d"]
Expand All @@ -97,28 +97,30 @@ def get_next_in_dir(datadir: Path, suffix: str, k=5):
max_num = current

# return padded string
return str(max_num + 1).rjust(k, "0") + suffix
return max_num, str(max_num + 1).rjust(k, "0")


if __name__ == "__main__":
rospy.init_node("odcl_node", anonymous=True)
rospy.loginfo("Started ODCL node...")

# odcl data dir is set to a rospy param
data_dir = rospy.get_param("/odcl/odcldir", "~/odcldata")
# make a folder with today's date
data_dir = Path(data_dir).resolve() / datetime.date.today().strftime("%Y%m%d")
os.makedirs(data_dir, exist_ok=True)
# make a directory to save raw images
raw_image_dir = data_dir / "rawimg"
raw_image_dir = data_dir / "raw"
# make a directory to save cropped images
crp_image_dir = data_dir / "crpimg"
for dir in (raw_image_dir, crp_image_dir):
crp_image_dir = data_dir / "crop"
for dir in (data_dir, raw_image_dir, crp_image_dir):
os.makedirs(dir, exist_ok=True)

# save odcl data txtfile
odcl_data_num = get_next_in_dir(data_dir, "-data.csv")
odcl_data_file = data_dir / odcl_data_num + "-data.csv"
# write headers to file
headers = "frame_no,in-frame-no,img_path,score,class,shapecolor,lettercolor,latitude,longitude\n"
run_id, run_id_str = get_next_in_dir(data_dir, "-data.csv")
odcl_data_file = data_dir / str(run_id_str + "-data.csv")

# write CSV headers to file
headers = "run_id,frame_no,target_no,target_id,img_path,crop_path,score,class,shape_color,letter_color,latitude,longitude,inference_time\n"
with open(odcl_data_file, "a") as f:
f.write(headers)

Expand All @@ -131,34 +133,38 @@ def get_next_in_dir(datadir: Path, suffix: str, k=5):
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 3000)
odcl_node = OdclNode(model_path, labels_path)

frameno = 0
frameno = 1
while True:
if capture.isOpened():
rospy.loginfo(f"New frame {frameno}")

# read raw image from camera
status, image_raw = capture.read()

# get frame number
frameno_str = str(frameno).rjust(5)
frame_id = run_id_str + "_" + frameno_str
rospy.loginfo(f"Captured Frame {frameno} with id {frame_id}")

# save raw image and log
raw_img_path = str(
raw_image_dir / get_next_in_dir(raw_image_dir, suffix="-raw.jpg")
)
cv2.imwrite(raw_img_path)
rospy.loginfo(f"Raw image saved to {raw_img_path}")
raw_img_path = raw_image_dir / str(frame_id + ".jpg")
cv2.imwrite(raw_img_path, image_raw)
rospy.loginfo(f"\tRaw image saved: {raw_img_path}")

# run the pipeline
t0 = time.time()
targets = odcl_node.pipeline.run(
image_raw,
odcl_node.gps,
odcl_node.altitude,
quat=odcl_node.quat,
)
# inference time in ms
inftime = (time.time() - t0) * 1000

rospy.loginfo("Performed inference on raw image.")
rospy.loginfo(f"Found {len(targets)} targets.")
frameno += 1
rospy.loginfo(f"\tPerformed inference on raw image, took {inftime}ms")
rospy.loginfo(f"\tFound {len(targets)} targets.")

for i, target in enumerate(targets):
targetno = str(i).rjust(2)
# unpack target information
lat = target["lat"]
lon = target["lon"]
Expand All @@ -170,17 +176,23 @@ def get_next_in_dir(datadir: Path, suffix: str, k=5):
score = round(target["score"], 3)

# log to rospy
targetinfo = f"Target {i}/{len(targets)}: shape={shape} ({score*100}%), scolor={scolor}, lcolor={lcolor}, lat={lat}, lon={lon}"
targetinfo = f"\tTarget {targetno}/{len(targets)}: shape={shape} ({score*100}%), scolor={scolor}, lcolor={lcolor}, lat={lat}, lon={lon}"
rospy.loginfo(targetinfo)

# save cropped image with unique identifier
targetid = frame_id + "_" + targetno
crop_fname = crp_image_dir / str(targetid + "jpg")
cv2.imwrite(crop_fname, img)
rospy.loginfo("\tSaved cropped image to {crop_fname}")

# write to csv
targetcsvline = f"{frameno},{i},{raw_img_path},{score},{shape},{scolor},{lcolor},{lat},{lon}\n"
targetcsvline = f"{run_id},{frameno},{targetno},{targetid},"
targetcsvline += f"{raw_img_path},{crop_fname},"
targetcsvline += f"{score},{shape},{scolor},{lcolor},{lat},{lon},"
targetcsvline += f"{inftime}\n"
with open(odcl_data_file, "a") as f:
f.write(targetcsvline)

# save cropped image with unique identifier
frameno_str = str(frameno).rjust(5)
i_str = str(i).rjust(2)
identifier = odcl_data_num[:5] + frameno_str + i_str
crop_fname = crp_image_dir / str(identifier + "jpg")
cv2.imwrite(crop_fname, img)
rospy.loginfo("\tRecorded data to {odcl_data_file}")

frameno += 1

0 comments on commit 9e9360f

Please sign in to comment.