an notAdirectoryerror in python3.7 - python

i am making a face recognition program but i am facing NotADirectoryError error whenever i run it
i tried coping the full directory as well as using os.join.path and nothing worked
i am using pycharm.
import face_recognition
import cv2
import os
import numpy
os.chdir('C:/Users/a/Desktop/ftp')
KNOWN_FACE_DIR = "known_faces"
print(KNOWN_FACE_DIR)
#UNKNOWN_FACES_DIR = r"unknown_faces"
TOLERANCE = 0.6
FRANE_THICKNESS = 3
FONT_THICKNESS = 2
MODEL = "cnn"
video = cv2.VideoCapture(0)
print("loading known faces ")
known_faces = []
known_names = []
for name in os.listdir(KNOWN_FACE_DIR):
for filename in os.listdir(f"{KNOWN_FACE_DIR}/{name}"):
image = face_recognition.load_image_file(f"{KNOWN_FACE_DIR}/{name}/{filename}")
print("processing unknown faces")
while True :
#print(filename)
#image = face_recognition.load_image_file(f"{UNKNOWN_FACES_DIR}/{filename}")
ret ,image = video.read()
locations = face_recognition.face_locations(image, model=MODEL)
encodings = face_recognition.face_encodings(image, locations)
#image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
for face_encoding, face_location in zip(encodings, locations):
results = face_recognition.compare_faces(known_faces, face_encoding, TOLERANCE)
match = None
if True in results:
match = known_names[results.index(True)]
print(f"Match found:{match}")
top_left = (face_location[3], face_location[0])
bottom_right = (face_location[1], face_location[2])
color = [0,255,0]
cv2.rectangle(image, top_left, bottom_right, color, FRANE_THICKNESS)
top_left = (face_location[3], face_location[2])
bottom_right = (face_location[1], face_location[2]+22)
cv2.rectangle(image, top_left, bottom_right, color, cv2.FILLED)
cv2.putText(image, match, (face_location[3]+10,face_location[2]+15).cv2.FONT_HERSHEY_SIMPLEX,0.5,(200,200,200),FONT_THICKNESS)
cv2.imshow(filename,image)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cv2.destroyWindow(filename)
the error
Traceback (most recent call last):
File "C:\Users\a\Desktop\ftp\face recognition.py", line 27, in <module>
for filename in os.listdir(f"{KNOWN_FACE_DIR}/{name}"):
NotADirectoryError: [WinError 267] The directory name is invalid: 'known_faces/Majd.jpg'
i tried coping the whole directory
and using os.join.path although i am now sure i used it correctly

The os.listdir function requires a directory name, and something called Madj.jpg is most likely to be a JPEG file rather than a directory.
It looks like your assumption that everything in C:/Users/a/Desktop/ftp/known_faces is a directory containing files that you want to process may not be correct.

Related

why do I get an error when writing sys.argv[1]

