fixed lagging issue

This commit is contained in:
gocivici 2023-10-19 14:22:05 +03:00
parent 088f772b5f
commit 67cee95f1e
3 changed files with 114 additions and 35 deletions

78
main.py
View File

@ -1,4 +1,5 @@
import math import math
import time
import numpy as np import numpy as np
import cv2 #pip install opencv-python ||| pip3 install opencv-contrib-python==4.4.0.46 import cv2 #pip install opencv-python ||| pip3 install opencv-contrib-python==4.4.0.46
@ -8,45 +9,44 @@ from PIL import ImageFont
from deepface import DeepFace #pip install deepface from deepface import DeepFace #pip install deepface
readings = np.array([]) cam = cv2.VideoCapture(0)
max_samples = 10
# img = cv2.imread("test.jpg")
cam = cv2.VideoCapture(1)
# cam.set(cv2.cv.CV_CAP_PROP_FPS, 10)
# cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
# cam.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
noFace = cv2.imread("noFace.png")
# ft = cv2.freetype.createFreeType2() cameraMode = False
# ft.loadFontData(fontFileName='HalloweenFont.ttf',id=0) TIMER = 5
# overlay = cv2.imread('feartext.png')
#custom resolution for CRT TV
# cam.set(3,768)
# cam.set(4,576)
#640 x 480 startScreen = cv2.imread("noFace.png")
if cam.isOpened(): if cam.isOpened():
while True: while True:
ret, img = cam.read() ret, img = cam.read()
if ret: if cameraMode and ret:
face = cv2.resize(img, (120,160), interpolation = cv2.INTER_AREA) prev = time.time()
#print(img.shape) while TIMER > 0:
try: ret, img = cam.read()
predictions = DeepFace.analyze(face,actions=['emotion']) # cv2.putText(img, str(TIMER), (200, 250), cv2.FONT_HERSHEY_SIMPLEX, 7, (0, 255, 255), 4, cv2.LINE_AA)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img = Image.fromarray(img)
draw = ImageDraw.Draw(img)
font_size = 350
font = ImageFont.truetype("HalloweenFont.ttf", font_size)
draw.text((231, 50), str(TIMER), font=font,fill=(255,0,0,255))
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
cv2.imshow('webcam',img)
# print(str(TIMER))
cur = time.time() #current time
if cur-prev >= 1:
prev = cur
TIMER = TIMER-1
key = cv2.waitKey(5) & 0xFF
if ord('q') == key:
break
else:
ret, img = cam.read()
predictions = DeepFace.analyze(img,actions=['emotion'])
fearPoint = predictions[0]["emotion"]["fear"] fearPoint = predictions[0]["emotion"]["fear"]
readings = np.append(readings, fearPoint)
avg = np.mean(readings)
if len(readings) == max_samples:
readings = np.delete(readings, 0)
print("FEAR:" + str(fearPoint)) print("FEAR:" + str(fearPoint))
print("AVG" + str(avg)) if fearPoint>20:
cv2.imwrite('scared.jpg', img)
# for x in range(6):
# avgNumber = avgNumber + fearPoint
# print(avgNumber)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img = Image.fromarray(img) img = Image.fromarray(img)
draw = ImageDraw.Draw(img) draw = ImageDraw.Draw(img)
@ -58,13 +58,21 @@ if cam.isOpened():
#print(30+math.floor(int(fearPoint)*580/100)) #print(30+math.floor(int(fearPoint)*580/100))
# ft.putText(img=img,text='TEST',org=(15, 70),fontHeight=60,color=(255, 255, 255),thickness=-1,line_type=cv2.LINE_AA,bottomLeftOrigin=True) # ft.putText(img=img,text='TEST',org=(15, 70),fontHeight=60,color=(255, 255, 255),thickness=-1,line_type=cv2.LINE_AA,bottomLeftOrigin=True)
cv2.rectangle(img,(30,400),(610,450),(255,255,255), 5) cv2.rectangle(img,(30,400),(610,450),(255,255,255), 5)
cv2.rectangle(img,(30,400),(30+math.floor(int(avg)*580/100),450),(255,255,255), -1) cv2.rectangle(img,(30,400),(30+math.floor(int(fearPoint)*580/100),450),(255,255,255), -1)
cv2.imshow('webcam',img) cv2.imshow('webcam',img)
except Exception as e: cv2.waitKey(5000)
print(e)
cv2.imshow('webcam',noFace)
cameraMode = False
TIMER = 5
else:
cv2.imshow('webcam',startScreen)
key = cv2.waitKey(5) & 0xFF key = cv2.waitKey(5) & 0xFF
if ord('t') == key:
cameraMode = True
if ord('q') == key: if ord('q') == key:
break break
cam.release() cam.release()

71
realtime.py Normal file
View File

@ -0,0 +1,71 @@
import math
import numpy as np
import cv2 #pip install opencv-python ||| pip3 install opencv-contrib-python==4.4.0.46
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from deepface import DeepFace #pip install deepface
readings = np.array([])
max_samples = 10
# img = cv2.imread("test.jpg")
cam = cv2.VideoCapture(0)
# cam.set(cv2.cv.CV_CAP_PROP_FPS, 10)
# cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
# cam.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
noFace = cv2.imread("noFace.png")
# ft = cv2.freetype.createFreeType2()
# ft.loadFontData(fontFileName='HalloweenFont.ttf',id=0)
# overlay = cv2.imread('feartext.png')
#custom resolution for CRT TV
# cam.set(3,768)
# cam.set(4,576)
#640 x 480
if cam.isOpened():
while True:
ret, img = cam.read()
if ret:
# img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face = cv2.resize(img, (120,160), interpolation = cv2.INTER_AREA)
#print(img.shape)
try:
predictions = DeepFace.analyze(face,actions=['emotion'])
fearPoint = predictions[0]["emotion"]["fear"]
readings = np.append(readings, fearPoint)
avg = np.mean(readings)
if len(readings) == max_samples:
readings = np.delete(readings, 0)
print("FEAR:" + str(fearPoint))
print("AVG" + str(avg))
# for x in range(6):
# avgNumber = avgNumber + fearPoint
# print(avgNumber)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img = Image.fromarray(img)
draw = ImageDraw.Draw(img)
font_size = 65
font = ImageFont.truetype("HalloweenFont.ttf", font_size)
text = "FEAR LEVEL"
draw.text((144, 308), str(text), font=font,fill=(255,0,0,255))
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
#print(30+math.floor(int(fearPoint)*580/100))
# ft.putText(img=img,text='TEST',org=(15, 70),fontHeight=60,color=(255, 255, 255),thickness=-1,line_type=cv2.LINE_AA,bottomLeftOrigin=True)
cv2.rectangle(img,(30,400),(610,450),(255,255,255), 5)
cv2.rectangle(img,(30,400),(30+math.floor(int(avg)*580/100),450),(255,255,255), -1)
cv2.imshow('webcam',img)
except Exception as e:
print(e)
cv2.imshow('webcam',noFace)
key = cv2.waitKey(5) & 0xFF
if ord('q') == key:
break
cam.release()

BIN
scared.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB