Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
hsidd1 committed Dec 6, 2023
1 parent d8c2820 commit f81b304
Show file tree
Hide file tree
Showing 18 changed files with 290 additions and 150 deletions.
7 changes: 4 additions & 3 deletions live_cameraclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def on_log(client, userdata, level, buf):
client.on_message = on_message

global cap
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) # external camera
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) # update index for external camera
cap.set(cv2.CAP_PROP_FRAME_WIDTH, config["LiveData"]["camera"]["width"])
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, config["LiveData"]["camera"]["height"])
cap.set(cv2.CAP_PROP_FPS, config["LiveData"]["camera"]["fps"])
Expand All @@ -56,13 +56,14 @@ def on_log(client, userdata, level, buf):
status = res[0]
if status == 0:
print(f"Send {len(payload)} bytes to topic data/livecamera")
#i += 1
# i += 1
else:
print("No frame received.")
except KeyboardInterrupt:
print("LIVE CAMERA: Process Terminated. Exiting Camera...")
break
#print(f"FPS is {num_frames/(time.perf_counter()-start)}")
# print(f"FPS is {num_frames/(time.perf_counter()-start)}")


def main():
try:
Expand Down
18 changes: 11 additions & 7 deletions processModule/camera_process.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
import yaml
import cv2
import yaml
import cv2

with open("config.yaml", "r") as f:
config = yaml.safe_load(f)
Expand All @@ -9,6 +9,7 @@
SAMPLE_IMG = "data/sample_frame.png"
data_array = cv2.imread(SAMPLE_IMG)


# used in pre-recorded data playback
def process_frames(frame_payload: bytearray) -> None:
# convert byte array to numpy array for cv2 to read
Expand All @@ -21,26 +22,29 @@ def process_frames(frame_payload: bytearray) -> None:
else:
cv2.waitKey(0)


# used in live data playback
def process_livecam(payload: bytearray) -> None:
timestamp = payload[-26:].decode("utf-8")
# print("Timestamp: ", timestamp)
frame_payload = payload[:-26] # remove timestamp
frame_payload = payload[:-26] # remove timestamp

# convert byte array to numpy array for cv2 to read
frame = np.frombuffer(frame_payload, dtype=np.uint8)
if not hasattr(process_livecam, 'window_created'):
if not hasattr(process_livecam, "window_created"):
cv2.namedWindow("Live Camera Feed")
process_livecam.window_created = True
font = cv2.FONT_HERSHEY_SIMPLEX
bottom_left_corner = (10, 30)
font_scale = 1
font_color = (255, 255, 255)
line_type = 2
frame = frame.reshape(data_array.shape) # (480, 640, 3)
cv2.putText(frame, timestamp, bottom_left_corner, font, font_scale, font_color, line_type)
frame = frame.reshape(data_array.shape) # (480, 640, 3)
cv2.putText(
frame, timestamp, bottom_left_corner, font, font_scale, font_color, line_type
)
cv2.imshow("Live Camera Feed", frame)
if config["CameraOutput"]["continuous_frame_mode"]:
cv2.waitKey(1)
else:
cv2.waitKey(0)
cv2.waitKey(0)
9 changes: 6 additions & 3 deletions processModule/radar_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@

with open("config.yaml", "r") as f:
config = yaml.safe_load(f)

radar_data_file = config["Files"]["radar_data_file"]
with open(radar_data_file) as json_file:
data = json.load(json_file)


# sensorhost format
def rd_process():
radar_points = []
for item in data["frames"]:
num_ob = item["sensorMessage"]["metadata"]["numOfDetectedObjects"]
detected_points = item["sensorMessage"]["object"]["detectedPoints"]
timestamp = item["timestamp"]
timestamp = item["timestamp"]

for j in range(num_ob):
s = dict()
Expand All @@ -30,8 +31,10 @@ def rd_process():
radar_points.append(s)
return radar_points


data = rd_process()


def process_radar(radar_payload: list[dict]) -> None:
# TODO: generate text and data points from radar data to plot over image
raise NotImplementedError
raise NotImplementedError
14 changes: 8 additions & 6 deletions processModule/save_data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cv2
import cv2
import numpy as np
import json, yaml
import datetime as dt
Expand All @@ -20,6 +20,7 @@
with open("config.yaml", "r") as f:
config = yaml.safe_load(f)


