Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

get image from cam stream with python and opencv

trying to safe a simple image from a stream and save it but the apiPreference cant be found on line 7. already tried several things and read the openCV information on the writer function but i cant find anything on the apiPreferences.

import cv2
import numpy as np
# Open the device at the ID 0

cap = cv2.VideoCapture(1)
#fcc = cv2.VideoWriter_foucc(*"JPG")
out = cv2.VideoWriter("newImage.jpg", fourcc=0, fps=0)#line7

#Check whether user selected camera is opened successfully.

if not (cap.isOpened()):
    print("Could not open video device")

#To set the resolution

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)

cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

while(True):
    # Returns true and capture frame-by-frame and
    ret, frame = cap.read()
    snapShot = frame
    # Display the resulting frame
    cv2.imshow('preview', frame)
    
    out.write(snapShot)
    #Waits for a user input to quit the applicatio or save an image
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture

cap.release()

cv2.destroyAllWindows()

just to get the fault:

Traceback (most recent call last):
 File"home/pi/Dokuments/cameraStream.py", line 7, in <module>
 out = cv2.VideoWriter("newImage.jpg", fourcc=0, fps=0)
TypeError: Required argument 'apiPreference' (pos 2) not found

any idea on how to fix that would be great as im running out of ideas.

question from:https://stackoverflow.com/questions/65849618/get-image-from-cam-stream-with-python-and-opencv

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

1 Answer

0 votes
by (71.8m points)

Let me start with the problems in your code.

    1. out = cv2.VideoWriter("newImage.jpg, fourcc=0, fps=0)#line7`
      

You initialize the filename variable to the image format ("newImage.jpg").

This is wrong. You need to initialize the filename by choosing one of the video-formats i.e. .avi, .mp4 ..

You initialize fourcc to 0. I can't say it is wrong, but I can give a suggestion for a better initialization.

fourcc = cv2.VideoWriter_fourcc(*"mp4v")

As you can see above, I initialized my fourcc parameter for mp4 video files.

You initialized fps to 0. Why? fps means frames-per-second meaning how many frames will you display in one second. My suggestion is set to 20. Of course, you can choose any other positive-integer.

  • Now, what will be the dimension of your output video?

You have to set while declaring the VideoWriter. If you don't know and you would like to set the same dimension with the input video. You could do:

ret, frame = cap.read()
(h, w) = frame.shape[:2]
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
out = cv2.VideoWriter("newVid.mp4", fourcc, 20, (w, h))

but, it seems you would like to set your dimensions to the (640, 480)

fourcc = cv2.VideoWriter_fourcc(*"mp4v")
out = cv2.VideoWriter("newVid.mp4", fourcc, 20, (640, 480))

One last thing is, specifying whether the output video is color or not.

out = cv2.VideoWriter("newVid.mp4", fourcc, 20, (w, h), isColor=True)
    1. if not (cap.isOpened()):
          print("Could not open video device")
      

You are checking whether the video will be opened or not. But what happens if the video fails to open? According to the current logic it will continue. We should put an else statement.

if not cap.isOpened():
    print("Could not open video device")
else
    .
    .
    1. while(True):
          ret, frame = cap.read()
          snapShot = frame
      

Now what happens if ret is False? Application will throws an exception. Therefore you should do:

while(True):
    ret, frame = cap.read()
    if ret:
        snapShot = frame

Now why did you set snapShot variable to the frame?

You can directly write the input frames' copy. Like:

while True:
    ret, frame = cap.read()
    if ret:
        cv2.imshow("preview", frame)
        out.write(frame.copy())

One last thing, when you finish make sure to relase VideoWriter object too.

out.release()
cap.release()

Code:


import cv2

cap = cv2.VideoCapture(0)
ret, frame = cap.read()
(h, w) = frame.shape[:2]
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
out = cv2.VideoWriter("newVid.mp4", fourcc, 20, (640, 480), isColor=True)
if not cap.isOpened():
    print("Could not open video device")
else:
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
    while True:
        ret, frame = cap.read()
        if ret:
            cv2.imshow("preview", frame)
            out.write(frame.copy())
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

out.release()
cap.release()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...