Files
fll2025/main2.py
2025-07-29 20:28:38 +08:00

162 lines
5.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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