Getting this black window instead of picture while using cv2.imshow - python-3.x

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.
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.

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

cv2.setMouseCallback() is not working in Colab

Below is my code:
import cv2
import numpy as np
from google.colab.patches import cv2_imshow
prevX,prevY=-1,-1
def printCoordinate(event, x, y, flags, params):
global prevX,prevY
if event==cv2.EVENT_LBUTTONDOWN:
cv2.circle(img,(x,y),3,(255,255,255),-1)
strXY='('+str(x)+','+str(y)+')'
font=cv2.FONT_HERSHEY_PLAIN
cv2.putText(img,strXY,(x+10,y-10),font,1,(255,255,255))
if prevX==-1 and prevY==-1:
prevX,prevY=x,y
else:
cv2.line(img,(prevX,prevY),(x,y),(0,0,255),5)
prevX,prevY=-1,-1
cv2_imshow(img)
img = np.zeros((800,800,3),dtype=np.uint8)
cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
cv2_imshow(img)
cv2.setMouseCallback('image',printCoordinate)
cv2.waitKey()
cv2.destroyAllWindows()
I am trying to draw line or polygon with mouse clicks on an image and need to coordinates of the points of line and polygon.
But the code crashes when it reaches line "cv2.setMouseCallback('image',printCoordinate)". Can anyone please guide me how can I fix it on colab?
PS: On my local PC the code works fine but it does not work on Colab.
I have replaced the cv2.imshow to cv2_imshow as .imshow is not supported by colab.

Argument error when compiling .exe from Python using PyInstaller

I am trying to write a screen recorder program in python. My code runs normally in the compiler. But when I convert it to .exe, it raises this error:
[ERROR:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap.cpp (415) cv::VideoWriter::open VIDEOIO(CV_IMAGES): raised OpenCV exception:OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\videoio\src\cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): project.avi in function 'cv::icvExtractPattern'
I used pyinstaller to convert to .exe.
This is my code:
from tkinter import*
from tkinter import messagebox as msj
from PIL import ImageTk, Image
from PIL import ImageGrab
import os
import time
import cv2
import numpy as np
import glob
recording=False
i = 0
size = 100, 100
mainWindow=Tk()
mainWindow.title("ScreenRecorder")
mainWindow.geometry("200x200")
scriptDirectory = (os.path.dirname(os.path.realpath(__file__)))
def convert(imageCount):
img_array = []
for ip in range(1,imageCount):
x="snap"+str(ip)+".jpg"
for filename in glob.glob(x):
img = cv2.imread(filename)
height, width, layers = img.shape
size = (width,height)
img_array.append(img)
out = cv2.VideoWriter('project.avi',cv2.VideoWriter_fourcc(*'DIVX'), 9, size)
for iz in range(len(img_array)):
out.write(img_array[iz])
out.release()
for a in range(1,imageCount+1):
os.remove("snap"+str(a)+".jpg")
def record():
global i
print(recording)
if(recording==True):
i+=1
fileName= ("snap"+str(i))
#time.sleep(0.00005)
image = ImageGrab.grab()
name=fileName+".jpg"
image.save(name,'JPEG')
imgX = (Image.open("snap"+str(i)+".jpg"))
imgX= imgX.resize(size, Image.ANTIALIAS)
imgX=ImageTk.PhotoImage(imgX)
mainWindow.after(1, record)
def startButton():
global recording
print("ehe")
recording=True
record()
def stopButton():
global recording
recording=False
record()
convert(i)
startButton=Button(text="Start",command=startButton)
startButton.pack()
stopButton=Button(text="Stop",command=stopButton)
stopButton.pack()
mainWindow.after(1, record)
mainWindow.mainloop()
I can just advise you to use another method, i think it's more simple, try to use 'auto py to exe'.This is a module that can be installed from the net or from the pip installer.
see from here.This the only way that i use for my codes.
Secondly, iknow that if the program is not opened using the format of .py will never be opened at .exe
hope i helped you.

In opencv imshow() function does not open the new window and display the image in jupyter notebook

import cv2
cap = cv2.VideoCapture(0)
status , photo = cap.read()
cv2.imwrite('Surendar.png',photo)
cap.release()
cv2.imshow('image', photo)
cv2.waitKey(5000)
cv2.destroyAllWindows()
I interpreted this code in my jupyter notebook. It just complies but does not show the new window of picture.
Try changing cv2.waitKey(5000) to cv2.waitKey(0) so the window will stay up until the user closes it. It looks like the window was waiting for 5000 milliseconds before it would destroy the window.
EDIT
Instead of using cv2 to display your image, try using matplot instead
import cv2
from matplotlib import pyplot as plt
cap = cv2.VideoCapture(0)
status , photo = cap.read()
cv2.imwrite('Surendar.png',photo)
cap.release()
plt.imshow(photo)
plt.show()

Live Screen Monitoring With Python3 Pytesseract

I am working on a python3 project on windows 10, and I was wondering if anyone knew of anyway to pass an opencv screen grab through pytesseract? If not, is there any other OCR that you could?
Here is the code for the opencv screen grab:
import numpy as np
from PIL import ImageGrab
import cv2
while True:
screen = np.array(ImageGrab.grab(bbox=(0,40,800,640)))
cv2.imshow('window', cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
I know very little about pytesseract, but this might get you started:
#!/usr/bin/env python3
import numpy as np
from PIL import ImageGrab
import pytesseract
from PIL import Image, ImageEnhance, ImageFilter
from textblob import TextBlob
# Grab some screen
screen = ImageGrab.grab(bbox=(0,0,800,640))
# Make greyscale
w = screen.convert('L')
# Save so we can see what we grabbed
w.save('grabbed.png')
text = pytesseract.image_to_string(w)
correctedText = TextBlob(text).correct()
print(correctedText)
From this grab:
I got:
# Terminal Shell Edit View Window Help
The writing is on the wall

Show resized image with cv2

Windows 10
I reduced the code to the very core and the problem still exists.
import cv2
img = cv2.imread('imm.jpg')
cv2.imshow('image',img)
cv2.waitKey()
In this way it works as it should: it shows an image with its resolution (4k). But if I resize it in this way:
import cv2
img = cv2.imread('imm.jpg')
res = cv2.resize(img, (160,90))
cv2.imshow('image',res)
cv2.waitKey()
it creates in the toolbar a new window, whose name's image, as it should but I cannot focus it and see the result.
What's the problem?

Resources