I am working on a website digital banking where I used face recognition to log-in. I have created a function named gen_frame() where I used variable called "name". and I want to use this variable outside function so that I can display it on HTML page using route. I used flask framework.
This is my app.py file
# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
def gen_frames():
while True:
success, frame = camera.read() # read the camera frame
if not success:
break
else:
# Resize frame of video to 1/4 size for faster face recognition processing
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_small_frame = small_frame[:, :, ::-1]
# Only process every other frame of video to save time
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
# Or instead, use the known face with the smallest distance to the new face
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)
# Display the results
for (top, right, bottom, left), name in zip(face_locations, face_names):
# Scale back up face locations since the frame we detected in was scaled to 1/4 size
top *= 4
right *= 4
bottom *= 4
left *= 4
# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
# Draw a label with a name below the face
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 255, 0), 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')
data = name
@app.route('/')
def index():
return render_template('Index.html',data = data)
@app.route('/video_feed')
def video_feed():
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__=='__main__':
app.run(debug=True)
i created data variable data = name
and used route to send the data to html
@app.route('/')
def index():
return render_template('Index.html',data = data)
This is my HTMl file code snippet
<div >
<div >
<div style="margin-top: 20px;box-shadow: 9px 9px 27px 2px hsl(250, 66%, 55%); border-radius: 70px; margin-right: 50px; text-align: center;">
<h4 style="text-align: center; color:hsl(250, 69%, 61%);" >Name:
<p>{{data}}</p>
</h4>
</div>
</div>
</div>
CodePudding user response:
try make the variable global putting at the start of the function:
global variable_name
CodePudding user response:
The fastest way to make global variable in your program is create the variable on the top.
# Initialize some variables
...
name = ''
This is not the best practices, when you use the program on multiple client/browser that access /video_feed
, the reference of name will be using same instance of memory. Sometimes, you won't get what you are expected, as Ahmed Mohamed AEK mentioned on the comment.
It will be better if your create the image processing page to trigger an API, and send the name using that trigger.