I have a project for face recognition in Python, but when I try to open the camera for recognition, it does not open. May be there is something wrong in my code, but I do not know where the problem is.
its the code app.py
maybe the wrong be in here def of facerecognition
`def face_recognition(): # generate frame by frame from camera
def draw_boundary(img, classifier, scaleFactor, minNeighbors, color, text, clf):
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
features = classifier.detectMultiScale(gray_image, scaleFactor, minNeighbors)
global justscanned
global pause_cnt
pause_cnt += 1
coords = []
for (x, y, w, h) in features:
cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
id, pred = clf.predict(gray_image[y:y + h, x:x + w])
confidence = int(100 * (1 - pred / 300))
if confidence > 85 and not justscanned:
global cnt
cnt += 1
n = (100 / 30) * cnt
# w_filled = (n / 100) * w
w_filled = (cnt / 30) * w
cv2.putText(img, str(int(n))+' %', (x + 20, y + h + 28), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (153, 255, 255), 2, cv2.LINE_AA)
cv2.rectangle(img, (x, y + h + 40), (x + w, y + h + 50), color, 2)
cv2.rectangle(img, (x, y + h + 40), (x + int(w_filled), y + h + 50), (153, 255, 255), cv2.FILLED)
mycursor.execute("select a.img_person, b.prs_name, b.prs_skill "
" from img_dataset a "
" left join prs_mstr b on a.img_person = b.prs_nbr "
" where img_id = " + str(id))
row = mycursor.fetchone()
pnbr = row[0]
pname = row[1]
pskill = row[2]
if int(cnt) == 30:
cnt = 0
mycursor.execute("insert into accs_hist (accs_date, accs_prsn) values('"+str(date.today())+"', '" + pnbr + "')")
mydb.commit()
cv2.putText(img, pname + ' | ' + pskill, (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (153, 255, 255), 2, cv2.LINE_AA)
time.sleep(1)
justscanned = True
pause_cnt = 0
else:
if not justscanned:
cv2.putText(img, 'UNKNOWN', (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2, cv2.LINE_AA)
else:
cv2.putText(img, ' ', (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2,cv2.LINE_AA)
if pause_cnt > 80:
justscanned = False
coords = [x, y, w, h]
return coords`
Related
This question already has answers here:
How to fix IndexError: invalid index to scalar variable
(6 answers)
Closed 2 months ago.
Im getting an error every time i run this script in my environment
Traceback (most recent call last):
File "FaceMark.py", line 55, in <module>
i = i[0]
IndexError: invalid index to scalar variable.
The camera does turns on but as soon it detects my hand or face the camera windows shuts down and it throws me the error posted above.
here's the entire script:
import cv2
import mediapipe as mp
import time
import numpy as np
thres = 0.45 # Threshold to detect object
nms_threshold = 0.2`your text`
cap = cv2.VideoCapture()
cap.set(3, 1280)
cap.set(4, 720)
cap.set(10, 150)
classNames = []
classFile = 'coco.names'
with open(classFile, 'rt') as f:
classNames = f.read().rstrip('\n').split('\n')
configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath = 'frozen_inference_graph.pb'
net = cv2.dnn_DetectionModel(weightsPath, configPath)
net.setInputSize(320, 320)
net.setInputScale(1.0 / 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
cap = cv2.VideoCapture(0)
pTime = 0
cTime = 0
mpDraw = mp.solutions.drawing_utils
mpFaceMesh = mp.solutions.face_mesh
faceMesh = mpFaceMesh.FaceMesh(max_num_faces=2)
drawSpec = mpDraw.DrawingSpec(thickness=1, circle_radius=2)
mpHands = mp.solutions.hands
hands = mpHands.Hands()
mpDrawHand = mp.solutions.drawing_utils
while True:
success, img = cap.read()
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
classIds, confs, bbox = net.detect(img, confThreshold=thres)
bbox = list(bbox)
confs = list(np.array(confs).reshape(1, -1)[0])
confs = list(map(float, confs))
indices = cv2.dnn.NMSBoxes(bbox, confs, thres, nms_threshold)
results = faceMesh.process(imgRGB)
resultsHand = hands.process(imgRGB)
for i in indices:
i = i[0]
box = bbox[i]
# colors = np.random.uniform(0, 255, size=(len(box), 3))
x, y, w, h = box[0], box[1], box[2], box[3]
cv2.rectangle(img, (x, y), (x + w, h + y), color=(0, 255, 0), thickness=2)
cv2.putText(img, classNames[classIds[i][0] - 1].upper(), (box[0] + 10, box[1] + 30),
cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0), 2)
print("Objects Ids: ", classIds)
if resultsHand.multi_hand_landmarks:
for handLms in resultsHand.multi_hand_landmarks:
for id, lm in enumerate(handLms.landmark):
print(id, lm)
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
cv2.circle(img, (cx, cy), 5, (255, 0, 255), cv2.FILLED)
mpDrawHand.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)
if results.multi_face_landmarks:
for faceLms in results.multi_face_landmarks:
mpDraw.draw_landmarks(img, faceLms, mpFaceMesh.FACE_CONNECTIONS,
drawSpec, drawSpec)
for id, lm in enumerate(faceLms.landmark):`
# print(lm)
ih, iw, ic = img.shape
x, y = int(lm.x * iw), int(lm.y * ih)
print("Face id: ", id, x, y)
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(img, f'FPS: {int(fps)}', (20, 70), cv2.FONT_HERSHEY_PLAIN,
3, (255, 0, 0), 3)
cv2.imshow('image', img)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
how can i solve the problem around the indexes loop? i have tried other solutions here in similar questions, but i havent had any luck.
The variable i returns an integer value. This is not a list or tuple. If you change the for loop as follows, it will likely work.
for i in indices:
box = bbox[i]
# colors = np.random.uniform(0, 255, size=(len(box), 3))
x, y, w, h = box[0], box[1], box[2], box[3]
cv2.rectangle(img, (x, y), (x + w, h + y), color=(0, 255, 0), thickness=2)
cv2.putText(img, classNames[classIds[i] - 1].upper(), (box[0] + 10, box[1] + 30),
cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0), 2)
print("Objects Ids: ", classIds)
I'm using
OS : Ubuntu
Python : 3.8*
Opencv version: 4.6.0
What I'm trying to do is capture the emotion & save the feed..For that I've used opencv 4.0
Code I've
while True:
ret, frame = cap.read()
frame = cv2.resize(frame, (720, 480))
if not ret:
break
grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = detector(grayFrame, 0)
for rect in rects:
shape = predictor(grayFrame, rect)
points = shapePoints(shape)
(x, y, w, h) = rectPoints(rect)
grayFace = grayFrame[y:y + h, x:x + w]
try:
grayFace = cv2.resize(grayFace, (emotionTargetSize))
except:
continue
grayFace = grayFace.astype('float32')
grayFace = grayFace / 255.0
grayFace = (grayFace - 0.5) * 2.0
grayFace = np.expand_dims(grayFace, 0)
grayFace = np.expand_dims(grayFace, -1)
emotion_prediction = emotionClassifier.predict(grayFace)
emotion_probability = np.max(emotion_prediction)
if (emotion_probability > 0.36):
emotion_label_arg = np.argmax(emotion_prediction)
color = emotions[emotion_label_arg]['color']
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
cv2.line(frame, (x, y + h), (x + 20, y + h + 20),
color,
thickness=2)
cv2.rectangle(frame, (x + 20, y + h + 20), (x + 110, y + h + 40),
color, -1)
cv2.putText(frame, emotions[emotion_label_arg]['emotion'],
(x + 25, y + h + 36), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(255, 255, 255), 1, cv2.LINE_AA)
else:
color = (255, 255, 255)
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
out.write(frame)
#Wait for user input - q, then you will stop the loop
k = cv2.waitKey(1)
if k == 27:
break
cap.release()
if args["isVideoWriter"] == True:
videoWrite.release()
cv2.destroyAllWindows()
Here Issue with wait key ...when I hit on esc program is not exiting...I've tried all the possible methods...the only method that working is keyboard interruption i.e, ctrl+c
Method1: Not working :(
c = cv2.waitKey(0) % 256
if c == ord('a'):
break
Method 2: Not working :(
if cv2.waitKey(0) & 0xFF == ord('q'):
break
Did i missing anything?...Thanks in advance
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!!
import cv2
import numpy as np
import wx
class Main(wx.Frame):
def init(self):
wx.Frame.init(self,parent = None, title = "Baby Sleep Monitor", size = (700,700))
splitter = wx.SplitterWindow(self)
top = TopPanel(splitter)
bottom = BottomPanel(splitter,top)
splitter.SplitHorizontally(top, bottom)
splitter.SetMinimumPaneSize(470)
class TopPanel (wx.Panel):
def init(self,parent):
wx.Panel.init(self,parent = parent)
class BottomPanel (wx.Panel):
def init(self,parent,top):
wx.Panel.init(self,parent = parent)
self.graph = top
self.SetBackgroundColour("gray")
self.togglebuttonStart = wx.ToggleButton(self, id = -1, label = "Start", pos = (10,10))
self.togglebuttonStart.Bind(wx.EVT_TOGGLEBUTTON, self.OnStartClick)
labelchannels = wx.StaticText(self, -1, "Analogue Inputs", pos = (200, 20))
self.cb1 = wx.CheckBox(self, -1, label = "A0", pos = (200, 40))
self.cb2 = wx.CheckBox(self, -1, label = "A1", pos = (200, 55))
self.cb3 = wx.CheckBox(self, -1, label = "A2", pos = (200, 70))
self.cb4 = wx.CheckBox(self, -1, label = "A3", pos = (200, 85))
self.textboxSampleTime = wx.TextCtrl(self, -1, "1000", pos=(200, 115), size=(50, -1))
self.buttonSend = wx.Button(self, -1, "Send", pos=(250, 115), size=(50, -1))
self.buttonSend.Bind(wx.EVT_BUTTON, self.OnSend)
self.Bind(wx.EVT_CHECKBOX,self.OnChecked)
labelMinY = wx.StaticText(self,-1, "Min Y", pos = (400,10))
self.textboxMinYAxis = wx.TextCtrl(self,-1,"0",pos = (400,30))
labelMaxY = wx.StaticText(self, -1, "Max Y", pos=(400, 60))
self.textboxMaxYAxis = wx.TextCtrl(self, -1, "1024", pos=(400, 80))
self.buttonRange = wx.Button(self,-1,"Set Y Axis", pos = (400,105))
self.buttonRange.Bind(wx.EVT_BUTTON, self.SetButtonRange)
def SetButtonRange(self,event):
min = self.textboxMinYAxis.GetValue()
max = self.textboxMaxYAxis.GetValue()
print(max)
print(min)
self.graph.changeAxes(min,max)
def OnChecked(self, event):
cb = event.GetEventObject()
print("%s is clicked" % (cb.GetLabel()))
def OnStartClick(self,event):
val = self.togglebuttonStart.GetValue()
if val == True:
self.togglebuttonStart.SetLabel("Stop")
cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SCRIPT_COMPLEX
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
fullb_cascade = cv2.CascadeClassifier('haarcascade_fullbody.xml')
left_cascade = cv2.CascadeClassifier('haarcascade_profileface.xml')
right_cascade = cv2.CascadeClassifier('haarcascade_profileface.xml')
back_cascade = cv2.CascadeClassifier('backcascade.xml')
while 1:
value = self.togglebuttonStart.GetValue()
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
full_body = fullb_cascade.detectMultiScale(gray, 1.3, 2)
for (fx, fy, fw, fh) in full_body:
cv2.rectangle(img, (fx, fy), (fx + fw, fy + fh), (0, 255, 255), 2)
left_face = left_cascade.detectMultiScale(gray, 1.3, 5)
for (x2, y2, w2, h2) in left_face:
cv2.rectangle(img, (x2, y2), (x2 + w2, y2 + h2), (255, 0, 0), 2)
roi_gray = gray[y2:y2 + h2, x2:x2 + w2]
roi_color = img[y2:y2 + h2, x2:x2 + w2]
cv2.putText(img, str('left face'), (x2, y2 + h2), font, 1, 255)
back = back_cascade.detectMultiScale(gray, 1.3, 5)
for (x4, y4, w4, h4) in back:
cv2.rectangle(img, (x4, y4), (x4 + w4, y4 + h4), (0, 0, 255), 2)
cv2.putText(img, str('danger'), (x4, y4 + h4), font, 1, 255)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
roi_gray = gray[y:y + h, x:x + w]
roi_color = img[y:y + h, x:x + w]
cv2.putText(img, str('face'), (x, y + h), font, 1, 255)
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
cv2.imshow('img', img)
k = cv2.waitKey(30) & 0xff
if value == False:
break
cap.release()
cv2.destroyAllWindows()
else:
self.togglebuttonStart.SetLabel("Start")
def OnSend(self, event):
val = self.textboxSampleTime.GetValue()
print(val)
if name == "main":
app = wx.App()enter code here
frame = Main()
frame.Show()
app.MainLoop()
so here is my code, I'm using OpenCV's cascade files to detect face and eye position and then using it in the following part.
# Importing relevant libraries
import cv2 import os import tensorflow from keras.models import load_model import numpy as np import pygame import time
# Adding alarm to raise alert pygame.init() sound = pygame.mixer.Sound('alarm.wav')
# initialising -> cascade files into cascade classifier -> OpenCV
face = cv2.CascadeClassifier('/Users/ankush/Desktop/sleepy-driver-alert/haar-cascade-files/haarcascade_frontalface_alt.xml') l_eye = cv2.CascadeClassifier('/Users/ankush/Desktop/sleepy-driver-alert/haar-cascade-files/haarcascade_lefteye_2splits.xml') r_eye = cv2.CascadeClassifier('/Users/ankush/Desktop/sleepy-driver-alert/haar-cascade-files/haarcascade_righteye_2splits.xml')
# start-of-execution label = ['Close', 'Open']
# load model and video capture
model = load_model('/Users/ankush/Desktop/sleepy-driver-alert/models/cnnCat2.h5') path = os.getcwd() cap = cv2.VideoCapture(0) font = cv2.FONT_HERSHEY_COMPLEX_SMALL count = 0 score = 0 dep = 2 rpred = [99] lpred = [99]
while (True):
ret, frame = cap.read()
height, width = frame.shape[:2]
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face.detectMultiScale(gray, minNeighbors=5, scaleFactor=1.1, minSize=(25, 25))
left_eye_gray = r_eye.detectMultiScale(gray)
right_eye_gray = l_eye.detectMultiScale(gray)
cv2.rectangle(frame, (0, height - 50), (200, height),
(0, 0, 0), thickness=cv2.FILLED)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (100, 100, 100), 1)
# Next few lines check if eyes are open or not ->rpred,lpred variables
# right
for (x, y, w, h) in right_eye_gray:
r_eye = frame[y:y + h, x:x + w]
count = count + 1
r_eye = cv2.cvtColor(r_eye, cv2.COLOR_BGR2GRAY)
r_eye = cv2.resize(r_eye, (24, 24))
r_eye = r_eye / 255
r_eye = r_eye.reshape(24, 24, -1)
r_eye = np.expand_dims(r_eye, axis=0)
rpred = model.predict_classes(r_eye)
if (rpred[0] == 1):
label = 'Open'
if (rpred[0] == 0):
label = 'Closed'
break
# left
for (x, y, w, h) in left_eye_gray:
l_eye = frame[y:y + h, x:x + w]
count = count + 1
l_eye = cv2.cvtColor(l_eye, cv2.COLOR_BGR2GRAY)
l_eye = cv2.resize(l_eye, (24, 24))
l_eye = l_eye / 255
l_eye = l_eye.reshape(24, 24, -1)
l_eye = np.expand_dims(l_eye, axis=0)
lpred = model.predict_classes(l_eye)
if (lpred[0] == 1):
label = 'Open'
if (lpred[0] == 0):
label = 'Closed'
break
# if closed eyes detected
if (rpred[0] == 0 and lpred[0] == 0):
score = score + 1
cv2.putText(frame, "Closed", (10, height - 20), font, 1, (255, 255, 255), 1, cv2.LINE_AA)
# if for some reason only one is closed, driver is not sleeping
else:
score = score - 1
cv2.putText(frame, "Open", (10, height - 20), font, 1, (255, 255, 255), 1, cv2.LINE_AA)
if (score < 0):
score = 0
cv2.putText(frame, 'Score:' + str(score), (100, height - 20), font, 1, (255, 255, 255), 1, cv2.LINE_AA)
# if person is very sleepy, high score, eyes closed
if (score > 15):
cv2.imwrites(os.path.join(path, 'image.jpg'), frame)
try:
sound.play()
except:
pass
if (dep < 16):
dep = dep + 2
else:
dep = dep - 2
if (dep < 2):
dep = 2
cv2.rectangle(frame, (0, 0), (width, height), (0, 0, 255), dep)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release() cv2.destroyAllWindows()
And the error that I'm getting is;
Traceback (most recent call last): File
"/Users/ankush/Desktop/sleepy-driver-alert/Sleepy-driver-detector.py",
line 44, in
right_eye_gray = l_eye.detectMultiScale(gray) AttributeError: 'numpy.ndarray' object has no attribute 'detectMultiScale'
What am I doing wrong? I'm a beginner.