1.设计并制作两个独立的摄像节点,每个节点由一个摄像头和相应的电路组成。两个摄像节点均可以拍摄到激光笔的运动视频并显示。
2.设计并制作终端节点。在终端显示器上可以分别和同时显示两个摄像节点拍摄的实时视频。在视频中可以识别出激光笔,并在视频中用红色方框实时框住激光笔轮廓。
3.测量系统在终端节点设置一键启动。从激光笔摆动开始计时,测量系统通过对激光笔周期摆动视频信号的处理,自动测量长度 l,50cm≤l≤150cm,θ角度自定。测量完成时,终端声光提示并显示长度 l。要求测量误差绝对值小于2cm,测量时间小于 30 秒。
发挥部分
4.一键启动后,测量系统通过两个独立摄像节点的网络协同工作,当 θ=0°和 θ=90°时,能自动测量长度 l,50cm≤ l ≤150cm。要求测量误差绝对值小于 2cm,测量时间小于 30 秒。
5.一键启动后,可以测量 θ,0°≤ θ ≤90°。要求测量误差绝对值小于 5°。测量时间小于 30秒。
三个点各放一个树莓派,通过交换机实现UDP通信。图像识别用最简单的定位像素点。
0_test 用作测试udp协议传输字符是否跑通
1 用作第一题中实时显示两个摄像节点,即:直接打开
摄像头用opencv对每一帧图像image-show(cv.imshow)
2345 2345题目都是对终端结点操作的,对于A结点来说只需
要一直发图片就行,所以没有别的区别,比赛的时候把
这个程序一直打开别关就行
ps,c&s分别代表udp传输过程中的client(摄像节点)
和server(终端节点)
# -*- coding: utf-8 -*-
import socket #导入socket模块
import time #导入time模块
#server 接收端
# 设置服务器默认端口号
PORT = 8000
# 创建一个套接字socket对象,用于进行通讯
# socket.AF_INET 指明使用INET地址集,进行网间通讯
# socket.SOCK_DGRAM 指明使用数据协议,即使用传输层的udp协议
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
address = ("169.254.216.25", PORT)
#A:169.254.216.25
#B:169.254.7.199
server_socket.bind(address) # 为服务器绑定一个固定的地址,ip和端口
server_socket.settimeout(10) #设置一个时间提示,如果10秒钟没接到数据进行提示
while True:
#正常情况下接收数据并且显示,如果10秒钟没有接收数据进行提示(打印 "time out")
#当然可以不要这个提示,那样的话把"try:" 以及 "except"后的语句删掉就可以了
try:
now = time.time() #获取当前时间
# 接收客户端传来的数据 recvfrom接收客户端的数据,默认是阻塞的,直到有客户端传来数据
# recvfrom 参数的意义,表示最大能接收多少数据,单位是字节
# recvfrom返回值说明
# receive_data表示接受到的传来的数据,是bytes类型
# client 表示传来数据的客户端的身份信息,客户端的ip和端口,元组
receive_data, client = server_socket.recvfrom(1024)
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(now))) #以指定格式显示时间
print("来自客户端%s,发送的%s\n" % (client, receive_data)) #打印接收的内容
except socket.timeout: #如果10秒钟没有接收数据进行提示(打印 "time out")
print ("time out")
# -*- coding: utf-8 -*-
import cv2
cap=cv2.VideoCapture(0) #调用摄像头‘0'一般是打开电脑自带摄像头,‘1'是打开外部摄像头(只有一个摄像头的情况)
width=640
height=480
cap.set(cv2.CAP_PROP_FRAME_WIDTH,width)#设置图像宽度
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,height)#设置图像高度
#显示图像
print("start camera")
while True:
ret,frame=cap.read()#读取图像(frame就是读取的视频帧,对frame处理就是对整个视频的处理)
cv2.imshow("frame",frame)
input=cv2.waitKey(20)
if input==ord('q'):#如过输入的是q就break,结束图像显示,鼠标点击视频画面输入字符
break
cap.release()#释放摄像头
cv2.destroyAllWindows()#销毁窗口
import cv2
import socket
import math
import pickle
import sys
max_length = 65000
host = "169.254.60.31"
#host = sys.argv[1]
port = 8000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
while ret:
# compress frame
retval, buffer = cv2.imencode(".jpg", frame)
if retval:
# convert to byte array
buffer = buffer.tobytes()
# get size of the frame
buffer_size = len(buffer)
num_of_packs = 1
if buffer_size > max_length:
num_of_packs = math.ceil(buffer_size/max_length)
frame_info = {"packs":num_of_packs}
# send the number of packs to be expected
print("Number of packs:", num_of_packs)
cv2.imshow('camera',frame)
sock.sendto(pickle.dumps(frame_info), (host, port))
left = 0
right = max_length
for i in range(num_of_packs):
print("left:", left)
print("right:", right)
# truncate data to send
data = buffer[left:right]
left = right
right += max_length
# send the frames accordingly
sock.sendto(data, (host, port))
ret, frame = cap.read()
print("done")
0_test 用作测试udp协议传输字符是否跑通
1 用作第一题中实时显示两个摄像节点,即:直接打开
摄像头用opencv对每一帧图像image-show(cv.imshow)
2345 2345题目都是对终端结点操作的,对于A结点来说只需
要一直发图片就行,所以没有别的区别,比赛的时候把
这个程序一直打开别关就行
ps,c&s分别代表udp传输过程中的client(摄像节点)
和server(终端节点)
# -*- coding: utf-8 -*-
import socket
import time
#client 发送端
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
PORT = 8000
while True:
start = time.time() #获取当前时间
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(start))) #以指定格式显示当前时间
msg=input("本客户端169.254.7.199,请输入要发送的内容:")
#A:169.254.216.25
#B:169.254.7.199
server_address = ("169.254.216.25", PORT) # 接收方 服务器的ip地址和端口号
client_socket.sendto(msg.encode(), server_address) #将msg内容发送给指定接收方
now = time.time() #获取当前时间
run_time = now-start #计算时间差,即运行时间
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(now)))
print("run_time: %d seconds\n" %run_time)
# -*- coding: utf-8 -*-
import cv2
cap=cv2.VideoCapture(0) #调用摄像头‘0'一般是打开电脑自带摄像头,‘1'是打开外部摄像头(只有一个摄像头的情况)
width=640
height=480
cap.set(cv2.CAP_PROP_FRAME_WIDTH,width)#设置图像宽度
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,height)#设置图像高度
#显示图像
print("start camera")
while True:
ret,frame=cap.read()#读取图像(frame就是读取的视频帧,对frame处理就是对整个视频的处理)
cv2.imshow("frame",frame)
input=cv2.waitKey(20)
if input==ord('q'):#如过输入的是q就break,结束图像显示,鼠标点击视频画面输入字符
break
cap.release()#释放摄像头
cv2.destroyAllWindows()#销毁窗口
import cv2
import socket
import math
import pickle
import sys
max_length = 65000
host = "169.254.60.31"
#host = sys.argv[1]
port = 5000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
while ret:
# compress frame
retval, buffer = cv2.imencode(".jpg", frame)
if retval:
# convert to byte array
buffer = buffer.tobytes()
# get size of the frame
buffer_size = len(buffer)
num_of_packs = 1
if buffer_size > max_length:
num_of_packs = math.ceil(buffer_size/max_length)
frame_info = {"packs":num_of_packs}
# send the number of packs to be expected
print("Number of packs:", num_of_packs)
fps=cap.get(cv2.CAP_PROP_FPS)
print("FPS",fps)
sock.sendto(pickle.dumps(frame_info), (host, port))
left = 0
right = max_length
for i in range(num_of_packs):
print("left:", left)
print("right:", right)
# truncate data to send
data = buffer[left:right]
left = right
right += max_length
# send the frames accordingly
sock.sendto(data, (host, port))
ret, frame = cap.read()
print("done")
1.第1题不涉及终端节点,这里没有操作(本project中统一用C点表示)
2.关于第2题:
2_usC-A与 2_usc-B 题目要求同步传输,实时终端,这里使用udp协议,
C终端节点作为server接收,因为自己的host IP地址不变,故更改port端口
号,其中A点从port=5000传到C,B点从port=8000传到C。
经实验,树莓派的PIcamera的采集视频速率最高为90fps,而UDP传输协议
+TPLink千兆交换机+超七类千兆宽带网线 最高为30fps,故总的传输速率为30fps。
3.关于第3题,要求一键启动、声光提示、30s内测量长度:3_usC-B
其一,一键启动把这个python文件改为可执行文件,直接双击文件即可一键启动
其二,声光提示加上了1个led和1个无源蜂鸣器,测量完成时led亮、蜂鸣器响
其三,第一份代码中测量的是200张图片。差不多2.5s,满足精度要求
4.关于第4题:4_usC-B
题目要求“网络协同工作”,和3题区别也在于没有了声光报警。
这里只是B点采集,C点对B点采集的图像流处理。
5.关于第5题:
通过两个摄像头检测到偏移的角度,测出tan(theta)
import cv2
import socket
import pickle
import numpy as np
import RPi.GPIO as GPIO
import time
ball_color = 'blue'
color_dist = {'red': {'Lower': np.array([0, 100, 100]), 'Upper': np.array([10, 255, 255])},
'blue': {'Lower': np.array([120, 0, 0]), 'Upper': np.array([179, 255, 255])},
'green': {'Lower': np.array([35, 43, 35]), 'Upper': np.array([90, 255, 255])},
}
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(26,GPIO.OUT)
GPIO.output(26,GPIO.LOW)
GPIO.setup(2,GPIO.OUT,initial=GPIO.LOW)
host = "169.254.60.31"
port = 5000
max_length = 65540
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((host, port))
frame_info = None
buffer = None
frame = None
cc=[0];
print("-> waiting for connection")
while True:
data, address = sock.recvfrom(max_length)
if len(data) < 100:
frame_info = pickle.loads(data)
if frame_info:
nums_of_packs = frame_info["packs"]
for i in range(nums_of_packs):
data, address = sock.recvfrom(max_length)
if i == 0:
buffer = data
else:
buffer += data
frame = np.frombuffer(buffer, dtype=np.uint8)
frame = frame.reshape(frame.shape[0], 1)
frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
frame = cv2.flip(frame, 1)
if frame is None:
continue
gs_frame = cv2.GaussianBlur(frame, (5, 5), 0)
hsv = cv2.cvtColor(gs_frame, cv2.COLOR_BGR2HSV)
erode_hsv = cv2.erode(hsv, None, iterations=2)
inRange_hsv = cv2.inRange(erode_hsv, color_dist[ball_color]['Lower'], color_dist[ball_color]['Upper'])
#cv2.imwrite('1234.jpg',erode_hsv)
#cv2.imwrite('123.jpg',inRange_hsv)
#cv2.imwrite('12.jpg',frame)
cnts = cv2.findContours(inRange_hsv.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if cnts != []:
c = max(cnts, key=cv2.contourArea)
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
cv2.drawContours(frame, [np.int0(box)], -1, (0, 255, 255), 2)
'''
boxnum = len(box)
for i in range(boxnum-1):
aa = box[i]
bb = aa[1]
cc = cc + [bb]
if len(cc)>200:
ref=max(cc)
cc=[0]
if ref>475:
length=148
elif ref>470:
length=145
elif ref>464:
length=144
elif ref>456:
length=143
elif ref>448:
length=142
elif ref>440:
length=141
elif ref>434:
length=140
elif ref>424:
length=139
elif ref>420:
length=138
elif ref>410:
length=137
elif ref>402:
length=136
elif ref>395:
length=135
elif ref>388:
length=134
elif ref>381:
length=133
elif ref>379:
length=132
elif ref>375:
length=131
elif ref>370:
length=130
elif ref>363:
length=129
elif ref>355:
length=128
elif ref>349:
length=127
elif ref>344:
length=126
elif ref>339:
length=125
elif ref>335:
length=124
elif ref>327:
length=123
elif ref>320:
length=122
elif ref>315:
length=121
elif ref>309:
length=120
elif ref>308:
length=119
elif ref>298:
length=118
elif ref>293:
length=117
elif ref>288:
length=116
elif ref>281:
length=115
elif ref>275:
length=114
elif ref>270:
length=113
elif ref>266:
length=112
elif ref>260:
length=111
elif ref>255:
length=110
elif ref>250:
length=109
elif ref>245:
length=108
elif ref>239:
length=107
elif ref>234:
length=106
elif ref>227:
length=105
elif ref>222:
length=104
elif ref>215:
length=103
elif ref>211:
length=102
elif ref>208:
length=101
elif ref>202:
length=100
elif ref>196:
length=99
elif ref>190:
length=98
elif ref>183:
length=97
elif ref>181:
length=95
elif ref>173:
length=94
elif ref>165:
length=93
elif ref>158:
length=92
elif ref>152:
length=91
elif ref>146:
length=90
elif ref>140:
length=89
elif ref>136:
length=88
elif ref>132:
length=87
elif ref>129:
length=86
elif ref>124:
length=85
elif ref>118:
length=84
elif ref>114:
length=83
elif ref>111:
length=82
elif ref>109:
length=81
elif ref>107:
length=80
elif ref>105:
length=79
elif ref>100:
length=78
elif ref>94:
length=77
elif ref>91:
length=76
elif ref>88:
length=75
elif ref>84:
length=74
elif ref>77:
length=73
elif ref>73:
length=72
elif ref>68:
length=71
elif ref>60:
length=70
elif ref>56:
length=69
elif ref>52:
length=68
elif ref>48:
length=67
elif ref>44:
length=66
elif ref>40:
length=65
elif ref>36:
length=64
elif ref>32:
length=63
elif ref > 30:
length=62
elif ref>28:
length=61
elif ref>27:
length=60
elif ref>24:
length=59
elif ref>17:
length=58
elif ref>13:
length=57
elif ref>10:
length=56
elif ref>7:
length=55
else:
length=52
img = np.zeros((320, 320, 3), np.uint8) # 生成一个空灰度图像
print(img.shape) # 输出:(320, 320, 3)
text = 'length:' + str(length)
org = (40, 80)
fontFace = cv2.FONT_HERSHEY_COMPLEX
fontScale = 1
fontcolor = (0, 255, 0) # BGR
thickness = 1
lineType = 4
bottomLeftOrigin = 1
# cv.putText(img, text, org, fontFace, fontScale, fontcolor, thickness, lineType, bottomLeftOrigin)
cv2.putText(img, text, org, fontFace, fontScale, fontcolor, thickness, lineType)
cv2.namedWindow("image")
cv2.imshow('image', img)
GPIO.output(26,GPIO.HIGH)
GPIO.output(2, GPIO.HIGH)
time.sleep(1)
GPIO.output(2, GPIO.LOW)
GPIO.output(26, GPIO.LOW)
cv2.waitKey(4000) # 显示 10000 ms 即 10s 后消失
cv2.destroyWindow('image')
'''
cv2.imshow("Stream", frame)
if cv2.waitKey(1) == 27:
break
print("goodbye")
import cv2
import socket
import pickle
import numpy as np
import RPi.GPIO as GPIO
import time
ball_color = 'blue'
color_dist = {'red': {'Lower': np.array([0, 100, 100]), 'Upper': np.array([10, 255, 255])},
'blue': {'Lower': np.array([120, 0, 0]), 'Upper': np.array([179, 255, 255])},
'green': {'Lower': np.array([35, 43, 35]), 'Upper': np.array([90, 255, 255])},
}
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(26,GPIO.OUT)
GPIO.output(26,GPIO.LOW)
GPIO.setup(2,GPIO.OUT,initial=GPIO.LOW)
host = "169.254.60.31"
port = 8000
max_length = 65540
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((host, port))
frame_info = None
buffer = None
frame = None
cc=[0];
print("-> waiting for connection")
while True:
data, address = sock.recvfrom(max_length)
if len(data) < 100:
frame_info = pickle.loads(data)
if frame_info:
nums_of_packs = frame_info["packs"]
for i in range(nums_of_packs):
data, address = sock.recvfrom(max_length)
if i == 0:
buffer = data
else:
buffer += data
frame = np.frombuffer(buffer, dtype=np.uint8)
frame = frame.reshape(frame.shape[0], 1)
frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
frame = cv2.flip(frame, 1)
if frame is None:
continue
gs_frame = cv2.GaussianBlur(frame, (5, 5), 0)
hsv = cv2.cvtColor(gs_frame, cv2.COLOR_BGR2HSV)
erode_hsv = cv2.erode(hsv, None, iterations=2)
inRange_hsv = cv2.inRange(erode_hsv, color_dist[ball_color]['Lower'], color_dist[ball_color]['Upper'])
#cv2.imwrite('1234.jpg',erode_hsv)
#cv2.imwrite('123.jpg',inRange_hsv)
#cv2.imwrite('12.jpg',frame)
cnts = cv2.findContours(inRange_hsv.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if cnts != []:
c = max(cnts, key=cv2.contourArea)
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
cv2.drawContours(frame, [np.int0(box)], -1, (0, 255, 255), 2)
'''
boxnum = len(box)
for i in range(boxnum-1):
aa = box[i]
bb = aa[1]
cc = cc + [bb]
if len(cc)>200:
ref=max(cc)
cc=[0]
if ref>475:
length=148
elif ref>470:
length=145
elif ref>464:
length=144
elif ref>456:
length=143
elif ref>448:
length=142
elif ref>440:
length=141
elif ref>434:
length=140
elif ref>424:
length=139
elif ref>420:
length=138
elif ref>410:
length=137
elif ref>402:
length=136
elif ref>395:
length=135
elif ref>388:
length=134
elif ref>381:
length=133
elif ref>379:
length=132
elif ref>375:
length=131
elif ref>370:
length=130
elif ref>363:
length=129
elif ref>355:
length=128
elif ref>349:
length=127
elif ref>344:
length=126
elif ref>339:
length=125
elif ref>335:
length=124
elif ref>327:
length=123
elif ref>320:
length=122
elif ref>315:
length=121
elif ref>309:
length=120
elif ref>308:
length=119
elif ref>298:
length=118
elif ref>293:
length=117
elif ref>288:
length=116
elif ref>281:
length=115
elif ref>275:
length=114
elif ref>270:
length=113
elif ref>266:
length=112
elif ref>260:
length=111
elif ref>255:
length=110
elif ref>250:
length=109
elif ref>245:
length=108
elif ref>239:
length=107
elif ref>234:
length=106
elif ref>227:
length=105
elif ref>222:
length=104
elif ref>215:
length=103
elif ref>211:
length=102
elif ref>208:
length=101
elif ref>202:
length=100
elif ref>196:
length=99
elif ref>190:
length=98
elif ref>183:
length=97
elif ref>181:
length=95
elif ref>173:
length=94
elif ref>165:
length=93
elif ref>158:
length=92
elif ref>152:
length=91
elif ref>146:
length=90
elif ref>140:
length=89
elif ref>136:
length=88
elif ref>132:
length=87
elif ref>129:
length=86
elif ref>124:
length=85
elif ref>118:
length=84
elif ref>114:
length=83
elif ref>111:
length=82
elif ref>109:
length=81
elif ref>107:
length=80
elif ref>105:
length=79
elif ref>100:
length=78
elif ref>94:
length=77
elif ref>91:
length=76
elif ref>88:
length=75
elif ref>84:
length=74
elif ref>77:
length=73
elif ref>73:
length=72
elif ref>68:
length=71
elif ref>60:
length=70
elif ref>56:
length=69
elif ref>52:
length=68
elif ref>48:
length=67
elif ref>44:
length=66
elif ref>40:
length=65
elif ref>36:
length=64
elif ref>32:
length=63
elif ref > 30:
length=62
elif ref>28:
length=61
elif ref>27:
length=60
elif ref>24:
length=59
elif ref>17:
length=58
elif ref>13:
length=57
elif ref>10:
length=56
elif ref>7:
length=55
else:
length=52
img = np.zeros((320, 320, 3), np.uint8) # 生成一个空灰度图像
print(img.shape) # 输出:(320, 320, 3)
text = 'length:' + str(length)
org = (40, 80)
fontFace = cv2.FONT_HERSHEY_COMPLEX
fontScale = 1
fontcolor = (0, 255, 0) # BGR
thickness = 1
lineType = 4
bottomLeftOrigin = 1
# cv.putText(img, text, org, fontFace, fontScale, fontcolor, thickness, lineType, bottomLeftOrigin)
cv2.putText(img, text, org, fontFace, fontScale, fontcolor, thickness, lineType)
cv2.namedWindow("image")
cv2.imshow('image', img)
GPIO.output(26,GPIO.HIGH)
GPIO.output(2, GPIO.HIGH)
time.sleep(1)
GPIO.output(2, GPIO.LOW)
GPIO.output(26, GPIO.LOW)
cv2.waitKey(4000) # 显示 10000 ms 即 10s 后消失
cv2.destroyWindow('image')
'''
cv2.imshow("Stream", frame)
if cv2.waitKey(1) == 27:
break
print("goodbye")
import cv2
import socket
import pickle
import numpy as np
import RPi.GPIO as GPIO
import time
ball_color = 'blue'
color_dist = {'red': {'Lower': np.array([0, 100, 100]), 'Upper': np.array([10, 255, 255])},
'blue': {'Lower': np.array([120, 0, 0]), 'Upper': np.array([179, 255, 255])},
'green': {'Lower': np.array([35, 43, 35]), 'Upper': np.array([90, 255, 255])},
}
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(26,GPIO.OUT)
GPIO.output(26,GPIO.LOW)
GPIO.setup(2,GPIO.OUT,initial=GPIO.LOW)
host = "169.254.60.31"
port = 8000
max_length = 65540
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((host, port))
frame_info = None
buffer = None
frame = None
cc=[0];
print("-> waiting for connection")
while True:
data, address = sock.recvfrom(max_length)
if len(data) < 100:
frame_info = pickle.loads(data)
if frame_info:
nums_of_packs = frame_info["packs"]
for i in range(nums_of_packs):
data, address = sock.recvfrom(max_length)
if i == 0:
buffer = data
else:
buffer += data
frame = np.frombuffer(buffer, dtype=np.uint8)
frame = frame.reshape(frame.shape[0], 1)
frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
frame = cv2.flip(frame, 1)
if frame is None:
continue
gs_frame = cv2.GaussianBlur(frame, (5, 5), 0)
hsv = cv2.cvtColor(gs_frame, cv2.COLOR_BGR2HSV)
erode_hsv = cv2.erode(hsv, None, iterations=2)
inRange_hsv = cv2.inRange(erode_hsv, color_dist[ball_color]['Lower'], color_dist[ball_color]['Upper'])
#cv2.imwrite('1234.jpg',erode_hsv)
#cv2.imwrite('123.jpg',inRange_hsv)
#cv2.imwrite('12.jpg',frame)
cnts = cv2.findContours(inRange_hsv.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if cnts != []:
c = max(cnts, key=cv2.contourArea)
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
cv2.drawContours(frame, [np.int0(box)], -1, (0, 255, 255), 2)
'''
boxnum = len(box)
for i in range(boxnum-1):
aa = box[i]
bb = aa[1]
cc = cc + [bb]
if len(cc)>200:
ref=max(cc)
cc=[0]
if ref>475:
length=148
elif ref>470:
length=145
elif ref>464:
length=144
elif ref>456:
length=143
elif ref>448:
length=142
elif ref>440:
length=141
elif ref>434:
length=140
elif ref>424:
length=139
elif ref>420:
length=138
elif ref>410:
length=137
elif ref>402:
length=136
elif ref>395:
length=135
elif ref>388:
length=134
elif ref>381:
length=133
elif ref>379:
length=132
elif ref>375:
length=131
elif ref>370:
length=130
elif ref>363:
length=129
elif ref>355:
length=128
elif ref>349:
length=127
elif ref>344:
length=126
elif ref>339:
length=125
elif ref>335:
length=124
elif ref>327:
length=123
elif ref>320:
length=122
elif ref>315:
length=121
elif ref>309:
length=120
elif ref>308:
length=119
elif ref>298:
length=118
elif ref>293:
length=117
elif ref>288:
length=116
elif ref>281:
length=115
elif ref>275:
length=114
elif ref>270:
length=113
elif ref>266:
length=112
elif ref>260:
length=111
elif ref>255:
length=110
elif ref>250:
length=109
elif ref>245:
length=108
elif ref>239:
length=107
elif ref>234:
length=106
elif ref>227:
length=105
elif ref>222:
length=104
elif ref>215:
length=103
elif ref>211:
length=102
elif ref>208:
length=101
elif ref>202:
length=100
elif ref>196:
length=99
elif ref>190:
length=98
elif ref>183:
length=97
elif ref>181:
length=95
elif ref>173:
length=94
elif ref>165:
length=93
elif ref>158:
length=92
elif ref>152:
length=91
elif ref>146:
length=90
elif ref>140:
length=89
elif ref>136:
length=88
elif ref>132:
length=87
elif ref>129:
length=86
elif ref>124:
length=85
elif ref>118:
length=84
elif ref>114:
length=83
elif ref>111:
length=82
elif ref>109:
length=81
elif ref>107:
length=80
elif ref>105:
length=79
elif ref>100:
length=78
elif ref>94:
length=77
elif ref>91:
length=76
elif ref>88:
length=75
elif ref>84:
length=74
elif ref>77:
length=73
elif ref>73:
length=72
elif ref>68:
length=71
elif ref>60:
length=70
elif ref>56:
length=69
elif ref>52:
length=68
elif ref>48:
length=67
elif ref>44:
length=66
elif ref>40:
length=65
elif ref>36:
length=64
elif ref>32:
length=63
elif ref > 30:
length=62
elif ref>28:
length=61
elif ref>27:
length=60
elif ref>24:
length=59
elif ref>17:
length=58
elif ref>13:
length=57
elif ref>10:
length=56
elif ref>7:
length=55
else:
length=52
img = np.zeros((320, 320, 3), np.uint8) # 生成一个空灰度图像
print(img.shape) # 输出:(320, 320, 3)
text = 'length:' + str(length)
org = (40, 80)
fontFace = cv2.FONT_HERSHEY_COMPLEX
fontScale = 1
fontcolor = (0, 255, 0) # BGR
thickness = 1
lineType = 4
bottomLeftOrigin = 1
# cv.putText(img, text, org, fontFace, fontScale, fontcolor, thickness, lineType, bottomLeftOrigin)
cv2.putText(img, text, org, fontFace, fontScale, fontcolor, thickness, lineType)
cv2.namedWindow("image")
cv2.imshow('image', img)
GPIO.output(26,GPIO.HIGH)
GPIO.output(2, GPIO.HIGH)
time.sleep(1)
GPIO.output(2, GPIO.LOW)
GPIO.output(26, GPIO.LOW)
cv2.waitKey(4000) # 显示 10000 ms 即 10s 后消失
cv2.destroyWindow('image')
'''
cv2.imshow("Stream", frame)
if cv2.waitKey(1) == 27:
break
print("goodbye")
import cv2
import socket
import pickle
import numpy as np
import RPi.GPIO as GPIO
import time
ball_color = 'blue'
color_dist = {'red': {'Lower': np.array([0, 100, 100]), 'Upper': np.array([10, 255, 255])},
'blue': {'Lower': np.array([120, 0, 0]), 'Upper': np.array([179, 255, 255])},
'green': {'Lower': np.array([35, 43, 35]), 'Upper': np.array([90, 255, 255])},
}
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(26,GPIO.OUT)
GPIO.output(26,GPIO.LOW)
GPIO.setup(2,GPIO.OUT,initial=GPIO.LOW)
host = "169.254.60.31"
port = 8000
max_length = 65540
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((host, port))
frame_info = None
buffer = None
frame = None
cc=[0];
print("-> waiting for connection")
while True:
data, address = sock.recvfrom(max_length)
if len(data) < 100:
frame_info = pickle.loads(data)
if frame_info:
nums_of_packs = frame_info["packs"]
for i in range(nums_of_packs):
data, address = sock.recvfrom(max_length)
if i == 0:
buffer = data
else:
buffer += data
frame = np.frombuffer(buffer, dtype=np.uint8)
frame = frame.reshape(frame.shape[0], 1)
frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
frame = cv2.flip(frame, 1)
if frame is None:
continue
gs_frame = cv2.GaussianBlur(frame, (5, 5), 0)
hsv = cv2.cvtColor(gs_frame, cv2.COLOR_BGR2HSV)
erode_hsv = cv2.erode(hsv, None, iterations=2)
inRange_hsv = cv2.inRange(erode_hsv, color_dist[ball_color]['Lower'], color_dist[ball_color]['Upper'])
#cv2.imwrite('1234.jpg',erode_hsv)
#cv2.imwrite('123.jpg',inRange_hsv)
#cv2.imwrite('12.jpg',frame)
cnts = cv2.findContours(inRange_hsv.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if cnts != []:
c = max(cnts, key=cv2.contourArea)
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
cv2.drawContours(frame, [np.int0(box)], -1, (0, 255, 255), 2)
boxnum = len(box)
for i in range(boxnum-1):
aa = box[i]
bb = aa[1]
cc = cc + [bb]
if len(cc)>200:
ref=max(cc)
cc=[0]
if ref>475:
length=148
elif ref>470:
length=145
elif ref>464:
length=144
elif ref>456:
length=143
elif ref>448:
length=142
elif ref>440:
length=141
elif ref>434:
length=140
elif ref>424:
length=139
elif ref>420:
length=138
elif ref>410:
length=137
elif ref>402:
length=136
elif ref>395:
length=135
elif ref>388:
length=134
elif ref>381:
length=133
elif ref>379:
length=132
elif ref>375:
length=131
elif ref>370:
length=130
elif ref>363:
length=129
elif ref>355:
length=128
elif ref>349:
length=127
elif ref>344:
length=126
elif ref>339:
length=125
elif ref>335:
length=124
elif ref>327:
length=123
elif ref>320:
length=122
elif ref>315:
length=121
elif ref>309:
length=120
elif ref>308:
length=119
elif ref>298:
length=118
elif ref>293:
length=117
elif ref>288:
length=116
elif ref>281:
length=115
elif ref>275:
length=114
elif ref>270:
length=113
elif ref>266:
length=112
elif ref>260:
length=111
elif ref>255:
length=110
elif ref>250:
length=109
elif ref>245:
length=108
elif ref>239:
length=107
elif ref>234:
length=106
elif ref>227:
length=105
elif ref>222:
length=104
elif ref>215:
length=103
elif ref>211:
length=102
elif ref>208:
length=101
elif ref>202:
length=100
elif ref>196:
length=99
elif ref>190:
length=98
elif ref>183:
length=97
elif ref>181:
length=95
elif ref>173:
length=94
elif ref>165:
length=93
elif ref>158:
length=92
elif ref>152:
length=91
elif ref>146:
length=90
elif ref>140:
length=89
elif ref>136:
length=88
elif ref>132:
length=87
elif ref>129:
length=86
elif ref>124:
length=85
elif ref>118:
length=84
elif ref>114:
length=83
elif ref>111:
length=82
elif ref>109:
length=81
elif ref>107:
length=80
elif ref>105:
length=79
elif ref>100:
length=78
elif ref>94:
length=77
elif ref>91:
length=76
elif ref>88:
length=75
elif ref>84:
length=74
elif ref>77:
length=73
elif ref>73:
length=72
elif ref>68:
length=71
elif ref>60:
length=70
elif ref>56:
length=69
elif ref>52:
length=68
elif ref>48:
length=67
elif ref>44:
length=66
elif ref>40:
length=65
elif ref>36:
length=64
elif ref>32:
length=63
elif ref > 30:
length=62
elif ref>28:
length=61
elif ref>27:
length=60
elif ref>24:
length=59
elif ref>17:
length=58
elif ref>13:
length=57
elif ref>10:
length=56
elif ref>7:
length=55
else:
length=52
img = np.zeros((320, 320, 3), np.uint8) # 生成一个空灰度图像
print(img.shape) # 输出:(320, 320, 3)
text = 'length:' + str(length)
org = (40, 80)
fontFace = cv2.FONT_HERSHEY_COMPLEX
fontScale = 1
fontcolor = (0, 255, 0) # BGR
thickness = 1
lineType = 4
bottomLeftOrigin = 1
# cv.putText(img, text, org, fontFace, fontScale, fontcolor, thickness, lineType, bottomLeftOrigin)
cv2.putText(img, text, org, fontFace, fontScale, fontcolor, thickness, lineType)
cv2.namedWindow("image")
cv2.imshow('image', img)
GPIO.output(26,GPIO.HIGH)
GPIO.output(2, GPIO.HIGH)
time.sleep(1)
GPIO.output(2, GPIO.LOW)
GPIO.output(26, GPIO.LOW)
cv2.waitKey(4000) # 显示 10000 ms 即 10s 后消失
cv2.destroyWindow('image')
cv2.imshow("Stream", frame)
if cv2.waitKey(1) == 27:
break
print("goodbye")
import cv2
import socket
import pickle
import numpy as np
import RPi.GPIO as GPIO
import time
ball_color = 'blue'
color_dist = {'red': {'Lower': np.array([0, 100, 100]), 'Upper': np.array([10, 255, 255])},
'blue': {'Lower': np.array([120, 0, 0]), 'Upper': np.array([179, 255, 255])},
'green': {'Lower': np.array([35, 43, 35]), 'Upper': np.array([90, 255, 255])},
}
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(26,GPIO.OUT)
GPIO.output(26,GPIO.LOW)
GPIO.setup(2,GPIO.OUT,initial=GPIO.LOW)
host = "169.254.60.31"
port = 5000
max_length = 65540
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((host, port))
frame_info = None
buffer = None
frame = None
cc=[0];
print("-> waiting for connection")
while True:
data, address = sock.recvfrom(max_length)
if len(data) < 100:
frame_info = pickle.loads(data)
if frame_info:
nums_of_packs = frame_info["packs"]
for i in range(nums_of_packs):
data, address = sock.recvfrom(max_length)
if i == 0:
buffer = data
else:
buffer += data
frame = np.frombuffer(buffer, dtype=np.uint8)
frame = frame.reshape(frame.shape[0], 1)
frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
frame = cv2.flip(frame, 1)
if frame is None:
continue
gs_frame = cv2.GaussianBlur(frame, (5, 5), 0)
hsv = cv2.cvtColor(gs_frame, cv2.COLOR_BGR2HSV)
erode_hsv = cv2.erode(hsv, None, iterations=2)
inRange_hsv = cv2.inRange(erode_hsv, color_dist[ball_color]['Lower'], color_dist[ball_color]['Upper'])
#cv2.imwrite('1234.jpg',erode_hsv)
#cv2.imwrite('123.jpg',inRange_hsv)
#cv2.imwrite('12.jpg',frame)
cnts = cv2.findContours(inRange_hsv.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if cnts != []:
c = max(cnts, key=cv2.contourArea)
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
cv2.drawContours(frame, [np.int0(box)], -1, (0, 255, 255), 2)
boxnum = len(box)
for i in range(boxnum-1):
aa = box[i]
bb = aa[1]
cc = cc + [bb]
if len(cc)>260:
cc.remove(0)
ref=min(cc)
print(ref)
length=int(235.36 - ref)
f = "/home/pi/Desktop/US4A.txt"
with open(f, "w") as file:
file.write(str(length))
file.close()
cc=[0]
cv2.imshow("Stream", frame)
if cv2.waitKey(1) == 27:
break
print("goodbye")
import cv2
import socket
import pickle
import numpy as np
import RPi.GPIO as GPIO
import time
import math
ball_color = 'blue'
color_dist = {'red': {'Lower': np.array([0, 100, 100]), 'Upper': np.array([10, 255, 255])},
'blue': {'Lower': np.array([120, 0, 0]), 'Upper': np.array([179, 255, 255])},
'green': {'Lower': np.array([35, 43, 35]), 'Upper': np.array([90, 255, 255])},
}
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(26,GPIO.OUT)
GPIO.output(26,GPIO.LOW)
GPIO.setup(2,GPIO.OUT,initial=GPIO.LOW)
host = "169.254.60.31"
port = 8000
max_length = 65540
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((host, port))
frame_info = None
buffer = None
frame = None
cc=[0];
print("-> waiting for connection")
while True:
data, address = sock.recvfrom(max_length)
if len(data) < 100:
frame_info = pickle.loads(data)
if frame_info:
nums_of_packs = frame_info["packs"]
for i in range(nums_of_packs):
data, address = sock.recvfrom(max_length)
if i == 0:
buffer = data
else:
buffer += data
frame = np.frombuffer(buffer, dtype=np.uint8)
frame = frame.reshape(frame.shape[0], 1)
frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
frame = cv2.flip(frame, 1)
if frame is None:
continue
gs_frame = cv2.GaussianBlur(frame, (5, 5), 0)
hsv = cv2.cvtColor(gs_frame, cv2.COLOR_BGR2HSV)
erode_hsv = cv2.erode(hsv, None, iterations=2)
inRange_hsv = cv2.inRange(erode_hsv, color_dist[ball_color]['Lower'], color_dist[ball_color]['Upper'])
#cv2.imwrite('1234.jpg',erode_hsv)
#cv2.imwrite('123.jpg',inRange_hsv)
#cv2.imwrite('12.jpg',frame)
cnts = cv2.findContours(inRange_hsv.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if cnts != []:
c = max(cnts, key=cv2.contourArea)
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
cv2.drawContours(frame, [np.int0(box)], -1, (0, 255, 255), 2)
boxnum = len(box)
for i in range(boxnum-1):
aa = box[i]
bb = aa[0]
cc = cc + [bb]
if len(cc)>400:
cc.remove(0)
ref=max(cc)
print(ref)
lengthB=ref - 378.44
f = open('/home/pi/Desktop/US4A.txt','r')
lengthA=int(f.read())
print("lengthA",lengthA)
print("lengthB",lengthB)
length=(lengthA+lengthB)/2
cc=[0]
img = np.zeros((320, 320, 3), np.uint8) # 生成一个空灰度图像
#print(img.shape) # 输出:(320, 320, 3)
text = 'angle:' + str(length)
org = (40, 80)
fontFace = cv2.FONT_HERSHEY_COMPLEX
fontScale = 1
fontcolor = (0, 255, 0) # BGR
thickness = 1
lineType = 4
bottomLeftOrigin = 1
# cv.putText(img, text, org, fontFace, fontScale, fontcolor, thickness, lineType, bottomLeftOrigin)
cv2.putText(img, text, org, fontFace, fontScale, fontcolor, thickness, lineType)
cv2.namedWindow("image")
cv2.imshow('image', img)
GPIO.output(26,GPIO.HIGH)
GPIO.output(2, GPIO.HIGH)
time.sleep(1)
GPIO.output(2, GPIO.LOW)
GPIO.output(26, GPIO.LOW)
cv2.waitKey(4000) # 显示 10000 ms 即 10s 后消失
cv2.destroyWindow('image')
cv2.imshow("Stream", frame)
if cv2.waitKey(1) == 27:
break
print("goodbye")
import cv2
import socket
import pickle
import numpy as np
import RPi.GPIO as GPIO
import time
ball_color = 'blue'
color_dist = {'red': {'Lower': np.array([0, 100, 100]), 'Upper': np.array([10, 255, 255])},
'blue': {'Lower': np.array([120, 0, 0]), 'Upper': np.array([179, 255, 255])},
'green': {'Lower': np.array([35, 43, 35]), 'Upper': np.array([90, 255, 255])},
}
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(26,GPIO.OUT)
GPIO.output(26,GPIO.LOW)
GPIO.setup(2,GPIO.OUT,initial=GPIO.LOW)
host = "169.254.60.31"
port = 5000
max_length = 65540
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((host, port))
frame_info = None
buffer = None
frame = None
cc=[0];
print("-> waiting for connection")
while True:
data, address = sock.recvfrom(max_length)
if len(data) < 100:
frame_info = pickle.loads(data)
if frame_info:
nums_of_packs = frame_info["packs"]
for i in range(nums_of_packs):
data, address = sock.recvfrom(max_length)
if i == 0:
buffer = data
else:
buffer += data
frame = np.frombuffer(buffer, dtype=np.uint8)
frame = frame.reshape(frame.shape[0], 1)
frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
frame = cv2.flip(frame, 1)
if frame is None:
continue
gs_frame = cv2.GaussianBlur(frame, (5, 5), 0)
hsv = cv2.cvtColor(gs_frame, cv2.COLOR_BGR2HSV)
erode_hsv = cv2.erode(hsv, None, iterations=2)
inRange_hsv = cv2.inRange(erode_hsv, color_dist[ball_color]['Lower'], color_dist[ball_color]['Upper'])
#cv2.imwrite('1234.jpg',erode_hsv)
#cv2.imwrite('123.jpg',inRange_hsv)
#cv2.imwrite('12.jpg',frame)
cnts = cv2.findContours(inRange_hsv.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if cnts != []:
c = max(cnts, key=cv2.contourArea)
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
cv2.drawContours(frame, [np.int0(box)], -1, (0, 255, 255), 2)
boxnum = len(box)
for i in range(boxnum-1):
aa = box[i]
bb = aa[0]
cc = cc + [bb]
if len(cc)>260:
cc.remove(0)
ref=min(cc)
print(ref)
length=int(235.36 - ref)
f = "/home/pi/Desktop/LA.txt"
with open(f, "w") as file:
file.write(str(length))
file.close()
cc=[0]
cv2.imshow("Stream", frame)
if cv2.waitKey(1) == 27:
break
print("goodbye")
import cv2
import socket
import pickle
import numpy as np
import RPi.GPIO as GPIO
import time
import math
ball_color = 'blue'
color_dist = {'red': {'Lower': np.array([0, 100, 100]), 'Upper': np.array([10, 255, 255])},
'blue': {'Lower': np.array([120, 0, 0]), 'Upper': np.array([179, 255, 255])},
'green': {'Lower': np.array([35, 43, 35]), 'Upper': np.array([90, 255, 255])},
}
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(26,GPIO.OUT)
GPIO.output(26,GPIO.LOW)
GPIO.setup(2,GPIO.OUT,initial=GPIO.LOW)
host = "169.254.60.31"
port = 8000
max_length = 65540
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((host, port))
frame_info = None
buffer = None
frame = None
cc=[0];
print("-> waiting for connection")
while True:
data, address = sock.recvfrom(max_length)
if len(data) < 100:
frame_info = pickle.loads(data)
if frame_info:
nums_of_packs = frame_info["packs"]
for i in range(nums_of_packs):
data, address = sock.recvfrom(max_length)
if i == 0:
buffer = data
else:
buffer += data
frame = np.frombuffer(buffer, dtype=np.uint8)
frame = frame.reshape(frame.shape[0], 1)
frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
frame = cv2.flip(frame, 1)
if frame is None:
continue
gs_frame = cv2.GaussianBlur(frame, (5, 5), 0)
hsv = cv2.cvtColor(gs_frame, cv2.COLOR_BGR2HSV)
erode_hsv = cv2.erode(hsv, None, iterations=2)
inRange_hsv = cv2.inRange(erode_hsv, color_dist[ball_color]['Lower'], color_dist[ball_color]['Upper'])
#cv2.imwrite('1234.jpg',erode_hsv)
#cv2.imwrite('123.jpg',inRange_hsv)
#cv2.imwrite('12.jpg',frame)
cnts = cv2.findContours(inRange_hsv.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if cnts != []:
c = max(cnts, key=cv2.contourArea)
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
cv2.drawContours(frame, [np.int0(box)], -1, (0, 255, 255), 2)
boxnum = len(box)
for i in range(boxnum-1):
aa = box[i]
bb = aa[0]
cc = cc + [bb]
if len(cc)>400:
cc.remove(0)
ref=max(cc)
print(ref)
lengthB=ref - 378.44
f = open('/home/pi/Desktop/LA.txt','r')
lengthA=int(f.read())
print("lengthA",lengthA)
print("lengthB",lengthB)
ahu=math.atan(lengthA/lengthB)
angle=ahu/3.1415926*180
cc=[0]
img = np.zeros((320, 320, 3), np.uint8) # 生成一个空灰度图像
#print(img.shape) # 输出:(320, 320, 3)
text = 'angle:' + str(angle)
org = (40, 80)
fontFace = cv2.FONT_HERSHEY_COMPLEX
fontScale = 1
fontcolor = (0, 255, 0) # BGR
thickness = 1
lineType = 4
bottomLeftOrigin = 1
# cv.putText(img, text, org, fontFace, fontScale, fontcolor, thickness, lineType, bottomLeftOrigin)
cv2.putText(img, text, org, fontFace, fontScale, fontcolor, thickness, lineType)
cv2.namedWindow("image")
cv2.imshow('image', img)
GPIO.output(26,GPIO.HIGH)
GPIO.output(2, GPIO.HIGH)
time.sleep(1)
GPIO.output(2, GPIO.LOW)
GPIO.output(26, GPIO.LOW)
cv2.waitKey(40000) # 显示 10000 ms 即 10s 后消失
cv2.destroyWindow('image')
cv2.imshow("Stream", frame)
if cv2.waitKey(1) == 27:
break
print("goodbye")