FearBooth/main.py

141 lines
6.3 KiB
Python
Raw Normal View History

2023-10-16 06:51:57 -05:00
import math
2023-10-19 06:22:05 -05:00
import time
2023-10-16 06:51:57 -05:00
import numpy as np
import cv2 #pip install opencv-python ||| pip3 install opencv-contrib-python==4.4.0.46
2023-10-20 05:12:12 -05:00
from gpiozero import Button
2023-10-19 08:36:07 -05:00
from escpos.printer import Serial
2023-10-16 06:51:57 -05:00
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from deepface import DeepFace #pip install deepface
2023-10-20 05:12:12 -05:00
button = Button(2)
2023-10-16 06:51:57 -05:00
2023-10-25 13:55:55 -05:00
cam = cv2.VideoCapture(0)
2023-10-20 07:03:10 -05:00
cv2.namedWindow("webcam", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("webcam",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
2023-10-26 12:20:13 -05:00
printer = Serial(devfile='/dev/serial0',baudrate=19200,bytesize=8,parity='N',stopbits=1.00,dsrdtr=True)
2023-10-26 11:53:48 -05:00
# printer.set(density=10)
2023-10-19 06:22:05 -05:00
cameraMode = False
TIMER = 5
2023-10-16 06:51:57 -05:00
2023-10-19 06:22:05 -05:00
startScreen = cv2.imread("noFace.png")
2023-10-16 06:51:57 -05:00
2023-10-15 05:17:13 -05:00
if cam.isOpened():
while True:
ret, img = cam.read()
2023-10-20 06:55:44 -05:00
# img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
2023-10-19 06:22:05 -05:00
if cameraMode and ret:
prev = time.time()
2023-10-21 07:04:02 -05:00
while TIMER >= 0:
2023-10-19 06:22:05 -05:00
ret, img = cam.read()
2023-10-20 07:03:10 -05:00
# img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
2023-10-21 05:28:52 -05:00
# cv2.putText(img, str(TIMER), (200, 250), cv2.FONT_HERSHEY_SIMPLEX, 7, (0, 255, 255), 4, cv2.LINE_AA)
2023-10-27 03:52:22 -05:00
img = cv2.flip(img, 0)
2023-10-21 05:33:31 -05:00
img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
2023-10-19 06:22:05 -05:00
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img = Image.fromarray(img)
draw = ImageDraw.Draw(img)
2023-10-21 07:42:16 -05:00
2023-10-27 04:13:49 -05:00
fontBIG = ImageFont.truetype("HalloweenFont.ttf", 350)
2023-10-21 07:42:16 -05:00
fontSmall = ImageFont.truetype("HalloweenFont.ttf", 60)
2023-10-21 07:04:02 -05:00
if TIMER>0:
2023-10-27 07:23:03 -05:00
draw.text((130, 150), str(TIMER), font=fontBIG,fill=(255,0,0,255))
2023-10-21 07:34:55 -05:00
else:
2023-10-27 06:57:51 -05:00
2023-10-21 07:46:10 -05:00
draw.text((30, 480), "ANALYZING...", font=fontSmall,fill=(255,0,0,255))
2023-10-19 06:22:05 -05:00
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
2023-10-21 05:33:31 -05:00
img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
2023-10-27 03:50:40 -05:00
2023-10-19 06:22:05 -05:00
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()
2023-10-27 03:52:22 -05:00
img = cv2.flip(img, 0)
2023-10-21 05:33:31 -05:00
img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
2023-10-20 06:55:44 -05:00
# img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
2023-10-27 04:01:12 -05:00
try:
predictions = DeepFace.analyze(img,actions=['emotion'])
fearValue = predictions[0]["emotion"]["fear"]
surpriseValue = predictions[0]["emotion"]["surprise"]
fearPoint = max(fearValue,surpriseValue)
print("FEAR:" + str(round(fearPoint,2)))
print("SURPRISE:" + str(round(surpriseValue,2)))
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgGray = cv2.equalizeHist(imgGray)
cv2.imwrite('scared.jpg', imgGray)
2023-10-27 07:03:11 -05:00
cv2.imwrite("/home/gogo/Desktop/Photos/Fear_{}.jpg".format(cur),img)
2023-10-27 04:01:12 -05:00
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img = Image.fromarray(img)
draw = ImageDraw.Draw(img)
2023-10-27 04:07:39 -05:00
font_size = 75
2023-10-27 04:01:12 -05:00
font = ImageFont.truetype("HalloweenFont.ttf", font_size)
text = "FEAR LEVEL"
2023-10-27 04:07:39 -05:00
draw.text((30, 452), str(text), font=font,fill=(255,0,0,255))
2023-10-27 04:01:12 -05:00
img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
2023-10-21 07:46:10 -05:00
2023-10-27 04:01:12 -05:00
cv2.rectangle(img,(30,550),(30+math.floor(int(fearPoint)*420/100),600),(255,255,255), -1)
cv2.rectangle(img,(30,550),(450,600),(0,0,255), 8)
img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
2023-10-27 03:50:40 -05:00
2023-10-27 04:01:12 -05:00
cv2.imshow('webcam',img)
cv2.waitKey(2000)
if fearPoint>0:
#rotoImg = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
# cv2.imshow('webcam',img)
basewidth = 384
imgCrop = Image.open('scared.jpg')
wpercent = (basewidth/float(imgCrop.size[0]))
hsize = int((float(imgCrop.size[1])*float(wpercent)))
imgCrop = imgCrop.resize((basewidth,hsize), Image.Resampling.LANCZOS)
imgCrop = imgCrop.save("cropScared.jpg")
# cv2.waitKey(2000)
2023-10-27 06:43:32 -05:00
printer.set(align='center',font='a',width=2,height=2)
2023-10-27 04:01:12 -05:00
printer.image("cropScared.jpg",high_density_vertical=True,high_density_horizontal=False,impl="bitImageRaster")
2023-10-27 07:17:43 -05:00
printer.text("Fear Level\n" + "%" + str(round(fearPoint,1))+"\n")
2023-10-27 04:01:12 -05:00
# printer.text("Surprise Level: \n" + str(round(surpriseValue,2))+"/100\n")
# printer.text("Overall: \n" + str(round(fearPoint,2))+"/100\n")
2023-10-27 07:09:36 -05:00
if fearPoint>80:
printer.text("(Scream Queen)\n")
if fearPoint<5:
printer.text("(The Killer)\n")
2023-10-27 04:01:12 -05:00
printer.text("\n\n\n\n")
#printer.set(align='center',font='b',width=1,height=1)
#printer.text("Spooky Night 2023")
#printer.text("2023\n")
# cv2.waitKey(5000)
except:
cameraMode = False
TIMER = 5
2023-10-21 06:47:35 -05:00
2023-10-16 06:51:57 -05:00
#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)
2023-10-21 05:28:52 -05:00
#cv2.rectangle(img,(30,400),(610,450),(255,255,255), 5)
#cv2.rectangle(img,(30,400),(30+math.floor(int(fearPoint)*580/100),450),(255,255,255), -1)
2023-10-21 06:50:31 -05:00
2023-10-21 06:52:20 -05:00
# cv2.waitKey(5000)
2023-10-19 06:22:05 -05:00
2023-10-15 05:17:13 -05:00
2023-10-19 06:22:05 -05:00
cameraMode = False
TIMER = 5
else:
cv2.imshow('webcam',startScreen)
2023-10-15 05:17:13 -05:00
key = cv2.waitKey(5) & 0xFF
2023-10-20 05:12:12 -05:00
if ord('t') == key or button.is_pressed:
2023-10-19 06:22:05 -05:00
cameraMode = True
2023-10-15 05:17:13 -05:00
if ord('q') == key:
break
2023-10-19 06:22:05 -05:00
2023-10-20 04:44:13 -05:00
cam.release()