Skip to content

Instantly share code, notes, and snippets.

@bakirillov
Last active September 11, 2020 14:25
Show Gist options
  • Save bakirillov/113e05acd33b7bd4cb12712bbbdc7bef to your computer and use it in GitHub Desktop.
Save bakirillov/113e05acd33b7bd4cb12712bbbdc7bef to your computer and use it in GitHub Desktop.
My own private findface
import io
import os
import dash
import base64
import dash_table
import pandas as pd
import pickle as pkl
import os.path as op
import face_recognition as fr
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, State, Output
def who_is_on_the_photo(imagefn):
image_coords = fr.face_encodings(fr.load_image_file(imagefn))
answer = {a:"uknown" for a in range(len(image_coords))}
for i,a in enumerate(image_coords):
who = {b:fr.compare_faces([people[b]], a)[0] for b in people}
for b in who:
if who[b]:
answer[i] = b
return(answer)
TEMP_DIR = "temp"
if not op.exists(TEMP_DIR):
os.makedirs(TEMP_DIR)
with open("pickled_people.pkl", "rb") as ih:
people = pkl.load(ih)
external_stylesheets = ["https://codepen.io/chriddyp/pen/bWLwgP.css"]
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.title = "My own private findface"
app.layout = html.Div(children=[
html.H1(children='My own private findface'),
dcc.Upload(
id='upload-data',
children=html.Div([
'Drag and Drop or ',
html.A('Select Files')
]),
style={
'width': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'
},
# Allow multiple files to be uploaded
multiple=False
),
html.H3(children="", id="answer")
])
#app.config['suppress_callback_exceptions']=True
@app.callback(
Output("answer", "children"),
[Input("upload-data", "contents")])
def process_image(file):
if file:
content_type, content_string = file.split(',')
file_string = base64.b64decode(content_string)
fname = op.join(TEMP_DIR, "ololo.png")
with open(fname, "wb") as oh:
oh.write(file_string)
answer = who_is_on_the_photo(fname)
answer_string = ",".join([answer[a] for a in answer])
return(answer_string)
app.run_server(debug=False)
import os
import json
import telebot
import pickle as pkl
import os.path as op
from flask import Flask
import face_recognition as fr
ih = open("config.json", "r")
config = json.load(ih)
ih.close()
TOKEN = config["bot_token"]
URL = config["bot_url"]
ALLOWED = config["allowed"]
bot = telebot.TeleBot(TOKEN)
server = Flask(__name__)
TEMP_DIR = "temp"
if not op.exists(TEMP_DIR):
os.makedirs(TEMP_DIR)
with open("pickled_people.pkl", "rb") as ih:
people = pkl.load(ih)
def get_file(message):
fileID = message.photo[-1].file_id
file_info = bot.get_file(fileID)
downloaded_file = bot.download_file(file_info.file_path)
return(downloaded_file)
def who_is_on_the_foto(imagefn):
image_coords = fr.face_encodings(fr.load_image_file(imagefn))
answer = {a:"uknown" for a in range(len(image_coords))}
for i,a in enumerate(image_coords):
who = {b:fr.compare_faces([people[b]], a)[0] for b in people}
for b in who:
if who[b]:
answer[i] = b
return(answer)
@bot.message_handler(commands=["start"])
def greet_and_identify(message):
if message.chat.id in ALLOWED:
bot.reply_to(message, "Welcome")
else:
bot.reply_to(message, "Get out, id "+str(message.chat.id)+" is not allowed")
@bot.message_handler(content_types=["photo"])
def process_photo(message):
if message.chat.id in ALLOWED:
file = get_file(message)
fn = op.join(TEMP_DIR, "test.png")
with open(fn, "wb") as oh:
oh.write(file)
answer = who_is_on_the_foto(fn)
for a in answer:
bot.send_message(message.chat.id, "The "+str(a+1)+"th person is "+answer[a])
else:
bot.reply_to(message, "Get out, id "+str(message.chat.id)+" is not allowed")
@server.route('/'+TOKEN, methods=['POST'])
def getMessage():
bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
return("!", 200)
@server.route("/")
def webhook():
bot.remove_webhook()
bot.set_webhook(url=URL+TOKEN)
return("!", 200)
#bot.polling()
server.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment