整体逻辑是这样的:
- 箕斗是否到位
- 是,灰度值是否超过阈值
- 是,status = 1
- 否,status = 0
- 否,status = 0
- 是,灰度值是否超过阈值
主程序:
初始:Top_warning = True
- 箕斗到位
- 否:pass
- 是,读取status
- status = 0, pass
- status =1, Top_warning = False
- 箕斗到位后10秒
- 检测Top_warning是否True,是,报警,不是,pass
箕斗离开卸载位,初始化:
- Top_warning = True
import cv2
import numpy as np
import pandas as pd
import time
import mysql.connector
import mysql_connect
def write_skipstatus(p_flag):
try:
conn = mysql.connector.connect( host='127.0.0.1',user='root',password='dashucun888', database='viewdata')
# mycursor = conn.cursor()
mycursor = conn.cursor()
sql1 = "update skipstatus set status = 1 where skip = 'wskip' "
sql2 = "update skipstatus set status = 0 where skip = 'wskip' "
if p_flag == 0:
mycursor.execute(sql2)
elif p_flag ==1:
mycursor.execute(sql1)
# results = mycursor.fetchall()
mycursor.close()
conn.close()
# return results
except:
pass
def save_data(filename,d):
filename = filename[:-3]
filename += "csv"
save_data = [(time.time(),d)]
labels = ['time','data']
df = pd.DataFrame.from_records(save_data, columns=labels)
df.to_csv(filename, mode='a',header=False,index = False)
def camera_show(filename):
Wskip_arrive = False
wskip_arrive_time = -1
# 东箕斗
# y1 = 290
# y2 = 307
# x1 = 253
# x2 = 271
# 西箕斗
y1 = 276
y2 = 300
x1 = 234
x2 = 285
url = filename
cap = cv2.VideoCapture(url)
# ret, frame = cap.read()
i = 0
# P_flag = False
while cap.isOpened():
ret, frame = cap.read()
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame1 = frame[x1:x2,y1:y2]
a = np.sum(frame1)/((x2-x1)*(y2-y1))
# save_data(filename,a)
############################
# 读箕斗位置
position = mysql_connect.read_skip_position()
if position[0][1]:
if not Wskip_arrive:
wskip_arrive_time = time.time()
Wskip_arrive = True
if a > 10 and time.time()-wskip_arrive_time<10:
write_skipstatus(1)
else:
write_skipstatus(0)
wskip_arrive_time = -1
Wskip_arrive = False
############################
print(i,a)
i+=1
cv2.rectangle(frame,(y1,x1),(y2,x2),(0,255,0),2)
cv2.imshow("frame",frame)
else:
break
if cv2.waitKey(1) & 0xFF == ord('q'):
break
print("It's over!")
cv2.destroyAllWindows()
cap.release()
if __name__ == "__main__":
filename = r"D:\outvideo92021-02-02-18.mp4"
# filename = "rtsp://admin:zxcvbnm123@192.168.121.8/Streaming/Channels/2" # 可以使用通道1或者2
camera_show(filename)
修改内容
- 箕斗煤量要实时,卸完煤就清空,更新卸载煤量时,同时更新箕斗煤量
- 主程序,报警与Topwarning逻辑运算。或者,独依靠Topwarning逻辑。测试一下。
网友评论