Python - CV2 strange behavior on Spyder while processing images - linux

I am working on YOLO network for object detection. I am getting unexpected result while working on images. My findings are:
While drawing rectangle on an image using cv2.rectangle() method, it changes the image type as NoneType on Spyder IDE, while it remains same (i-e: numpy.ndarray) on Jupyter-notebook.
The code as follows:
img = cv2.imread('1527502132336.jpeg', 1)
result = tfnet.return_predict(img)
tl = (result[0]['topleft']['x'], result[0]['topleft']['y'])
br = (result[0]['bottomright']['x'], result[0]['bottomright']['y'])
label = result[0]['label']
img = cv2.rectangle(img, tl, br, (255, 0, 0), 3)
type(img)
I want to get the same result as returned by Jupyter for further processing. Why I am getting this strange behavior.

Related

OpenCV with Nvidia VPI (Vision Programming Interface), core dumped

I'm playing around with Vision Programming Interface (VPI) and trying to bend images. I came across this Lens Distortion Correction example (https://docs.nvidia.com/vpi/algo_ldc.html) and added some code so it takes an input image and shows the undistorted output image. The following code runs fine and I'm able to view the output image.
I'd like to run it in a loop for a video input. As soon as I uncomment the "videoCapture" line, I get the following error:
"Segmentation fault (core dumped).
Anyone able to help me use this code for video input?
import vpi
import numpy as np
import cv2
import PIL
from PIL import Image
img = cv2.imread('input.jpeg')
#cap = cv2.VideoCapture(0)
vpi_image = vpi.asimage(np.asarray(img))
grid = vpi.WarpGrid((2064,1544))
sensorWidth = 7.12
focallength = 3.5
f = focallength * (2064 / sensorWidth)
K = [[f, 0, 2064/2],
[0, f, 1544/2]]
X = np.eye(3,4)
warp = vpi.WarpMap.fisheye_correction(grid, K=K, X=X,
mapping=vpi.FisheyeMapping.EQUIDISTANT,
coeffs=[-0.01, 0.22])
with vpi.Backend.CUDA:
output = vpi_image.remap(warp, interp=vpi.Interp.CATMULL_ROM, border=vpi.Border.ZERO)
with output.rlock():
output = Image.fromarray(output.cpu()).save('output.jpeg')
pil_image = PIL.Image.open('output.jpeg').convert('RGB')
cv2_image = np.array(pil_image)
cv2_image = cv2_image[:, :, ::-1].copy()
cv2_image = cv2.resize(cv2_image, (920,590))
img = cv2.resize(img, (920, 590))
sbs = cv2.hconcat([img, cv2_image])
cv2.imshow("sbs", sbs)
cv2.waitKey(0)

When pasting a reshaped image, why does it lose its shape?

I'm using Pillow with Python 3. I want to reshape a square image into a circle, then pasting it on another image.
My problem is, that the reshaping is done properly, and the image is made a circle, but when pasting it on the other image, it becomes a square again:
That's my code:
#client.command()
async def test(ctx):
owms = Image.open("bg.jpg")
asset = ctx.author.avatar_url_as(size = 128)
data = BytesIO(await asset.read())
img=Image.open(data).convert("RGB")
npImage=np.array(img)
h,w=img.size
alpha = Image.new('L', img.size,0)
draw = ImageDraw.Draw(alpha)
draw.pieslice([0,0,h,w],0,360,fill=255)
npAlpha=np.array(alpha)
npImage=np.dstack((npImage,npAlpha))
pfp = Image.fromarray(npImage)
pfp.save("outpfp.png")
owms.paste(Image.fromarray(npImage), (101, 67))
owms.save("outlvl.jpg")
await ctx.send(file = discord.File("outpfp.png"))
await ctx.send(file = discord.File("outlvl.jpg"))
(I have made it such that the output is both the circle shaped image and the intended original output.)
The solution I seek is the pasted image to be circle and not square like in the image above.
The only thing left is to properly use the mask parameter in Image.paste. Also, there's no need for this NumPy detour solely for adding the alpha channel. There's Image.putalpha for that. Here's the minimized code (I left out the Discord stuff):
from PIL import Image, ImageDraw
owms = Image.open('bg.jpg')
img = Image.open('get/your/avatar/here').resize((128, 128))
h, w = img.size
alpha = Image.new('L', img.size, 0)
draw = ImageDraw.Draw(alpha)
draw.pieslice([0, 0, h, w], 0, 360, fill=255)
img.putalpha(alpha)
img.save('outpfp.png')
owms.paste(img, (101, 67), mask=img)
owms.save('outlvl.jpg')
I get the following outputs:
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.9.1
PyCharm: 2021.1.1
Pillow: 8.2.0
----------------------------------------

