Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmacdermott committed Feb 25, 2024
2 parents aa14925 + 364e406 commit 9bc71db
Show file tree
Hide file tree
Showing 12 changed files with 879 additions and 95 deletions.
61 changes: 34 additions & 27 deletions AI/SpeedTesting.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import cv2
import numpy as np
from keras.models import model_from_json
import time
from keras.models import model_from_json

emotion_dict = {0: "Angry", 1: "Disgusted", 2: "Fearful", 3: "Happy", 4: "Neutral", 5: "Sad", 6: "Surprised"}

cumulativeEmotionArray = [0] *7

# load json and create model
json_file = open("AI\model\emotion_model.json", 'r')
json_file = open("AI/model/emotion_model.json", 'r')
loaded_model_json = json_file.read()
json_file.close()
emotion_model = model_from_json(loaded_model_json)

# load weights into new model
emotion_model.load_weights("AI\model\emotion_model.h5")
emotion_model.load_weights("AI/model/emotion_model.h5")
print("Loaded model from disk")

def colourBasedEmotion(emotion):
colourDict = {0: (0, 0, 255), 1: (0, 255, 0), 2: (255, 0, 255), 3: (0, 255, 255), 4: (255,255,255), 5: (255, 0, 0), 6: (0, 165, 255)}
return colourDict[emotion]

def takeSinglePhoto(frame):
face_detector = cv2.CascadeClassifier('AI\haarcascade_frontalface_default.xml')
face_detector = cv2.CascadeClassifier('AI/haarcascade_frontalface_default.xml')
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
num_faces = face_detector.detectMultiScale(gray_frame, scaleFactor=1.3, minNeighbors=5)
maxindex = -1
Expand All @@ -36,33 +37,39 @@ def takeSinglePhoto(frame):
# predict the emotions
emotion_prediction = emotion_model.predict(cropped_img)
maxindex = int(np.argmax(emotion_prediction))
if (maxindex != -1):
cumulativeEmotionArray[maxindex] += 1
return [maxindex, x, y, w, h]


def getCumulativeArray():
return cumulativeEmotionArray

def mainLoop(frame):

