import socket
from collections import deque
import numpy as np
import cv2
import time
import select
def TestClient():
server_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# server_client.connect(("192.168.10.103", 19999))
server_client.connect(("localhost", 19999))
# 初始化摄像头
camera1 = cv2.VideoCapture(0)
# camera2 = cv2.VideoCapture(1)
current_camera = camera1
redLower = np.array([156, 43, 46])
redUpper = np.array([180, 255, 255])
mybuffer = 64
pts = deque(maxlen=mybuffer)
zero_x = 320
zero_y = 240
index = 0
data = 0
while True: # 遍历每一帧
# 使用 select 模块进行非阻塞 IO
ready_to_read, _, _ = select.select([server_client], [], [], 0)
if ready_to_read:
data = server_client.recv(1024).decode()
if data == '1':
zero_x = 320
zero_y = 240
if data == '2':
zero_x = 420
zero_y = 340
print(data)
# 根据接收到的数据切换摄像头
# if data == 'switch':
# if current_camera == camera1:
# current_camera = camera2
# else:
# current_camera = camera1
# 从当前摄像头读取每一帧
(ret, frame) = current_camera.read()
if not ret:
print('No Camera')
break
gs_frame = cv2.GaussianBlur(frame, (5, 5), 0)
hsv = cv2.cvtColor(gs_frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, redLower, redUpper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
cv2.line(frame, (zero_x - 10, zero_y), (zero_x + 10, zero_y), (192, 192, 192), 2)
cv2.line(frame, (zero_x, zero_y - 10), (zero_x, zero_y + 10), (192, 192, 192), 2)
center = None
if len(cnts) > 0:
c = max(cnts, key=cv2.contourArea)
((center_x, center_y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
cv2.circle(frame, (int(center_x), int(center_y)), int(radius), (0, 255, 255), 2)
cv2.circle(frame, center, 5, (0, 0, 255), -1)
cv2.line(frame, (cx - 10, cy), (cx + 10, cy), (255, 0, 0), 2)
cv2.line(frame, (cx, cy - 10), (cx, cy + 10), (255, 0, 0), 2)
delta_x = zero_x - int(center_x)
delta_y = zero_y - int(center_y)
if data == '0':
pass
if data == '1':
if (-30 <= delta_x <= 30) and (-30 <= delta_y <= 30):
server_client.send(
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".encode('utf-8'))
print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
else:
server_client.send(f"({delta_x},{delta_y})".encode('utf-8'))
print(f"({delta_x},{delta_y})")
if data == '2':
if (-30 <= delta_x <= 30) and (-30 <= delta_y <= 30):
server_client.send(
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".encode('utf-8'))
print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
else:
server_client.send(f"({delta_x},{delta_y})".encode('utf-8'))
print(f"({delta_x},{delta_y})")
time.sleep(0) # 等待秒数
cv2.imshow('Frame', frame)
k = cv2.waitKey(5) & 0xFF # 键盘检测,检测到esc键退出
if k == ord('s'): # 按下s键,进入下面的保存图片操作
cv2.imwrite("D:/gyw/" + str(index) + ".jpg", frame)
print("save" + str(index) + ".jpg successfuly!")
print("-------------------------")
index += 1
if k == 27:
break
current_camera.release() # 摄像头释放
cv2.destroyAllWindows() # 销毁所有窗口
server_client.close()
print("server end, exit!")
exit()
if __name__ == '__main__':
TestClient()
色块定位多场景切换
最新推荐文章于 2024-11-02 22:17:47 发布