def save_data(topic: str, payload: bytes | str) -> dict | None:
"""
saves live camera frames and radar data to ./liveDataLog/camera_data and ./liveDataLog/radcam_log.json
Expand All @@ -33,8 +34,8 @@ def save_data(topic: str, payload: bytes | str) -> dict | None:
sub_ts = str(dt.datetime.now().isoformat())
if topic == CAMERA_TOPIC:
save_data.frame_id += 1
cam_ts = payload[-26:].decode("utf-8") # ts is 26 bytes extension
frame_payload = payload[:-26] # remove timestamp
cam_ts = payload[-26:].decode("utf-8") # ts is 26 bytes extension
frame_payload = payload[:-26] # remove timestamp
frame = np.frombuffer(frame_payload, dtype=np.uint8)
height = config["LiveData"]["camera"]["height"]
width = config["LiveData"]["camera"]["width"]
Expand All @@ -46,7 +47,7 @@ def save_data(topic: str, payload: bytes | str) -> dict | None:
"topic": topic,
"sub_ts": sub_ts,
"pub_ts": cam_ts,
"frame_id": save_data.frame_id
"frame_id": save_data.frame_id,
}
# save radar data
if topic == RADAR_TOPIC:
Expand All @@ -59,7 +60,7 @@ def save_data(topic: str, payload: bytes | str) -> dict | None:
except json.JSONDecodeError:
radar_object2 = {"radar_payload": payload.decode("utf-8")}
radar_object = {**radar_object1, **radar_object2}

with open(RADCAM_PATH, "a") as f:
if f.tell() == 0:
f.write("[")
Expand All @@ -70,6 +71,7 @@ def save_data(topic: str, payload: bytes | str) -> dict | None:
if radar_object:
json.dump(radar_object, f)


"""
class CamPayload:
def __init__(self, sub_ts, pub_ts, frame_id) -> None:
Expand Down Expand Up @@ -106,4 +108,4 @@ def to_json(self) -> dict:
"pub_ts": self.pub_ts,
"radar_json": self.radar_json
}
"""
"""
4 changes: 3 additions & 1 deletion processModule/serverConnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
broker = config["mqtt"]["broker"]
port = config["mqtt"]["port"]


def connect_mqtt(client_id) -> mqtt_client:
def on_connect(client, userdata, flags, rc):
if rc == 0:
Expand All @@ -21,6 +22,7 @@ def on_connect(client, userdata, flags, rc):
client.connect(broker, port)
return client


def on_disconnect(client, userdata, rc):
if rc != 0:
print(f"Unexpected MQTT disconnection. Will auto-reconnect (rc={rc})")
print(f"Unexpected MQTT disconnection. Will auto-reconnect (rc={rc})")
6 changes: 3 additions & 3 deletions scripts/clear_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
RADCAMLOG = "../liveDataLog/radcam_log.json"
IMGS = "../liveDataLog/camera_data/"

with open (RADCAMLOG, "w") as f:
with open(RADCAMLOG, "w") as f:
f.write("")
print("Cleared radcam_log.json")

# delete all jpgs
for file in os.listdir(IMGS):
if file.endswith(".jpg"):
os.remove(IMGS + file)
#print(f"Deleted {file}")
print("Cleared camera_data folder.")
# print(f"Deleted {file}")
print("Cleared camera_data folder.")
4 changes: 2 additions & 2 deletions scripts/extract_logitech_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
ret, frame = cap.read()
cv2.imshow("frame", frame)
print(frame.shape)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if cv2.waitKey(1) & 0xFF == ord("q"):
break
4 changes: 2 additions & 2 deletions scripts/frame_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
with open("config.yaml", "r") as f:
config = yaml.safe_load(f)

if __name__ == '__main__':
if __name__ == "__main__":
cap = cv2.VideoCapture(config["Files"]["video_file"])
ret, frame = cap.read()
with open("data/sample_frame.txt", "a") as f:
f.write(str(bytearray(frame)))
print("Sample frame written to data/sample_frame.txt")
cap.release()
cv2.destroyAllWindows()
cv2.destroyAllWindows()
6 changes: 5 additions & 1 deletion scripts/image_extract.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import cv2

