**Good afternoon, I am trying to do a 3D reconstruction using two images, one RGB and one depth, but when I create the 3D image it does not reconstruct the objects correctly.
My code is as follows:
**
import paho.mqtt.client as mqtt
import json
import time
import os
import shutil
import fcntl
import socket
import cv2
import depthai as dai
import numpy as np
from datetime import timedelta
# TCP Variablessocket_connection = Nonesocket_connected = False
# Dont allow more than one instancelock_file = open('/tmp/capture_image.lock', 'w')try: fcntl.lockf(lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB)except IOError: sys.exit(0)
# Funcio per crear la imatge de profunditatdef colorizeDepth(frameDepth): frameDepth_p = np.clip(frameDepth, 0, 2**24 - 1).astype(np.uint32)
R = (frameDepth_p >> 16) & 0xFF G = (frameDepth_p >> 8) & 0xFF B = frameDepth_p & 0xFF
encoded_frameDepth = np.stack([B, G, R], axis=-1).astype(np.uint8)
return encoded_frameDepth
# Funcio per enviar un missatge per socket TCPdef send_message_tcp(filename, server_ip, server_port): global socket_connection, socket_connected if not connect_to_tcp(server_ip, server_port): write_to_log(logfiles_path_json, logfile_name_error_json, "Error connectant amb el servidor TCP") return
try: socket_connection.sendall(filename.encode('utf-8')) except Exception as e: write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Error enviant missatge per TCP: {e}") write_to_log(logfiles_path_json, logfile_name_error_json, f"Error enviant missatge per TCP: {e}") try: socket_connection.close() except: pass socket_connection = None socket_connected = False # Funcio per connectar al socket TCP per enviar el path de la fotografiadef connect_to_tcp(server_ip, server_port): global socket_connection, socket_connected if socket_connected: return True try: socket_connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) socket_connection.connect((server_ip, server_port)) write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Connexio realitzada al socket correctament") socket_connected = True return True except Exception as e: socket_connected = False socket_connection = None write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Error al connectar al servidor TCP: {e}") write_to_log(logfiles_path_json, logfile_name_error_json, f"Error al connectar al servidor TCP: {e}") return False
# Funcio per escriure en el fitxer de logsdef write_to_log(pathfile, filename, log_message): try: new_filename = time.strftime("%Y-%m-%d", time.localtime()) + "_" + filename
if not os.path.exists(pathfile + new_filename): with open(pathfile + new_filename, 'w'): pass
with open(pathfile + new_filename, 'a') as log: current_time = time.strftime("%H%M%S", time.localtime()) log_entry = f"{current_time}: {log_message}" log.write(log_entry + "\n") except Exception as e: pass
# Funcio per realitzar la fotografiadef take_picture(): try: timestamp = time.strftime("%Y%m%d_%H%M%S") messageGroup = queue.get() frameRgb = messageGroup["rgb"] frameDepth = messageGroup["depth_aligned"] if frameDepth is not None: cvFrame = frameRgb.getCvFrame() cvFrameUndistorted = cv2.undistort( cvFrame, np.array(frameRgb.getTransformation().getIntrinsicMatrix()), np.array(frameRgb.getTransformation().getDistortionCoefficients()), ) alignedDepthColorized = colorizeDepth(frameDepth.getFrame()) nameColor = f"{save_path_json}{timestamp}{name_image_rgb_json}" nameDepth = f"{save_path_json}{timestamp}{name_image_depth_json}" cv2.imwrite(nameColor, cvFrameUndistorted) write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Finalitzat la captura de la imatge {nameColor}") cv2.imwrite(nameDepth, alignedDepthColorized) write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Finalitzat la captura de la imatge {nameDepth}") #Send images by TCP tcp_filename = f"{path_windows_json}{timestamp}{name_image_rgb_json}{character_to_split_images_json}{path_windows_json}{timestamp}{name_image_depth_json}" send_message_tcp(tcp_filename, server_tcp_ip_json, server_tcp_port_json) write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Enviat per TCP les imatges") except Exception as e: write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Error fent la foto: {e}") write_to_log(logfiles_path_json, logfile_name_error_json, f"Error fent la foto: {e}")
# Funcio per connectar amb el mosquitto, broker del MQTTdef on_connect(client, userdata, flags, rc): if rc == 0: write_to_log(logfiles_path_json, logfile_name_traceability_json, "Connectat al broker MQTT") client.subscribe(mqtt_take_picture_json) else: write_to_log(logfiles_path_json, logfile_name_traceability_json, "Error: No s'ha connectat al broker MQTT") write_to_log(logfiles_path_json, logfile_name_error_json, "No s'ha connectat al broker MQTT")
# Funcio per gestionar els missatges que es rebendef on_message(client, userdata, msg): take_picture()
# Funcio per gestionar si es desconecta la cua MQTT, que es torni a reconectar automaticament def on_disconnect(client, userdata, rc): write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Desconnectat del servidor MQTT, missatge {rc}") while True: try: client.reconnect() write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Reconnexio al MQTT realitzada correctament") break except Exception as e: write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Error: Al intentar reconnectar al MQTT s'ha obtingut aquest error: {e}") write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Al intentar reconnectar al MQTT s'ha obtingut aquest error: {e}") time.sleep(5)
# Variables globalssave_path_json = ""width_resolution_rgb_json = 0height_resolution_rgb_json = 0width_resolution_depth_json = 0height_resolution_depth_json = 0FPS_json = 0name_image_rgb_json = ""name_image_depth_json = ""logfiles_path_json = ""logfile_name_traceability_json = ""logfile_name_error_json = ""path_windows_json = ""jsonData = ""mqtt_take_picture_json = ""mqtt_ip_json = ""mqtt_port_json = 0mqtt_keepalive_json = 60
try: with open("appsettings.json", "r") as file: jsonData = json.load(file)except Exception as e: write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Error obrint appsettings: {e}") write_to_log(logfiles_path_json, logfile_name_error_json, f"Error obrint appsettings: {e}")
# read JSON values save_path_json = jsonData["basePath"]width_resolution_rgb_json = jsonData["widthResolutionRGB"]height_resolution_rgb_json = jsonData["heightResolutionRGB"]width_resolution_depth_json = jsonData["widthResolutionDepth"]height_resolution_depth_json = jsonData["heightResolutionDepth"]FPS_json = jsonData["FPS"]time_to_next_photo_json = jsonData["timeToNextPhoto"]name_image_rgb_json = jsonData["nameImageRGB"]name_image_depth_json = jsonData["nameImageDepth"]logfiles_path_json = jsonData["logfilesPath"]logfile_name_traceability_json = jsonData["logFileNameTraceability"]logfile_name_error_json = jsonData["logFileNameError"]server_tcp_ip_json = jsonData["serverTcpIp"]server_tcp_port_json = jsonData["serverTcpPort"]path_windows_json = jsonData["pathWindows"]character_to_split_images_json = jsonData["characterToSplitImages"]mqtt_take_picture_json = jsonData["MQTT"]["takePicture"]mqtt_ip_json = jsonData["MQTT"]["ip"]mqtt_port_json = jsonData["MQTT"]["port"]mqtt_keepalive_json = jsonData["MQTT"]["keepAlive"] # Save load configurations in logwrite_to_log(logfiles_path_json, logfile_name_traceability_json, f"Inicialitzant apliacio")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Base Path: {save_path_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Width Resolution RGB: {width_resolution_rgb_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Height Resolution RGB: {height_resolution_rgb_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Width Resolution Depth: {width_resolution_depth_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Height Resolution Depth: {height_resolution_depth_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"FPS: {FPS_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Time to Next Photo: {time_to_next_photo_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Name Image RGB: {name_image_rgb_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Name Image Depth: {name_image_depth_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Logfiles Path: {logfiles_path_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Logfile Name Traceability: {logfile_name_traceability_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Logfile Name Error: {logfile_name_error_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Server IP: {server_tcp_ip_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Server Port: {server_tcp_port_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Path Windows: {path_windows_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Character to split images: {character_to_split_images_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"MQTT take picture: {mqtt_take_picture_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"MQTT IP: {mqtt_ip_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"MQTT port: {mqtt_port_json}")write_to_log(logfiles_path_json, logfile_name_traceability_json, f"MQTT keep alive: {mqtt_keepalive_json}")
# Camera starttry: device = dai.Device() pipeline = dai.Pipeline(device) platform = pipeline.getDefaultDevice().getPlatform()
# Define sources and outputs camRgb = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_A) left = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_B) right = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_C) stereo = pipeline.create(dai.node.StereoDepth) stereo.setDefaultProfilePreset(dai.node.StereoDepth.PresetMode.DEFAULT) stereo.initialConfig.setMedianFilter(dai.MedianFilter.KERNEL_7x7) stereo.setLeftRightCheck(True) stereo.setExtendedDisparity(False) stereo.setSubpixel(False) sync = pipeline.create(dai.node.Sync) if platform == dai.Platform.RVC4: align = pipeline.create(dai.node.ImageAlign) sync.setSyncThreshold(timedelta(seconds=1/(2*FPS_json)))
rgbOut = camRgb.requestOutput(size = (width_resolution_rgb_json, height_resolution_rgb_json), fps = FPS_json) leftOut = left.requestOutput(size = (width_resolution_depth_json, height_resolution_depth_json), fps = FPS_json) rightOut = right.requestOutput(size = (width_resolution_depth_json, height_resolution_depth_json), fps = FPS_json) # Linking rgbOut.link(sync.inputs["rgb"]) leftOut.link(stereo.left) rightOut.link(stereo.right) if platform == dai.Platform.RVC4: stereo.depth.link(align.input) rgbOut.link(align.inputAlignTo) align.outputAligned.link(sync.inputs["depth_aligned"]) else: stereo.depth.link(sync.inputs["depth_aligned"]) rgbOut.link(stereo.inputAlignTo)
queue = sync.out.createOutputQueue() pipeline.start() time.sleep(1) print("Preparat") write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Camara iniciada correctament")except Exception as e: write_to_log(logfiles_path_json, logfile_name_traceability_json, f"Error al inicialitzar la camara: {e}") write_to_log(logfiles_path_json, logfile_name_error_json, f"Error al inicialitzar la camara: {e}")
# Creem el client MQTTclient = mqtt.Client(protocol=mqtt.MQTTv311)client.on_connect = on_connectclient.on_message = on_messageclient.on_disconnect = on_disconnect
# Connnectem al mosquittoclient.connect(mqtt_ip_json, mqtt_port_json, mqtt_keepalive_json)
# Bucle infinit per poder gestionar els missatgesclient.loop_forever()
Do you know why the reconstruction could be so poor?
I have tried recalibrating the camera, changing the background colors, and adjusting the lighting, but it still looks just as bad.