Getting this black window instead of picture while using cv2.imshow

Viewed 691

I don't know what term I should use for the window I am getting so I am attaching a screenshot of the window for reference.

The ScreenShot of the window I m getting

I am getting this window about 7 of the 10 times I am running this code:

import cv2
import numpy as np
import face_recognition


imgElon = face_recognition.load_image_file("BasicFaceRecImg/ElonMusk2.jpg")
imgElon = cv2.cvtColor(imgElon, cv2.COLOR_BGR2RGB)
imgElon_face_loc = face_recognition.face_locations(imgElon)[0]
print(imgElon_face_loc)

imgElon_encode = face_recognition.face_encodings(imgElon)[0]
cv2.rectangle(imgElon, (imgElon_face_loc[0], imgElon_face_loc[3]), (imgElon_face_loc[1], imgElon_face_loc[2]),(255, 0, 255), 2)
cv2.imshow('Elon Musk', imgElon)
cv2.waitKey(0)

And the funny thing is that I am not getting this problem every time. It runs perfectly sometimes.

3 Answers

I installed opencv-python-4.2.0.32 and the problem seems to have went away. I was using version 4.3.0.36 when having the issues.

This will help. You need to resize the image sometime the OpenCV messes with an image format this will work in all OpenCV versions hope this helps

import cv2
import numpy as np
import face_recognition

# cv2.namedWindow('image', cv2.WINDOW_NORMAL)
imgElon = face_recognition.load_image_file("elon.jpg")
print(type(imgElon.shape))
a,b,c=imgElon.shape


imgElon = cv2.cvtColor(imgElon, cv2.COLOR_BGR2RGB)
imgElon_face_loc = face_recognition.face_locations(imgElon)[0]
print(imgElon_face_loc)

imgElon_encode = face_recognition.face_encodings(imgElon)[0]
cv2.rectangle(imgElon, (imgElon_face_loc[0], imgElon_face_loc[3]), (imgElon_face_loc[1], imgElon_face_loc[2]),(255, 0, 255), 2)


imgElon=cv2.resize(imgElon, (a, b))

cv2.imshow('image', imgElon)
cv2.waitKey(0)
cv2.destroyAllWindows()

I had the same problem, and was able to solve it using this code:

image = cv2.imread("./faces/test.jpg", cv2.IMREAD_COLOR)
Related