minor adjustments

This commit is contained in:
Piotr Dobrowolski 2020-04-03 21:03:35 +02:00
parent a8ac06189d
commit e88be83958

View File

@ -5,7 +5,8 @@ import os
import time import time
import numpy as np import numpy as np
from prometheus_client import start_http_server, Counter, Gauge 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') cut_count = Counter('cut_count', 'Total number of jebnięcie')
machine_on = Gauge('machine_on', 'Current machine engine status') machine_on = Gauge('machine_on', 'Current machine engine status')
stream_on = Gauge('stream_on', 'Current camera stream status') stream_on = Gauge('stream_on', 'Current camera stream status')
@ -32,7 +33,7 @@ def run(path):
if not cap.isOpened(): if not cap.isOpened():
raise Exception('VideoCapture not opened') raise Exception('VideoCapture not opened')
print('Opened') logging.info('Stream opened')
stream_on.set(1) stream_on.set(1)
spots = [] spots = []
@ -40,6 +41,8 @@ def run(path):
turnedon = False turnedon = False
framenum = 0 framenum = 0
framerate = 30
# Read until video is completed # Read until video is completed
while(cap.isOpened()): while(cap.isOpened()):
# Capture frame-by-frame # Capture frame-by-frame
@ -50,13 +53,13 @@ def run(path):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0) blurred = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(blurred, 240, 255, cv2.THRESH_BINARY)[1] thresh = cv2.threshold(blurred, 235, 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh, cv2.RETR_LIST, cnts = cv2.findContours(thresh, cv2.RETR_LIST,
cv2.CHAIN_APPROX_SIMPLE)[-2] cv2.CHAIN_APPROX_SIMPLE)[-2]
for c in cnts: for c in cnts:
area = cv2.contourArea(c) area = cv2.contourArea(c)
if area > 20 and area < 45: if area > 30 and area < 60:
# compute the center of the contour # compute the center of the contour
M = cv2.moments(c) M = cv2.moments(c)
cX = int(M["m10"] / M["m00"]) cX = int(M["m10"] / M["m00"])
@ -82,24 +85,26 @@ def run(path):
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
for spot in spots: 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']) d = dist(spots[0]['pos'], spot['pos'])
if d < 30 and d > 5: l = spot['lifetime'] / framerate
print('PRYJEBAŁ :___DDD %.2f' % (spot['lifetime'] / 30.0,)) print(l, d)
if d < 30 and d > 5 and l > 1.0 and l < 8.0:
logging.info('PRYJEBAŁ :___DDD %.2f' % (l,))
cut_count.inc() cut_count.inc()
if len(spots) and spots[0]['lifetime'] > 50: if len(spots) and spots[0]['lifetime'] > framerate * 5:
if not turnedon: if not turnedon:
print('just turned on') logging.info('just turned on')
machine_on.set(1) machine_on.set(1)
turnedon = True turnedon = True
else: else:
if turnedon: if turnedon:
print('just turned off') logging.info('just turned off')
machine_on.set(0) machine_on.set(0)
turnedon = False turnedon = False
spots = [spot for spot in spots if spot['lastseen'] > framenum - 50] spots = [spot for spot in spots if spot['lastseen'] > framenum - framerate]
spots.sort(key=lambda v: v['lastseen'], reverse=True) spots.sort(key=lambda v: v['lastseen'], reverse=True)
framenum += 1 framenum += 1
@ -122,6 +127,6 @@ while True:
try: try:
run(sys.argv[1]) run(sys.argv[1])
except Exception as exc: except Exception as exc:
print(exc) logging.exception('Failed?')
stream_on.set(0) stream_on.set(0)
time.sleep(5.0) time.sleep(5.0)