cv2.error: (-215) s >= 0 in function setSize - python

I get this error while I run my code. I try to catch a picture from a webcam with a raspberry pi but some time the first picture that I catch is empty. So I verify it like this
ret, img = cam.read();
if not ret: continue
What can I do to avoid this error?
Corrupt JPEG data: 1 extraneous bytes before marker 0xd1 OpenCV
Error: Assertion failed (s >= 0) in setSize, file
/home/pi/opencv-3.2.0/modules/core/src/matrix.cpp, line 307 Traceback
(most recent call last): File "total.py", line 248, in
facialReco(directory) File "total.py", line 236, in facialReco
id, dist, it = reco(faceDetec) File "total.py", line 188, in reco
recognizer.predict_collect(gray[yHMax:yHMax + hMax, xHMax:xHMax + wHMax], collector)
cv2.error: /home/pi/opencv-3.2.0/modules/core/src/matrix.cpp:307:
error: (-215) s >= 0 in function setSize
My whole code is:
def reco(faceDetec):
recognizer = cv2.face.createLBPHFaceRecognizer()
recognizer.load("recognizer/trainingData_LBPHF.yml")
id = 0
it = 0
dist = 0
cam = cv2.VideoCapture(0)
# prendre les rectangle ayant la plus grde largeur seulement.
while it < 20:
ret, img = cam.read();
if not ret: continue
cv2.imshow("Face", img);
cv2.waitKey(1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceDetec.detectMultiScale(
img,
scaleFactor=1.2,
minNeighbors=7,
minSize=(50, 50)
)
hMax=0
wHMax=0
xHMax=0
yHMax=0
for (x, y, w, h) in faces:
if h>hMax:
hMax=h
wHMax=w
xHMax=x
yHMax=y
collector = cv2.face.StandardCollector_create()
recognizer.predict_collect(gray[yHMax:yHMax + hMax, xHMax:xHMax + wHMax], collector)
if collector.getMinDist()<65:
it += 1
dist = dist + collector.getMinDist()
id = collector.getMinLabel()
numberOfRec(id)
cam.release()
cv2.destroyAllWindows()
req="SELECT studentId FROM Student WHERE numberOfRec=(SELECT MAX(numberOfRec) FROM Student);"
cursor.execute(req)
rows = cursor.fetchall()
for row in rows:
id=row[0]
req="UPDATE Student SET numberOfRec = %(numberOfRec)"
values = {"numberOfRec": 0}
cursor.execute(req, values)
db.commit()
return id, dist, it

I manage to correct the error by adding : 'if not img is None:'
def reco(faceDetec):
recognizer = cv2.face.createLBPHFaceRecognizer()
recognizer.load("recognizer/trainingData_LBPHF.yml")
id = 0
it = 0
dist = 0
cam = cv2.VideoCapture(0)
# prendre les rectangle ayant la plus grde largeur seulement.
while it < 20:
ret, img = cam.read();
if not img is None:
if not ret: continue
cv2.imshow("Face", img);
cv2.waitKey(1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceDetec.detectMultiScale(
img,
scaleFactor=1.2,
minNeighbors=7,
minSize=(50, 50)
)
hMax=0
wHMax=0
xHMax=0
yHMax=0
for (x, y, w, h) in faces:
if h>hMax:
hMax=h
wHMax=w
xHMax=x
yHMax=y
collector = cv2.face.StandardCollector_create()
recognizer.predict_collect(gray[yHMax:yHMax + hMax, xHMax:xHMax + wHMax], collector)
if collector.getMinDist()<65:
it += 1
dist = dist + collector.getMinDist()
id = collector.getMinLabel()
numberOfRec(id)
cam.release()
cv2.destroyAllWindows()
req="SELECT studentId FROM Student WHERE numberOfRec=(SELECT MAX(numberOfRec) FROM Student);"
cursor.execute(req)
rows = cursor.fetchall()
for row in rows:
id=row[0]
req="UPDATE Student SET numberOfRec = %(numberOfRec)"
values = {"numberOfRec": 0}
cursor.execute(req, values)
db.commit()
return id, dist, it

Related

OpenCV DNN forward() Overload resolution failed

I was tasked to create a football players detection using OpenCV (4.5.5) and python and found a problem where my notebook is returning these error codes but I am not knowledgeable enough in python and openCV to fix this:
<ipython-input-12-4322b53719b8> in <module>
19
20 net.setInput(blob)
---> 21 outs = net.forward(output_layers)
22 outs = get_players(outs, height, width)
23 for i in range(len(outs)):
error: OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'forward'
> Overload resolution failed:
> - Can't convert object to 'str' for 'outputName'
> - Can't parse 'outputBlobs'. Input argument doesn't provide sequence protocol
> - Can't parse 'outputBlobs'. Input argument doesn't provide sequence protocol
> - Can't parse 'outBlobNames'. Input argument doesn't provide sequence protocol
> - Can't parse 'outBlobNames'. Input argument doesn't provide sequence protocol
OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function 'forward'
> Overload resolution failed:
> - Can't convert object to 'str' for 'outputName'
> - Can't parse 'outputBlobs'. Input argument doesn't provide sequence protocol
> - Can't parse 'outputBlobs'. Input argument doesn't provide sequence protocol
> - Can't parse 'outBlobNames'. Input argument doesn't provide sequence protocol
> - Can't parse 'outBlobNames'. Input argument doesn't provide sequence protocol
Here's my full code that i'm running, need some guidance to resolve this issue or perhaps anyone has done a football video analysis using openCV and python. Thanks.
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import load_model
from numpy import argmax
model = load_model(r'C:/Users/Benjamin Tiopan/players-matching/files/model.h5')
cap = cv2.VideoCapture(r'C:/Users/Benjamin Tiopan/players-matching/data/video.avi')
temp=cv2.imread(r'C:/Users/Benjamin Tiopan/players-matching/data/temp.jpg',0)
ground=cv2.imread(r'C:/Users/Benjamin Tiopan/players-matching/data/dst.jpg')
wt, ht = temp.shape[::-1]
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
#if you want to write video
out = cv2.VideoWriter('match.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 20, (1920,1080))
out2 = cv2.VideoWriter('plane.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 20, (900,600))
if (cap.isOpened()== False):
print("Error opening video stream or file")
# Load Yolo
net = cv2.dnn.readNet('C:/Users/Benjamin Tiopan/players-matching/files/yolov3.weights', 'C:/Users/Benjamin Tiopan/players-matching/files/yolov3.cfg')
classes = []
with open('C:/Users/Benjamin Tiopan/players-matching/files/coco.names', "r") as f:
classes = [line.strip() for line in f.readlines()]
layer_names = net.getLayerNames()
output_layers = (layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers())
colors = np.random.uniform(0, 255, size=(len(classes), 3))
def plane(players,ball):
coptemp=ground.copy()
matrix=np.array([[ 2.56945407e-01, 5.90910632e-01, 1.94094537e+02],
[-1.33508274e-02, 1.37658562e+00, -8.34967286e+01],
[-3.41878940e-05, 1.31509536e-03, 1.00000000e+00]])
for p in players:
x=p[0]+int(p[2]/2)
y=p[1]+p[3]
pts3 = np.float32([[x,y]])
pts3o=cv2.perspectiveTransform(pts3[None, :, :],matrix)
x1=int(pts3o[0][0][0])
y1=int(pts3o[0][0][1])
pp=(x1,y1)
if(p[4]==0):
cv2.circle(coptemp,pp, 15, (255,0,0),-1)
elif p[4]==1:
cv2.circle(coptemp,pp, 15, (255,255,255),-1)
elif p[4]==2:
#print hakm
#cv2.circle(coptemp,pp, 15, (0,0,255),-1)
pass
if len(ball) !=0:
xb=ball[0]+int(ball[2]/2)
yb=ball[1]+int(ball[3]/2)
pts3ball = np.float32([[xb,yb]])
pts3b=cv2.perspectiveTransform(pts3ball[None, :, :],matrix)
x2=int(pts3b[0][0][0])
y2=int(pts3b[0][0][1])
pb=(x2,y2)
cv2.circle(coptemp,pb, 15, (0,0,0),-1)
return coptemp
def get_players(outs,height, width):
class_ids = []
confidences = []
boxes = []
players=[]
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
# Object detected
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
# Rectangle coordinates
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
for i in range(len(boxes)):
if i in indexes:
x, y, w, h = boxes[i]
label = str(classes[class_ids[i]])
if label=='person':
players.append(boxes[i])
return players
opr=0
while(cap.isOpened()):
ret, frame = cap.read()
players=[]
ball=[]
if opr<310:
opr=opr+1
continue
if ret == True :
copy=frame.copy()
gray= cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
height, width, channels = frame.shape
blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)
outs = get_players(outs, height, width)
for i in range(len(outs)):
x, y, w, h = outs[i]
roi = frame[y:y+h,x:x+w]
#some frames are bad so resize function throw an error
try:
roi=cv2.resize(roi, (96,96))
except:
continue
ym=model.predict(np.reshape(roi,(1,96,96,3)))
ym=argmax(ym)
players.append([x,y,w,h,ym])
if ym==0:
cv2.rectangle(copy, (x, y), (x + w, y + h), (0,0,255), 2)
elif ym==1:
cv2.rectangle(copy, (x, y), (x + w, y + h), (0,255,0), 2)
elif ym==2:
cv2.rectangle(copy, (x, y), (x + w, y + h), (255,0,0), 2)
res = cv2.matchTemplate(gray,temp,cv2.TM_SQDIFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
if min_val < 0.05:
top_left = min_loc
bottom_right = (top_left[0] + wt, top_left[1] + ht)
ball.append(top_left[0])
ball.append(top_left[1])
ball.append(wt)
ball.append(ht)
cv2.rectangle(copy,top_left, bottom_right, (0,255,100), 2)
p=plane(players, ball)
out.write(copy)
out2.write(p)
cv2.imshow('img',copy)
cv2.imshow('plane',p)
# this will run the video without stop and maybe the cv2 window will stop between every frame
# depending on your pc power ( i recommend to use (opencv with gpu) and colab to run script quickly )
# if you want script stop between every frame and manually you allow the script to continue change it t ocv2.waitKey(0)
if cv2.waitKey(1)==27:
break
# When everything done, release the video capture object
cap.release()
out.release()
out2.release()
# Closes all the frames
cv2.destroyAllWindows()

How use for loop many variable?

I am using Yolov5. I want change my webcam -> lancamera
class LoadStreams: # multiple IP or RTSP cameras
def __init__(self, sources='streams.txt', img_size=640):
self.mode = 'images'
self.img_size = img_size
if os.path.isfile(sources):
with open(sources, 'r') as f:
sources = [x.strip() for x in f.read().splitlines() if len(x.strip())]
else:
sources = [sources]
n = len(sources)
self.imgs = [None] * n
self.sources = sources
for i, s in enumerate(sources):
# Start the thread to read frames from the video stream
print('%g/%g: %s... ' % (i + 1, n, s), end='')
cap = cv2.VideoCapture(eval(s) if s.isnumeric() else s)
assert cap.isOpened(), 'Failed to open %s' % s
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS) % 100
_, self.imgs[i] = cap.read() # guarantee first frame
thread = Thread(target=self.update, args=([i, cap]), daemon=True)
print(' success (%gx%g at %.2f FPS).' % (w, h, fps))
thread.start()
print('') # newline
# check for common shapes
s = np.stack([letterbox(x, new_shape=self.img_size)[0].shape for x in self.imgs], 0) # inference shapes
self.rect = np.unique(s, axis=0).shape[0] == 1 # rect inference if all shapes equal
if not self.rect:
print('WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.')
def update(self, index, cap):
# Read next stream frame in a daemon thread
n = 0
while cap.isOpened():
n += 1
# _, self.imgs[index] = cap.read()
cap.grab()
if n == 4: # read every 4th frame
_, self.imgs[index] = cap.retrieve()
n = 0
time.sleep(0.01) # wait time
def __iter__(self):
self.count = -1
return self
def __next__(self):
self.count += 1
img0 = self.imgs.copy()
if cv2.waitKey(1) == ord('q'): # q to quit
cv2.destroyAllWindows()
raise StopIteration
# Letterbox
img = [letterbox(x, new_shape=self.img_size, auto=self.rect)[0] for x in img0]
# Stack
img = np.stack(img, 0)
# Convert
img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB, to bsx3x416x416
img = np.ascontiguousarray(img)
return self.sources, img, img0, None
def __len__(self):
return 0 # 1E12 frames = 32 streams at 30 FPS for 30 years
this code return 'self.sources, img, img0, None'
if webcam:
view_img = True
cudnn.benchmark = True # set True to speed up constant image size inference
dataset = LoadStreams(source, img_size=imgsz)
print((dataset))
I use 'dataset'
for path, img, im0s, vid_cap in dataset:
img = torch.from_numpy(img).to(device)
img = img.half() if half else img.float() # uint8 to fp16/32
img /= 255.0 # 0 - 255 to 0.0 - 1.0
How to use for path, img, im0s, vid_cap in dataset: ??
my lancam code
def livecame():
vimba = Vimba()
vimba.startup()
system = vimba.system()
system.run_feature_command("GeVDiscoveryAllOnce")
time.sleep(0.1)
camera_ids = vimba.camera_ids()
# for cam_id in camera_ids:
# print("Camera found: ", cam_id)
print(camera_ids[0])
c0 = vimba.camera(camera_ids[0])
c0.open()
pixel_format = c0.feature("PixelFormat")
pixel_format.value = "BayerBG8"
try:
c0.StreamBytesPerSecond = 100000000
except:
pass
frame = c0.new_frame()
frame.announce()
c0.start_capture()
try:
frame.queue_for_capture()
success = True
except:
success = False
c0.run_feature_command("AcquisitionStart")
c0.run_feature_command("AcquisitionStop")
frame.wait_for_capture(1000)
frame_data = frame.buffer_data()
k = cv2.waitKey(1)
if k == 0x1b:
cv2.destroyAllWindows()
if success:
img = np.ndarray(buffer=frame_data,
dtype=np.uint8,
shape=(frame.data.height, frame.data.width, 1))
img = cv2.cvtColor(img, cv2.COLOR_BAYER_BG2RGB)
img0 = img.copy()
img = img.tolist()
img = [letterbox(x, new_shape=(800,400), auto= True)[0] for x in img0]
#img = np.ascontiguousarray(img)
img = np.stack(img, 0)
#img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB, to bsx3x416x416
img = np.ascontiguousarray(img)
return ['0'], img, img0
but I use dataset = new_file.livecame()
I can see error ValueError: not enough values to unpack (expected 3, got 1)
in for path, img, im0s, vid_cap in dataset:
how to use many variable? in for loop?
In Python OpenCV, one way is simply to use zip.
for component in zip(contours, hierarchy):
cntr = component[0]
hier = component[1]

OpenCV error: (-215:Assertion failed) s >= 0 in function 'cv::setSize' in Face Recognition program: Python

I have written two scripts main_program.py and detector.py to do perform face detection and recognition. I first train the classifier then use it recognize faces. But the training isn't happening because the program exits with an error.
here is the code for both the scripts:
For main_program.py :-
import cv2
import os
import numpy as np
import detector as dec
test_img = cv2.imread(r'C:\Users\JasonPC\Desktop\FaceDetection\Test Images\j1.jpg')
faces_detected, gray_img = dec.faceDetect(test_img)
print('Faces Detected: ', faces_detected)
faces, faceID = dec.labels_for_training_data(r'C:\Users\JasonPC\Desktop\FaceDetection\Resources')
face_recognizer = dec.train_classifier(faces, faceID)
name = {0:'Obama', 1:'Trump'}
for faces in faces_detected:
(x, y, w, h) = face
roi_gray = gray_img[y : y + h, x : x + h]
label, confidence = face_recognizer.predict(roi_gray)
confidence('confidence: ', confidence)
print('label: ', label)
dec.draw_rect(test_img, face)
predicted_name = name[label]
dec.put_text(test_img, predicted_name, x, y)
result_img = cv2.resize(test_img, (200, 200))
cv2.imshow('Face Detection', result_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
And here the code for detector.py
import cv2
import os
import numpy as np
def faceDetect(img):
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
classifier = cv2.CascadeClassifier(r'C:\Users\JasonPC\Documents\CodeVault\Python\FaceDetection\Classifiers\haarcascade_frontalface_alt.xml')
# classifier = cv2.CascadeClassifier(r'C:\Users\JasonPC\Documents\CodeVault\Python\FaceRecognition\Classifier.xml')
# val = classifier.load(r'C:\Users\JasonPC\Documents\CodeVault\Python\FaceRecognition\Classifier.xml')
faces = classifier.detectMultiScale(gray_img, scaleFactor = 1.5, minNeighbors = 5 )
return faces, gray_img
def labels_for_training_data(directory):
faces = []
faceID = []
for path, subdirnames, filenames in os.walk(directory):
for filename in filenames:
if filename.startswith("."):
print('Skipping system file...')
continue
id = os.path.basename(path)
img_path = os.path.join(path, filename)
print('img_path: ', img_path)
print('id: ', id)
test_img = cv2.imread(img_path)
if test_img is None:
print('Image is not loaded properly')
continue
faces_rect, gray_img = faceDetect(test_img)
if len(faces_rect) != 1:
continue # since multiple faces has been detected in an single image
(x, y , w, h) = faces_rect[0]
roi_gray = gray_img[y : y + 1, x : x + h]
faces.append(roi_gray)
faceID.append(int(id))
return faces, faceID
def train_classifier(faces, faceID):
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
face_recognizer.train(faces, np.array(faceID))
return face_recognizer
def draw_rect(test_img, face):
(x, y , w, h) = face
cv2.rectangle(test_img, (x, y), (x + w, y + h), (255, 0, 0), thickness = 5)
def put_text(test_img, text, x, y):
cv2.putText(test_img, text, (x, y), cv2.FONT_HERSHEY_DUPLEX, 5, (255,0, 0), 6)
But I am stuck with an error!
Traceback (most recent call last):
File "c:/Users/JasonPC/Desktop/FaceDetection/main_program.py", line
29, in <module>
face_recognizer = dec.train_classifier(faces, faceID)
File "c:\Users\JasonPC\Desktop\FaceDetection\detector.py", line 44,
in train_classifier
face_recognizer.train(faces, np.array(faceID))
cv2.error: OpenCV(4.3.0) C:\projects\opencv-python\opencv\modules\core\src\matrix.cpp:235: error: (-215:Assertion failed) s >= 0 in function 'cv::setSize'
Please help me debug this error!
many many thanks!

YUV frame averaging using opencv

I am trying to read a yuv video, then perform frame averaging, however, I run into problems when doing so, my code is shown below (this was made by combining a yuv reader and well as code to split the frame into the separate y, u,v components as well as adding what I thought was the way to proceed with this)
I get an error that the argument should be int or none not float but doesn't reference the line, but I do know this has to do with the reading the yuv file part. I am trying to average every two frames within the video. The YUV file is 10bit, 420
import cv2
import numpy as np
class readYUV:
def __init__(self, filename, size):
self.height, self.width = size
self.frame_len = self.width * self.height * 3 / 2
self.f = open(filename, 'rb')
self.shape = (int(self.height*1.5), self.width)
def read_raw(self):
try:
raw = self.f.read(self.frame_len)
yuv = np.frombuffer(raw, dtype=np.uint8)
yuv = yuv.reshape(self.shape)
except Exception as e:
print (str(e))
return False, None
return True, yuv
def read(self):
ret, yuv = self.read_raw()
if not ret:
return ret, yuv
bgr = cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR_NV21)
return ret, bgr
def make_lut_u():
return np.array([[[i, 255 - i, 0] for i in range(256)]], dtype=np.uint8)
def make_lut_v():
return np.array([[[0, 255 - i, i] for i in range(256)]], dtype=np.uint8)
# otherwise, split the frame into its respective channels
def splitter(img):
cv2.imshow("frame", img)
cv2.waitKey(30)
img_yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
y, u, v = cv2.split(img_yuv)
lut_u, lut_v = make_lut_u(), make_lut_v()
# Convert back to BGR so we can apply the LUT and stack the images
y = cv2.cvtColor(y, cv2.COLOR_GRAY2BGR)
u = cv2.cvtColor(u, cv2.COLOR_GRAY2BGR)
v = cv2.cvtColor(v, cv2.COLOR_GRAY2BGR)
u_mapped = cv2.LUT(u, lut_u)
v_mapped = cv2.LUT(v, lut_v)
result = np.vstack([img, y, u_mapped, v_mapped])
return result
if __name__ == "__main__":
filename = "file.yuv"
size = (3840, 2160)
cap = readYUV(filename, size)
while 1:
ret, frame = cap.read()
cv2.waitKey(30)
result1 = splitter(frame)
ret2, frame2 = cap.read()
cv2.waitKey(30)
result2 = splitter(frame2)
result = (result1 +result2)/2
cv2.imwrite('average.png', result)
Thanks in advance

Face Recognition with multiple faces don't detect multiple faces

I've written a small program, which detects faces and saves them to an Train file for the recognition.
I have some trouble with this algorithm. Sometimes it throws the error, that the LBPH::Train was feed with empty data, which is wrong.
OpenCV Error: Unsupported format or combination of formats (Empty training data was given. You'll need more than one sample to learn a model.) in cv::LBPH::train, file ........\opencv\modules\contrib\src\facerec.cpp, line 917
Traceback (most recent call last):
Moreover the algorithm detects multiple faces, but recognizes it just as the same face, which is wrong.
Could someone give me a hint on what I'm missing?
import cv2
import os
import numpy as np
import sys
i = 0
global allFaces
global first
first = True
allFaces = []
cap = cv2.VideoCapture(0)
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
recognizer = cv2.createLBPHFaceRecognizer()
font=cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_COMPLEX_SMALL,1,1,0,1)
id = 0
class Face:
def __init__(self, id, face):
self.id = id
self.face = face
self.gatheredFaces = []
def main(self):
print("main")
def getFace(self):
return self.face
def setKnownFace(self):
self.known = False
def getKownFace(self):
return self.knwon
def getId(self):
return self.id
def setFacesInfo(self, frame, face):
x,y,h,w = face
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
self.gatheredFaces.append(gray[y:y+h, x:x+w])
# count = 0
# while (count != 10):
# gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# cv2.imshow("frame in set", frame)
# faces = faceCascade.detectMultiScale(gray)
# for face in faces:
# self.gatheredFaces.append(gray[y:y+h,x:x+w])
# cv2.imshow("gathered Faces", self.gatheredFaces[0])
# cv2.imwrite("dataSet/User"+ str(self.getId()) +".jpg", gray)
# count = count+1
# cv2.waitKey(30)
def getFacesInfo(self):
return self.gatheredFaces
def trainDetector(self):
faceSamples = []
Ids = []
print("laenge von gathered FAces")
print(len(allFaces[0].getFacesInfo()))
for (i) in range(len(allFaces)):
temp = allFaces[i].getFacesInfo()
for (j) in range(len(temp)):
imageNP = np.array(temp[j], 'uint8')
id = allFaces[i].getId()
faces = faceCascade.detectMultiScale(imageNP)
for (x,y,h,w) in faces:
faceSamples.append(imageNP)
Ids.append(id)
recognizer.train(faceSamples, np.array(Ids))
recognizer.save('recognizer/train.yml')
def updateDetector(self):
recognizer.load('recognizer/train.yml')
faceSamples = []
Ids = []
for (i) in range(len(allFaces)):
temp = allFaces[i].getFacesInfo()
for (j) in range(len(temp)):
imageNP = np.array(temp[j], 'uint8')
id = allFaces[i].getId()
faces = faceCascade.detectMultiScale(imageNP)
for (x,y,h,w) in faces:
faceSamples.append(imageNP)
Ids.append(id)
recognizer.update(faceSamples, np.array(Ids))
recognizer.save('recognizer/train.yml')
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow("actual Frame", frame)
cv2.imshow("gray", gray)
faces = faceCascade.detectMultiScale(gray, 1.3, 5)
print(faces)
for face in faces:
x,y,h,w = face
temp = Face(id, frame[y:y+h,x:x+w])
allFaces.append(temp)
temp = None
id = id+1
###Detector
detector = cv2.SIFT()
FLANN_INDEX_KDTREE = 0
flannParam = dict(algorithm = FLANN_INDEX_KDTREE, tree = 5)
flann = cv2.FlannBasedMatcher(flannParam,{})
trainImg = allFaces[0].getFace()
trainKP, trainDecs = detector.detectAndCompute(trainImg, None)
if((len(allFaces)==1) and first):
print("only one object in allFaces")
for i in range(10):
print(i)
allFaces[0].setFacesInfo(frame, face)
allFaces[0].trainDetector()
first = False
else:
for(i) in range(len(allFaces)):
QueryImg = cv2.cvtColor(allFaces[i].getFace(), cv2.COLOR_BGR2GRAY)
queryKP, queryDesc = detector.detectAndCompute(QueryImg, None)
matches = flann.knnMatch(queryDesc, trainDecs, k = 2)
goodMatch = []
for m, n in matches:
if(m.distance < 0.75 * n.distance):
goodMatch.append(m)
if(len(goodMatch) > 30):
print("good match")
#allFaces[i].
tp = []
qp = []
for m in goodMatch:
tp.append(trainKP[m.trainIdx].pt)
qp.append(queryKP[m.queryIdx].pt)
tp, qp = np.float32((tp, qp))
H, status = cv2.findHomography(tp, qp, cv2.RANSAC, 3.0)
allFaces.pop(len(allFaces)-1)
break
else:
print ("bad match")
for i in range(10):
allFaces[len(allFaces)-1].setFacesInfo(frame, face)
allFaces[len(allFaces)-1].updateDetector()
cv2.waitKey(10)
for (x,y,w,h) in faces:
cv2.rectangle(frame, (x,y), (x+w,y+h), (0,0,255),2)
tempid, conf = recognizer.predict(gray[y:y+h,x:x+w])
cv2.cv.PutText(cv2.cv.fromarray(frame), str(tempid),(x,y+h),font,(0,0,255))
cv2.waitKey(30)
cv2.imshow("detectedFace", frame)
cv2.waitKey(30)

Categories

Resources