Hi KwinT
Try this:
Updated Server Code
import cv2
import depthai as dai
import socket
import json
from ultralytics import YOLO
# Create YOLO model
model = YOLO('yolov8n-seg.pt')
# Create a DepthAI pipeline
pipeline = dai.Pipeline()
# Define source and output
camRgb = pipeline.create(dai.node.ColorCamera)
xoutVideo = pipeline.create(dai.node.XLinkOut)
xoutVideo.setStreamName("video")
# Properties
camRgb.setBoardSocket(dai.CameraBoardSocket.RGB)
camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
camRgb.setVideoSize(640, 480)
# Linking
camRgb.video.link(xoutVideo.input)
# Create Socket
server_port = 1080
host = socket.gethostname()
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('', server_port))
server_socket.listen(5)
print("Socket is listening")
# Accept client connection
client_socket, client_address = server_socket.accept()
print(f"Got connection from {client_address}")
# Connect to device and start pipeline
with dai.Device(pipeline) as device:
video_queue = device.getOutputQueue(name="video", maxSize=1, blocking=False)
while True:
video_frame = video_queue.get()
# Get BGR frame from NV12 encoded video frame to show with OpenCV
frame = video_frame.getCvFrame()
# Perform YOLOv8 inference on the frame
results = model(frame)
# Get annotated frame with YOLOv8 predictions
annotated_frame = results.show()
# All data needed to be transferred
output = []
for box in results.xyxy[0]:
x1, y1, x2, y2, _, class_id = [round(x) for x in box.tolist()]
class_name = results.names[int(class_id)]
output.append([x1, y1, x2, y2, class_name])
print(output)
# Send detection results to the client
client_socket.send(json.dumps(output).encode())
# Display the annotated frame
cv2.imshow("YOLOv8 Inference", annotated_frame)
if cv2.waitKey(1) == ord('q'):
break
client_socket.close()
server_socket.close()
print("Sockets closed")
cv2.destroyAllWindows()
Changes Made:
- Set the camera board socket to RGB: Adjusted
camRgb.setBoardSocket(dai.CameraBoardSocket.CAM_A)
to camRgb.setBoardSocket(dai.CameraBoardSocket.RGB)
for the correct socket.
- Use the correct YOLO method: Changed the method to display the annotated frame to
results.show()
which is the assumed correct method from the YOLO library.
- JSON encoding: Encoded the output list to a JSON string before sending it over the socket for easier parsing on the client side.
- Socket handling: The server now accepts the connection before entering the main loop, and properly closes sockets upon exit.
- Removed recursive server calls: Removed the unnecessary
server(s)
calls inside the while loop.
Updated Client Code
import socket
import json
# Set up the client socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_port = 1080
host = socket.gethostname()
# Connect to the server
s.connect((host, server_port))
print("Connected to the server")
try:
while True:
# Receive data from the server
data = s.recv(4096)
if not data:
break
detections = json.loads(data.decode())
print(detections)
finally:
print("Closing the client socket")
s.close()
Changes Made:
- Consistent data handling: Implemented a JSON-based protocol for the client to receive and decode the data.
- Exception handling: Added try-finally block to ensure the client socket is always closed properly.
Thanks,
Jaka