import socket
import struct
import json
import math
import numpy as np
import cv2
import pyzed.sl as zed
import threading
# 与Qt接收端建立TCP连接
host = '192.168.137.12' # Qt接收端的IP地址
port = 49279 # Qt接收端的端口
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host, port))
print("连接成功")
# 与Qt接收端建立TCP连接2
host2 = '192.168.137.12' # Qt接收端的IP地址
port2 = 49278 # Qt接收端的端口
client_socket2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket2.connect((host2, port2))
print("连接成功2")
# 初始化ZED相机
cam = zed.Camera()
init = zed.InitParameters()
init.camera_resolution = zed.RESOLUTION.HD720
init.depth_mode = zed.DEPTH_MODE.ULTRA
init.coordinate_units = zed.UNIT.METER
init.depth_minimum_distance = 0.1
init.depth_maximum_distance = 20
err = cam.open(init)
if err != zed.ERROR_CODE.SUCCESS:
print(repr(err))
cam.close()
exit(1)
# 参数
runtime = zed.RuntimeParameters(confidence_threshold=50)
depth_image = zed.Mat()
point_cloud = zed.Mat()
# 设置HSV阈值用于检测白色物体
lower_white = np.array([0, 0, 255], dtype=np.uint8)
upper_white = np.array([180, 30, 255], dtype=np.uint8)
# 设置HSV阈值用于排除蓝色背景
lower_blue = np.array([100, 150, 0], dtype=np.uint8)
upper_blue = np.array([140, 255, 255], dtype=np.uint8)
# 控制变量
frame_count = 0
transmit_frequency = 10;# 每20帧传输一次数据
# 图像处理函数
def image_processing():
global frame_count
while True:
if cam.grab(runtime) == zed.ERROR_CODE.SUCCESS:
cam.retrieve_image(depth_image, zed.VIEW.LEFT)
frame = depth_image.get_data()
cam.retrieve_measure(point_cloud, zed.MEASURE.XYZRGBA)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask_white = cv2.inRange(hsv, lower_white, upper_white)
mask_blue = cv2.inRange(hsv, lower_blue, upper_blue)
mask_white = cv2.bitwise_and(mask_white, cv2.bitwise_not(mask_blue))
kernel = np.ones((5, 5), np.uint8)
mask_white = cv2.dilate(mask_white, kernel)
opening = cv2.morphologyEx(mask_white, cv2.MORPH_OPEN, kernel)
bila = cv2.bilateralFilter(mask_white, 10, 200, 200)
edges = cv2.Canny(opening, 50, 100)
circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, 1, 100, param1=100, param2=32, minRadius=10, maxRadius=50)
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0]:
center_x = int(i[0])
center_y = int(i[1])
err, point3D = point_cloud.get_value(center_x, center_y)
if err == zed.ERROR_CODE.SUCCESS and not math.isnan(point3D[2]):
distance = math.sqrt(point3D[0] ** 2 + point3D[1] ** 2 + point3D[2] ** 2)
world_x = point3D[0]
world_y = point3D[1]
world_z = point3D[2]
print(f"圆心世界坐标位置: x={world_x:.3f}m, y={world_y:.3f}m, z={world_z:.3f}m, 距离: {distance:.3f}m")
data = {"x": world_x, "y": world_y, "z": world_z, "distance": distance}
if frame_count % transmit_frequency == 0:
encoded_data = json.dumps(data).encode()
client_socket.send( encoded_data)
cv2.circle(frame, (center_x, center_y), i[2], (0, 255, 0), 2)
cv2.circle(frame, (center_x, center_y), 2, (0, 0, 255), 3)
# cv2.imshow('Frame', frame)
frame_count += 1
key = cv2.waitKey(1)
if key == 27: # 如果按下ESC键,则退出
break
# 数据传输函数
def transmit_data():
while True:
if cam.grab(runtime) == zed.ERROR_CODE.SUCCESS:
cam.retrieve_image(depth_image, zed.VIEW.LEFT)
frame = depth_image.get_data()
if frame_count % transmit_frequency == 0:
_, img_encoded = cv2.imencode('.jpg', frame)
img_bytes = img_encoded.tobytes()
img_size = len(img_bytes)
#client_socket.sendall(struct.pack("L", len(img_bytes)) + img_bytes)
client_socket.sendall(struct.pack("Q", img_size) + img_bytes)
key = cv2.waitKey(1)
if key == 27: # 如果按下ESC键,则退出
break
# 创建并启动图像处理线程
image_thread = threading.Thread(target=image_processing, args=(client_socket2,))
image_thread.start()
# 创建并启动数据传输线程
transmit_thread = threading.Thread(target=transmit_data, args=(client_socket,))
transmit_thread.start()
# 等待图像处理线程结束
image_thread.join()
# 等待数据传输线程结束
transmit_thread.join()
# 释放资源
client_socket.close()
cam.close()
cv2.destroyAllWindows()
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
TypeError: image_processing() takes 0 positional arguments but 1 was given
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
TypeError: transmit_data() takes 0 positional arguments but 1 was given
Traceback (most recent call last):
File "/home/b407/cv2.py", line 6, in <module>
import cv2
File "/home/b407/cv2.py", line 144, in <module>
cv2.destroyAllWindows()
AttributeError: module 'cv2' has no attribute 'destroyAllWindows