"""
Extracting random frame as sample for resizing, to account for
not being able to transmit a numpy array over MQTT. To reference
a reshape size, this script is used to extract a frame from the
video file and save it as an image.
"""

frame_num = 50 # extract 50th frame as sample (arbitrary)
frame_num = 50 # extract 50th frame as sample (arbitrary)


def save_frame(video_path, output_path):
cap = cv2.VideoCapture(video_path)
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_num)
Expand All @@ -20,6 +23,7 @@ def save_frame(video_path, output_path):
# Release the video capture object
cap.release()


if __name__ == "__main__":
input_video_path = "data/camdata.avi"
output_image_path = "data/sample_frame.png"
Expand Down
53 changes: 32 additions & 21 deletions t1_cam_client.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import cv2
import yaml
import datetime as dt
import time
import time
from processModule.serverConnect import connect_mqtt

with open("config.yaml", "r") as f:
config = yaml.safe_load(f)
CLIENT_ID = config["mqtt"]["client_id2"]


def on_log(client, userdata, level, buf):
print("log: ",buf)
print("log: ", buf)


def on_message(client, userdata, message):
if message.topic == "data/camera/frame":
print(f"Received {len(message.payload)} bytes from topic {message.topic}")
else:
print("message received " ,str(message.payload.decode("utf-8")))
print("message topic=",message.topic)
print("message qos=",message.qos)
print("message retain flag=",message.retain)
print("message received ", str(message.payload.decode("utf-8")))
print("message topic=", message.topic)
print("message qos=", message.qos)
print("message retain flag=", message.retain)


def publish(client):
def publish(client):
frame_id = 0
i = 0
frame_start = config["CameraOutput"]["frame_start"]
Expand All @@ -33,46 +36,53 @@ def publish(client):
continue
frame_id += 1
if frame_id % frame_skip != 0:
i+=1
i += 1
continue

if not ret:
break

msg_str = f"f_id {frame_id}: {frame}"
topic="data/camera/frame"
topic = "data/camera/frame"
msg = bytearray(frame)
res = client.publish(topic, payload=msg, qos=0) # QoS 0 for frames
res = client.publish(topic, payload=msg, qos=0) # QoS 0 for frames
status = res[0]
if status == 0:
if config["mqtt"]["compressed_output"]:
print(f"{CLIENT_ID}: Send `{msg[:10]}` to topic `{topic} (fid={frame_id})`\n")
print(
f"{CLIENT_ID}: Send `{msg[:10]}` to topic `{topic} (fid={frame_id})`\n"
)
else:
print(f"{CLIENT_ID}: Send `{msg}` to topic `{topic} (fid={frame_id})`\n")
print(
f"{CLIENT_ID}: Send `{msg}` to topic `{topic} (fid={frame_id})`\n"
)
else:
print(f"{CLIENT_ID}: Failed to send frame message to topic {topic}")
timestamp = dt.datetime.now().isoformat()
topic="data/camera/ts"
res = client.publish(topic, payload=timestamp, qos=1) # QoS 1 for timestamps
topic = "data/camera/ts"
res = client.publish(topic, payload=timestamp, qos=1) # QoS 1 for timestamps
status = res[0]
if status == 0:
print(f"{CLIENT_ID}: Send `{timestamp}` to topic `{topic}`\n")
else:
print(f"{CLIENT_ID}: Failed to send timestamp message to topic {topic}")
time.sleep(0.033)
i+=1
i += 1


cap = cv2.VideoCapture(config["Files"]["video_file"])


def run():
def exit_handler(client):
cap.release()
client.disconnect()
cv2.destroyAllWindows()

try:
client = connect_mqtt(CLIENT_ID)
client = connect_mqtt(CLIENT_ID)
if config["mqtt"]["show_log"]:
client.on_message=on_message
client.on_message = on_message
# client.on_log=on_log
client.loop_start()
publish(client)
Expand All @@ -85,5 +95,6 @@ def exit_handler(client):
print(e)
exit_handler(client)

if __name__ == '__main__':
run()

if __name__ == "__main__":
run()
Loading

0 comments on commit f81b304

Please sign in to comment.