简体   繁体   中英

OpenCV does not find VideoCapture for webcam or show on Windows 10

Webcam capture does not show when the data is read

I tried video_capture= cv2.VideoCapture() method for both, 0 and 700 as argument, because of suggestions and this happened:

  • 0 gives out the error; CvCapture_MSMF::grabFrame videoio(MSMF): can't grab frame.
  • 700 gives out a blank screen only first row of pixels are filled;
  • video_capture = cv2.VideoCapture(700) works only when I debug and breakpoint at cv2.imshow("Capturing", gray). Screen shows a snapshot of a webcam video, however runtime shows the following image;
  • Video screen
  • I have tried on my mac with the same python file, it worked perfectly fine. There is something wrong with Windows 10 that I can't figure out.

=======

import cv2

# Create an object. 700 for my external camera
video_capture = cv2.VideoCapture(700)
video_capture.set(cv2.CAP_PROP_FRAME_WIDTH,640)
video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT,480)

faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')


while True:

    ret, frame = video_capture.read()

    #Check if the video is being read
    if ret == False:
        print("Connection Failed!")
    else:

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        faces = faceCascade.detectMultiScale(
            gray,
            scaleFactor=1.1,
            minNeighbors=5,
            minSize=(30, 30),
            flags=cv2.CASCADE_SCALE_IMAGE
        )

        # Draw a rectangle around the faces
        for (x, y, w, h) in faces:
            cv2.rectangle(gray, (x, y), (x+w, y+h), (0, 255, 0), 2)

        # Display the resulting frame
        cv2.imshow('Video', gray)
        
        if cv2.waitKey(1)==ord('e'):
            break

video_capture.release()
cv2.destroyAllWindows()

Kaspersky seemed to block my live webcam capture on terminal running code so on pycharm I have enabled, Run with Python Console. Everything is working fine now. Pycharm Console Config

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM