更新 main2.py
This commit is contained in:
322
main2.py
322
main2.py
@@ -1,162 +1,162 @@
|
|||||||
import cv2
|
import cv2
|
||||||
from ultralytics import YOLO
|
from ultralytics import YOLO
|
||||||
def getxy(cvx,cvy):
|
def getxy(cvx,cvy):
|
||||||
returny="南"+str((cvy+446.6)*18/1000*0.54)+"海里"
|
returny="南"+str((cvy+446.6)*18/1000*0.54)+"海里"
|
||||||
returnx=""
|
returnx=""
|
||||||
if cvx<335:
|
if cvx<335:
|
||||||
returnx+="西"
|
returnx+="西"
|
||||||
returnx+=str((335-cvx)*18/1000*0.54)+"海里"
|
returnx+=str((335-cvx)*18/1000*0.54)+"海里"
|
||||||
elif cvx>335:
|
elif cvx>335:
|
||||||
returnx+="东"
|
returnx+="东"
|
||||||
returnx+=str((cvx-335)*18/1000*0.54) + "海里"
|
returnx+=str((cvx-335)*18/1000*0.54) + "海里"
|
||||||
else:
|
else:
|
||||||
returnx+="中0海里"
|
returnx+="中0海里"
|
||||||
return returnx,returny
|
return returnx,returny
|
||||||
model = YOLO("./best4.pt")
|
model = YOLO("./best4_ncnn_model")
|
||||||
import matplotlib.pyplot as plt
|
import matplotlib.pyplot as plt
|
||||||
plt.rcParams['font.family'] = 'SimHei' # 设置字体为中文黑体
|
plt.rcParams['font.family'] = 'SimHei' # 设置字体为中文黑体
|
||||||
def zh_ch(string):
|
def zh_ch(string):
|
||||||
return string.encode("gbk").decode(errors="ignore")
|
return string.encode("gbk").decode(errors="ignore")
|
||||||
cap = cv2.VideoCapture(1)
|
cap = cv2.VideoCapture(0)
|
||||||
cv2.namedWindow('window', cv2.WINDOW_NORMAL)
|
cv2.namedWindow('window', cv2.WINDOW_NORMAL)
|
||||||
cv2.setWindowProperty('window', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
|
cv2.setWindowProperty('window', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
|
||||||
while cap.isOpened():
|
while cap.isOpened():
|
||||||
# 读取摄像头帧
|
# 读取摄像头帧
|
||||||
success, frame = cap.read()
|
success, frame = cap.read()
|
||||||
|
|
||||||
if not success:
|
if not success:
|
||||||
break
|
break
|
||||||
|
|
||||||
# 使用 YOLOv8 进行检测(conf 设置置信度阈值)
|
# 使用 YOLOv8 进行检测(conf 设置置信度阈值)
|
||||||
results = model(frame, conf=0.91)
|
results = model(frame, conf=0.91)
|
||||||
|
|
||||||
# 初始化绿潮检测标志
|
# 初始化绿潮检测标志
|
||||||
green_tide_detected = False
|
green_tide_detected = False
|
||||||
red_tide_detected = False
|
red_tide_detected = False
|
||||||
fluorescent_sea_detected = False
|
fluorescent_sea_detected = False
|
||||||
# 遍历检测结果
|
# 遍历检测结果
|
||||||
for result in results:
|
for result in results:
|
||||||
for box in result.boxes:
|
for box in result.boxes:
|
||||||
# 获取坐标(xyxy 格式)
|
# 获取坐标(xyxy 格式)
|
||||||
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
||||||
|
|
||||||
# 获取类别 ID 和名称
|
# 获取类别 ID 和名称
|
||||||
class_id = int(box.cls)
|
class_id = int(box.cls)
|
||||||
class_name = model.names[class_id]
|
class_name = model.names[class_id]
|
||||||
|
|
||||||
# 获取置信度(保留 2 位小数)
|
# 获取置信度(保留 2 位小数)
|
||||||
confidence = round(float(box.conf), 2)
|
confidence = round(float(box.conf), 2)
|
||||||
|
|
||||||
# 绘制检测框
|
# 绘制检测框
|
||||||
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
||||||
|
|
||||||
# 显示类别和置信度
|
# 显示类别和置信度
|
||||||
label = f"{class_name}: {confidence:.2f}"
|
label = f"{class_name}: {confidence:.2f}"
|
||||||
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
||||||
|
|
||||||
# 检查是否是绿潮
|
# 检查是否是绿潮
|
||||||
if class_name == "greentide":
|
if class_name == "greentide":
|
||||||
green_tide_detected = True
|
green_tide_detected = True
|
||||||
if class_name == "redtide":
|
if class_name == "redtide":
|
||||||
red_tide_detected = True
|
red_tide_detected = True
|
||||||
if class_name=="fluorescent_sea":
|
if class_name=="fluorescent_sea":
|
||||||
fluorescent_sea_detected = True
|
fluorescent_sea_detected = True
|
||||||
|
|
||||||
# 如果检测到绿潮,在右上角显示提示
|
# 如果检测到绿潮,在右上角显示提示
|
||||||
if green_tide_detected:
|
if green_tide_detected:
|
||||||
x1_return,y1_return=getxy(x1,y1)
|
x1_return,y1_return=getxy(x1,y1)
|
||||||
x2_return,y2_return=getxy(x2,y2)
|
x2_return,y2_return=getxy(x2,y2)
|
||||||
centerx_return,centery_return=getxy((x1+x2)*0.5,(y1+y2)*0.5)
|
centerx_return,centery_return=getxy((x1+x2)*0.5,(y1+y2)*0.5)
|
||||||
text="检测到发生绿潮,左上位置:"+x1_return+","+y1_return+",\n右下位置:"+x2_return+","+y2_return+",\n中心位置:"+centerx_return+","+centery_return
|
text="检测到发生绿潮,左上位置:"+x1_return+","+y1_return+",\n右下位置:"+x2_return+","+y2_return+",\n中心位置:"+centerx_return+","+centery_return
|
||||||
#text = "检测到发生绿潮,左上坐标:("+str(x1*18)+","+str(y1*18)+"),右下坐标:("+str(x2*18)+","+str(y2*18)+"),\n中心点坐标:("+str(int(0.5*18*(x1+x2)))+","+str(int(0.5*18*(y1+y2)))+")"
|
#text = "检测到发生绿潮,左上坐标:("+str(x1*18)+","+str(y1*18)+"),右下坐标:("+str(x2*18)+","+str(y2*18)+"),\n中心点坐标:("+str(int(0.5*18*(x1+x2)))+","+str(int(0.5*18*(y1+y2)))+")"
|
||||||
font = cv2.FONT_HERSHEY_SIMPLEX
|
font = cv2.FONT_HERSHEY_SIMPLEX
|
||||||
font_scale = 0.5
|
font_scale = 0.5
|
||||||
font_thickness = 2
|
font_thickness = 2
|
||||||
font_color = (0, 255, 0) # 绿色
|
font_color = (0, 255, 0) # 绿色
|
||||||
|
|
||||||
# 获取文字尺寸
|
# 获取文字尺寸
|
||||||
(text_width, text_height), _ = cv2.getTextSize(text, font, font_scale, font_thickness)
|
(text_width, text_height), _ = cv2.getTextSize(text, font, font_scale, font_thickness)
|
||||||
|
|
||||||
# 计算右上角位置(留出边距)
|
# 计算右上角位置(留出边距)
|
||||||
margin = 10
|
margin = 10
|
||||||
text_x = frame.shape[1] - text_width - margin
|
text_x = frame.shape[1] - text_width - margin
|
||||||
text_y = text_height + margin
|
text_y = text_height + margin
|
||||||
|
|
||||||
# 添加半透明背景(可选)
|
# 添加半透明背景(可选)
|
||||||
cv2.rectangle(
|
cv2.rectangle(
|
||||||
frame,
|
frame,
|
||||||
(text_x - 5, text_y - text_height - 5),
|
(text_x - 5, text_y - text_height - 5),
|
||||||
(text_x + text_width + 5, text_y + 5),
|
(text_x + text_width + 5, text_y + 5),
|
||||||
(0, 0, 0),
|
(0, 0, 0),
|
||||||
-1,
|
-1,
|
||||||
)
|
)
|
||||||
|
|
||||||
# 绘制文字
|
# 绘制文字
|
||||||
cv2.putText(frame, text, (text_x, text_y), font, font_scale, font_color, font_thickness)
|
cv2.putText(frame, text, (text_x, text_y), font, font_scale, font_color, font_thickness)
|
||||||
if red_tide_detected:
|
if red_tide_detected:
|
||||||
x1_return, y1_return = getxy(x1, y1)
|
x1_return, y1_return = getxy(x1, y1)
|
||||||
x2_return, y2_return = getxy(x2, y2)
|
x2_return, y2_return = getxy(x2, y2)
|
||||||
centerx_return, centery_return = getxy((x1 + x2) * 0.5, (y1 + y2) * 0.5)
|
centerx_return, centery_return = getxy((x1 + x2) * 0.5, (y1 + y2) * 0.5)
|
||||||
text = "检测到发生赤潮,左上位置:" + x1_return + "," + y1_return + ",\n右下位置:" + x2_return + "," + y2_return + ",\n中心位置:" + centerx_return + "," + centery_return
|
text = "检测到发生赤潮,左上位置:" + x1_return + "," + y1_return + ",\n右下位置:" + x2_return + "," + y2_return + ",\n中心位置:" + centerx_return + "," + centery_return
|
||||||
font = cv2.FONT_HERSHEY_SIMPLEX
|
font = cv2.FONT_HERSHEY_SIMPLEX
|
||||||
font_scale = 0.5
|
font_scale = 0.5
|
||||||
font_thickness = 2
|
font_thickness = 2
|
||||||
font_color = (0, 0, 255) # 绿色
|
font_color = (0, 0, 255) # 绿色
|
||||||
|
|
||||||
# 获取文字尺寸
|
# 获取文字尺寸
|
||||||
(text_width, text_height), _ = cv2.getTextSize(text, font, font_scale, font_thickness)
|
(text_width, text_height), _ = cv2.getTextSize(text, font, font_scale, font_thickness)
|
||||||
|
|
||||||
# 计算右上角位置(留出边距)
|
# 计算右上角位置(留出边距)
|
||||||
margin = 10
|
margin = 10
|
||||||
text_x = frame.shape[1] - text_width - margin
|
text_x = frame.shape[1] - text_width - margin
|
||||||
text_y = text_height + margin
|
text_y = text_height + margin
|
||||||
|
|
||||||
# 添加半透明背景(可选)
|
# 添加半透明背景(可选)
|
||||||
cv2.rectangle(
|
cv2.rectangle(
|
||||||
frame,
|
frame,
|
||||||
(text_x - 5, text_y - text_height - 5),
|
(text_x - 5, text_y - text_height - 5),
|
||||||
(text_x + text_width + 5, text_y + 5),
|
(text_x + text_width + 5, text_y + 5),
|
||||||
(0, 0, 0),
|
(0, 0, 0),
|
||||||
-1,
|
-1,
|
||||||
)
|
)
|
||||||
|
|
||||||
# 绘制文字
|
# 绘制文字
|
||||||
cv2.putText(frame, text, (text_x, text_y), font, font_scale, font_color, font_thickness)
|
cv2.putText(frame, text, (text_x, text_y), font, font_scale, font_color, font_thickness)
|
||||||
if fluorescent_sea_detected:
|
if fluorescent_sea_detected:
|
||||||
x1_return, y1_return = getxy(x1, y1)
|
x1_return, y1_return = getxy(x1, y1)
|
||||||
x2_return, y2_return = getxy(x2, y2)
|
x2_return, y2_return = getxy(x2, y2)
|
||||||
centerx_return, centery_return = getxy((x1 + x2) * 0.5, (y1 + y2) * 0.5)
|
centerx_return, centery_return = getxy((x1 + x2) * 0.5, (y1 + y2) * 0.5)
|
||||||
text = "检测到发生荧光海,左上位置:" + x1_return + "," + y1_return + ",\n右下位置:" + x2_return + "," + y2_return + ",\n中心位置:" + centerx_return + "," + centery_return
|
text = "检测到发生荧光海,左上位置:" + x1_return + "," + y1_return + ",\n右下位置:" + x2_return + "," + y2_return + ",\n中心位置:" + centerx_return + "," + centery_return
|
||||||
font = cv2.FONT_HERSHEY_SIMPLEX
|
font = cv2.FONT_HERSHEY_SIMPLEX
|
||||||
font_scale = 0.5
|
font_scale = 0.5
|
||||||
font_thickness = 2
|
font_thickness = 2
|
||||||
font_color = (255, 0,0) # 绿色
|
font_color = (255, 0,0) # 绿色
|
||||||
|
|
||||||
# 获取文字尺寸
|
# 获取文字尺寸
|
||||||
(text_width, text_height), _ = cv2.getTextSize(text, font, font_scale, font_thickness)
|
(text_width, text_height), _ = cv2.getTextSize(text, font, font_scale, font_thickness)
|
||||||
|
|
||||||
# 计算右上角位置(留出边距)
|
# 计算右上角位置(留出边距)
|
||||||
margin = 10
|
margin = 10
|
||||||
text_x = frame.shape[1] - text_width - margin
|
text_x = frame.shape[1] - text_width - margin
|
||||||
text_y = text_height + margin
|
text_y = text_height + margin
|
||||||
|
|
||||||
# 添加半透明背景(可选)
|
# 添加半透明背景(可选)
|
||||||
cv2.rectangle(
|
cv2.rectangle(
|
||||||
frame,
|
frame,
|
||||||
(text_x - 5, text_y - text_height - 5),
|
(text_x - 5, text_y - text_height - 5),
|
||||||
(text_x + text_width + 5, text_y + 5),
|
(text_x + text_width + 5, text_y + 5),
|
||||||
(0, 0, 0),
|
(0, 0, 0),
|
||||||
-1,
|
-1,
|
||||||
)
|
)
|
||||||
|
|
||||||
# 绘制文字
|
# 绘制文字
|
||||||
cv2.putText(frame, text, (text_x, text_y), font, font_scale, font_color, font_thickness)
|
cv2.putText(frame, text, (text_x, text_y), font, font_scale, font_color, font_thickness)
|
||||||
# 显示实时检测画面
|
# 显示实时检测画面
|
||||||
cv2.imshow("window", frame)
|
cv2.imshow("window", frame)
|
||||||
# 按 'q' 退出
|
# 按 'q' 退出
|
||||||
if cv2.waitKey(1) & 0xFF == ord('q'):
|
if cv2.waitKey(1) & 0xFF == ord('q'):
|
||||||
break
|
break
|
||||||
|
|
||||||
# 释放摄像头并关闭窗口
|
# 释放摄像头并关闭窗口
|
||||||
cap.release()
|
cap.release()
|
||||||
cv2.destroyAllWindows()
|
cv2.destroyAllWindows()
|
||||||
Reference in New Issue
Block a user