From 82c49b3ca09ce0c6ff981f506cd5a117a757554d Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Tue, 9 Aug 2022 11:04:49 -0700 Subject: [PATCH] a bit of work to try to detect blinking leds... I'm temporarily abandoning this because the issue is that the camera isn't a great sensor. Issues w/ long shutter speed (blurs sampling period), and sampling frequency makes this difficult. I cannot find api information about how to adjust the shutter speed in app, though there does appear to be able to do this via other apps... --- blinkled/sampledata/info.txt | 13 ++++ blinkled/sampledata/t.py | 126 +++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 blinkled/sampledata/info.txt create mode 100644 blinkled/sampledata/t.py diff --git a/blinkled/sampledata/info.txt b/blinkled/sampledata/info.txt new file mode 100644 index 0000000..3b61687 --- /dev/null +++ b/blinkled/sampledata/info.txt @@ -0,0 +1,13 @@ +https://stackoverflow.com/questions/8877228/opencv-detect-blinking-lights + +ffmpeg -i PXL_20220806_231420638.mp4 -filter:v "crop=200:200:725:275" output.mp4 + +PXL_20220806_231420638.mp4 725x275 200x200 +PXL_20220806_231644224.mp4 790x380 200x200 + +Mon Aug 8 17:24:55 PDT 2022 + +PXL_20220808_233318199.mp4 + +captures w/ a delay of 35ms instead of previous 34ms (for 4224.mp4) +also w/ a smaller resistor, 68Ω instead of 100Ω. diff --git a/blinkled/sampledata/t.py b/blinkled/sampledata/t.py new file mode 100644 index 0000000..783d2cf --- /dev/null +++ b/blinkled/sampledata/t.py @@ -0,0 +1,126 @@ +import cv2 +import numpy as np +import sys + +winname = 'Diff' + +def tovalues(values, histo): + return [ 0 if x < histo[1][1] else 2 if x > histo[1][2] else 1 for x in values ] + +def frameiter(fname): + #vc = cv2.VideoCapture('output.mp4') + #vc = cv2.VideoCapture('PXL_20220806_231420638.mp4') + #vc = cv2.VideoCapture('PXL_20220806_231644224.mp4') + #vc = cv2.VideoCapture('PXL_20220808_233318199.mp4') + vc = cv2.VideoCapture('PXL_20220809_162724414.mp4') + + while True: + r, frame = vc.read() + if not r: + break + + # base image processing + frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY, 1) + + yield frame + +def pixtot(frame): + colsum = np.sum(frame, 0, np.float64) + colsum = np.sum(colsum, 0, np.float64) + + return colsum + +def numpixels(frame): + return pixtot(frame) / 255 + +sumhist = [] +cont = False +#cont = True +key = 0 + +valuehist = [] + +basefr = None +for frame in frameiter('output.mp4'): + if basefr is None: + basefr = frame + continue + + frdiff = cv2.absdiff(basefr, frame) + + #hist = np.histogram(frdiff, bins=5) + #print(repr(hist)) + #frdiff = frame - basefr + + #frdiffbin = cv2.threshold(frdiff, 50, 100, cv2.THRESH_BINARY) + frdiffbin = cv2.compare(frdiff, 50, cv2.CMP_GE) + kernel = np.ones((4,4), np.uint8) + frdiffbin = cv2.erode(frdiffbin, kernel, iterations = 2) + + numpix = numpixels(frdiffbin) + print('np:', repr(numpix)) + + if numpix: + # detected change + # get average pixel value + selected = cv2.bitwise_and(frame, frdiffbin) + + avg = pixtot(selected) / numpix + print('avg:', avg) + + valuehist.append(avg) + + cv2.imshow('selected', selected) + cv2.moveWindow('Base Frame', 1200, 300) + + if len(valuehist) > 6: + print(repr(sorted(valuehist))) + hist = np.histogram(valuehist, bins=3) + print('hist:', repr(hist)) + + #print(repr(frdiffbin)) + #moments = cv2.moments(frdiffbin) + #((x, y), radius) = cv2.minEnclosingCircle(conts[0][0]) + #x = int(x) + #y = int(y) + #radius = int(radius) + #circ = ((x, y), radius) + + #white = (255,) + + #print(repr(moments)) + #frdiff = cv2.circle(frdiff, circ[0], circ[1], white) + cv2.imshow('Base Frame', basefr) + cv2.moveWindow('Base Frame', 300, 300) + cv2.imshow('Diff', frdiffbin) + cv2.moveWindow('Diff', 600, 300) + cv2.imshow('Next Frame', frame) + cv2.moveWindow('Next Frame', 900, 300) + + # sum up the diffs + colsum = np.sum(frdiff, 0, np.float64) + colsum = np.sum(colsum, 0, np.float64) + colsum = np.sum(colsum, 0, np.float64) + sumhist.append(colsum) + print(repr(colsum)) + + if not cont: + key = cv2.waitKey(0) + + if key == ord('q'): + break + elif key == ord('c'): + cont = True + + basefr = frame + +print(len(sumhist)) +hist = np.histogram(sumhist, bins=5) +print(repr(hist)) +print(len(hist)) +print(len(hist[1])) + +print(repr(valuehist)) +hist = np.histogram(valuehist, bins=3) +print(repr(hist)) +print(repr(tovalues(valuehist, hist)))