Skip to content

Commit

Permalink
fix: cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
rise0chen committed Jun 24, 2024
1 parent 32315b5 commit 16ff45c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 28 deletions.
4 changes: 2 additions & 2 deletions apriltag/bin/src/apriltag.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ def get_ip():
val = "127.0.0.1"
return val
def shoot_img():
lebai.set_item("plugin_camera_cmd", "shoot")
lebai.set_item("plugin_camera_cmd_shoot", "shoot")
while True:
time.sleep(0.1)
cmd = (lebai.get_item("plugin_camera_cmd"))['value']
cmd = (lebai.get_item("plugin_camera_cmd_shoot"))['value']
if not cmd or cmd == "":
break

Expand Down
14 changes: 9 additions & 5 deletions camera/bin/src/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
lebai = lebai_sdk.connect("127.0.0.1", True)

def get_cmd():
cmd = (lebai.get_item("plugin_camera_cmd"))['value']
if not cmd:
cmd = ""
return cmd
cmd = (lebai.get_item("plugin_camera_cmd_reinit"))['value']
if cmd and cmd != "":
return "reinit"
cmd = (lebai.get_item("plugin_camera_cmd_shoot"))['value']
if cmd and cmd != "":
return "shoot"

return ""

def init_camera():
index = (lebai.get_item("plugin_camera_index"))['value']
Expand Down Expand Up @@ -60,7 +64,7 @@ def main():
if cmd == "reinit":
cap.release()
cap = init_camera()
lebai.set_item("plugin_camera_cmd", "")
lebai.set_item("plugin_camera_cmd_{}".format(cmd), "")
exit(2)
# 释放摄像头
cap.release()
Expand Down
4 changes: 2 additions & 2 deletions camera/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
};
ws.send(JSON.stringify(rpc_data));
rpc_data.params[0] = {
"key": "plugin_camera_cmd",
"key": "plugin_camera_cmd_reinit",
"value": "reinit"
};
ws.send(JSON.stringify(rpc_data));
Expand Down Expand Up @@ -110,7 +110,7 @@
rpc_data.id = 10;
rpc_data.method = "set_item";
rpc_data.params[0] = {
"key": "plugin_camera_cmd",
"key": "plugin_camera_cmd_shoot",
"value": "shoot"
};
ws.send(JSON.stringify(rpc_data));
Expand Down
30 changes: 20 additions & 10 deletions camera_calibrater/bin/src/camera_calibrater.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,20 @@ def get_i():
i = int(i)
return i
def get_cmd():
cmd = (lebai.get_item("plugin_camera_calibrater_cmd"))['value']
if not cmd:
cmd = ""
return cmd
cmd = (lebai.get_item("plugin_camera_calibrater_cmd_clear"))['value']
if cmd and cmd != "":
return "clear"
cmd = (lebai.get_item("plugin_camera_calibrater_cmd_calibrate"))['value']
if cmd and cmd != "":
return "calibrate"
cmd = (lebai.get_item("plugin_camera_calibrater_cmd_record"))['value']
if cmd and cmd != "":
return "record"
cmd = (lebai.get_item("plugin_camera_calibrater_cmd_preview"))['value']
if cmd and cmd != "":
return "preview"

return ""
def get_pose(i):
pose = (lebai.get_item("plugin_camera_calibrater_pose_{}".format(i)))['value']
if not pose:
Expand All @@ -65,10 +75,10 @@ def clear_imgs():
os.remove(os.path.join(images_dir, "camera_calibrater.{}.webp".format(i)))

def shoot_img():
lebai.set_item("plugin_camera_cmd", "shoot")
lebai.set_item("plugin_camera_cmd_shoot", "shoot")
while True:
time.sleep(0.1)
cmd = (lebai.get_item("plugin_camera_cmd"))['value']
cmd = (lebai.get_item("plugin_camera_cmd_shoot"))['value']
if not cmd or cmd == "":
break