Unable to detect facial landmarks using OpenCV2

I have developed a script using dlib and cv2 to draw facial landmarks on images having one face in that image. Here is the scripts;
import cv2
import dlib
img_path = 'landmarks.png'
detector = dlib.get_frontal_face_detector()
shape_predictor = 'shape_predictor_68_face_landmarks.dat'
predictor = dlib.shape_predictor(shape_predictor)
count = 1
ready = True
while ready:
frame = cv2.imread("demo.jpg")
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
for face in faces:
x1 = face.left()
y1 = face.top()
x2 = face.right()
y2 = face.bottom()
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 3)
landmarks = predictor(gray, face)
for n in range(0, 68):
x = landmarks.part(n).x
y = landmarks.part(n).y
cv2.circle(frame, (x, y), 4, (255, 0, 0), -1)
cv2.imshow("Frame", frame)
cv2.waitKey(0)
ready = False
Now, here what makes me crazy. When I try to download any of the images(with or without mask) from google to test it, this script is working fine. Likewise, you can see these results such as,
But when I try over these following images, it does nothing.
I have made a couple of searches over the internet but I haven't found anything that is serving the current purpose.
Even, I have tried the combination of
cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
m_cascade = cv2.CascadeClassifier('haarcascade_mcs_mouth.xml')
I also have looked into the following useful links out there;
Face Bounding Box
Detect Face Landmarks in Android (Even not same domain)
Landmarks detection
OpenCV2 Detect Facial Landmarks
but it's also not working on these images. CV2 detector shows an empty list when I debug through script such as;
I just want to draw fiducial landmarks using the above images. What would the best possible solution, I can go through? Maybe, I am missing something in cv2 & Dlib, but unable to get the results as required.
I have also find the confidence score for dlib using the recommended implementation from a Stack Overflow geek such as;
import dlib
detector = dlib.get_frontal_face_detector()
img = dlib.load_rgb_image('demo.jpg')
dets, scores, idx = detector.run(img, 1, -1)
for i, d in enumerate(dets):
print("Detection {}, score: {}, face_type:{}".format(
d, scores[i], idx[i]))
Here is the result of a confidence score for the first image in the above-given images in the second row;
Looking forward to getting better research from any of the awesome guys out there. Thanks
First, I might try to see if you can get confidence scores out of dlib. I'm not sure what the confidence threshold is, but perhaps faces are detected that are below the limit. From the dlib Git Repo, here is an example of how to get confidence from the detections:
if (len(sys.argv[1:]) > 0):
img = dlib.load_rgb_image(sys.argv[1])
dets, scores, idx = detector.run(img, 1, -1)
for i, d in enumerate(dets):
print("Detection {}, score: {}, face_type:{}".format(
d, scores[i], idx[i]))
Alternatively, consider another face detector, for example a CNN-based detector like this MobileNet SSD face detector. I have not used this particular model, but I have used similar models, like the Google TPU-based face detector model here with very good results.
Download "shape_predictor_68_face_landmarks.dat" link:
enter link description here
100% working Code Try This One:
import cv2
import dlib
import numpy as np
img= cv2.imread('Capture 8.PNG')
gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
p = "shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(p)
faces = detector(gray)
for face in faces:
x1=face.left()
y1=face.top()
x2=face.right()
y2=face.bottom()
cv2.rectangle(img, (x1,y1), (x2,y2),(0,255,0),3)
landmarks=predictor(gray, face)
for n in range(0,68):
x=landmarks.part(n).x
y=landmarks.part(n).y
cv2.circle(img, (x, y), 4, (0, 0, 255), -1)
cv2.imshow(img)

