Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/improve server #73

Merged
merged 7 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.tflite filter=lfs diff=lfs merge=lfs -text
*ttf filter=lfs diff=lfs merge=lfs -text
*mp4 filter=lfs diff=lfs merge=lfs -text
Binary file added assets/meme-gen-app-design.mp4
Binary file not shown.
172 changes: 172 additions & 0 deletions restapi/memegen/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import os
import random
import shutil
from datetime import datetime

import numpy as np
import cv2
from PIL import ImageFont, ImageDraw, Image
from flask import Flask, request, render_template, jsonify, send_file
from werkzeug.utils import secure_filename
import tensorflow as tf

app = Flask(__name__)

# Configuration
STATIC = os.getenv("UPLOAD_FOLDER", "./static")
app.config["UPLOAD_FOLDER"] = os.path.join(STATIC, "inputs")
app.config["RESULTS_FOLDER"] = os.path.join(STATIC, "results")
app.config["MAX_CONTENT_LENGTH"] = 16 * 1024 * 1024
ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg"}
LABELS = ["happy", "disgust", "neutral", "sad", "fear", "anger", "surprise"]


def allowed_file(filename):
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS


def get_interpreter(model_path):
interpreter = tf.lite.Interpreter(model_path=model_path)
interpreter.allocate_tensors()
return interpreter


def predict_emotion(interpreter, labels, frame):
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
interpreter.set_tensor(input_details[0]["index"], frame)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]["index"])
return {labels[i]: output_data[0][i] for i in range(len(labels))}


def detect_face(img):
face_cascade = cv2.CascadeClassifier(
"facedetection/haarcascade_frontalface_default.xml"
)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
if len(faces) == 0:
return False
for x, y, w, h in faces:
roi_gray = img[y : y + h, x : x + w]
cropped_img = np.expand_dims(
np.expand_dims(cv2.resize(roi_gray, (224, 224)), -1), 0
)
return roi_gray


def get_font_size(text):
length = len(text)
if length < 12:
return 36
elif length < 20:
return 26
else:
return 48 - length


def add_text_to_image(label, img):
label_file = f"memes/{label}.txt"
with open(label_file, "r") as f:
texts = [x.strip() for x in f]

chosen_text = random.choice(texts)
lines = chosen_text.split("|")
color = (255, 255, 255)
shadowcolor = (0, 0, 0)

img = cv2.resize(img, (300, 300))
cv2_im_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
pil_im = Image.fromarray(cv2_im_rgb)
draw = ImageDraw.Draw(pil_im)

for i, line in enumerate(lines):
font_size = get_font_size(line)
font = ImageFont.truetype("1.ttf", font_size)
x, y = 29 - len(line) + 2, 230 - 30 * (len(lines) - 1) + 30 * i
draw_text_with_border(draw, (x, y), line, font, color, shadowcolor)

return cv2.cvtColor(np.array(pil_im), cv2.COLOR_RGB2BGR)


def draw_text_with_border(draw, position, text, font, color, shadowcolor):
x, y = position
# thin border
draw.text((x - 1, y), text, font=font, fill=shadowcolor)
draw.text((x + 1, y), text, font=font, fill=shadowcolor)
draw.text((x, y - 1), text, font=font, fill=shadowcolor)
draw.text((x, y + 1), text, font=font, fill=shadowcolor)
# thicker border
draw.text((x - 1, y - 1), text, font=font, fill=shadowcolor)
draw.text((x + 1, y - 1), text, font=font, fill=shadowcolor)
draw.text((x - 1, y + 1), text, font=font, fill=shadowcolor)
draw.text((x + 1, y + 1), text, font=font, fill=shadowcolor)
# main text
draw.text((x, y), text, font=font, fill=color)


def save_image_with_logo(image_path, result_path):
Image1 = Image.open(image_path)
Image1copy = Image1.copy()
Image2 = Image.open("static/dontdelete/logo.jpeg")
Image2copy = Image2.copy()
Image2copy.putalpha(128)
Image2copy = Image2copy.resize((60, 30), Image.LANCZOS)
Image1copy.paste(Image2copy, (235, 265), Image2copy)
Image1copy.save(result_path)


def cleanup_folder(folder):
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
if os.path.isfile(file_path) and file_path.lower().endswith(('.png', '.jpg', '.jpeg')):
os.unlink(file_path)


@app.route("/")
def index():
return render_template("memslide.html")


@app.route("/", methods=["POST"])
def upload_file():
# Clean up previous files
cleanup_folder(app.config["UPLOAD_FOLDER"])
cleanup_folder(app.config["RESULTS_FOLDER"])

