I'm trying to make run a face recognition program using OpenCV and Python.
I found this code here on stackoverflow, but the main problem is an error which says:
Traceback (most recent call last):
File "/Users/n1/Desktop/FaceDetection/face.py", line 8, in <module>
gray = imread(fname, CV_LOAD_IMAGE_GRAYSCALE )
NameError: name 'CV_LOAD_IMAGE_GRAYSCALE' is not defined
The code is this one :
from cv2 import *
import numpy as np
face_cascade = CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = CascadeClassifier('haarcascade_eye.xml')
fname='123.jpg'
img = imread(fname)
gray = imread(fname, CV_LOAD_IMAGE_GRAYSCALE ( 0) )
rows,cols = gray.shape
gray = np.array(gray, dtype='uint8')
faces = face_cascade.detectMultiScale(gray, 1.3, 5, 0)
print ('faces=', faces)
for (x,y,w,h) in faces:
rectangle(img, (x,y), ((x+w),(x+h)), (255,0,0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey, ew, eh) in eyes:
rectangle(roi_color, (x,y), ((x+w), (y+h)), (50, 50, 50), 3)
imshow('eyes=%s' % (eyes,), roi_color)
imshow("img", img)
waitKey(0)
destroyAllWindows()
>>> import cv2
>>> help(cv2)
...
IMREAD_ANYCOLOR = 4
IMREAD_ANYDEPTH = 2
IMREAD_COLOR = 1
IMREAD_GRAYSCALE = 0 #that will be it ;)
IMREAD_LOAD_GDAL = 8
IMREAD_UNCHANGED = -1
...
VERSION
3.0.0-dev
( CV_LOAD_IMAGE_GRAYSCALE is from the outdated [and now removed] cv api )
In OpenCV 3.1 for C++ you have to use cv::ImreadModes::IMREAD_GRAYSCALE which is located on <opencv2/imgcodecs.hpp>
simpy change: "CV_LOAD_IMAGE_GRAYSCALE"
to: "IMREAD_GRAYSCALE"
it will work.
Related
I'm trying to get real-time face recognition for a trained VGG16 model(It has 6 classes). When I tried, I got the above error. These are my codes.
from PIL import Image
from tensorflow.keras.applications.vgg16 import preprocess_input
import base64
from io import BytesIO
import json
import random
import cv2
from keras.models import load_model
import numpy as np
from keras_preprocessing import image
model = load_model('FAceRec.h5', compile=False)
face_cas = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
def face_extr(img):
faces = face_cas.detectMultiScale(img, 1.3, 5)
if faces is ():
return None
for (x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w, y+h), (0,255,255), 2)
cropped_face = img[y:y+h, x:x+w]
return cropped_face
## web cam
video_cap = cv2.VideoCapture(0)
while True:
_, frame = video_cap.read()
face = face_extr(frame)
if type(face) is np.ndarray:
face = cv2.resize(face, (244, 244),3)
face = face.reshape(1, 224, 224, 3)
img = Image.fromarray(face, 'RGB')
img_array = np.array(img)
img_array = np.expand_dims(img_array, axis=0)
pred = model.predict(img_array)
print(pred)
name = "No Matching"
if(pred[1][1]>0.5):
name = "Suhail"
cv2.putText(frame, name, (50,50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
else:
cv2.putText(frame, "No Matching Face", (50,50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
cv2.imshow('Result', frame)
k=cv2.waitKey(1)
if k==ord('q'):
break
video_cap.release()
cv2.destroyAllWindows()
can someone please help me to figure this out. even when I change the reshape with (244,244,3) it shows the same error again and again. Can somebody explain why its happening and how to solve this?
Good evening, I have some problems correcting an image after removing grid to get area of that curve. I have tried erode and dilate after and before removing grid, but i have had a bad curve after that. Maybe you can advise me a more efficient way to correct the curve.
import sys
import cv2
import numpy as np
def remove_grid_lines(src):
clean_lines_h = remove_lines(src, np.ones((1, 10), np.uint8), np.ones((1, 5), np.uint8))
clean_lines_v = remove_lines(src, np.ones((10, 1), np.uint8), np.ones((5, 1), np.uint8))
return cv2.bitwise_not(cv2.bitwise_not(src) - clean_lines_h - clean_lines_v)
def remove_lines(src, kernel1, kernel2):
erosion = cv2.erode(src, kernel1, iterations=1)
dilation = cv2.bitwise_not(cv2.dilate(erosion, kernel1, iterations=1))
clean_lines = cv2.dilate(cv2.erode(dilation, kernel2, iterations=6), kernel2, iterations=6)
return clean_lines
def main(argv):
original_image = cv2.resize(cv2.imread(argv[0]), (640, 640))
grayscale_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
grayscale_image = cv2.GaussianBlur(grayscale_image, (3, 3), 0)
(thresh, binary_image) = cv2.threshold(grayscale_image, 120, 255, cv2.THRESH_BINARY)
binary_image = cv2.bitwise_not(binary_image)
cv2.imshow("Original", original_image)
cv2.imshow("Grayscale", grayscale_image)
cv2.imshow("Binary", binary_image)
binary_image = remove_grid_lines(binary_image)
cv2.imshow("Clean", binary_image)
cv2.waitKey()
cv2.destroyAllWindows()
if __name__ == "__main__":
main(sys.argv[1:])
This code is about warping the given image and detecting the circular shaped objects (checkers) in the image using hough transfom.
Input files for my code below
This is the input image used
A JSON file having the required dimensions used to calculate the perspective.
{
"canonical_board": {
"tl_tr_br_bl": [
[
622,
85
],
[
1477,
66
],
[
1420,
835
],
[
674,
837
]
],
"bar_width_to_checker_width": 0.716,
"board_width_to_board_height": 1.03,
"pip_length_to_board_height": 0.36
}
}
My code
#import necessary packages
import cv2
import json
import numpy as np
from operator import itemgetter
from glob import glob
#load file
input_file=open('3913.jpg.info.json', 'r')
json_decode = json.load(input_file)
result = []
result.append(json_decode['canonical_board']['tl_tr_br_bl'])
result.append(json_decode['canonical_board']['bar_width_to_checker_width'])
result.append(json_decode['canonical_board']['board_width_to_board_height'])
result.append(json_decode['canonical_board']['pip_length_to_board_height'])
print("tl_tr_br_bl:",result[0])
print("bar_width_to_checker_width:",result[1])
print("board_width_to_board_height",result[2])
print("pip_length_to_board_height",result[3])
normal_img = cv2.imread('3913.jpg')
pts1 = np.float32([[454, 83], [1240, 79], [1424, 808], [275, 842]])
pts2 = np.array([[0.397],[0.986],[0.402]], dtype=np.float32)
M = cv2.getPerspectiveTransform(pts1.astype(np.float32), pts2)
dst = cv2.warpPerspective(normal_img, M, (1300, 800))
#perspective of the original image shown
cv2.imshow(dst)
#converting the image into grayscale
gray = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY)
#locating the circles using hough transform
# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.2, 100)
# ensure at least some circles were found
if circles is not None:
circles = np.round(circles[0, :]).astype("int")
no_of_circles = len(circles)
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
cv2.circle(output, (x, y), r, (0, 255, 0), 4)
cv2.imshow("output", np.hstack([image, output]))
cv2.waitKey(0)
#number of circles
print("number of circles detected-",no_of_circles)
Error I am getting
error Traceback (most recent call last)
<ipython-input-12-efcd2ec83d0c> in <module>
37 pts2 = np.array([[0.397],[0.986],[0.402]], dtype=np.float32)
38
---> 39 M = cv2.getPerspectiveTransform(pts1.astype(np.float32), pts2)
40
41 dst = cv2.warpPerspective(normal_img, M, (1300, 800))
error: OpenCV(4.1.2) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/imgwarp.cpp:3391: error: (-215:Assertion failed) src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4 in function 'getPerspectiveTransform'
your pts2 array is wrong. it needs to be four points, not three. and the points need to be two-dimensional, not one-dimensional.
i am buliding face recognition code with python and opencv. this line giving the error in ; .when i replace ; with > or any other operator it works but lt become unknown this shows error like this.... NameError: name 'lt' is not defined.
This is my whole code in detector.py
import cv2
import numpy as np
faceDetect= cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cam = cv2.VideoCapture(0);
rec= cv2.face.LBPHFaceRecognizer_create()
rec.read("recognizer\\trainingData.yml")
id=0
fontFace = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
fontColor = (255, 255, 255)
#font = cv2.InitFont(cv2.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 1, 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)
#print(rec.predict(gray[y:y+h,x:x+w]))
id, conf = rec.predict(gray[y:y+h,x:x+w])
if(conf< >= 50):
if(id==1):
id="Osama"
elif(id==2):
id="Psycho"
else:
id = conf
cv2.putText(img,str(id), (x,y+h), fontFace, fontScale, fontColor)
cv2.imshow('Face',img)
if cv2.waitKey(1) ==ord('q'):
break
cam.release()
cv2.destroyAllWindows()
So your problem is that you replace ; with an operator, when you should in fact replace the whole code (< which stands for lower than) with <.
So if(conf<50) would give you if(conf<50).
See special chars in HTML.
Please help me to rectify the errors. This is an Opencv feature extraction code.
from __future__ import division
import numpy as np
import cv2
ESC=27
camera = cv2.VideoCapture(0)
orb = cv2.ORB()
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
imgTrainColor=cv2.imread('train.jpg')
imgTrainGray = cv2.cvtColor(imgTrainColor, cv2.COLOR_BGR2GRAY)
kpTrain = orb.detect(imgTrainGray,None)
kpTrain, desTrain = orb.compute(imgTrainGray, kpTrain)
firsttime=True
while True:
ret, imgCamColor = camera.read()
imgCamGray = cv2.cvtColor(imgCamColor, cv2.COLOR_BGR2GRAY)
kpCam = orb.detect(imgCamGray,None)
kpCam, desCam = orb.compute(imgCamGray, kpCam)
matches = bf.match(desCam,desTrain)
dist = [m.distance for m in matches]
thres_dist = (sum(dist) / len(dist)) * 0.5
matches = [m for m in matches if m.distance < thres_dist]
if firsttime==True:
h1, w1 = imgCamColor.shape[:2]
h2, w2 = imgTrainColor.shape[:2]
nWidth = w1+w2
nHeight = max(h1, h2)
hdif = (h1-h2)/2
firsttime=False
result = np.zeros((nHeight, nWidth, 3), np.uint8)
result[hdif:hdif+h2, :w2] = imgTrainColor
result[:h1, w2:w1+w2] = imgCamColor
for i in range(len(matches)):
pt_a=(int(kpTrain[matches[i].trainIdx].pt[0]), int(kpTrain[matches[i].trainIdx].pt[1]+hdif))
pt_b=(int(kpCam[matches[i].queryIdx].pt[0]+w2), int(kpCam[matches[i].queryIdx].pt[1]))
cv2.line(result, pt_a, pt_b, (255, 0, 0))
cv2.imshow('Camara', result)
key = cv2.waitKey(30)
if key == ESC:
break
cv2.destroyAllWindows()
camera.release()
ERRORS APPEARING:
Traceback (most recent call last):
File "sift.py", line 39, in
result[hdif:hdif+h2, :w2] = imgTrainColor
ValueError: could not broadcast input array from shape (700,227,3) into shape (0,227,3)
Without digging through your code in detail
result[hdif:hdif+h2, :w2] = imgTrainColor
... from shape (700,227,3) into shape (0,227,3)
I duduce that imgTrainColor is 3d with shape (700,227,3).
result must has (3,) last dimension; the :w2 must be slicing 227 vales. But the hdif:hdif+h2 is slicing 0, probably because h2 is 0.
In other words, you are trying to put the imgTrainColor values into a block of result that is too small.
Can I leave to you to figure out why h2 is wrong? Another possibility is the hdif is too large (>700). You may need to print those indexing values just before this error.
Oh, and clean up the indentation.