arrayOfEmotions = [0,0,0,0,0,0,0]
# arrayOfEmotions = [0,0,0,0,0,0,0]
frame = cv2.resize(frame, (1024, 576))
while True:
# Find haar cascade to draw bounding box around face
maxindex, x, y, w, h = takeSinglePhoto(frame)
if x != -1 and y != -1 and w != -1 and h != -1:
frame = cv2.rectangle(frame, (x, y-50), (x+w, y+h+10), colourBasedEmotion(maxindex), 4)
if maxindex != -1:
frame = cv2.putText(frame, emotion_dict[maxindex], (x+5, y-20), cv2.FONT_HERSHEY_SIMPLEX, 1, colourBasedEmotion(maxindex), 2, cv2.LINE_AA)
arrayOfEmotions[maxindex] +=1
# If more than 5 seconds have elapsed
# if needArray:
# return arrayOfEmotions
cv2.imshow('Emotion Detection', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
img_encode = cv2.imencode('.jpg', frame)[1]
data_encode = np.array(img_encode)
# byte_encode = data_encode.tobytes()
return img_encode
# while True:
# # Find haar cascade to draw bounding box around face
return takeSinglePhoto(frame)
# if x != -1 and y != -1 and w != -1 and h != -1:
# frame = cv2.rectangle(frame, (x, y-50), (x+w, y+h+10), colourBasedEmotion(maxindex), 4)
# if maxindex != -1:
# frame = cv2.putText(frame, emotion_dict[maxindex], (x+5, y-20), cv2.FONT_HERSHEY_SIMPLEX, 1, colourBasedEmotion(maxindex), 2, cv2.LINE_AA)
# arrayOfEmotions[maxindex] +=1
# # If more than 5 seconds have elapsed
# # if needArray:
# # return arrayOfEmotions
# cv2.imshow('Emotion Detection', frame)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
# img_encode = cv2.imencode('.jpg', frame)[1]
# data_encode = np.array(img_encode)
# # byte_encode = data_encode.tobytes()
# return img_encode


cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
print(mainLoop(frame))
# cap = cv2.VideoCapture(0)
# while True:
# ret, frame = cap.read()
# print(mainLoop(frame))
Binary file added AI/__pycache__/SpeedTesting.cpython-310.pyc
Binary file not shown.
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pillow = "*"
numpy = "*"
opencv-python-headless = "*"
flask-cors = "*"
keras = "*"
tensorflow = "*"

[dev-packages]

Expand Down
578 changes: 576 additions & 2 deletions Pipfile.lock

Large diffs are not rendered by default.

103 changes: 77 additions & 26 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from AI.SpeedTesting import mainLoop, getCumulativeArray
from APIStuff.OpenAPI.initialPrompt import main
from flask import Flask, request
from flask_cors import CORS, cross_origin
from flask_cors import CORS
from io import BytesIO
import json

import cv2
import base64
import numpy as np
from scipy import misc

#something


#somethin
app = Flask(__name__)

CORS(app)
Expand All @@ -25,41 +29,88 @@ def webp_to_jpg(webp_base64_string, output_file_path='output.jpg'):
# Open WebP image using Pillow
with Image.open(BytesIO(binary_data)) as webp_image:
# Save as JPG
webp_image.convert('RGB').save(output_file_path, format='JPEG')
webp_image.convert('RGB').save(output_file_path, format='JPEG')

# Example usage


@app.route("/", methods=["POST","GET"])
def index():
if request.method == "GET":
return "hello world indeed"
# print(test)
# print(request.headers)
return "hello world"

data = request.json["imageData"]
webp_to_jpg(data)
# print(jpg_original)
# img = cv2.imdecode(jpg_as_np, flags=1)
# print(img)
# cv2.imwrite('./images.webp', img)
image = Image.open(BytesIO(data))
numpy_array = np.array(image)

webp_to_jpg(numpy_array)


return {"1":"hello world"}


@app.route("/api/frame", methods=["POST","GET"])


@app.route("/api/frame", methods=["POST"])
def imageProcess():
if request.method == "GET":
return "hello world"
# print(test)
# print(request.headers)
data = request.json["imageData"]
webp_to_jpg(data)
# print(jpg_original)
# img = cv2.imdecode(jpg_as_np, flags=1)
# print(img)
# cv2.imwrite('./images.webp', img)

base64_string = request.json["imageData"]
_, data = base64_string.split(',', 1)
# Decode the base64 data
decoded_data = base64.b64decode(data)
# Create a PIL Image
image = Image.open(BytesIO(decoded_data))

return {"1":"hello world"}
with BytesIO() as jpeg_buffer:
image.save(jpeg_buffer, format="JPEG")
jpeg_buffer.seek(0)
jpeg_image = Image.open(jpeg_buffer)

# Now convert the JPEG image to a NumPy array
numpy_array = np.array(jpeg_image)

# Here, numpy_array is ready and you can use it with mainLoop or any other function
newImg = list(map(str, mainLoop(numpy_array)))
img_str = json.dumps(newImg)

# Send as JSON
return img_str


@app.route("/api/emotionsArray", methods=["POST"])
def getEmotionsArray():

array = getCumulativeArray()
array = main()

newImg = list(map(str, array))
img_str = json.dumps(newImg)

return img_str

@app.route("/api/promt", methods=["POST"])
def getGPTOutput():

# Get array from json request - might not work ahahahh loser
jsonResponse = request.json()

# Parse the JSON data
parsedData = json.loads(jsonResponse)

# Access the array
dataArray = parsedData['data']

prompt = main(dataArray)

# newImg = list(map(str, array))
# img_str = json.dumps(newImg)



return img_str





if __name__ == "__main__":
app.run(port=2223)
80 changes: 80 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"react-router-dom": "^6.22.1",
"react-scripts": "5.0.1",
"react-webcam": "^7.2.0",
"socket.io-client": "^4.7.4",
"web-vitals": "^2.1.4"
},
"scripts": {
Expand Down
38 changes: 38 additions & 0 deletions frontend/src/components/BoundingBox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useState, useEffect } from 'react';

const BoundingBox = ({ boxData }) => {
const [ignore, x, y, width, height] = boxData;

const emotion_dict = {0: "Angry", 1: "Disgusted", 2: "Fearful", 3: "Happy", 4: "Neutral", 5: "Sad", 6: "Surprised"}
const colour_dict = {0: "red", 1: "green", 2: "pink", 3: "yellow", 4: "white", 5: "blue", 6: "orange"}
const colour = colour_dict[ignore]
const textStyle = {
position: 'absolute',
top: `${y-25}px`,
left: `${x}px`,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
color: colour, // Customize text color
fontSize: '16px', // Customize font size
fontWeight: 'bold', // Customize font weight
};


return (
<div>
<div className='bounding-box'
style={{
top: `${y}px`,
left: `${x}px`,
left: `calc(8vw + ${x}px)`,
width: `${width}px`,
height: `${height}px`,
borderColor: colour,
}}
></div>
<div className='bounding-text' style={textStyle}>{emotion_dict[ignore]}</div>
</div>
);
};
export default BoundingBox
Loading

0 comments on commit 9bc71db

Please sign in to comment.