Commit 8f2e8e72 authored by NilanDMeegoda's avatar NilanDMeegoda

attention model optimization

parent 7463ea5a
import cv2 as cv
import numpy as np
from trainAnn import headPose,headDistence,facialExperssions,blinkCount,detector
import os
from keras.models import load_model
from sklearn.preprocessing import OneHotEncoder
......@@ -76,4 +76,7 @@ def captureImage(link,frameSkip):
if __name__=="__main__":
print("Controller working")
directory = os.getcwd()
destination = directory + r"\uploads\att.mp4"
scoreLst=captureImage(destination,10)
print(scoreLst)
......@@ -70,7 +70,7 @@ def headDistence(frame3,detector):
# Finding distance
f = 650
d = (W * f) / w
cvzone.putTextRect(frame3, f'Depth: {int(d)}cm',(facedis[10][0] - 100, facedis[10][1] - 50),scale=2)
# cvzone.putTextRect(frame3, f'Depth: {int(d)}cm',(facedis[10][0] - 100, facedis[10][1] - 50),scale=2)
return frame3,str(d),True
return frame3,False,False
......@@ -87,8 +87,8 @@ def blinkCount(frame2,detector):
frame2, faces = detector.findFaceMesh(frame2, draw=False)
if faces:
face = faces[0]
for id in idList:
cv.circle(frame2, face[id], 2, (255, 0, 255), cv.FILLED)
# for id in idList:
# cv.circle(frame2, face[id], 2, (255, 0, 255), cv.FILLED)
leftUp = face[159]
leftDown = face[23]
......@@ -98,8 +98,8 @@ def blinkCount(frame2,detector):
lengthVerticl, _ = detector.findDistance(leftUp, leftDown)
lengthHorz, _ = detector.findDistance(leftLEnd, leftREnd)
cv.line(frame2, leftUp, leftDown, (0, 200, 0), 2)
cv.line(frame2, leftLEnd, leftREnd, (0, 200, 0), 2)
# cv.line(frame2, leftUp, leftDown, (0, 200, 0), 2)
# cv.line(frame2, leftLEnd, leftREnd, (0, 200, 0), 2)
ratio = int((lengthVerticl / lengthHorz) * 100)
ratioList.append(ratio)
......@@ -119,8 +119,8 @@ def blinkCount(frame2,detector):
counter = 0
# writer3.writerow([blinkCounter])
cvzone.putTextRect(frame2, f'Blink Time: {blinkTime}', (20, 50), 1, 2, (255, 255, 255), (255, 0, 0), cv.FONT_HERSHEY_SIMPLEX)
imgPlot = plotY.update(ratioAvg, color=(255, 0, 0))
# cvzone.putTextRect(frame2, f'Blink Time: {blinkTime}', (20, 50), 1, 2, (255, 255, 255), (255, 0, 0), cv.FONT_HERSHEY_SIMPLEX)
# imgPlot = plotY.update(ratioAvg, color=(255, 0, 0))
return frame2,blinkTime
......@@ -143,9 +143,9 @@ def headPose(image,imgCount=0):
for face_landmarks in results.multi_face_landmarks:
for idx, lm in enumerate(face_landmarks.landmark):
if idx == 33 or idx == 263 or idx == 1 or idx == 61 or idx == 291 or idx == 199:
if idx == 1:
nose_2d = (lm.x * img_w, lm.y * img_h)
nose_3d = (lm.x * img_w, lm.y * img_h, lm.z * 3000)
# if idx == 1:
# nose_2d = (lm.x * img_w, lm.y * img_h)
# nose_3d = (lm.x * img_w, lm.y * img_h, lm.z * 3000)
x, y = int(lm.x * img_w), int(lm.y * img_h)
......@@ -185,44 +185,44 @@ def headPose(image,imgCount=0):
z = angles[2] * 360
# See where the user's head tilting
if y < -10:
text = "Looking Left"
elif y > 10:
text = "Looking Right"
elif x < -10:
text = "Looking Down"
elif x > 10:
text = "Looking Up"
else:
text = "Forward"
# if y < -10:
# text = "Looking Left"
# elif y > 10:
# text = "Looking Right"
# elif x < -10:
# text = "Looking Down"
# elif x > 10:
# text = "Looking Up"
# else:
# text = "Forward"
# Display the nose direction
nose_3d_projection, jacobian = cv.projectPoints(nose_3d, rot_vec, trans_vec, cam_matrix, dist_matrix)
# nose_3d_projection, jacobian = cv.projectPoints(nose_3d, rot_vec, trans_vec, cam_matrix, dist_matrix)
p1 = (int(nose_2d[0]), int(nose_2d[1]))
p2 = (int(nose_2d[0] + y * 10), int(nose_2d[1] - x * 10))
# p1 = (int(nose_2d[0]), int(nose_2d[1]))
# p2 = (int(nose_2d[0] + y * 10), int(nose_2d[1] - x * 10))
cv.line(image, p1, p2, (255, 0, 0), 3)
# cv.line(image, p1, p2, (255, 0, 0), 3)
# Add the text on the image
# writer.writerow([str(np.round(x, 2)), str(np.round(y, 2)), str(np.round(z, 2)), text])
text=str(imgCount+1)+" "+text
# text=str(imgCount+1)+" "+text
cvzone.putTextRect(image, text, (20, 50), 1, 2, (255, 255, 255),
(255, 0, 0), cv.FONT_HERSHEY_SIMPLEX)
cv.putText(image, "x: " + str(np.round(x, 2)), (500, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv.putText(image, "y: " + str(np.round(y, 2)), (500, 100), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv.putText(image, "z: " + str(np.round(z, 2)), (500, 150), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# cvzone.putTextRect(image, text, (20, 50), 1, 2, (255, 255, 255),
# (255, 0, 0), cv.FONT_HERSHEY_SIMPLEX)
# cv.putText(image, "x: " + str(np.round(x, 2)), (500, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# cv.putText(image, "y: " + str(np.round(y, 2)), (500, 100), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# cv.putText(image, "z: " + str(np.round(z, 2)), (500, 150), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
end = time.time()
totalTime = end - start
# totalTime = end - start
try:
fps = 1 / totalTime
except:
fps =0
# try:
# fps = 1 / totalTime
# except:
# fps =0
# print("FPS: ", fps)
cv.putText(image, f'FPS: {int(fps)}', (20, 450), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
# cv.putText(image, f'FPS: {int(fps)}', (20, 450), cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
mp_drawing.draw_landmarks(
image=image,
......@@ -255,10 +255,10 @@ def facialExperssions(image):
img_arr = np.asarray(image2).reshape((224, 224, 3))
pre=model.predict(np.array([img_arr]))
predictions =np.argmax(pre,axis=1)
# predictions =np.argmax(pre,axis=1)
# print(expressions[predictions[0]])
cv.putText(image,expressions[predictions[0]], (x-padding, y-padding-20), cv.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 255), 2,cv.LINE_4,)
cv.rectangle(image, (x-padding, y-padding), (x + w+padding, y + h+padding), (255, 255, 255), thickness = 2)
# cv.putText(image,expressions[predictions[0]], (x-padding, y-padding-20), cv.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 255), 2,cv.LINE_4,)
# cv.rectangle(image, (x-padding, y-padding), (x + w+padding, y + h+padding), (255, 255, 255), thickness = 2)
# cv.imshow("image",image)
return image,pre,True
except:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment