From ee8313e0ea90c4a16ce8440f025f58f38e9c2c74 Mon Sep 17 00:00:00 2001 From: Pavol Debnar Date: Sun, 5 Mar 2023 20:55:07 +0100 Subject: [PATCH] updated cam client to save images with highest variance across 5 consecutive frames --- src/cam.py | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/cam.py b/src/cam.py index f3fff94..53dbde1 100644 --- a/src/cam.py +++ b/src/cam.py @@ -2,6 +2,12 @@ import cv2 import numpy as np import os + +def variance_of_laplacian(image): + return cv2.Laplacian(image, cv2.CV_64F).var() + + + os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"]="rtsp_transport;tcp" gstreamerstr = "tcpclientsrc host=192.168.0.144 port=5000 ! jpegdec ! videoconvert ! appsink" @@ -9,16 +15,46 @@ gstreamerstr = "tcpclientsrc host=192.168.0.144 port=5000 ! jpegdec ! videoconve #if not capture.isOpened() : print("CANNOT OPEN STREAM") - +capture = cv2.VideoCapture("vid.mp4") +keypressNo = 1 while(True): - capture = cv2.VideoCapture(gstreamerstr,cv2.CAP_GSTREAMER) + #capture = cv2.VideoCapture(gstreamerstr,cv2.CAP_GSTREAMER) + ret, frame = capture.read() if not ret: print('fail') break - cv2.imshow('frame',frame) - cv2.waitKey(1) + frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + variance = variance_of_laplacian(frame) + + cv2.putText(frame, "{}: {:.2f}".format("Variance:", variance), (10, 30), + cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3) + cv2.imshow('frame',frame) + #cv2.waitKey(0) + + if cv2.waitKey(70) == ord('v'): + frames=[] + maxVarIdx = 0 + maxVar = 0 + for i in range(5): + ret, frame = capture.read() + frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + frames.append(frame) + var = variance_of_laplacian(frame) + if var > maxVar: + maxVar = var + maxVarIdx = i + cv2.waitKey(20) + #test to see if the image with the highest variance gets picked + #cv2.putText(frame, "{}: {:.2f}".format("Variance:", var), (10, 30), + #cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3) + #writeString="imgtest"+str(keypressNo)+str(i)+".jpg" + #cv2.imwrite(writeString, frames[i]) + writeString="img"+str(keypressNo)+".jpg" + cv2.imwrite(writeString, frames[maxVarIdx]) + keypressNo=keypressNo+1 + capture.release() cv2.destroyAllWindows()