how to write filename in utf-8 in python? - python

I am trying to save the frames in to "veriseti" file but framenames can not be shown regularly.
cam = cv2.VideoCapture(0)
harcascadePath = "haarcascade_frontalface_default.xml"
detector = cv2.CascadeClassifier(harcascadePath)
sampleNum = 0
while (True):
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
# incrementing sample number
sampleNum = sampleNum + 1
# saving the captured face in the dataset folder TrainingImage
cv2.imwrite("veriseti\ " + username1 + "." + id1 + '.' + str(sampleNum) + ".jpg", gray[y:y + h, x:x + w])*

Did you include all of your code? Your last line has an asterisk at the end so it made me wonder. I don't know what username1 or id1 are but I assume strings, you could wrap those in str(), couldn't hurt.

Related

cv2.face.LBPHFaceRecognizer_create().predict() is recognizing All users as my name

cv2.face.LBPHFaceRecognizer_create().predict() is showing all users with my name!
I was making a Face Recognition System; I almost succeed but I noticed, my name is showing for Elon Musk! Where it should be shown as Unknown!
Face Recognizer.py:
import json
import cv2
import numpy as np
print("Please press ESC to close!")
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('Trainer/trainer.yml')
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)
font = cv2.FONT_HERSHEY_SIMPLEX
id = 2
with open('index.json', 'r') as f:
db = json.load(f)
names = {}
for i in db:
for face in db['faces']:
names[face] = db['faces'][face]
cam = cv2.VideoCapture(0, cv2.CAP_DSHOW)
cam.set(3, 640)
cam.set(4, 480)
minW = 0.1 * cam.get(3)
minH = 0.1 * cam.get(4)
while True:
ret, img = cam.read()
converted_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
converted_image,
scaleFactor=1.2,
minNeighbors=5,
minSize=(int(minW), int(minH)),
)
for (x, y, w, h) in faces:
id, accuracy = recognizer.predict(converted_image[y:y + h, x:x + w])
if (accuracy < 100):
id = names[str(id)]
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
else:
id = "Unknown"
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
if cv2.imwrite(f"Unknowns/{str(datetime.datetime.now().strftime('%Y-%m-%d-%X')).replace(':', '_')}.jpg", img):
print("Image Saved!")
winsound.Beep(2000, 500)
cv2.putText(img, str(id), (x + 5, y - 5), font, 1, (255, 255, 255), 2)
cv2.imshow('Face Detection', img)
k = cv2.waitKey(10) & 0xff
if k == 27:
break
cam.release()
cv2.destroyAllWindows()
index.json:
{
"faces": {
"1": "Tahsin"
}
}
This is how I'm taking the Samples!
import json
import cv2
import numpy as np
from PIL import Image
import os
cam = cv2.VideoCapture(0,
cv2.CAP_DSHOW)
cam.set(3, 640)
cam.set(4, 480)
detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
face_id = input("Enter a Numeric user ID: ")
face_name = input("Enter a Name: ")
print("Taking samples, look at camera... ")
count = 0
while True:
ret, img = cam.read()
converted_image = cv2.cvtColor(img,
cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(converted_image, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
count += 1
cv2.imwrite("samples/face." + str(face_id) + '.' + str(count) + ".jpg", converted_image[y:y + h, x:x + w])
k = cv2.waitKey(100) & 0xff
if k == 27:
break
elif count >= 50:
break
cv2.imshow('image', img)
with open('index.json', 'r') as f:
db = json.load(f)
if 'faces' in db:
db['faces'][face_id] = face_name
else:
db['faces'] = {}
db['faces'][face_id] = face_name
with open('index.json', 'w') as f:
json.dump(db, f, indent=4)
print("Samples taken!")
cam.release()
cv2.destroyAllWindows()
path = 'samples'
recognizer = cv2.face.LBPHFaceRecognizer_create()
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
def Images_And_Labels(path):
imagePaths = [os.path.join(path, f) if f.split('.')[1] == face_id else None for f in os.listdir(path)]
faceSamples = []
ids = []
for imagePath in imagePaths:
if imagePath != None:
gray_img = Image.open(imagePath).convert('L')
img_arr = np.array(gray_img, 'uint8')
id = int(os.path.split(imagePath)[-1].split(".")[1])
faces = detector.detectMultiScale(img_arr)
for (x, y, w, h) in faces:
faceSamples.append(img_arr[y:y + h, x:x + w])
ids.append(id)
return faceSamples, ids
print("Training faces. It will take a few seconds. Wait ...")
faces, ids = Images_And_Labels(path)
recognizer.train(faces, np.array(ids))
recognizer.write('trainer/trainer.yml')
print("Model trained, Now we can recognize your face.")
Sample: Elon Musk is showing as Tahsin:

Opencv giving a C++ exception error with LBPHFaceRecognizer

i am trying face recognition with opencv's LBPH with GUI made by tkinter. the first time my program runs . but when i exit camera with 'q' button and start again with push button in GUI it gives error. i have tried many workarounds but still no answer. can someone help me out?
HERE IS MY CODE
recognizer.read('C:/data.yml')
id = 0
# set text style
fontface = cv2.FONT_HERSHEY_SIMPLEX
fontscale = 1
fontcolor = (203, 23, 252)
cam = cv2.VideoCapture(0)
name_to_track=value_.get()
print(name_to_track)
# get data from sqlite by ID
while (True):
# camera read
ret, img = cam.read()
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
result = face.process(rgb)
if result.detections:
for id_, detect in enumerate(result.detections):
bbox_ = detect.location_data.relative_bounding_box
ih, iw, ic = img.shape
bbox = int(bbox_.xmin * iw), int(bbox_.ymin * ih), \
int(bbox_.width * iw), int(bbox_.height * ih)
x = bbox[0]
y = bbox[1]
w = bbox[2]
h = bbox[3]
cv2.rectangle(img, bbox, (255, 0, 255), 2)
img_size=gray[y:y + h, x:x + w]
img_size=np.array(img_size)
print("x:"+str(x)+"y:"+str(y))
if(x<0 or y<0 or w <0 or h<0):
continue
id, conf = recognizer.predict(gray[y:y + h, x:x + w])
#print(conf)
if (conf < 50):
profile = getProfile(id)
# set text to window
if (profile != None):
# cv2.PutText(cv2.fromarray(img),str(id),(x+y+h),font,(0,0,255),2);
cv2.putText(img, "ID: " + str(profile[0]) + ' Acc:' + str(round(float(1 - conf / 100), 2)),
(x, y + h + 30), fontface, fontscale, fontcolor, 2)
if (profile[1] == name_to_track):
cv2.putText(img, "Tracking", (x, y + h + 60), fontface, fontscale, fontcolor, 2)
else:
cv2.putText(img, "Name " + str(profile[1]), (x, y + h + 60), fontface, fontscale, fontcolor, 2)
else:
cv2.putText(img, "Unknown", (x, y + h + 30), fontface, fontscale, [255, 0, 0], 2)
cv2.imshow("face", img)
if cv2.waitKey(1) == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
THANKS!!

Make eye tracker that only scans the upper 1/2 of my face

My original idea can be seen in the code below but it's still detecting eyes on the lower half of my face. The goal is to make it only scan the upper half of my face, therefore weeding out incorrect matches.
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
cap = cv2.VideoCapture(0) # sets up webcam
while 1: # capture frame, converts to greyscale, looks for faces
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces: # draws box around face
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
roi_gray = gray[y:y + h, x:x + w]
roi_color = img[y:y + h, x:x + w]
half_point = y
print("half point: " + str(half_point))
eyes = eye_cascade.detectMultiScale(roi_gray) # looks for eyes
for (ex, ey, ew, eh) in eyes: # draws boxes around eyes
check_point = ey
print("check_point: " + str(check_point))
if check_point > half_point:
pass
else:
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
cv2.imshow('img', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
I only modified the line 15 and 16
cv2.rectangle(img, (x, y), (x + w, y + int(h / 2)), (255, 0, 0), 2)
roi_gray = gray[y:y + int(h / 2), x:x + w]
The full code:
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
cap = cv2.VideoCapture(0) # sets up webcam
while 1: # capture frame, converts to greyscale, looks for faces
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces: # draws box around face
cv2.rectangle(img, (x, y), (x + w, y + int(h / 2)), (255, 0, 0), 2) #Modified
roi_gray = gray[y:y + int(h / 2), x:x + w] #Modified
roi_color = img[y:y + h, x:x + w]
half_point = y
print("half point: " + str(half_point))
eyes = eye_cascade.detectMultiScale(roi_gray) # looks for eyes
for (ex, ey, ew, eh) in eyes: # draws boxes around eyes
check_point = ey
print("check_point: " + str(check_point))
if check_point > half_point:
pass
else:
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
cv2.imshow('img', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
But, I recommend to use dlib insead. It's more reliable.
Here is my example:
import numpy as np
import cv2
import dlib
cap = cv2.VideoCapture(0)
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
detector = dlib.get_frontal_face_detector()
def draw_on_frame(eye):
coordinates = np.array([])
for i in eye:
x = landmarks.part(i).x
y = landmarks.part(i).y
cv2.circle(frame, (x, y), 3, (0, 0, 255), -1)
coordinates = np.append(coordinates, [x, y])
x1, y1, w1, h1 = cv2.boundingRect(coordinates.reshape(-1, 2).astype(int))
cv2.rectangle(frame, (x1, y1), (x1 + w1, y1 + h1), (0, 255, 0), 1)
return x1, y1, w1, h1
while (cap.isOpened()):
ret, frame = cap.read()
if ret == True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
for face in faces:
landmarks = predictor(gray, face)
left_eye = range(36, 42)
right_eye = range(42, 48)
left = draw_on_frame(left_eye)
right = draw_on_frame(right_eye)
roi_left = frame[left[1]:left[1]+left[3], left[0]:left[0]+left[2]]
roi_right = frame[right[1]:right[1] + right[3], right[0]:right[0] + right[2]]
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()

How to make the Speed Calculation being able to calculate from both direction (above to the bottom and from the bottom to the top)?

I am currently working on a project on human walking speed estimation by using Haar-cascade. The program works as such: the detected object passes two imaginary lines and when it passes the second line the program will show the speed. The currently program that I've worked only calculate the detected object that walks from above to the bottom, but I wanted the program will catch the calculation from below to the top too so it can calculating in both ways.
here's the program:
import cv2
import time
cascade_src = 'haarcascade_fullbody.xml'
video_src = 'video-1.mp4'
#line a
ax1=15
ay=225
ax2=600
#line b
bx1=15
by=275
bx2=600
#car num
i = 1
start_time = time.time()
#video ....
cap = cv2.VideoCapture(video_src)
human_cascade = cv2.CascadeClassifier(cascade_src)
videoWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
videoHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('wisuda-14.mp4', fourcc, 25.0, (videoWidth,videoHeight))
def Speed_Cal(time):
try:
Speed = (9.144*3600)/(time*1000)
return Speed
except ZeroDivisionError:
print (5)
while True:
ret, img = cap.read()
if (type(img) == type(None)):
break
#bluring to have exacter detection
blurred = cv2.blur(img, ksize=(3,3))
gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
human = human_cascade.detectMultiScale(gray, scaleFactor=1.04865, minNeighbors=6)
#line a #i know road has got
cv2.line(img,(ax1,ay),(ax2,ay),(255,0,0),2)
#line b
cv2.line(img,(bx1,by),(bx2,by),(255,0,0),2)
for (x,y,w,h) in human:
cv2.rectangle(img, (x,y), (x + w, y + h), (0, 0, 255), 2)
roi_blurred = blurred[x: x + h, y:y + w]
roi_gray = gray[x: x + h, y:y + w]
roi_img = img[x: x + h, y:y + w]
cv2.circle(img,(int((x+x+w)/2),int((y+y+h)/2)), 2,(0,255,0), -1)
#cv2.putText(img, "ID : " + str(i), (x, y-15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1);
while int(ay) == int((y+y+h)/2):
start_time = time.time()
break
while int(ay) <= int((y+y+h)/2):
if int(by) <= int((y+y+h)/2)&int(by+10) >= int((y+y+h)/2):
cv2.line(img, (bx1,by), (bx2,by), (0,255,0), 2)
Speed = Speed_Cal(time.time() - start_time)
print("ID Number "+str(i)+" Speed: " + str(int(Speed)))
i = i + 1
cv2.putText(img, "Speed: "+str(int(Speed))+"km/jam", (x,y), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2);
break
else :
break
out.write(img)
cv2.imshow('video', img)
cv2.imshow('Gray', gray)
cv2.imshow('Blurr', blurred)
if cv2.waitKey(33) == 27:
break
cap.release()
out.release()
cv2.destroyAllWindows()
Hope you can help guys

opencv TypeError: 'int' object is not iterable

I am trying to detect a face using OpenCV. I have a file recognizer.py as follows:
import cv2
faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cam = cv2.VideoCapture(0)
rec = cv2.face.createLBPHFaceRecognizer()
rec.load('recognizer/trainningData.yml')
id = 0
font = cv2.FONT_HERSHEY_SIMPLEX
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])
cv2.putText(img, str(id), (x, y + h), font, 255, (255, 0, 0))
cv2.imshow("Face", img)
if cv2.waitKey(1) == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
When I am trying to run this code, the program is running successfully and opening a camera window.
But whenever, I am trying to show my face in front of the camera, the program is terminating with exit code 1 and showing the following error:
Traceback (most recent call last):
File "/home/prateek/recognizer.py", line 15, in <module>
id, conf = rec.predict(gray[y:y + h, x:x + w])
TypeError: 'int' object is not iterable
Process finished with exit code 1
Means, I am getting the error on line 15 which is as follows:
id, conf = rec.predict(gray[y:y + h, x:x + w])
I don't know how to resolve this problem. I am using Python3 and OpenCV3.3.
Finally, I have got the solution. The problem with was version of opencv. This code is for opencv2.4 and I was trying to run it on opencv3.
Well, The final code for opencv3 is as following:
import cv2
faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cam = cv2.VideoCapture(0)
rec = cv2.face.createLBPHFaceRecognizer()
rec.load('recognizer/trainningData.yml')
id = 0
font = cv2.FONT_HERSHEY_SIMPLEX
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= rec.predict(gray[y:y + h, x:x + w])
cv2.putText(img, str(id), (x, y + h), font, 255, (255, 0, 0))
cv2.imshow("Face", img)
if cv2.waitKey(1) == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
There is no need to mention the variable conf.
rec.predict(gray[y:y + h, x:x + w]) is returning the id of the person from the database.

Categories