Search code examples
pythonopencvvideo-processingface-recognitionwebcam-capture

The cv2.imshow not showing webcam video and not opening any window


I have an issue with showing up the window and cam capture using OpenCV

When i'm run the script, i see that the cam is working, but the window with this cam is not showing anywhere, i've had just the Python icon showed up but it`s even not clickable.

here

setup:

  • macOS Big Sur 11.4
  • python 3.8
  • cv2 4.2.0

I`ve tried to:

  • Add sleep before ret, frame = video_capture.read()
  • Change cv2.waitKey() to 0, 1, and 500
  • Had tested on my notebook cam, and on phone using IriunWebcam
  • Tried different IDEs(Visual code, and PyCharm), and tried to run the script in the terminal
import cv2
import sys
import logging as log
import datetime as dt
from time import sleep

faceCascade = cv2.CascadeClassifier('/Users/***/Documents/GitHub/FaceAuth/haarcascade_frontalface_default.xml')
log.basicConfig(filename='webcam.log', level=log.INFO)

video_capture = cv2.VideoCapture(0)  # 0 for phone cam, 1 for pc cam
anterior = 0

while True:
    if not video_capture.isOpened():
        print('Unable to load camera.')
        sleep(5)
        pass

    # Capture frame-by-frame
    ret, frame = video_capture.read()

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

    faces = faceCascade.detectMultiScale(gray, 1.1, 4)

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

    if anterior != len(faces):
        anterior = len(faces)
        log.info("faces: " + str(len(faces)) + " at " + str(dt.datetime.now()))

    # Display the resulting frame
    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    # Display the resulting frame
    cv2.imshow('Video', frame)

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()

here This is my variables in debug breakpoint, before cv2.imshow('Video', frame)


Solution

  • From @idonthaveaname in the comments section:

    Have you tried upgrading opencv to 4.4.0? Maybe it should work, 4.4.0 is the latest version.