Expand All @@ -82,7 +92,7 @@ def main():
shoot_img()
img = cv2.imread(os.path.join(images_dir, "img.webp"))
if img.size == 0:
lebai.set_item("plugin_camera_calibrater_cmd", "")
lebai.set_item("plugin_camera_calibrater_cmd_preview", "")
continue
row, col, width = get_row_col_width()
ret, corners = cv2.findChessboardCorners(img, (row, col), None)
Expand Down Expand Up @@ -133,12 +143,12 @@ def main():
image_points_len = len(image_points)
if image_points_len < 3:
print("image not enough")
lebai.set_item("plugin_camera_calibrater_cmd", "")
lebai.set_item("plugin_camera_calibrater_cmd_calibrate", "")
continue
ret, camera_matrix, dist_coeffs, rvecs, tvecs = cv2.calibrateCamera(obj_points, image_points, img.shape[::-1], None, None)
if not ret:
print("failed to calibrateCamera")
lebai.set_item("plugin_camera_calibrater_cmd", "")
lebai.set_item("plugin_camera_calibrater_cmd_calibrate", "")
continue
lebai.set_item("plugin_camera_calibrater_camera_matrix", json.dumps(camera_matrix.tolist())) # 相机内参
lebai.set_item("plugin_camera_calibrater_dist_coeffs", json.dumps(dist_coeffs.tolist())) # 相机畸变
Expand All @@ -157,7 +167,7 @@ def main():

clear_imgs()
lebai.set_item("plugin_camera_calibrater_i", str(0))
lebai.set_item("plugin_camera_calibrater_cmd", "")
lebai.set_item("plugin_camera_calibrater_cmd_{}".format(cmd), "")

if __name__ == '__main__':
main()
35 changes: 26 additions & 9 deletions camera_calibrater/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,17 @@
ws.send(JSON.stringify(rpc_data));
rpc_data.id = 51;
rpc_data.params[0] = {
"key": "plugin_camera_calibrater_cmd",
"key": "plugin_camera_calibrater_cmd_record",
};
ws.send(JSON.stringify(rpc_data));
rpc_data.id = 52;
rpc_data.params[0] = {
"key": "plugin_camera_calibrater_cmd_clear",
};
ws.send(JSON.stringify(rpc_data));
rpc_data.id = 53;
rpc_data.params[0] = {
"key": "plugin_camera_calibrater_cmd_calibrate",
};
ws.send(JSON.stringify(rpc_data));
rpc_data.id = 61;
Expand All @@ -151,7 +161,7 @@
rpc_data.id = 10;
rpc_data.method = "set_item";
rpc_data.params[0] = {
"key": "plugin_camera_calibrater_cmd",
"key": "plugin_camera_calibrater_cmd_record",
"value": "record"
};
ws.send(JSON.stringify(rpc_data));
Expand All @@ -161,7 +171,7 @@
rpc_data.id = 10;
rpc_data.method = "set_item";
rpc_data.params[0] = {
"key": "plugin_camera_calibrater_cmd",
"key": "plugin_camera_calibrater_cmd_clear",
"value": "clear"
};
ws.send(JSON.stringify(rpc_data));
Expand All @@ -171,21 +181,22 @@
rpc_data.id = 10;
rpc_data.method = "set_item";
rpc_data.params[0] = {
"key": "plugin_camera_calibrater_cmd",
"key": "plugin_camera_calibrater_cmd_calibrate",
"value": "calibrate"
};
ws.send(JSON.stringify(rpc_data));
wait_cmd_finish();
}
function wait_cmd_finish() {
get();
let timer;
timer = setInterval(() => {
get();
if (window.lebai_plugin_camera_calibrater_cmd == "") {
if (window.cmd_record == "" && window.cmd_clear == "" && window.cmd_calibrate == "") {
clearInterval(timer);
refresh_img();
}
}, 500);
}, 1000);
}
function refresh_img() {
document.getElementById("pic1").src = "../../camera/images/camera_calibrater.1.webp?" + Date.now();
Expand Down Expand Up @@ -232,7 +243,13 @@
document.getElementById("width").value = value || "0.03";
}
if (id == 51) {
window.lebai_plugin_camera_calibrater_cmd = value || "";
window.cmd_record = value || "";
}
if (id == 52) {
window.cmd_clear = value || "";
}
if (id == 53) {
window.cmd_calibrate = value || "";
}
if (id == 61) {
document.getElementById("matrix").value = value || "";
Expand All @@ -253,12 +270,12 @@
connect();
}
}, 1000);

function preview() {
rpc_data.id = 10;
rpc_data.method = "set_item";
rpc_data.params[0] = {
"key": "plugin_camera_calibrater_cmd",
"key": "plugin_camera_calibrater_cmd_preview",
"value": "preview"
};
ws.send(JSON.stringify(rpc_data));
Expand Down

0 comments on commit 16ff45c

Please sign in to comment.