sztancarka_exporter
This commit is contained in:
commit
ae72627756
4
.dockerignore
Normal file
4
.dockerignore
Normal file
@ -0,0 +1,4 @@
|
||||
*.mp4
|
||||
.ropeproject
|
||||
*.pyc
|
||||
*.pyo
|
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
.ropeproject
|
||||
*.mp4
|
4
Dockerfile
Normal file
4
Dockerfile
Normal file
@ -0,0 +1,4 @@
|
||||
FROM debian:buster-slim
|
||||
RUN apt-get update && apt-get -y --no-install-recommends install python3 python3-opencv
|
||||
RUN apt-get -y --no-install-recommends install python3-prometheus-client
|
||||
ADD process.py /usr/src
|
123
process.py
Normal file
123
process.py
Normal file
@ -0,0 +1,123 @@
|
||||
import cv2
|
||||
import math
|
||||
import sys
|
||||
import os
|
||||
import time
|
||||
import numpy as np
|
||||
from prometheus_client import start_http_server, Counter, Gauge
|
||||
|
||||
cut_count = Counter('cut_count', 'Total number of jebnięcie')
|
||||
machine_on = Gauge('machine_on', 'Current machine engine status')
|
||||
|
||||
start_http_server(8888)
|
||||
|
||||
def dist(pos1, pos2):
|
||||
(x1, y1) = pos1
|
||||
(x2, y2) = pos2
|
||||
return math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
|
||||
|
||||
|
||||
def run(path):
|
||||
gui = 'DISPLAY' in os.environ
|
||||
|
||||
machine_on.set(0)
|
||||
|
||||
# Create a VideoCapture object and read from input file
|
||||
# If the input is the camera, pass 0 instead of the video file name
|
||||
cap = cv2.VideoCapture(path)
|
||||
|
||||
# Check if camera opened successfully
|
||||
if not cap.isOpened():
|
||||
raise Exception('VideoCapture not opened')
|
||||
|
||||
print('Opened')
|
||||
|
||||
spots = []
|
||||
|
||||
turnedon = False
|
||||
|
||||
framenum = 0
|
||||
# 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]
|
||||
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:
|
||||
# compute the center of the contour
|
||||
M = cv2.moments(c)
|
||||
cX = int(M["m10"] / M["m00"])
|
||||
cY = int(M["m01"] / M["m00"])
|
||||
|
||||
for spot in spots:
|
||||
if dist(spot['pos'], (cX, cY)) < 10:
|
||||
spot['pos'] = (cX, cY)
|
||||
spot['lastseen'] = framenum
|
||||
spot['lifetime'] += 1
|
||||
found = True
|
||||
break
|
||||
else:
|
||||
spots.append({
|
||||
'pos': (cX, cY),
|
||||
'lastseen': framenum,
|
||||
'lifetime': 1,
|
||||
'firstseen': framenum,
|
||||
})
|
||||
|
||||
if gui:
|
||||
cv2.putText(thresh, str(area), (cX - 20, cY - 20),
|
||||
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
|
||||
|
||||
for spot in spots:
|
||||
if spot['lastseen'] <= framenum - 50 and turnedon:
|
||||
d = dist(spots[0]['pos'], spot['pos'])
|
||||
if d < 30 and d > 5:
|
||||
print('PRYJEBAŁ :___DDD %.2f' % (spot['lifetime'] / 30.0,))
|
||||
cut_count.inc()
|
||||
|
||||
if len(spots) and spots[0]['lifetime'] > 50:
|
||||
if not turnedon:
|
||||
print('just turned on')
|
||||
machine_on.set(1)
|
||||
turnedon = True
|
||||
else:
|
||||
if turnedon:
|
||||
print('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)
|
||||
framenum += 1
|
||||
|
||||
if gui:
|
||||
cv2.imshow('frame',frame)
|
||||
cv2.imshow('thresh',thresh)
|
||||
|
||||
# Press Q on keyboard to exit
|
||||
if cv2.waitKey(25) & 0xFF == ord('q'):
|
||||
break
|
||||
|
||||
# Break the loop
|
||||
else:
|
||||
break
|
||||
|
||||
# When everything done, release the video capture object
|
||||
cap.release()
|
||||
|
||||
while True:
|
||||
try:
|
||||
run(sys.argv[1])
|
||||
except Exception as exc:
|
||||
print(exc)
|
||||
time.sleep(5.0)
|
Loading…
x
Reference in New Issue
Block a user