Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

python - How to use an image(s)/frame(s) captured from a webcam to post process in the same script

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")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If the last image you worked with in your loop is the one you want to keep for further analysis, save it in a variable (selected_frame), for example before saving it to disk:

for contour in contours:
    if cv2.contourArea(contour) > 100:
        throw_count += 1
        selected_frame = frame.copy()
        cv2.imwrite(filename='dart_1.png', img=frame)
        print("Image Saved")

The first frame is already saved in the variable first_frame and ready to use in subsequent code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...