updated cam client to save images with highest variance across 5 consecutive frames

main
Pavol Debnar 2 years ago
parent a1ee1b9e8c
commit ee8313e0ea

@ -2,6 +2,12 @@ import cv2
import numpy as np import numpy as np
import os import os
def variance_of_laplacian(image):
return cv2.Laplacian(image, cv2.CV_64F).var()
os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"]="rtsp_transport;tcp" os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"]="rtsp_transport;tcp"
gstreamerstr = "tcpclientsrc host=192.168.0.144 port=5000 ! jpegdec ! videoconvert ! appsink" gstreamerstr = "tcpclientsrc host=192.168.0.144 port=5000 ! jpegdec ! videoconvert ! appsink"
@ -9,15 +15,45 @@ gstreamerstr = "tcpclientsrc host=192.168.0.144 port=5000 ! jpegdec ! videoconve
#if not capture.isOpened() : print("CANNOT OPEN STREAM") #if not capture.isOpened() : print("CANNOT OPEN STREAM")
capture = cv2.VideoCapture("vid.mp4")
keypressNo = 1
while(True): while(True):
capture = cv2.VideoCapture(gstreamerstr,cv2.CAP_GSTREAMER) #capture = cv2.VideoCapture(gstreamerstr,cv2.CAP_GSTREAMER)
ret, frame = capture.read() ret, frame = capture.read()
if not ret: if not ret:
print('fail') print('fail')
break break
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.imshow('frame',frame)
cv2.waitKey(1) #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() capture.release()

Loading…
Cancel
Save