I would use a pi camera and to stream I use..
and this will receive it..
To find the ip address to set on the receiver..
Open the terminal on the server
2. Type in: ifconfig
3. Look for: inet addr:
Code:
rpicam-vid -t 0 --framerate 25 --codec libav --libav-format mpegts -o - | cvlc stream:///dev/stdin --sout '#rtp{sdp=rtsp://:5000/stream1}'To find the ip address to set on the receiver..
Open the terminal on the server
2. Type in: ifconfig
3. Look for: inet addr:
Code:
#!/usr/bin/python3# Press RIGHT Mouse button on picture to EXIT# Press LEFT Mouse button to move a box, click on one box to move then click on new position.import randomimport numpy as npimport cv2import timeimport datetimeimport os,sysimport mathimport smtplib, sslfrom picamera2 import Picamera2# rtsp stream addressurl = 'rtsp://192.168.1.163:5000/stream1'# for demo set = 1, for stream = 0demo = 0# sending emails, set to 1 to enablesend_email = 0#detection window sizewindow = 10# number of detection areas areas = 13# set trigger levelrtrigger = 130gtrigger = 130# set alarm period (MINUTES)alarm = 10labels = ["ZONE_1 ","ZONE_2 ","ZONE_3 ","ZONE_4 ","HEAT_1 ","HEAT_2 ","HEAT_3 ","COOL_1 ","COOL_2 ","FAN ","PURGE ","EM_HEAT","","","","","",""]from email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email.mime.base import MIMEBasefrom email import encodersfrom email.mime.image import MIMEImagedef sendMail(data): port = 465 # For SSL smtp_server = "smtp.gmail.com" # Enter email server sender_email = "xxx@xxx.xxx" # Enter your address receiver_email = "yyy@yyy.yyy" # Enter receiver address password = "zzzzzzzz" # Enter email password message = data context = ssl.create_default_context() with smtplib.SMTP_SSL(smtp_server, port, context=context) as server: server.login(sender_email, password) server.sendmail(sender_email, receiver_email, message) if demo == 0: # stream address cap = cv2.VideoCapture(url)# initialise variablescv2.namedWindow('Monitoring Panel')if demo == 1: q = np.zeros((600,450,3), np.uint8) r = np.zeros((120,640,3), np.uint8)else: q = np.zeros((600,450,3), np.uint8) r = np.zeros((120,640,3), np.uint8)points = [0]*100pointz = [0]*100rtimes = [0]*50rtriggered = [0]*50rcleared = [0]*50gtimes = [0]*50gtriggered = [0]*50gcleared = [0]*50ytimes = [0]*50ytriggered = [0]*50ycleared = [0]*50emailed = [0]*50move = 0log_list = ["-"]*50# Mouse. LEFT button to move boxes, RIGHT button to EXITdef point(event, x, y, flags, param): global triggered, window , move, store, points, pointz, areas, times, cleared if event == cv2.EVENT_RBUTTONDOWN: cv2.destroyAllWindows() sys.exit() if event == cv2.EVENT_LBUTTONDOWN: if move == 0: minm = 1000 for j in range (0,areas*2,2): temp = math.sqrt((abs(points[j] - y) * abs(points[j] - y) + abs(points[j+1] - x) * abs(points[j+1] - x))) if temp < minm: minm = temp store = j move = 1 elif move == 1: points[store] = y - int(window/2) points[store+1] = x - int(window/2) move = 0 pointz[:] = points[:] for j in range (0,areas*2,2): pointz[j] = pointz[j] + int(window/2) pointz[j+1] = pointz[j+1] + int(window/2) with open('points2.txt', 'w') as f: for item in pointz: f.write("%s\n" % item) # setup mouse callback cv2.setMouseCallback('Monitoring Panel',point)# check points2.txt exists, if not then write default valuesif not os.path.exists('points2.txt'): points = [234,336,270,336,305,336,342,336, 225,318,244,318,262,318,280,318,298,318,315,318,334,318,352,318,196,318] with open('points2.txt', 'w') as f: for item in points: f.write("%s\n" % item) # read points2.txtpoints = []with open("points2.txt", "r") as file: line = file.readline() while line: points.append(line.strip()) line = file.readline()points = list(map(int,points))for x in range (0,areas*2,2): points[x] = points[x] - int(window/2) points[x+1] = points[x+1] - int(window/2)now = datetime.datetime.now()logstamp = now.strftime("%Y%m%d")if os.path.exists(logstamp + '.txt'): with open(logstamp + ".txt", "r") as file: line = file.readline() while line: log_list.append(line.strip()) line = file.readline() log_list.reverse() if len(log_list) > 50: log_list = log_list[0:49] for k in range(0,42): cv2.rectangle(q, (180,k*18),(600,(k*18)+20), (0,0,0),-1) cv2.putText(q,log_list[k], (185,10 + (k * 14)), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (130, 130, 130),1) timer = 0lights = [0] * 100raverage = 130gaverage = 130baverage = 130# main loopwhile True: if demo == 1: a = random.randint(0,99) b = random.randint(0,3) lights[a] = b # GET demo picture img = cv2.imread('panel.jpg') for z in range (0,areas*2,2): if lights[int(z/2)] == 1 and int(z/2) != areas - 1: cv2.rectangle(img, (points[z+1]+int(window/3),points[z]+int(window/3)), (points[z+1]+int(window/1.5),points[z]+int(window/1.5)), (0,255,0),-1) elif lights[int(z/2)] == 2 and int(z/2) != areas - 1: cv2.rectangle(img, (points[z+1]+int(window/3),points[z]+int(window/3)), (points[z+1]+int(window/1.5),points[z]+int(window/1.5)), (0,0,255),-1) elif lights[int(z/2)] == 3 and int(z/2) != areas - 1: cv2.rectangle(img, (points[z+1]+int(window/3),points[z]+int(window/3)), (points[z+1]+int(window/1.5),points[z]+int(window/1.5)), (0,255,255),-1) if lights[int(z/2)] == 0 and int(z/2) != areas - 1: cv2.rectangle(img, (points[z+1],points[z]), (points[z+1]+window,points[z]+window), (baverage,gaverage,raverage),-1) time.sleep(.5) (B, G, rgray) = cv2.split(img) # red (B, ggray, R) = cv2.split(img) # green (bgray, G, R) = cv2.split(img) # blue else: ret, img = cap.read() (B, G, rgray) = cv2.split(img) # red (B, ggray, R) = cv2.split(img) # green (bgray, G, R) = cv2.split(img) # blue # check for triggered lights every frame for box in range (0,areas*2,2): rwindowed = rgray[points[box]:points[box]+window, points[box+1]:points[box+1]+window] raverage = np.average(rwindowed) # red value gwindowed = ggray[points[box]:points[box]+window, points[box+1]:points[box+1]+window] gaverage = np.average(gwindowed) # green value bwindowed = bgray[points[box]:points[box]+window, points[box+1]:points[box+1]+window] baverage = np.average(gwindowed) # blue value if int(box/2) == areas - 1: gtrigger = np.average(gwindowed) + 20 # green trigger rtrigger = np.average(rwindowed) + 20 # red trigger # display average levels to help setup cv2.putText(img,str(int(raverage)), (points[box+1] + 130, points[box] + 10 ), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (100, 200, 100),1) cv2.putText(img,str(int(gaverage)), (points[box+1] + 230, points[box] + 10 ), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (100, 200, 100),1) # check for RED lights ON if raverage > rtrigger and gtrigger > gaverage and rtriggered[int(box/2)] == 0 and int(box/2) != areas - 1: now = datetime.datetime.now() timestamp = now.strftime("%Y/%m/%d %H:%M:%S") rtriggered[int(box/2)] = timestamp rtimes[int(box/2)] = time.time() rcleared[int(box/2)] = 0 logstamp = now.strftime("%Y%m%d") if ytriggered[int(box/2)] != 0: with open(logstamp + '.txt', 'a') as f: if int(box/2) < 9: item = "0" + str(int(box/2)+1) + " Y- " + labels[int(box/2)] + " " + str(timestamp) + " " + str(int((time.time() - float(ytimes[int(box/2)]))/60)) else: item = str(int(box/2)+1) + " Y- " + labels[int(box/2)] + " " + str(timestamp) + " " + str(int((time.time() - float(ytimes[int(box/2)]))/60)) f.write("%s\n" % item) ytriggered[int(box/2)] = 0 ytimes[int(box/2)] = 0 ycleared[int(box/2)] = timestamp log_list.insert(0,item) del log_list[-1] if gtriggered[int(box/2)] != 0: with open(logstamp + '.txt', 'a') as f: if int(box/2) < 9: item = "0" + str(int(box/2)+1) + " G- " + labels[int(box/2)] + " " + str(timestamp) + " " + str(int((time.time() - float(gtimes[int(box/2)]))/60)) else: item = str(int(box/2)+1) + " G- " + labels[int(box/2)] + " " + str(timestamp) + " " + str(int((time.time() - float(gtimes[int(box/2)]))/60)) f.write("%s\n" % item) gtriggered[int(box/2)] = 0 gtimes[int(box/2)] = 0 gcleared[int(box/2)] = timestamp log_list.insert(0,item) del log_list[-1] with open(logstamp + '.txt', 'a') as f: if int(box/2) < 9: item = "0" + str(int(box/2)+1) + " R+ " + labels[int(box/2)] + " " + str(timestamp) else: item = str(int(box/2)+1) + " R+ " + labels[int(box/2)] + " " + str(timestamp) f.write("%s\n" % item) log_list.insert(0,item) del log_list[-1] for k in range(0,42): cv2.rectangle(q, (180,k*18),(600,(k*18)+20), (0,0,0),-1) cv2.putText(q,log_list[k], (185,10 + (k * 14)), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (130, 130, 130),1) # check for RED lights going OFF elif rtrigger > raverage and gtrigger > gaverage and rtriggered[int(box/2)] != 0 and int(box/2) != areas - 1: now = datetime.datetime.now() timestamp = now.strftime("%Y/%m/%d %H:%M:%S") rcleared[int(box/2)] = timestamp rtriggered[int(box/2)] = 0 emailed[int(box/2)] = 0 logstamp = now.strftime("%Y%m%d") with open(logstamp +'.txt', 'a') as f: if int(box/2) < 9: item = "0" + str(int(box/2)+1) + " R- " + labels[int(box/2)] + " " + str(timestamp) + " " + str(int((time.time() - float(rtimes[int(box/2)]))/60)) else: item = str(int(box/2)+1) + " R- " + labels[int(box/2)] + " " + str(timestamp) + " " + str(int((time.time() - float(rtimes[int(box/2)]))/60)) f.write("%s\n" % item) rtimes[int(box/2)] = 0 log_list.insert(0,item) del log_list[-1] for k in range(0,42): cv2.rectangle(q, (180,k*18),(600,(k*18)+20), (0,0,0),-1) cv2.putText(q,log_list[k], (185,10 + (k * 14)), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (130, 130, 130),1) # check for GREEN lights ON elif gaverage > gtrigger and rtrigger > raverage and gtriggered[int(box/2)] == 0 and int(box/2) != areas - 1: now = datetime.datetime.now() timestamp = now.strftime("%Y/%m/%d %H:%M:%S") gtriggered[int(box/2)] = timestamp gtimes[int(box/2)] = time.time() gcleared[int(box/2)] = 0 logstamp = now.strftime("%Y%m%d") with open(logstamp + '.txt', 'a') as f: if int(box/2) < 9: item = "0" + str(int(box/2)+1) + " G+ " + labels[int(box/2)] + " " + str(timestamp) else: item = str(int(box/2)+1) + " G+ " + labels[int(box/2)] + " " + str(timestamp) f.write("%s\n" % item) log_list.insert(0,item) del log_list[-1] if ytriggered[int(box/2)] != 0: with open(logstamp + '.txt', 'a') as f: if int(box/2) < 9: item = "0" + str(int(box/2)+1) + " Y- " + labels[int(box/2)] + " " + str(timestamp) + " " + str(int((time.time() - float(ytimes[int(box/2)]))/60)) else: item = str(int(box/2)+1) + " Y- " + labels[int(box/2)] + " " + str(timestamp) + " " + str(int((time.time() - float(ytimes[int(box/2)]))/60)) f.write("%s\n" % item) ytriggered[int(box/2)] = 0 ytimes[int(box/2)] = 0 ycleared[int(box/2)] = timestamp log_list.insert(0,item) del log_list[-1] if rtriggered[int(box/2)] != 0: with open(logstamp + '.txt', 'a') as f: if int(box/2) < 9: item = "0" + str(int(box/2)+1) + " R- " + labels[int(box/2)] + " " + str(timestamp) + " " + str(int((time.time() - float(rtimes[int(box/2)]))/60)) else: item = str(int(box/2)+1) + " R- " + labels[int(box/2)] + " " + str(timestamp) + " " + str(int((time.time() - float(rtimes[int(box/2)]))/60)) f.write("%s\n" % item) rtriggered[int(box/2)] = 0 rtimes[int(box/2)] = 0 rcleared[int(box/2)] = timestamp log_list.insert(0,item) del log_list[-1] for k in range(0,42): cv2.rectangle(q, (180,k*18),(600,(k*18)+20), (0,0,0),-1) cv2.putText(q,log_list[k], (185,10 + (k * 14)), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (130, 130, 130),1) # check for GREEN lights going OFF elif gtrigger > gaverage and rtrigger > raverage and gtriggered[int(box/2)] != 0 and int(box/2) != areas - 1: now = datetime.datetime.now() timestamp = now.strftime("%Y/%m/%d %H:%M:%S") gcleared[int(box/2)] = timestamp gtriggered[int(box/2)] = 0 emailed[int(box/2)] = 0 logstamp = now.strftime("%Y%m%d") with open(logstamp +'.txt', 'a') as f: if int(box/2) < 9: item = "0" + str(int(box/2)+1) + " G- " + labels[int(box/2)] + " " + str(timestamp) + " " + str(int((time.time() - float(gtimes[int(box/2)]))/60)) else: item = str(int(box/2)+1) + " G- " + labels[int(box/2)] + " " + str(timestamp) + " " + str(int((time.time() - float(gtimes[int(box/2)]))/60)) f.write("%s\n" % item) gtimes[int(box/2)] = 0 log_list.insert(0,item) del log_list[-1] for k in range(0,42): cv2.rectangle(q, (180,k*18),(600,(k*18)+20), (0,0,0),-1) cv2.putText(q,log_list[k], (185,10 + (k * 14)), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (130, 130, 130),1) # check for YELLOW lights ON elif raverage > rtrigger and gaverage > gtrigger and ytriggered[int(box/2)] == 0 and int(box/2) != areas - 1: now = datetime.datetime.now() timestamp = now.strftime("%Y/%m/%d %H:%M:%S") ytriggered[int(box/2)] = timestamp ytimes[int(box/2)] = time.time() ycleared[int(box/2)] = 0 logstamp = now.strftime("%Y%m%d") if rtriggered[int(box/2)] != 0: with open(logstamp + '.txt', 'a') as f: if int(box/2) < 9: item = "0" + str(int(box/2)+1) + " R- " + labels[int(box/2)] + " " + str(timestamp) + " " + str(int((time.time() - float(rtimes[int(box/2)]))/60)) else: item = str(int(box/2)+1) + " R- " + labels[int(box/2)] + " " + str(timestamp) + " " + str(int((time.time() - float(rtimes[int(box/2)]))/60)) f.write("%s\n" % item) rtriggered[int(box/2)] = 0 rtimes[int(box/2)] = 0 rcleared[int(box/2)] = timestamp log_list.insert(0,item) del log_list[-1] if gtriggered[int(box/2)] != 0: with open(logstamp + '.txt', 'a') as f: if int(box/2) < 9: item = "0" + str(int(box/2)+1) + " G- " + labels[int(box/2)] + " " + str(timestamp) + " " + str(int((time.time() - float(gtimes[int(box/2)]))/60)) else: item = str(int(box/2)+1) + " G- " + labels[int(box/2)] + " " + str(timestamp) + " " + str(int((time.time() - float(gtimes[int(box/2)]))/60)) f.write("%s\n" % item) gtriggered[int(box/2)] = 0 gtimes[int(box/2)] = 0 gcleared[int(box/2)] = timestamp log_list.insert(0,item) del log_list[-1] with open(logstamp + '.txt', 'a') as f: if int(box/2) < 9: item = "0" + str(int(box/2)+1) + " Y+ " + labels[int(box/2)] + " " + str(timestamp) else: item = str(int(box/2)+1) + " Y+ " + labels[int(box/2)] + " " + str(timestamp) f.write("%s\n" % item) log_list.insert(0,item) del log_list[-1] for k in range(0,42): cv2.rectangle(q, (180,k*18),(600,(k*18)+20), (0,0,0),-1) cv2.putText(q,log_list[k], (185,10 + (k * 14)), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (130, 130, 130),1) # check for YELLOW lights going OFF elif rtrigger > raverage and gtrigger > gaverage and ytriggered[int(box/2)] != 0 and int(box/2) != areas - 1: now = datetime.datetime.now() timestamp = now.strftime("%Y/%m/%d %H:%M:%S") ycleared[int(box/2)] = timestamp ytriggered[int(box/2)] = 0 emailed[int(box/2)] = 0 logstamp = now.strftime("%Y%m%d") with open(logstamp +'.txt', 'a') as f: if int(box/2) < 9: item = "0" + str(int(box/2)+1) + " Y- " + labels[int(box/2)] + " " + str(timestamp) + " " + str(int((time.time() - float(ytimes[int(box/2)]))/60)) else: item = str(int(box/2)+1) + " Y- " + labels[int(box/2)] + " " + str(timestamp) + " " + str(int((time.time() - float(ytimes[int(box/2)]))/60)) f.write("%s\n" % item) ytimes[int(box/2)] = 0 log_list.insert(0,item) del log_list[-1] for k in range(0,42): cv2.rectangle(q, (180,k*18),(600,(k*18)+20), (0,0,0),-1) cv2.putText(q,log_list[k], (185,10 + (k * 14)), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (130, 130, 130),1) rcount = 0 cv2.rectangle(q, (0,0),(180,600), (0,0,50),-1) data ="" rabc = ['x']*35 for z in range (0,areas*2,2): if rtriggered[int(z/2)] != 0 and rcleared[int(z/2)] == 0: room = str(int(z/2)+1) if int(z/2) < 9: room = "0" + str(int(z/2)+1) rabc[rcount] = str(rtriggered[int(z/2)]) + " " + room + str(rtimes[int(z/2)]) rcount +=1 rabc.sort() gcount = 0 gabc = ['x']*35 for z in range (0,areas*2,2): if gtriggered[int(z/2)] != 0 and gcleared[int(z/2)] == 0: room = str(int(z/2)+1) if int(z/2) < 9: room = "0" + str(int(z/2)+1) gabc[gcount] = str(gtriggered[int(z/2)]) + " " + room + str(gtimes[int(z/2)]) gcount +=1 gabc.sort() ycount = 0 yabc = ['x']*35 for z in range (0,areas*2,2): if ytriggered[int(z/2)] != 0 and ycleared[int(z/2)] == 0: room = str(int(z/2)+1) if int(z/2) < 9: room = "0" + str(int(z/2)+1) yabc[ycount] = str(ytriggered[int(z/2)]) + " " + room + str(ytimes[int(z/2)]) ycount +=1 yabc.sort() for z in range (0,rcount): duration = int((time.time() - float(rabc[z][23:39]))/60) if duration > 999: duration = 999 if duration >= alarm: cv2.putText(q,rabc[z][0:23] + " " + str(int(duration)) , (2,30 + int(z) * 15), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255),1) else: cv2.putText(q,rabc[z][0:23] + " " + str(int(duration)) , (2,30 + int(z) * 15), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 200),1) for z in range (0,gcount): duration = int((time.time() - float(gabc[z][23:39]))/60) if duration > 999: duration = 999 if duration >= alarm: cv2.putText(q,gabc[z][0:23] + " " + str(int(duration)) , (2,400 + int(z) * 15), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 255, 0),1) else: cv2.putText(q,gabc[z][0:23] + " " + str(int(duration)) , (2,400 + int(z) * 15), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 200, 0),1) for z in range (0,ycount): duration = int((time.time() - float(yabc[z][23:39]))/60) if duration > 999: duration = 999 if duration >= alarm: cv2.putText(q,yabc[z][0:23] + " " + str(int(duration)) , (2,200 + int(z) * 15), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 255, 255),1) else: cv2.putText(q,yabc[z][0:23] + " " + str(int(duration)) , (2,200 + int(z) * 15), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 200, 200),1) # draw boxes data2 = "" for z in range (0,areas*2,2): cv2.rectangle(img, (0,0), (639,479), (100,100,100), 2) # draw boxes if int(z/2) != areas - 1: cv2.rectangle(img, (points[z+1],points[z]), (points[z+1]+window,points[z]+window), (0,0,200), 1) else: cv2.rectangle(img, (points[z+1],points[z]), (points[z+1]+window,points[z]+window), (200,0,0), 1) # show yellow box to be moved if move == 1 and store == z: cv2.rectangle(img, (points[z+1],points[z]), (points[z+1]+window,points[z]+window), (0,200,200), 1) # show led numbers if rtriggered[int(z/2)] != 0: if int(z/2) < 4: cv2.putText(img,str(int(z/2) + 1), (points[z+1] + 55, points[z] + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 250),2) else: cv2.putText(img,str(int(z/2) + 1), (points[z+1] - 50, points[z] + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 250),2) elif gtriggered[int(z/2)] != 0: if int(z/2) < 4: cv2.putText(img,str(int(z/2) + 1), (points[z+1] + 55, points[z] + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 250, 0),2) else: cv2.putText(img,str(int(z/2) + 1), (points[z+1] - 50, points[z] + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 250, 0),2) elif ytriggered[int(z/2)] != 0: if int(z/2) < 4: cv2.putText(img,str(int(z/2) + 1), (points[z+1] + 55, points[z] + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 250, 250),2) else: cv2.putText(img,str(int(z/2) + 1), (points[z+1] - 50, points[z] + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 250, 250),2) else: if int(z/2) < 4: cv2.putText(img,str(int(z/2) + 1), (points[z+1] + 55, points[z] + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (150, 150, 155),1) else: cv2.putText(img,str(int(z/2) + 1), (points[z+1] - 50, points[z] + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (150, 150, 155),1) cv2.putText(q," Date Time Led Dur", (0, 11), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 200, 200),1) # show frame now = datetime.datetime.now() timestamp = now.strftime("%Y/%m/%d %H:%M:%S") cv2.rectangle(r, (0,0), (400,50), (0,0,0), -1) cv2.putText(r,timestamp, (0, 31), cv2.FONT_HERSHEY_SIMPLEX, 1, (200, 200, 200),1) ver = np.vstack((img,r)) hor = np.hstack((ver,q)) cv2.imshow('Monitoring Panel',hor) cv2.waitKey(1)Statistics: Posted by gordon77 — Mon Mar 31, 2025 3:12 pm