Compare commits
8 Commits
ae72627756
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 2f3934d5eb | |||
| 1130906f03 | |||
| 514034d2c8 | |||
| 75cc736490 | |||
| 1f2a438b75 | |||
| e88be83958 | |||
| a8ac06189d | |||
| 17986590d6 |
19
docker-compose.yml
Normal file
19
docker-compose.yml
Normal file
@@ -0,0 +1,19 @@
|
||||
version: "3.3"
|
||||
|
||||
services:
|
||||
exporter:
|
||||
build: .
|
||||
ports:
|
||||
- 8888:8888
|
||||
command: python3 -u /usr/src/process.py rtsp://ubnt:ubnt@10.8.0.124:554/live/ch00_0
|
||||
restart: always
|
||||
|
||||
prometheus:
|
||||
image: prom/prometheus:v2.17.1
|
||||
volumes:
|
||||
- ./prometheus.yml:/prometheus/prometheus.yml
|
||||
command:
|
||||
- '--config.file=/prometheus/prometheus.yml'
|
||||
ports:
|
||||
- 9090:9090
|
||||
restart: always
|
||||
43
process.py
43
process.py
@@ -5,9 +5,11 @@ import os
|
||||
import time
|
||||
import numpy as np
|
||||
from prometheus_client import start_http_server, Counter, Gauge
|
||||
|
||||
import logging
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s] %(name)s: %(message)s')
|
||||
cut_count = Counter('cut_count', 'Total number of jebnięcie')
|
||||
machine_on = Gauge('machine_on', 'Current machine engine status')
|
||||
stream_on = Gauge('stream_on', 'Current camera stream status')
|
||||
|
||||
start_http_server(8888)
|
||||
|
||||
@@ -20,6 +22,7 @@ def dist(pos1, pos2):
|
||||
def run(path):
|
||||
gui = 'DISPLAY' in os.environ
|
||||
|
||||
stream_on.set(0)
|
||||
machine_on.set(0)
|
||||
|
||||
# Create a VideoCapture object and read from input file
|
||||
@@ -30,30 +33,33 @@ def run(path):
|
||||
if not cap.isOpened():
|
||||
raise Exception('VideoCapture not opened')
|
||||
|
||||
print('Opened')
|
||||
logging.info('Stream opened')
|
||||
stream_on.set(1)
|
||||
|
||||
spots = []
|
||||
|
||||
turnedon = False
|
||||
|
||||
framenum = 0
|
||||
framerate = 30
|
||||
|
||||
# Read until video is completed
|
||||
while(cap.isOpened()):
|
||||
# Capture frame-by-frame
|
||||
ret, frame = cap.read()
|
||||
if ret == True:
|
||||
h, w, channels = frame.shape
|
||||
frame = frame[int(h*0.5):int(h*0.9), int(w*0.62):int(w*0.8)]
|
||||
|
||||
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
||||
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
|
||||
thresh = cv2.threshold(blurred, 240, 255, cv2.THRESH_BINARY)[1]
|
||||
smol = frame[int(h*0.5):int(h*0.9), int(w*0.63):int(w*0.8)]
|
||||
gray = cv2.cvtColor(smol, cv2.COLOR_BGR2GRAY)
|
||||
blurred = cv2.GaussianBlur(gray, (7, 7), 0)
|
||||
thresh = cv2.threshold(blurred, 235, 255, cv2.THRESH_BINARY)[1]
|
||||
cnts = cv2.findContours(thresh, cv2.RETR_LIST,
|
||||
cv2.CHAIN_APPROX_SIMPLE)[-2]
|
||||
|
||||
for c in cnts:
|
||||
area = cv2.contourArea(c)
|
||||
if area > 20 and area < 45:
|
||||
if area > 20 and area < 80:
|
||||
# compute the center of the contour
|
||||
M = cv2.moments(c)
|
||||
cX = int(M["m10"] / M["m00"])
|
||||
@@ -79,29 +85,31 @@ def run(path):
|
||||
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
|
||||
|
||||
for spot in spots:
|
||||
if spot['lastseen'] <= framenum - 50 and turnedon:
|
||||
if spot['lastseen'] <= framenum - framerate and turnedon:
|
||||
d = dist(spots[0]['pos'], spot['pos'])
|
||||
if d < 30 and d > 5:
|
||||
print('PRYJEBAŁ :___DDD %.2f' % (spot['lifetime'] / 30.0,))
|
||||
l = spot['lifetime'] / framerate
|
||||
if d < 30 and d > 5 and l > 1.0 and l < 8.0:
|
||||
logging.info('PRYJEBAŁ :___DDD %.2f' % (l,))
|
||||
cut_count.inc()
|
||||
|
||||
if len(spots) and spots[0]['lifetime'] > 50:
|
||||
if len(spots) and spots[0]['lifetime'] > framerate * 5:
|
||||
if not turnedon:
|
||||
print('just turned on')
|
||||
logging.info('just turned on')
|
||||
machine_on.set(1)
|
||||
turnedon = True
|
||||
else:
|
||||
if turnedon:
|
||||
print('just turned off')
|
||||
logging.info('just turned off')
|
||||
machine_on.set(0)
|
||||
turnedon = False
|
||||
|
||||
spots = [spot for spot in spots if spot['lastseen'] > framenum - 50]
|
||||
spots.sort(key=lambda v: v['lastseen'], reverse=True)
|
||||
spots = [spot for spot in spots if spot['lastseen'] > framenum - framerate]
|
||||
spots.sort(key=lambda v: v['lifetime'], reverse=True)
|
||||
framenum += 1
|
||||
|
||||
if gui:
|
||||
cv2.imshow('frame',frame)
|
||||
cv2.imshow('frame',blurred)
|
||||
#cv2.imshow('edges',edges)
|
||||
cv2.imshow('thresh',thresh)
|
||||
|
||||
# Press Q on keyboard to exit
|
||||
@@ -119,5 +127,6 @@ while True:
|
||||
try:
|
||||
run(sys.argv[1])
|
||||
except Exception as exc:
|
||||
print(exc)
|
||||
logging.exception('Failed?')
|
||||
stream_on.set(0)
|
||||
time.sleep(5.0)
|
||||
|
||||
8
prometheus.yml
Normal file
8
prometheus.yml
Normal file
@@ -0,0 +1,8 @@
|
||||
global:
|
||||
scrape_interval: 15s
|
||||
evaluation_interval: 15s
|
||||
|
||||
scrape_configs:
|
||||
- job_name: sztancarka
|
||||
static_configs:
|
||||
- targets: ["exporter:8888"]
|
||||
Reference in New Issue
Block a user