Difficulty reading text with pytesseract

I need to read the highest temperature on thermographic images, as shown below:
IR_1544_INFRA.jpg
IR_1546_INFRA.jpg
IR_1560_INFRA.jpg
IR_1564_INFRA.jpg
I used the following code, this was the best result.
I also tried several other ways, such as: blur, gray scale, binarization, and others but they all failed.
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Users\User\AppData\Local\Tesseract-OCR\tesseract.exe"
# Load image, grayscale, Otsu's threshold
entrada = cv2.imread('IR_1546_INFRA.jpg')
image = entrada[40:65, 277:319]
#image = cv2.imread('IR_1546_INFRA.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = 255 - cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Blur and perform text extraction
thresh = cv2.GaussianBlur(thresh, (3,3), 0)
data = pytesseract.image_to_string(thresh, lang='eng', config='--psm 6')
print(data)
cv2.imshow('thresh', thresh)
cv2.waitKey()
In the first image, I found
this
In the second image, I found this.
The imagem layout is always the same, that is, the temperature is always in the same place, so I cropped the image to isolate only the number. I would like (97.7 here, and 85.2 here).
My code needs to find from these images to always detect this temperature and generate a list indicating from highest to lowest.
What do you indicate for me to improve the assertiveness of pytesseract in the case of these images?
Note 1: When I annalyze the entire image (without cropping), it returns data that is not even present.
Note 2: In some images even with the binary number, pytesseract (image_to_string) does not return any data.
Thank you all and sorry for the typos, writing in english is still a challenge for me.
Because you have same images, you can crop the area you want and then do processing there. The processing is also simple. Change to gray, get threshold, invert, resize, and then do the OCR. You can see it in my code below. It works on all your attached images.
import cv2
import pytesseract
import os
image_path = "temperature"
for nama_file in sorted(os.listdir(image_path)):
print(nama_file)
img = cv2.imread(os.path.join(image_path, nama_file))
crop = img[43:62, 278:319]
gray = cv2.cvtColor(crop, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.bitwise_not(thresh)
double = cv2.resize(thresh, None, fx=2, fy=2)
custom_config = r'-l eng --oem 3 --psm 7 -c tessedit_char_whitelist="1234567890." '
text = pytesseract.image_to_string(double, config=custom_config)
print("detected: " + text)
cv2.imshow("img", img)
cv2.imshow("double", double)
cv2.waitKey(0)
cv2.destroyAllWindows()

Issue with T-API(with OpenCL) python3

I'm experimenting with T-Api of opencv python. Currently I'm trying to convert a normal RGB image to gray scale and save it with T-Api enabled. Here's the snippet
import cv2
import dlib
im = cv2.UMat(cv2.imread('input.png',1))
print(im)
imMat = cv2.UMat(im)
gray = cv2.cvtColor(imMat,cv2.COLOR_BGR2GRAY)
cv2.imwrite('gray.png',gray)
The output is as follows along with the error
<cv2.UMat object at 0x7fd1e400e378>
<cv2.UMat object at 0x7fc97d0ec390>
Segmentation fault (core dumped)
I have tried this before the above operations
cv2.ocl.setUseOpenCL(True)
print(cv2.ocl.haveOpenCL())
The above print statement is outputting false, I was thinking I need to compile by opencv with OpenCL support inorder for cv2.UMat to work but i'm able to print out both im outputs. I copied the code from this example, it seems to work fine
import cv2
img = cv2.UMat(cv2.imread("image.jpg", cv2.IMREAD_COLOR))
imgUMat = cv2.UMat(img)
gray = cv2.cvtColor(imgUMat, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 1.5)
gray = cv2.Canny(gray, 0, 50)
cv2.imshow("edges", gray)
cv2.waitKey();
Where exactly am I going wrong?
You should rebuild with CMake using the flag WITH_OPENCL=ON and WITH_OPENCLAMDFFT=ON, WITH_OPENCLAMDBLAS=ON if you have AMD’S FFT and BLAS libraries. source

Resources