Cv2 camera error picture is only when black when camera is activated

Viewed 46

When I run my code, it outputs a completely black image, rather than the expected video frame.

import cv2

videoCaptureObject = cv2.VideoCapture(0)
result = True
while(result):
    ret,frame = videoCaptureObject.read()
    cv2.imwrite("NewPicture.jpg",frame)
    result = True
videoCaptureObject.release()
cv2.destroyAllWindows()
1 Answers

First off, you have an infinite loop there? Also, it is a better idea to use imshow instead of imwrite. Here is my alternative video capture read:

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    
    frame = cv2.resize(frame, (0,0), fx=0.5, fy=0.5)
    cv2.imshow('Frame', frame)
    
    ch = cv2.waitKey(1)
    
    if ch == ord('q'):
        break
        
cap.release()
cv2.destroyAllWindows()   

Here what ch == ord('q') means the interface will end when you press the key q.

Related