import sys
import pygame
import random
#command line
image = sys.argv[1]
#loading the image and importing
inputImage = pygame.image.load(image)
#getting the image's height and width
(width,height) = inputImage.get_size()
#creating the bigger windom of the image by multiplying original size by 6
window = pygame.display.set_mode((width*6,height*6))
for x in range(height):
for y in range(width):
#rgb coordinates in the x and y image
(r,g,b,_) = inputImage.get_at((x,y))
#Calculate required number of circles
n1 = int(r/5)
n2 = int(g/5)
n3 = int(b/5)
#drawing random red circles on the image
while(n1 > 0):
pygame.draw.circle(window,(255,0,0),(random.randint((x*5)-15,(x*5)),random.randint((y*5)-15,(y*5))),1)
n1 = n1-1
#drawing random green circles on the image
while(n2 > 0):
pygame.draw.circle(window,(0,255,0),(random.randint((x*5)-15,(x*5)),random.randint((y*5)-15,(y*5))),1)
n2= n2-1
#drawing random blue circles on the image
while(n3 > 0):
pygame.draw.circle(window,(0,0,255),(random.randint((x*5)-15,(x*5)),random.randint((y*5)-15,(y*5))),1)
n3 = n3-1
#updating the image
pygame.display.update()
pygame.time.delay(5000)
My code sends me an error saying what the title is, so when I try to change the sys.argv line into
image = sys.argv[0]
It gives me a different error saying: pygame.error: Unsupported image format and I am not sure what that means? Am I missing something important?
Also, if you guys can help me as to how to show the pygame window after fixing the code will much appreciated :)
This method does not catch any user errors (which is what you're experiencing).
#command line
image = sys.argv[1]
You need to check a few things here:
Does the sys.argv list actually have a second element (at index [1]).
Test the filename specified actually exists.
Use a try, except block to catch any errors loading the image.
import os.path
# See if we can load the user's image
inputImage = None
if ( len( sys.argv ) >= 1 ):
image_filename = sys.argv[1]
if ( not os.path.exists( image_filename ) ):
sys.stderr.write( "Filename [" + image_filename + "] does not exist\n" )
else:
try:
inputImage = pygame.image.load( image_filename )
except:
sys.stderr.write( "Failed to load [" + image_filename + "] is it an image?\n" )
inputImage = None
else:
sys.stderr.write( "No Image Filename argument specified\n" )
### inputImage is either a valid image, or None

Unable to convert os.path.split(imagePath)[-1].split('.')[1] to integer

I am trying to create a face recognition software using OpenCV, but the code I found in the library is made in Python 2. Is there a Python 3 version of this?
Here's the link: https://github.com/thecodacus/Face-Recognition
I already have a folder for dataset and trainer.
import cv2
import numpy as np
from PIL import Image
import os
# Path for face image database
path = 'dataset'
recognizer = cv2.face.LBPHFaceRecognizer_create()
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml");
# function to get the images and label data
def getImagesAndLabels(path):
imagePaths = [os.path.join(path,f) for f in os.listdir(path)]
faceSamples=[]
ids = []
for imagePath in imagePaths:
PIL_img = Image.open(imagePath).convert('L') # convert it to grayscale
img_numpy = np.array(PIL_img,'uint8')
id = int(os.path.split(imagePath)[-1].split('.')[1])
faces = detector.detectMultiScale(img_numpy)
for (x,y,w,h) in faces:
faceSamples.append(img_numpy[y:y+h,x:x+w])
ids.append(id)
return faceSamples,ids
print ("\n [INFO] Training faces. It will take a few seconds. Wait ...")
faces,ids = getImagesAndLabels(path)
recognizer.train(faces, np.array(ids))
# Save the model into trainer/trainer.yml
recognizer.write('trainer/trainer.yml') # recognizer.save() worked on Mac, but not on Pi
# Print the numer of faces trained and end program
print("\n [INFO] {0} faces trained. Exiting Program".format(len(np.unique(ids))))
Error:
Traceback (most recent call last):
File "/Users/user/Desktop/FacialRecognition/02_face_training.py", line 46, in <module>
faces,ids = getImagesAndLabels(path)
File "/Users/user/Desktop/FacialRecognition/02_face_training.py", line 36, in getImagesAndLabels
id = int(os.path.split(imagePath)[-1].split('.')[1])
ValueError: invalid literal for int() with base 10: 'User'
On that repository there's a dataSet directory, with a file named like:
In [665]: name='Face-Recognition/dataSet/face-1.1.jpg'
Applied to that name, your code sample does:
In [668]: os.path.split(name)
Out[668]: ('Face-Recognition/dataSet', 'face-1.1.jpg')
In [669]: os.path.split(name)[-1]
Out[669]: 'face-1.1.jpg'
In [670]: os.path.split(name)[-1].split('.')
Out[670]: ['face-1', '1', 'jpg']
In [671]: os.path.split(name)[-1].split('.')[1]
Out[671]: '1'
In [672]: int(os.path.split(name)[-1].split('.')[1])
Out[672]: 1
Apparently your file has a different name format, one that includes 'User' in a slot where this code expects a number.
You need to correct the file name, or change this parsing code.
The image name which you got in the dataset are as User.*somename*, so remove User from all the image names.
try to change format images is 'face.1.1.jpg'
then you can split the dot with this code
faceID = int(os.path.split(imagePath)[-1].split(".")[2])
This did the job for me:
Id=int(os.path.split(imagePath)[-1].split(".")[0])

face recognition can't detect if image face is not on portrait

I try using face recognition from this link: face recognition
then modif the code like this, main.py:
#!/usr/bin/env python
import cgitb, cgi
cgitb.enable()
print("Content-Type: text/html;charset=utf-8")
print "Content-type:text/html\r\n\r\n"
import base64
import simplejson as json
import re
import face_recognition
import numpy as np
import io
from imageio import imread
from PIL import Image
import datetime
import os, errno
import shutil
params = cgi.FieldStorage()
now = datetime.datetime.now()
date = str(now)
date2 = date.replace(" ","")
img = params.getvalue('img')
data1 = json.loads(img)
data2 = data1['img2']['data']
numparray = data1['img1']
numparray2 = numparray.replace(" ", "+")
b=bytes(numparray2)
imgdata = base64.b64decode(b)
os.makedirs(date2)
with open(date2+"/img1.png", "wb") as f:
f.write(imgdata)
image = face_recognition.load_image_file(date2+'/img1.png')
try:
face_encode = face_recognition.face_encodings(image)[0]
#print("face_encode = ".format(face_encode))
except IndexError:
print("encode image failed")
quit()
known_faces = []
y = 1
for images in data2:
ir = images.replace(" ", "+")
ib = bytes(ir)
imagedata = base64.b64decode(ib)
x = str(y)
with open(date2+"/compare"+x+".png", "wb") as g:
g.write(imagedata)
compare = face_recognition.load_image_file(date2+"/compare"+x+".png")
try:
compare_encode = face_recognition.face_encodings(compare)[0]
#print("face_encode = ".format(face_encode))
except IndexError:
print("encode image compare failed")
quit()
known_faces.append(compare_encode)
y = y+1
results = face_recognition.face_distance(known_faces, face_encode)
datahasil = []
#hasilakhir = "{"
for i, face_distance in enumerate(results):
h = "{:.2}".format(face_distance, i)
#hasilakhir = hasilakhir+"compare{}"
datahasil.append(h)
hasilakhir = ','.join(datahasil)
shutil.rmtree(date2, ignore_errors=True)
print("{\"hasilcompare\" : \"" +hasilakhir+ "\"}")
the final result is compare between 2 image and give the score, in case photo of image is potrait compare is successfull, but when one of image is not on potrait (face not on potrait potision) or like face angle more than 90 degree, that give error message in catch encode failed..
i have try another way with face detection before sending 2 image to main.py to detect the image in face but when it can't detect faces i try to rotate the image untill the code detect face, but sometimes face detection can detect face with angle 90degree but in face recognition(main.py) still can't read the face.
code of rotate is here, rotate.py:import numpy as np
import cv2
from scipy import ndimage, misc
import os
from PIL import Image
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
for counter in range (0, 4):
img = cv2.imread('img/1.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
if(len(faces) == 1):
i = False
print ("face found")
break
else:
print("no face found")
i = False
img = Image.open("img/1.jpg")
img.rotate(90).save("img/1.jpg")
Yes, I think this is the problem with the library. I tested it on the same image. Once after rotation and once on the original image. It does not detect in the rotated image, both the face_locations and face_encodings are empty lists.

Reading multiple image from a file for image process python

Hi i've a script to run image process on a image. But i'm trying to get a loop or another way to read multiple images from a file
e.g
C:\Users\student\Desktop\Don\program (opencv version)\Images\move1
move1 contains images named as frame1.jpg , frame2.jpg , frame3.jpg...
The script i'm using to run the image process is something like
img = cv2.imread('frame1.jpg')
mimg = cv2.medianBlur(img,15)
gimg = cv2.cvtColor(mimg,cv2.COLOR_RGB2GRAY)
ret,th1 = cv2.threshold(gimg, 160,255,cv2.THRESH_BINARY)
ret,th2 = cv2.threshold(th1, 160,255,cv2.THRESH_BINARY_INV)
cv2.imwrite('threshbinaryinv.jpg', th2)
My script above could only read images that i manually keyed in e.g 'frame1.jg'. Sorry i'm very new to python. Thanks!
EDIT
This the code i edited with you guys help.. still getting error as "Traceback (most recent call last):
File "C:\Users\student\Desktop\Don\program (opencv version)\prog.py", line 32, in
gimg = cv2.cvtColor(mimg,cv2.COLOR_RGB2GRAY) #convert RBG to Grayscale
cv2.error: D:\Build\OpenCV\opencv-3.3.1\modules\imgproc\src\color.cpp:11048: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor"
CODE
path_of_images = 'C:/Users/student/Desktop/Don/program (opencv version)/Images'
list_of_images = os.listdir(path_of_images)
for image in list_of_images:
img = cv2.imread(os.path.join(path_of_images, image))
mimg = cv2.medianBlur(img,15)
gimg = cv2.cvtColor(mimg,cv2.COLOR_RGB2GRAY)
ret,th1 = cv2.threshold(gimg, 160,255,cv2.THRESH_BINARY)
ret,th2 = cv2.threshold(th1, 160,255,cv2.THRESH_BINARY_INV)
cv2.imwrite('threshbinaryinv.jpg', th2)
You can use os.listdir() to get the names of all images in your specified path which is "C:\Users\student\Desktop\Don\program (opencv version)\Images". Then you can loop over the names of images like :
import os
import cv2
path_of_images = r"C:\Users\student\Desktop\Don\program (opencv version)\Images"
list_of_images = os.listdir(path_of_images)
for image in list_of_images:
img = cv2.imread(os.path.join(path_of_images, image))
"""Your code here"""
It can be done using a for loop and generating a new str file name and then processing it as:
IMG_FOLDER_PREFIX = "absolute/path/to/frame"
IMG_EXTENSION = ".jpg"
NUM_IMAGES = 10
for i in xrange(NUM_IMAGES):
image_path = IMG_FOLDER_PREFIX + str(i) + IMG_EXTENSION
img = cv2.imread(image_path)
# Other Image Processing.
A better way to iterate images would be os.listdir, glob, etc. but in that case you may have lesser control over the order of files traversed.

Issue With Opencv Face Detection

I have been trying to get the openCV 2.4.13 python27 face Detection from images please help.The error that I get is:
Traceback (most recent call last):
File "C:\Users\sam_o\Desktop\Alrehman\Attendance.py", line 36, in <module>
profile = getProfile(id)
File "C:\Users\sam_o\Desktop\Alrehman\Attendance.py", line 19, in getProfile
profile = none
NameError: global name 'none' is not defined
The code I wrote is as follows:
import cv2,os
import numpy as np
from PIL import Image
import pickle
import sqlite3
recognizer = cv2.createLBPHFaceRecognizer()
cascadePath = "Classifiers/face.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);
path = 'EmployeeRecord'
def getProfile(id):
conn=sqlite3.connect("Alrehman.db")
cmd="SELECT * FROM Person WHERE ID="+str(id)
cursor=conn.execute(cmd)
profile=none
for row in cursor:
profile=row
conn.close()
return profile
cam = cv2.VideoCapture(0)
font = cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 1, 1) #Creates a font
while True:
ret, im =cam.read()
gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
faces=faceCascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(100, 100), flags=cv2.CASCADE_SCALE_IMAGE)
for(x,y,w,h) in faces:
id, conf = recognizer.predict(gray[y:y+h,x:x+w])
cv2.rectangle(im,(x,y),(x+w,y+h),(225,0,0),2)
profile=getProfile(id)
if(profile!=None):
cv2.cv.PutText(cv2.cv.fromarray(im),str(profile[1]), (x,y+h+30),font, 255) #Draw the text
cv2.cv.PutText(cv2.cv.fromarray(im),str(profile[2]), (x,y+h+60),font, 255) #Draw the text
cv2.imshow('im',im)
cv2.waitKey(10)
Does anyone have any idea what I might be doing wrong here?
It looks like either your camera is not capturing a photo, or capturing a grayscale photo. (error code 215 is common in such cases, in other opencv functions as well)
You can test this further by loading a sample image from the computer, and passing that to cv2.cvtColor().
I tried it This is code is working for me.....
import cv2
import numpy as np
import os
import pickle
import sqlite3
faceDetect=cv2.CascadeClassifier('haarcascade_frontalface_default.xml');
def getProfile(id):
conn=sqlite3.connect("FaceBase.db")
cmd="SELECT * FROM People WHERE ID="+str(id)
cursor=conn.execute(cmd)
profile=None
for row in cursor:
profile=row
conn.close()
return profile
cam=cv2.VideoCapture(0);
rec=cv2.createLBPHFaceRecognizer();
rec.load("recognizer\\trainner.yml");
id=0
font=cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_COMPLEX_SMALL,1,1,0,1)
while(True):
ret,img=cam.read();
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces=faceDetect.detectMultiScale(gray,1.3,5);
for(x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
id,conf=rec.predict(gray[y:y+h,x:x+w])
profile=getProfile(id)
if(profile!=None):
cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[1]),(x,y+h+30),font,255);
cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[2]),(x,y+h+60),font,255);
cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[3]),(x,y+h+90),font,255);
# cv2.cv.PutText(cv2.cv.fromarray(img),str(profile[4]),(x,y+h+30),font,255);
cv2.imshow("Face",img);
if(cv2.waitKey(1)==ord('q')):
break;
cam.release();
cv2.destoryAllWindows()

Categories

Resources