I essentially have a motion detector that works using my webcam and it takes the first frame as "dart_0" and then when motion is detected is takes a frame as "dart_1".
I want to be able to have these images saved so I can immediately post process them in the same python file/script.
Currently it saves them as a .png file, but following the while loop, it won't let me read the newly created files. It only lets me do this in a separate script which is not what I want.
What I am asking is if there is a way that when the while loop is broken, I can have the images kind of saved locally in the script which I can immediately work upon after they have been obtained.
This is my code so far which works perfectly as I want it, but I cannot use the images I have just created, following the break of the while loop.
import cv2
first_frame = None
video = cv2.VideoCapture(0, cv2.CAP_DSHOW)
video.set(cv2.CAP_PROP_BUFFERSIZE, 0)
img_counter = 1
throw_count = 1
throw_limit = 5
while throw_count < throw_limit:
status, frame = video.read()
#print(status)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
if first_frame is None:
first_frame = gray
cv2.imwrite(filename='dart_0.png', img=frame)
print("Empty Picture Captured")
continue
#cv2.imshow("Frame", frame)
#cv2.imshow("Capturing", gray)
delta_frame = cv2.absdiff(first_frame, gray)
thresh_delta = cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]
thresh_delta = cv2.dilate(thresh_delta, None, iterations=0)
contours, res = cv2.findContours(thresh_delta.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#cv2.imshow("Delta", delta_frame)
#cv2.imshow("Thresh", thresh_delta)
for contour in contours:
if cv2.contourArea(contour) > 100:
#(x, y, w, h) = cv2.boundingRect(contour)
#cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)
throw_count += 1
cv2.imwrite(filename='dart_1.png', img=frame)
print("Image Saved")
print("Loop Ended")