How do I fix this cv2.imshow() syntax error? - python

I cannot get the following code to run:
import cv2 # import Open Cv moudle
import numpy as np
import face_recognition
import matplotlib as plt
imgEloun=face_recognition.load_image_file ("image/elounMask.jpg") # load Image
imgEloun=cv2.cvtColor(imgEloun,cv2.COLOR_BGR2RGB) # changing BGR clr to RGB
imgTest=face_recognition.load_image_file ("image/steve jobs.jpg") # Load Image
imgTest=cv2.cvtColor(imgTest,cv2.COLOR_BGR2RGB) # changing BGR clr to RGB
faceLocation=face_recognition.face_locations(imgEloun)[0] #Detect face loaction in image
encodeELOUN=face_recognition.face_encodings(imgEloun)[0] #encoding face location
cv2.rectangle(imgEloun,(faceLocation[3],(faceLocation[0]),(faceLocation[1],(faceLocation[2]),(255,0,255))
cv2.imshow('elounMask',imgEloun) ######## here is error
cv2.imshow('steve jobs',imgTest) # Show Image as output
cv2.waitKey(0)
Here is a Screenshot of my error in PyCharm.
Is anyone able to help out?

cv2.rectangle(imgEloun,((faceLocation[3],(faceLocation[0])),((faceLocation[1],(faceLocation[2])),(255,0,255))
you missed the parenthesis when giving the coordinates for rectangle. It should be in this format,
eg: cv2.rectangle(image, (5,5), (200,200), (255,255,0), 2)

Related

cv2 in Python crashes when I try to open a 4 level CMYK numpy array

import numpy as np
from PIL import Image
import cv2
with Image.open("image.png") as im:
im = im.convert("CMYK")# not a true CMYK conversion here
im.show(title="image")
img = np.array(im)
#cv2.imshow('image', img)
I need to view a CMYK file hopefully using OpenCV and read pixel values in the CMYK space. I tried to load an image, convert it to CMYK(just 4 color levels) and view it using cv2. Note, I have cv2* commented out because it will cause Python to crash and OpenCv will need to be reinstalled. Will OpenCv allow me to view a (x, x, 0:3).uint8 numpy array? If so, throw me a line.
My solution was simple. I forgot the following:
cv2.waitKey(0)
cv2.destroyAllWindows()

OpenCV Retinaface images corrdinate

How to get the detected faces image coordinates which are detected. Can't find relevant info or documentation. any pointers?
My code is simple and detects faces but i need to store the coordinates for and crop the image and replace it with another image.
Code:
from matplotlib import pyplot as plt
from retinaface.pre_trained_models import get_model
from retinaface.utils import vis_annotations
image = cv2.imread("crowd.jpg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
model = get_model("resnet50_2020-07-20", max_size=2048)
model.eval()
After this i can't figure out how to store this X,Y coordinates values
plt.imshow(vis_annotations(image, annotation))

Displaying Hole Filled image in python with OpenCV

I am working on some image analysis in python using OpenCV. I want to display an image that I filled in holes with using scipy.ndimage.binary_filled_holes. Upon doing this I could not see anything being displayed when I used cv2.imshow, so I used plt.imshow and saw that the holes in my original image were filled. I want to use the cv2.imshow function to display the image. I did convert the image so that the datatype is uint8, yet still, nothing shows up. Any help would be appreciated.
import cv2
import matplotlib.pyplot as plt
import numpy as np
import scipy.ndimage
img = cv2.imread('Funky 647.jpg', cv2.IMREAD_GRAYSCALE)
dst = cv2.fastNlMeansDenoising(img,None,10,7,21)
ret, thresh2 = cv2.threshold(dst, 40, 255, cv2.THRESH_BINARY)
hole_filled= np.uint8(scipy.ndimage.binary_fill_holes(thresh2))
# plt.imshow(hole_filled)
cv2.imshow('No Holes', hole_filled)
cv2.waitKey(0)
cv2.destroyAllWindows()
Hole Filled Image via matplotlib:

OpenCV not showing image in python

This is the code that I am using for OpenCV to display image. It only shows me a blank screen instead of showing a picture.
import cv2
# location and name of file is completely correct
img = cv2.imread("./Resources/img-2.jpg")
# Doesn't give a null so its okay
print(img.shape)
# suspecting that problem is here
cv2.imshow("preview", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
The image is stored in the right location and when I'm using a similar approach for a video and a webcam, it works perfectly.
The following is what the out is -
Try using matplotlib instead :
import matplotlib.pyplot as plt
import cv2
img = cv2.imread("./Resources/img-2.jpg")
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # convert img pixels to RGB format, so that matplotlib displays the image properly
plt.imshow(img)
plt.show()
If it still gives you a blank image, then the problem might come from your file or filename.

Python-tesseract does not recognize anything

This is the image that I will import
My python code
try:
import Image
except ImportError:
from PIL import Image
import pytesseract
print(pytesseract.image_to_string(Image.open('/home/milenko/Pictures/Screenshot from 2018-03-06 19-03-19.png')))
When I run code
python a72.py
As an output I got empty line.It does not make any sense.
Why?
Try to tweak your command a little bit using e.g.: other Page Segmentation Method As you can see the default value is "Fully automatic page segmentation, but no OSD." so it does not perform orientation and script detection (OSD).
This one gives me some output:
print(pytesseract.image_to_string(Image.open('image.png'), config='-psm 12'))
You can use OpenCV to prepare this image for OCR, e.g:
#!/usr/bin/python
import cv2 as cv
import numpy as np
import pytesseract
import Image
from matplotlib import pyplot as plt
img = cv.imread('/tmp/image.png',0)
ret,thresh = cv.threshold(img, 220, 255, cv.THRESH_BINARY)
plt.axis('off')
plt.imshow(thresh, 'gray')
plt.show()
print(pytesseract.image_to_string(thresh, config='-psm 12'))
In the next step you could divide this image into some parts (x-axis, y-axis, trend line) and use OCR for each part separately with the proper PSM value set for each one of them.

Categories

Resources