File size: 3,132 Bytes
37d73c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a619ea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from flask import Flask, render_template, jsonify, request
from flask_socketio import SocketIO, emit
from fire import firestore_client as fc
import dlib
import base64
import cv2
import numpy as np
import time


app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socket = SocketIO(app, async_mode="eventlet")
img = None

hog_face_detector = dlib.get_frontal_face_detector() 


def base64_to_img(base64_image):

    # Remove the "data:image/jpeg;base64," prefix from the string
    image_data = base64_image.split(',')[1]

    # Decode the base64-encoded image data
    decoded_data = base64.b64decode(image_data)

    # Convert the binary data to a NumPy array
    np_array = np.frombuffer(decoded_data, np.uint8)

    # Decode the NumPy array to an OpenCV image
    image = cv2.imdecode(np_array, cv2.IMREAD_COLOR)

    return image

def prep_image(image,name,fc):
    image = base64_to_img(image)
    count = 0
    while count < 10:
        time.sleep(2)
        face_hog = hog_face_detector(image,1)
        for face in face_hog:
            x = face.left()
            y = face.top()
            w = face.right() - x
            h = face.bottom() - y 
        crop = image[y:y+h,x:x+w] 
        crop = cv2.resize(crop, (224, 224)) 
        retval, buffer = cv2.imencode('.jpg', crop)
        res = base64.b64encode(buffer)
        doc= name+"_"+str(count)
        print(res)
        doc_ref = fc.collection("faces").document(str(doc))
        doc_ref.set({
            "img":res
        })
        count += 1
    
    return(count)    

@socket.on("connect")
def test_connect():
    print("Connected")
    emit("my response", {"data": "Connected"})

@socket.on("image")
def receive_image(image):
    global img
    img = image['image']
    # Emit the received image as base64 string to the '/camera' route
    socket.emit('forward_image', {'image': img}, namespace='/camera')

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

@app.route("/camera", methods=['POST',"GET"])
def camera():
    if request.method == 'GET':
        # image_data = request.form['image']
        # Process the received image data as needed

        # Return the processed image data
        return jsonify({'image': img})
    else:
        return "Method Not Allowed", 405

@app.route("/login")
def login():
    doc_ref = fc.collection("current_users").document('user')
    doc = doc_ref.get()
    if doc.exists:
        doc = doc.to_dict()
    else:
        print("No such document!")
    res = doc['name']
    
    return render_template('login.html',name = res)

@app.route("/sign", methods=('GET', 'POST'))
def sign():
    doc_ref = fc.collection("faces").document('users')
    doc = doc_ref.get()
    if doc.exists:
        doc = doc.to_dict()
    else:
        print("No such document!")
    res = doc['name']
    result = "error"

    if request.method == "POST":
        name = request.form.get("name")
        res.append(name)   
        result = prep_image(img,name,fc) 
    
    return render_template('sign.html',data = result)

if __name__ == '__main__':
    socket.run(app, debug=True,port=7860,host="0.0.0.0")