0

face recognition works but when I want to redirect or move pages there is an error like this

------------------------------ ERROR MESSAGE ----------------------------------------------

File "c:\Users\Home\Python\FlaskCRUDAuth\app.py", line 67, in gen_frames
    return redirect(url_for('home', _external=False))
File "C:\Users\Home\anaconda3\envs\facerecognition\lib\site-packages\flask\helpers.py", line 307, in url_for
    "Attempted to generate a URL without the application context being"
RuntimeError: Attempted to generate a URL without the application context being pushed. This has to be executed when application context is available.

i have tried to render_template, redirect , url_for but still error

my Code : -------------------------------- Code(app.py) --------------------------------------------

# Store this code in 'app.py' file

from flask import Flask, render_template, request, redirect, url_for, session, Response
import re
import cv2
import numpy as np
import face_recognition

# Inialisasi Flask
app = Flask(__name__)

# Face Recog
bechkam_image = face_recognition.load_image_file("Bechkam/beckham.jpg")
bechkam_face_encoding = face_recognition.face_encodings(bechkam_image)[0]
 
rooney_image = face_recognition.load_image_file("Rooney/rooney.jpg")
rooney_face_encoding = face_recognition.face_encodings(rooney_image)[0]

with open('label.txt') as f:
    lines = f.read()

kn_fc_nm = lines.split(',')

known_face_encodings = [
    bechkam_face_encoding,
    rooney_face_encoding
]
known_face_names = kn_fc_nm

face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

def gen_frames():
    # Inialisasi OpenCV
    camera = cv2.VideoCapture(0, cv2.CAP_DSHOW)
    while True:
        success, frame = camera.read()
        if not success:
            break
        else:
            small_frame = cv2.resize(frame, (0,0), fx=0.25, fy=0.25)
            rgb_small_frame = small_frame[:, :, ::-1]
            face_locations = face_recognition.face_locations(rgb_small_frame)
            face_encodings = face_recognition.face_encodings(
                rgb_small_frame, face_locations)
            face_names = []
            auth = []
            for face_encoding in face_encodings:
                matches = face_recognition.compare_faces(
                    known_face_encodings, face_encoding)
                name = "Unknown"
                face_distances = face_recognition.face_distance(
                    known_face_encodings, face_encoding)
                best_match_index = np.argmin(face_distances)
                if matches[best_match_index]:
                    name = known_face_names[best_match_index]

                face_names.append(name)
            
                if face_names[0] in known_face_names:
                    print("Akses Diterima")
                    return redirect(url_for('home'))
                elif face_names[0] == "Unknown":
                    print('Unknown')
                else:
                    print('Tidak diketahui')
                
            
            for (top, right, bottom, left), name in zip(face_locations, face_names):
                top *= 4
                right *= 4
                bottom *= 4
                left *= 4

                cv2.rectangle(frame, (left, top),
                              (right, bottom), (0, 0, 255), 2)

                cv2.rectangle(frame, (left, bottom - 35),
                              (right, bottom), (0, 0, 255), cv2.FILLED)
                font = cv2.FONT_HERSHEY_DUPLEX
                cv2.putText(frame, name, (left + 6, bottom - 6),
                            font, 1.0, (255, 255, 255), 1)

            ret, buffer = cv2.imencode('.jpg', frame)
            frame = buffer.tobytes()
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

@app.route('/video_feed')
def video_feed():
    return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')

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

@app.route('/face_login')
def face_login():
    return render_template('face.html')

if __name__ == '__main__':
    app.run(debug=True)

error message

davidism
  • 121,510
  • 29
  • 395
  • 339
Renol N
  • 9
  • 1
  • 3
    Does this answer your question? [Flask.url\_for() error: Attempted to generate a URL without the application context being pushed](https://stackoverflow.com/questions/31766082/flask-url-for-error-attempted-to-generate-a-url-without-the-application-conte) – Bohdan Oct 07 '21 at 04:52
  • i change return redirect(url_for('home')) to return redirect(url_for('static', filename='templates/home.html')) but still error – Renol N Oct 07 '21 at 06:54
  • i try solution to add with app.app_context(): return redirect(url_for('home')) error message is missing but still cant move / redirect page – Renol N Oct 07 '21 at 07:08

0 Answers0