bump project

This commit is contained in:
2026-05-01 21:24:21 +03:30
parent dc4b1a1400
commit 139b3e26da
83 changed files with 2455 additions and 18349 deletions

215
index.py
View File

@@ -1,214 +1,7 @@
import cv2, os
import sys
import json
import base64
import sqlite3
import numpy as np
import face_recognition
from jdatetime import date
from time import localtime, strftime
from flask import Flask, render_template, request, abort, Response
from app import create_app
app = Flask(__name__)
newimg_li = [0, 0, 0, 0]
database = "/home/FaceAtt/mysite/database.db"
cnt = sqlite3.connect(database)
con = cnt.cursor()
app = create_app()
def ReadSQL(sql):
cnt = sqlite3.connect(database)
con = cnt.cursor()
return list(con.execute(sql))
def ExecSQL(sql):
cnt = sqlite3.connect(database)
con = cnt.cursor()
try:
con.execute(sql)
cnt.commit()
return "1"
except:
return "0"
def get_plast(id):
ini = ReadSQL(
"""SELECT * FROM attendances WHERE "user_id" = '{}' AND "type" = 'ورود' ORDER BY "id" DESC LIMIT 0,1""".format(
id))
out = ReadSQL(
"""SELECT * FROM attendances WHERE "user_id" = '{}' AND "type" = 'خروج' ORDER BY "id" DESC LIMIT 0,1""".format(
id))
ini = ini[0] if len(ini) != 0 else ("", "", "", "00:00", "00-00-00")
out = out[0] if len(out) != 0 else ("", "", "", "00:00", "00-00-00")
return "از " + ini[4] + " " + ini[3] + " تا " + out[4] + " " + out[3]
def readb64(uri):
encoded_data = uri.split(',')[1]
nparr = np.fromstring(base64.b64decode(encoded_data), np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
return img
@app.route("/")
def hello():
return render_template('index.html',
namep="-----",
navp="-----",
timep="از 00:00 تا 00:00",
)
@app.route("/add", methods=['GET', 'POST'])
def add():
if request.method == 'POST':
global newimg_li
newimg_li = [0, 0, 0, 0]
return "1"
return "0"
@app.route("/one_img", methods=['GET', 'POST'])
@app.route("/two_img", methods=['GET', 'POST'])
@app.route("/tre_img", methods=['GET', 'POST'])
@app.route("/for_img", methods=['GET', 'POST'])
def one_img():
try:
if request.method == 'POST':
global newimg_li
data = request.form['image'].split(',')[0]
if "image/png" not in data and "image/jpeg" not in data:
return "لطفا تصویر وارد کنید."
img = readb64(request.form['image'])
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
face_code = face_recognition.face_encodings(img)
if len(face_code) == 0:
return "چهره ای در تصویر شناسایی نشد."
if len(face_code) > 1:
return "لطفا تصویری با یک چهره وارد کنید."
face_code = list(face_code[0])
if "one" in request.url:
newimg_li[0] = face_code
if "two" in request.url:
if newimg_li[0] == 0:
return "تصویر اول را وارد کنید."
newimg_li[1] = face_code
if "tre" in request.url:
if newimg_li[0] == 0:
return "تصویر اول را وارد کنید."
if newimg_li[1] == 0:
return "تصویر دوم را وارد کنید."
newimg_li[2] = face_code
if "for" in request.url:
if newimg_li[0] == 0:
return "تصویر اول را وارد کنید."
if newimg_li[1] == 0:
return "تصویر دوم را وارد کنید."
if newimg_li[2] == 0:
return "تصویر سوم را وارد کنید."
newimg_li[3] = face_code
return "1"
except:
pass
return '0'
@app.route("/savenew", methods=['GET', 'POST'])
def savenew():
try:
if request.method == 'POST':
name = request.form['name']
nave = request.form['nav']
if name == "":
return "نام را وارد کنید."
if nave == "":
return "کد ملی را وارد کنید."
try:
int(nave)
except:
return "کد ملی باید عدد باشد."
naves = ReadSQL("SELECT * FROM users WHERE nave = '{nav}'".format(nav=nave))
if len(naves) != 0:
return "کد ملی تکراری است."
if 0 in newimg_li:
return "تمامی تصاویر را وارد کنید."
return ExecSQL("INSERT INTO users VALUES (null, '{name}', '{nav}', '{code}')".format(name=name, nav=nave,
code=json.dumps(
newimg_li)))
except:
pass
return '0'
@app.route('/search', methods=['GET', 'POST'])
def search():
try:
img = readb64(request.form['image'])
users = ReadSQL("SELECT * FROM users")
face_codes = face_recognition.face_encodings(img)
out_li = []
l = 0
for face_code in face_codes:
l += 1
for user in users:
if True in face_recognition.compare_faces(json.loads(user[3]), face_code):
au = list(user[:3])
au.append(get_plast(user[0]))
out_li.append(au)
continue
if len(out_li) != l:
out_li.append("0")
if len(out_li) == 0:
out_li.append("0")
locs = face_recognition.face_locations(img)
for loc in locs:
rec = cv2.rectangle(img, (loc[3], loc[0]), (loc[1], loc[2]), (255, 0, 255), 3)
retval, buffer = cv2.imencode('.png', img)
img_base64 = "data:image/png;base64," + str(base64.b64encode(buffer))[2:-1]
out_li.append(img_base64)
return json.dumps(out_li)
except:
return '0'
@app.route("/ini/<path:id>")
@app.route("/out/<path:id>")
def init(id):
if id == "0":
return "00:00"
typ = "ورود" if "ini" in request.url else "خروج"
ExecSQL(
"INSERT INTO attendances VALUES (null, '{}', '{}', '{}', '{}')".format(id, typ, strftime("%H:%M", localtime()),
str(date.today())))
return get_plast(id)
@app.route("/users")
def users_show():
theads = ["نام", "کد ملی"]
tbodys = ReadSQL("SELECT name,nave FROM users")
return render_template('table.html', title="کاربران", columns="[0, 1]", theads=theads, tbodys=tbodys)
@app.route("/atts")
def atts_show():
theads = ["نام", "کد ملی", "نوع", "تاریخ", "زمان"]
tbodys = ReadSQL(
"SELECT users.name, users.nave, type, date, time FROM attendances INNER JOIN users ON attendances.user_id = users.id;")
return render_template('table.html', title="ورود و خروج", columns="[0, 1, 2, 3, 4]", theads=theads, tbodys=tbodys)
if __name__ == "__main__":
app.run()