file = request.files.get("photo")
if file and allowed_file(file.filename):
now = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
filename = secure_filename(file.filename)
file_extension = filename.split(".")[-1]
saved_filename = f"{now}.{file_extension}"
file_path = os.path.join(app.config["UPLOAD_FOLDER"], saved_filename)
file.save(file_path)

img = cv2.imread(file_path)
face_img = detect_face(img)

if face_img is False:
img = add_text_to_image("noface", img)
else:
img = cv2.resize(img, (224, 224))
face_img = cv2.resize(face_img, (224, 224))
interpreter = get_interpreter("models/memegen1.tflite")
results = predict_emotion(interpreter, LABELS, [face_img])
dominant_emotion = max(results, key=results.get)
img = add_text_to_image(dominant_emotion, img)

result_path = os.path.join(app.config["RESULTS_FOLDER"], saved_filename)
cv2.imwrite(result_path, img)
save_image_with_logo(result_path, result_path)

result_dic = {"im": f"results/{saved_filename}"}
return render_template("memslide.html", image=result_dic)
else:
result_dic = {"im": "dontdelete/file.jpg"}
return render_template("memslide.html", image=result_dic)


if __name__ == "__main__":
app.run(host="0.0.0.0", port=1234)
54 changes: 53 additions & 1 deletion restapi/memegen/memes/anger.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,56 @@ Why are u taking the |backpack to the bathroom | *me: I'll kill you
Wen lecturers dont | give attendance after | sitting for whole 2 hrs
Wen ur guide doesnt|sign ur project report
Wen ur roommate| sets continuous alarms |but doesnt wake up
Wen u forget a semicolon |in a code and |it shows 3666 errors
Wen u forget a semicolon |in a code and |it shows 3666 errors
When your coworker | takes credit for | your idea in a meeting
That moment when | your internet goes out | during a crucial online test
When your boss | sends an email at 5:01 PM | on a Friday
The look on my face | when my favorite series | gets canceled
When someone cuts | in front of you in line | at the coffee shop
When you finally get | comfortable in bed and | remember you left | the kitchen light on
When you spill coffee | on your new white shirt | first thing in the morning
When you get a parking ticket | just as you're about to leave
When someone leaves | their shopping cart | in the middle of the parking lot
When your phone | battery dies and you | forgot your charger at home
When you're starving | and your food order | gets messed up
When someone plays | loud music on public transport
When you find out | there's a surprise meeting | right before lunch
When your favorite | restaurant is out of | your favorite dish
When you're ready to leave | but your friend is still | getting ready
When you see your ex | with someone new
When you find out | your favorite shirt | has a stain on it
When someone spoils | the ending of a movie | you were excited to see
When your alarm | doesn't go off and | you oversleep
When you have | a million tabs open | and your browser crashes
When someone borrows | something and returns | it broken
When you're stuck in traffic | and you have to use | the bathroom
When your favorite | team loses because | of a bad call
When you can't find | matching socks
When someone parks | too close and you can't | get into your car
When you realize you | left your wallet at home | after shopping
When someone keeps | interrupting you
When you get to the gym | and realize you forgot | your workout clothes
When you step in a puddle | and your socks get wet
When you burn your tongue | on hot coffee
When someone keeps | clicking their pen | during a meeting
When you have a great idea | and forget it | two minutes later
When you can't find | your keys and you're | already late
When your favorite pen | runs out of ink
When you get a | paper cut on | the same spot again
When you miss the bus | by a second
When you get | a parking spot | but it's too tight
When you're in a hurry | and the printer jams
When you have a | song stuck in your head | but can't remember the name
When you drop your phone | and it lands face down
When you finally sit down | to eat and the doorbell rings
When you clean | the house and someone | messes it up
When your friend cancels | plans at the last minute
When you get your hair done | and it doesn't turn out | the way you wanted
When your favorite app | keeps crashing
When you spill something | on your laptop
When you realize you | sent a text to | the wrong person
When you wait forever | for an elevator and | it's full when it arrives
When someone starts | a sentence with "no offense" | but it's definitely offensive
When you can't find | the TV remote | and it's right in front of you
When you drop food | on your clean clothes
When your favorite | snack is sold out | at the store
53 changes: 52 additions & 1 deletion restapi/memegen/memes/disgust.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,55 @@ Wen u hit ur toe|while walking
Wen ur guide doesnt|sign ur project report
Wen u forget a semicolon |in a code and |it shows 3666 errors
Wen someone asks u |to say disgusting things|becoz u have a |disgusting face
Wen ur roommate| sets continuous alarms |but doesnt wake up
Wen ur roommate| sets continuous alarms |but doesnt wake up
When someone chews | with their mouth open | right next to you
When you see someone | not washing their hands | after using the restroom
When your coworker | microwaves fish in | the office kitchen
When someone clips | their nails in public
When you find hair | in your food
When you realize | your socks are wet | from a puddle you stepped in
When your friend | talks about their | conspiracy theories
When someone sneezes | without covering their mouth
When you see a couple | overly PDA-ing in public
When you accidentally | drink spoiled milk
When someone picks | their nose in public
When you see a | questionable stain | on public transportation
When your roommate | leaves dirty dishes | for days
When someone talks | with food in their mouth
When your friend | won’t stop talking | about their diet
When you have to clean | someone else’s mess
When your friend | doesn’t cover their | coughs or sneezes
When you find mold | in the fridge
When someone burps | loudly at the table
When someone licks | their fingers after | eating
When you see someone | spit on the sidewalk
When someone doesn’t | cover their mouth | while yawning
When you find out | your favorite snack | has been discontinued
When your friend tells | a gross story | at the dinner table
When you smell | someone’s bad breath
When someone says | they never brush | their teeth before bed
When you find out | your hotel room | wasn’t cleaned properly
When your friend | wears the same clothes | multiple days in a row
When someone puts | their feet on | the table
When you see someone | double dip at a party
When someone uses | your towel without | asking
When your friend’s pet | has an accident | on the floor
When someone leaves | food out overnight
When you realize | you’ve been wearing | mismatched socks all day
When you see someone | eating loudly in | a quiet place
When you notice | a stranger’s dandruff | on their shirt
When someone leaves | hair in the shower drain
When you find out | your roommate | hasn’t done laundry | in weeks
When your friend | talks about their | gross medical issue
When you see someone | not wearing deodorant
When you hear | someone snoring loudly
When your friend | sneezes on your | phone screen
When you find out | someone doesn’t | change their bed sheets | regularly
When your friend | doesn’t flush the toilet
When someone leaves | the bathroom without | washing their hands
When you discover | your coworker’s desk | is a mess
When someone tells you | they don’t believe | in personal hygiene
When you see a cockroach | in your kitchen
When your friend | never takes out | the trash
When someone eats | with their mouth open
When you find out | your friend's pet | isn't house-trained
54 changes: 53 additions & 1 deletion restapi/memegen/memes/fear.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,56 @@ Wen you give 1000s of |speeches about social distancing |but become the first pe
The moment I get to know|we are living in red zone
Me trying to open books|and study something|a day before exams
Wen u write |800 lines of code|and exit without saving
Wen u are asked to|publish a research paper
Wen u are asked to|publish a research paper
When you see the dentist | pulling out the biggest drill
When you realize | you sent a text to | the wrong person
When the IT guy | asks to meet | after seeing your search history
When you hear the phrase | "we need to talk"
When you check your bank account | after a weekend of | impulse shopping
When you see a spider | in your room and | it suddenly disappears
When you realize | you’ve been talking to | someone with spinach | in your teeth
When your car makes | a weird noise | and you have no idea why
When you remember | you left the stove on | just as you fall asleep
When you hear | unexpected knocks | on the door
When you check the weather | and see a | severe storm warning
When you realize | you missed an important | meeting at work
When your phone rings | and it's an unknown number
When you accidentally | delete an important | file from your computer
When you see the | "low battery" alert | and you're far from | a charger
When you find a | surprise bill in the mail
When your boss | asks you to come | into their office
When you hear | a loud noise | in the middle of the night
When you realize | you forgot to | study for the exam
When the plane hits | turbulence mid-flight
When you hear a | cracking noise | from your phone screen
When you realize | you forgot your | anniversary
When you get a | message from your ex | out of the blue
When you see | the "blue screen of death" | on your computer
When you realize | you left your keys | inside the car
When you hear footsteps | behind you at night
When you wake up | late for an | important interview
When you hear | a weird noise | coming from the basement
When your GPS | loses signal in | the middle of nowhere
When you realize | your essay is due | in one hour
When you get a | friend request from | your boss
When you see a | rat run across | the kitchen floor
When you hear a | loud crash | from the other room
When you realize | your phone | isn’t in your pocket
When you check | your email and see | a message from HR
When you have to | walk down a dark | alley alone
When you realize | you left the | garage door open
When you get | an unexpected | "we need to talk" text
When you notice | a creepy shadow | in the corner of the room
When you hear | the fire alarm | go off at 2 AM
When your friend | says "guess what | I heard about you"
When you feel | your phone vibrate | but there’s no notification
When you realize | you locked yourself | out of your house
When your car | won’t start in | a deserted parking lot
When you get | an unexpected call | from your doctor
When you hear | the sound of glass | breaking downstairs
When you see | the check engine light | turn on
When you realize | you forgot to submit | your assignment
When you hear | someone call your name | but you’re alone
When you find | an unexpected | guest in your bed
When you realize | the food you ate | might have been expired
When you hear | the siren of a police car | behind you
Loading