I am trying dlib for face recognition. But when i execute the program i have an error with skimage. Can somebody help me? I have try to solve it but i can't
from skimage.io import imread
import sys
import os
import dlib
import glob
import numpy
if len(sys.argv) != 4:
print(
"Call this program like this:\n"
" ./face_recognition.py shape_predictor_68_face_landmarks.dat dlib_face_recognition_resnet_model_v1.dat ../examples/faces\n"
"You can download a trained facial shape predictor and recognition model from:\n"
" http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2\n"
" http://dlib.net/files/dlib_face_recognition_resnet_model_v1.dat.bz2")
exit()
predictor_path = sys.argv[1]
face_rec_model_path = sys.argv[2]
faces_folder_path = sys.argv[3]
detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor(predictor_path)
facerec = dlib.face_recognition_model_v1(face_rec_model_path)
win = dlib.image_window()
for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
print("Processing file: {}".format(f))
img = io.imread(f)
win.clear_overlay()
win.set_image(img)
# Ask the detector to find the bounding boxes of each face. The 1 in the
# second argument indicates that we should upsample the image 1 time. This
# will make everything bigger and allow us to detect more faces.
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets)))
# Now process each face we found.
for k, d in enumerate(dets):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
k, d.left(), d.top(), d.right(), d.bottom()))
# Get the landmarks/parts for the face in box d.
shape = sp(img, d)
# Draw the face landmarks on the screen so we can see what face is currently being processed.
win.clear_overlay()
win.add_overlay(d)
win.add_overlay(shape)
# Compute the 128D vector that describes the face in img identified by
# shape. In general, if two face descriptor vectors have a Euclidean
# distance between them less than 0.6 then they are from the same
# person, otherwise they are from different people. He we just print
# the vector to the screen.
face_descriptor = facerec.compute_face_descriptor(img, shape)
print(face_descriptor)
# It should also be noted that you can also call this function like this:
# face_descriptor = facerec.compute_face_descriptor(img, shape, 100)
# The version of the call without the 100 gets 99.13% accuracy on LFW
# while the version with 100 gets 99.38%. However, the 100 makes the
# call 100x slower to execute, so choose whatever version you like. To
# explain a little, the 3rd argument tells the code how many times to
# jitter/resample the image. When you set it to 100 it executes the
# face descriptor extraction 100 times on slightly modified versions of
# the face and returns the average result. You could also pick a more
# middle value, such as 10, which is only 10x slower but still gets an
# LFW accuracy of 99.3%.
dlib.hit_enter_to_continue()
And my error message like this
Traceback (most recent call last):
File "C:/Users/Android/Downloads/Compressed/dlib-19.4/dlib-19.4/python_examples/face_recognition.py", line 48, in <module>
from skimage.io import imread
File "C:\Users\Android\AppData\Local\Programs\Python\Python35\lib\site-packages\skimage\io\__init__.py", line 11, in <module>
from ._io import *
File "C:\Users\Android\AppData\Local\Programs\Python\Python35\lib\site-packages\skimage\io\_io.py", line 7, in <module>
from ..color import rgb2grey
File "C:\Users\Android\AppData\Local\Programs\Python\Python35\lib\site-packages\skimage\color\__init__.py", line 1, in <module>
from .colorconv import (convert_colorspace,
File "C:\Users\Android\AppData\Local\Programs\Python\Python35\lib\site-packages\skimage\color\colorconv.py", line 59, in <module>
from scipy import linalg
File "C:\Users\Android\AppData\Local\Programs\Python\Python35\lib\site-packages\scipy\__init__.py", line 61, in <module>
from numpy._distributor_init import NUMPY_MKL # requires numpy+mkl
ImportError: cannot import name 'NUMPY_MKL'
Please help me with my problem. Thank you before
Imread is available from the mahotas package.
Example:
import mahotas as mh
from mahotas.features import surf
image = mh.imread('zipper.jpg', as_grey=True)
Related
i would like to detect faces with mask, here is one example of image :
classical side face detector
faceCascade =cv2.CascadeClassifier("haarcascade_profileface.xml")
does not detect faces well, therefore i tried to search a little more and found following documentation :
yunet documentation
i have tried to implement this model in my code :
import numpy as np
import matplotlib.pyplot as plt
import cv2
import math
#faceCascade =cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
#faceCascade =cv2.CascadeClassifier("haarcascade_profileface.xml")
frame =cv2.imread("distance_Measure.jpg")
frame =cv2.resize(frame,(500,500))
gray =cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
face_detector = cv2.FaceDetectorYN_create("yunet.onnx", "", (0, 0))
#faces =faceCascade.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(20,20))
_, faces = face_detector.detect(gray)
l =[]
lf =[]
i =1
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
s =str(i)
cv2.putText(frame,s,(x,y),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
i+=1
l =[]
l.append(x)
l.append(y)
lf.append(l)
print(l)
print(lf)
close_person =""
for i in range(len(lf)):
for j in range(i+1,len(lf)):
d =math.sqrt((lf[j][1]-lf[i][1])**2+((lf[j][0]-lf[i][0])**2))
print("P",i+1,"-P",j+1,"=",d)
if d <150:
close_person ="Person " +str(i+1)+" and Person "+str(j+1)+";"
cv2.line(frame,(lf[i][0],lf[i][1]),(lf[j][0],lf[j][1]),(0,0,255),2)
close_person+=" are not following social distance"
print(close_person)
cv2.imshow("Displayed_Persons",frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
plt.show()
but it returns following error :
Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\AI_Project\Social_Distance_Measurement_Example.py", line 10, in <module>
face_detector = cv2.FaceDetectorYN_create("yunet.onnx", "", (0, 0))
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\dnn\src\onnx\onnx_importer.cpp:194: error: (-5:Bad argument) Can't read ONNX file: yunet.onnx in function 'cv::dnn::dnn4_v20211220::ONNXImporter::ONNXImporter'
i have searched again and found following documentation :
searched solution
but i could not clarify how to solve given problem, please help me
I have the following little code:
from google.colab import drive
from IPython.display import display
import PIL
from PIL import Image, ImageDraw
import kraken
from kraken import pageseg
import cv2 as cv
img = Image.open("/content/drive/My Drive/images/dropfire.jpg")
face_cascade = cv.CascadeClassifier(cv.data.haarcascades + "/content/drive/My Drive/datas/haarcascade_frontalface_default.xml")
eye_cascade = cv.CascadeClassifier(cv.data.haarcascades + "/content/drive/My Drive/datas/haarcascade_eye.xml")
file_name = "/content/drive/My Drive/images/dropfire.jpg"
img = cv.imread(file_name)
pil_img = Image.open(file_name)
cv_img = pil_img.convert('L')
cv_img = cv.imread(file_name)
faces = face_cascade.detectMultiScale(cv_img)
When I run the last cell (with faces), it raises:
error Traceback (most recent call last)
<ipython-input-23-2bd7582f8a20> in <module>()
----> 1 faces = face_cascade.detectMultiScale(cv_img)
error: OpenCV(4.1.2) /io/opencv/modules/objdetect/src/cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'detectMultiScale'
Until this cell everything works fine. According to a Stack Overflow answer, I added cv.data.haarcascades + in the brackets of cv.CascadeClassifier. Although people said this worked for them, it doesn't for me somehow.
imho, that "little code" of yours has too much noise (unnecessary lines of code) already.
I think it makes for you even harder to understand what's going on.
Comment out/delete all the lines except these:
import cv2 as cv
face_cascade = cv.CascadeClassifier(cv.data.haarcascades + "haarcascade_frontalface_default.xml")
eye_cascade = cv.CascadeClassifier(cv.data.haarcascades + "haarcascade_eye.xml")
file_name = "/content/drive/My Drive/images/dropfire.jpg"
img = cv.imread(file_name) # this reads the image already
cv_img = cv.imread(file_name) # this reads same image once more, not sure if intended
faces = face_cascade.detectMultiScale(cv_img)
If your image "dropfire" really exists at that path, this should work without error, but it won't show any results of face detection. You can add these lines at the end to see the result:
for (x, y, w, h) in faces:
cv.rectangle(cv_img, (x, y), (x+w, y+h), (0, 0, 255), 3)
cv.imshow("Nice face", cv_img)
cv.waitKey(0)
cv.data.haarcascades already has path to all those cv2 xml files, so you only need to use file names.
The issue is to check whether XML is loaded or not, use
eye_cascade.empty() to check whether it's loaded or not.
I'm trying to apply a flood_fill method to a certain image. Unfortunately, even though it works on an exemplary image, it doesn't work on mine, which is already binarized.
The code that works:
from skimage import data, filters
from skimage.segmentation import flood, flood_fill
import cv2 as cv
cameraman = data.camera()
flooded = flood_fill(cameraman, (200, 100), 255, tolerance=10)
cv.imshow("aaa",flooded)
cv.waitKey()
And the code that does not:
from skimage import data, filters
from skimage.segmentation import flood, flood_fill
import cv2 as cv
import numpy as np
img = cv.imread("Tubka_binar.png")
flooded = flood_fill(img, (200, 100), 100, tolerance = 10)
cv.imshow("aaa",flooded)
cv.waitKey()
And the errors I get:
Traceback (most recent call last):
File "C:/Users/User/Documents/PW/MAGISTERSKIE/__PRACA/Python/Grubość Tuby.py", line 8, in <module>
flooded = flood_fill(img, (200, 100), 100, tolerance = 10)
File "C:\Users\User\Desktop\PROJEKT_PYTHONOWY\venv\lib\site-packages\skimage\morphology\_flood_fill.py", line 104, in flood_fill
tolerance=tolerance)
File "C:\Users\User\Desktop\PROJEKT_PYTHONOWY\venv\lib\site-packages\skimage\morphology\_flood_fill.py", line 235, in flood
working_image.shape, order=order)
File "<__array_function__ internals>", line 6, in ravel_multi_index
ValueError: parameter multi_index must be a sequence of length 3
Process finished with exit code 1
The image variables in both cases seem to be the same type. The image that I read in the second case is a binarized photo, that takes only two values: 0 and 255.
What is causing this?
Best regards
It looks to me like your second image is not actually grayscale but rather saved (or loaded) as a 3-channel RGB image. If you print img.shape, I bet it’ll be something like (512, 512, 3). You can fix this by changing your reading code to:
img = cv.imread("Tubka_binar.png")[..., 0]
I was following this topic to try to get the buffer data from a camera and save it as a numpy array. Following the topic I reused this code:
base.graphicsEngine.renderFrame()
dr = base.camNode.getDisplayRegion(0)
tex = dr.getScreenshot()
data = tex.getRamImage()
image = np.frombuffer(data,np.uint8)
image.shape = (tex.getYSize(),tex.getXSize(),tex.getNumComponents())
print(image)
However, I get this error:
File "main.py", line 137, in __init__
image = np.frombuffer(data,np.uint8)
AttributeError: 'panda3d.core.ConstPointerToArray_unsigned_char' object has no attribute '__buffer__'
Any advice?
Solved it by changing the original code to the following:
base.graphicsEngine.renderFrame()
dr = base.camNode.getDisplayRegion(0)
tex = dr.getScreenshot()
data = tex.getRamImage()
v = memoryview(data).tolist()
img = np.array(v,dtype=np.uint8)
img = img.reshape((tex.getYSize(),tex.getXSize(),4))
img = img[::-1]
cv2.imshow('img',img)
cv2.waitKey(0)
The image ends up being flipped for whatever reason after reshaping the numpy array hence the 3rd line from the bottom. You should see an identical image of whatever your camera sees when you run this snippet as an accept key or something. Hope this helps someone having the same issues.
I was having trouble with a few bugs on pyrender, so i wanted to use panda3d as a offscreen render, and this was very usefull, expanding on what Masa Hu posted, here is the full example on how to do that:
from direct.showbase.ShowBase import ShowBase
import cv2
import numpy as np
base = ShowBase( windowType='offscreen')
box = base.loader.loadModel("meshes/box.egg")
box.setPos(0,10,0)
box.reparentTo(base.render)
base.graphicsEngine.renderFrame()
dr = base.camNode.getDisplayRegion(0)
tex = dr.getScreenshot()
data = tex.getRamImage()
v = memoryview(data).tolist()
img = np.array(v,dtype=np.uint8)
img = img.reshape((tex.getYSize(),tex.getXSize(),4))
img = img[::-1]
cv2.imshow('img',img)
cv2.waitKey(0)
I am trying to apply cv2.createBackgroundSubtractorMOG() to this Image:
to eliminate all background brightness and only leave the two bright objects in the middle for further analysis. Is this the right approach for this task? If not, how would I do that?
import cv2
img = cv2.imread('image.png')
sharp_img = cv2.createBackgroundSubtractorMOG().apply(img)
Output:
Traceback (most recent call last):
File "/home/artur/Desktop/test.py", line 4, in <module>
sharp_img = cv2.createBackgroundSubtractorMOG().apply(img)
AttributeError: module 'cv2.cv2' has no attribute 'createBackgroundSubtractorMOG
Edit:
MOG does not seem to work.
Code:
import cv2
img = cv2.imread('image.png')
sharp_img = cv2.bgsegm.createBackgroundSubtractorMOG().apply(img)
cv2.imwrite('image2.png', sharp_img)
Output:
Traceback (most recent call last):
File "/home/artur/Desktop/test.py", line 4, in <module>
sharp_img = cv2.bgsegm.createBackgroundSubtractorMOG().apply(img)
AttributeError: module 'cv2.cv2' has no attribute 'bgsegm'
MOG2 seems to work but with no satisfying result:
Code:
import cv2
img = cv2.imread('image.png')
sharp_img = cv2.createBackgroundSubtractorMOG2().apply(img)
cv2.imwrite('image2.png', sharp_img)
Output Image:
I tried to play around with the args of the MOG2 Method from the docs but with no change.
from the docs, try this:
sharp_img = cv.bgsegm.createBackgroundSubtractorMOG().apply(img)
or
sharp_img = cv2.createBackgroundSubtractorMOG2().apply(img)
import cv2
img = cv2.imread('image.png')
max,min = img.max(),imgg.min()
print(max,min) #helps in giving thresholding values
threshold_img = cv2.threshold(blurred, 127, 255,cv2.THRESH_BINARY) #good starting point to give t1 value as half of max value of image
cv2.imshow(threshold_img)
This approach is a good starting point in your case, as you have two bright peaks that you want to separate from the noise. Once you have identified the required threshold limits, you should be able to isolate the two spots from the noise in the background. You can further use cv2.erode and cv2.dilate if needed to remove further noise.